Strange DAC behavior at certain frequency step values

thassan
Posts: 5
Joined: Mon Nov 21, 2022 9:19 am

Strange DAC behavior at certain frequency step values

Postby thassan » Mon Nov 21, 2022 11:32 am

Greetings !
The issue is that I am getting very low frequency (7-10Hz) cosine waves for certain frequency-step values given to DAC control register (SENS_SAR_DAC_CTRL1_REG). I am expecting frequencies of around 20KHz and 25KHz on those frequency-step points.

I am using a modified version of DAC Cosine Wave Generator example and a few elements from a GitHub repository in which the author is generating cosine wave by writing directly to the registers. Links to both resources here :

https://github.com/espressif/esp-idf/bl ... ple_main.c
https://github.com/krzychb/dac-cosine/b ... c-cosine.c

In addition to that, I have added a timer callback that updates the frequency of the Cosine Wave by a random frequency-step value. The calculation for that frequency step value is given in section 29.5.4 : Cosine Waveform Generator of the ESP32 technical reference manual.

freq = dig_clk_rtc_freq · SENS_SAR_SW_FSTEP/65536

In short, providing a factor of 8 to SENS_SAR_SW_FSTEP, produces a output frequency (freq) of approximately 1KHz, given the ESP32 clock frequency (dig_clk_rtc_freq) is 8MHz.

So coming back to my application. I am producing cosine waves of frequencies between 20KHz - 30KHz over a period of 1000ms. After 1000ms, the wave generation is stopped. Furthermore, every 50ms, the wave frequency is randomly changed between the specified range of 20KHz - 30KHz using a timer callback function.

My timer callback has the following structure :
  1. static void TimerCallback(void* arg)
  2. {
  3.     // NOTE : for a output frequency range 20KHz - 30KHz, a frequencyStep in range (160-240) is required
  4.     //calculate a random frequencyStep value (between 160-240) here
  5.     //provide that value to the DAC control register
  6.     SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL1_REG, SENS_SW_FSTEP, frequencyStep, SENS_SW_FSTEP_S);
  7. }

Using my code, a normal cosine wave for me would look like as displayed like this :
normal_freq.PNG
normal_freq.PNG (551.81 KiB) Viewed 2739 times

However, when certain frequency-step values occur such as 160, 216 (& some others), I am getting glitches all the time. Like this :
DAC_GLITCHES_1.PNG
DAC_GLITCHES_1.PNG (2.09 MiB) Viewed 2739 times
DAC_GLITCHES_2.png
DAC_GLITCHES_2.png (1.82 MiB) Viewed 2739 times
Furthermore, I have verified that the correct frequency-step value is being fed to the DAC control register by using the following get method :
  1. GET_PERI_REG_BITS(SENS_SAR_DAC_CTRL1_REG,7,0);

My query is why such a strange behavior is being exhibited by the DAC control register on certain values ?

ESP_Sprite
Posts: 9619
Joined: Thu Nov 26, 2015 4:08 am

Re: Strange DAC behavior at certain frequency step values

Postby ESP_Sprite » Tue Nov 22, 2022 6:45 am

Are you sure your scopes sample rate isn't simply aliasing with the frequency you output? As in, if you zoom in, does the 'behaviour' change?

thassan
Posts: 5
Joined: Mon Nov 21, 2022 9:19 am

Re: Strange DAC behavior at certain frequency step values

Postby thassan » Tue Nov 22, 2022 7:56 am

ESP_Sprite wrote:
Tue Nov 22, 2022 6:45 am
Are you sure your scopes sample rate isn't simply aliasing with the frequency you output? As in, if you zoom in, does the 'behaviour' change?
Yes. If I run the program with oscilloscope 'zoomed in' (for the glitchy values), I see normal cosine wave. Here's the wave I see for frequency-step '216' :
50_micro_sec_zoomed_in.jpg
50_micro_sec_zoomed_in.jpg (495.94 KiB) Viewed 2670 times

ESP_Sprite
Posts: 9619
Joined: Thu Nov 26, 2015 4:08 am

Re: Strange DAC behavior at certain frequency step values

Postby ESP_Sprite » Wed Nov 23, 2022 7:07 am

Then it's likely an aliasing issue: your scopes sample rate and the output frequency align in such a way that the scope happens to take a sample every time the sine that is output is at the same value, and as such the scope shows a flat line rather than the actual signal. If your oscilloscope has some method to set the sample rate, make sure your sample frequency is >> 2 times the output frequency (as in: sample frequency should be at the very least the Nyquist of the highest input signal)

thassan
Posts: 5
Joined: Mon Nov 21, 2022 9:19 am

Re: Strange DAC behavior at certain frequency step values

Postby thassan » Wed Nov 23, 2022 7:40 am

ESP_Sprite wrote:
Wed Nov 23, 2022 7:07 am
Then it's likely an aliasing issue: your scopes sample rate and the output frequency align in such a way that the scope happens to take a sample every time the sine that is output is at the same value, and as such the scope shows a flat line rather than the actual signal. If your oscilloscope has some method to set the sample rate, make sure your sample frequency is > 4 times the output frequency.
In the oscilloscope settings for sampling , I only see the 'Real Time' mode for sampling. I see the following definition in the user manual :
sampling_def.PNG
sampling_def.PNG (15.04 KiB) Viewed 2603 times
Also, is it possible that a higher frequency (i.e. 30KHz) is being sampled properly while we see aliasing at a lower frequency (i.e 8.3KHz). Which I have overserved here :
3_frequencies.jpg
3_frequencies.jpg (1.09 MiB) Viewed 2603 times

ESP_Sprite
Posts: 9619
Joined: Thu Nov 26, 2015 4:08 am

Re: Strange DAC behavior at certain frequency step values

Postby ESP_Sprite » Wed Nov 23, 2022 8:11 am

Aliasing actually works a bit more complicated than that... it's very likely that the waveform you see at 8.3 and 30KHz actually also aren't correct (because the frequencies still are higher than the sample frequency) but just happen to look correct as the intersection of the measured and the sample frequency produce something that looks 'about right' on your low-res scope screen.

I think that given the options your scope has (or rather: doesn't have) wrt sample rate, it may basically be ill-equipped for the measurements that you're trying to do. You'd probably be better off getting a scope with a large-ish sample memory: that way, it can keep sampling at a high enough sample rate while still being able to store the entire waveform you're trying to view. (E.g. for a max freq of 30KHz and a timebase of 2 sec for your entire screen, you'd want a minimum of about 30000Hz*2*2sec=120Ksamples of sample memory.)

thassan
Posts: 5
Joined: Mon Nov 21, 2022 9:19 am

Re: Strange DAC behavior at certain frequency step values

Postby thassan » Wed Nov 23, 2022 9:29 am

ESP_Sprite wrote:
Wed Nov 23, 2022 8:11 am
Aliasing actually works a bit more complicated than that... it's very likely that the waveform you see at 8.3 and 30KHz actually also aren't correct (because the frequencies still are higher than the sample frequency) but just happen to look correct as the intersection of the measured and the sample frequency produce something that looks 'about right' on your low-res scope screen.

I think that given the options your scope has (or rather: doesn't have) wrt sample rate, it may basically be ill-equipped for the measurements that you're trying to do. You'd probably be better off getting a scope with a large-ish sample memory: that way, it can keep sampling at a high enough sample rate while still being able to store the entire waveform you're trying to view. (E.g. for a max freq of 30KHz and a timebase of 2 sec for your entire screen, you'd want a minimum of about 30000Hz*2*2sec=120Ksamples of sample memory.)
The oscilloscope that I have is UNI-T UTD2052CL. Its specification doc states that it can support sample rate of upto 500 MS/s and a bandwidth of 50 MHz. Shouldn't it be good enough for my range of 20-30KHz wave generation ?

ESP_Sprite
Posts: 9619
Joined: Thu Nov 26, 2015 4:08 am

Re: Strange DAC behavior at certain frequency step values

Postby ESP_Sprite » Wed Nov 23, 2022 1:55 pm

It can absolutely show you the waveform, as you can see in the zoomed-in picture: the signal looks like a perfect sine there. The issue is that your scope only has 25Ksamples worth of sample memory. This means that if you zoom out by a lot, it needs to lower the sample rate otherwise it'll overflow that sample memory. Say you zoomed out to 500ms/div, like the 3rd picture of your original post. That means an entire screenful of data is (12*500ms)=6 seconds. It needs to take samples for 6 seconds to fill the screen, but it only has 25Ksamples worth of space to put those samples in, so it has to lower its sample rate to at max 1/(6 seconds / 25000 samples = ) 25258Hz. It simply can't go any faster, as if it did, it would run out of sample memory before it captured the full 6 seconds. Given that that 25KHz is way under the nyquist frequency of (2*30KHz=)60KHz, you get aliasing.

Who is online

Users browsing this forum: littlerd and 90 guests