Why am I getting huge notch response at my sampling rate? - Page 1
Forum section: electronics / general

author: prosper
author_rank: Regular Contributor
timestamp: May 09, 2023, 09:28:51 pm
content: I set up a simple noise source, to use for testing DSP functions and filtering. Everything seems to work as expected - I see a bump at my PWM frequency (188kHz), and even a little 65kHz bump from my power supply. But I get some *huge* notches right at my sample rate (and its harmonics).The relevant chunk of code is as follows:Code:uint16_t next = 4800; // 100uswhile(1){if (SysTick->CNT > next){t1pwm_setpw(1, (uint8_t)pRNG()); //T1CH1 // PD0 // TSSOP p8t1pwm_setpw(4, (uint8_t)pRNG()); //T1CH4 // PC4 // TSSOP p14SysTick->CNT = 0;}This results in the following responses (blue is raw PWM output, yellow is through an overdamped RCLC filter). Why are there notches that correspond with my sampling rate, and its harmonics? (10kHz)It's definitely related to the sample rate. When I randomize it, thusly:Code:uint16_t next = 4800; // 100uswhile(1){if (SysTick->CNT > next){t1pwm_setpw(1, (uint8_t)pRNG()); //T1CH1 // PD0 // TSSOP p8t1pwm_setpw(4, (uint8_t)pRNG()); //T1CH4 // PC4 // TSSOP p14next = (pRNG() + 1024) & 0x00000FFF; // 1024-4096 // randomize the next sample time to eliminate 10kHz harmonicsSysTick->CNT = 0;}}I get this:

author: tggzzz
author_rank: Super Contributor
timestamp: May 09, 2023, 09:48:08 pm
content: Quote from: prosper on May 09, 2023, 09:28:51 pmDraw a picture of 10 cycles of a 10kHz sine wave. Put a "sampling dot" on the sine wave every 100µs. What do you notice?Repeat with a 20kHz sine wave and 100µs.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: May 09, 2023, 10:38:40 pm
content: If your sample rate is 10kHz, how are you reading anything above 5kHz at all?

author: Someone
author_rank: Super Contributor
timestamp: May 09, 2023, 11:25:54 pm
content: Quote from: T3sl4co1l on May 09, 2023, 10:38:40 pmWhen there is little to no output reconstruction filter its all harmonics of the "actual" signal, its the logical (and rarely talked about) inverse to aliasing.

author: prosper
author_rank: Regular Contributor
timestamp: May 09, 2023, 11:37:08 pm
content: Ooo, interesting. Food for thought, thanks!

author: prosper
author_rank: Regular Contributor
timestamp: May 09, 2023, 11:43:58 pm
content: Just spit balling, but id guess that Nyquist says you need at least 2x samples to *recreate the original signal,* not that there won't be *any* output at all beyond that limit. It just won't be a representation of your input. But if the goal is just to produce random noise, it works

author: T3sl4co1l
author_rank: Super Contributor
timestamp: May 10, 2023, 12:45:13 am
content: Quote from: Someone on May 09, 2023, 11:25:54 pmYeah, but why would the spectrum differ between images? What the hell is the first figure if not a DFT of 10k sample rate, according to the description? It sure as hell ain't that, if the axes are to be believed.Tim

author: Someone
author_rank: Super Contributor
timestamp: May 10, 2023, 03:32:40 am
content: Quote from: T3sl4co1l on May 10, 2023, 12:45:13 amYou'll need to explain in more detail (reference to image is suddenly indeterminate). Those plots look roughly like I'd expect them to based on the information in the OP, and match to simulation until the peaking above 100k.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: May 10, 2023, 05:08:50 am
content: There's several things going on here and none of them has been defined... a diagram would be greatly illuminating.What is this, Discovery2, is that a board? -- Analog Discovery? Is that being used as an o-scope/spec? Is its sample rate specified here or what? Is it related to the question at all, or able to be ignored (the interface sets everything adequately for the given analysis)? It sounds like a sampled acquisition system to me so I don't know.Quote- What DSP functions?- What PWM frequency? 188kHz apparently, but what is that in relation to? Just arbitrary?- What sample rate? ...What's being sampled? The output (from the PWM, as... PWMDAC I suppose?)? Is that what the Discovery2 is doing?The spectrum looks more or less like the sinc(f) response you would get from a PRBS, implying 10kHz is the symbol rate of that. But what does that have to do with a PWM? Or the clearly digital acquisition that's been screenshotted?The sample code seems to suggest (and being such a brief excerpt, there's no way to tell what setup was, or what other interrupts may be running) updating a PWM value every tick. I would assume SysTick is an ARM element, but it could simply be a construct on any platform. I don't recognize the timer functions.The most straightforward interpretation seems to be:- PWM timer running at 188kHz (for some reason)- Unrelated timer running at 10kHz (or random), updating the PWM timer (does this have any race conditions? which would skew the probability distribution function (PDF)?)- PWM output is acquired by a spec analyzer (is this all that it's doing, is there anything more we need to know about it?)- Output without AA filter is sinc(f) which seems to be the observation, and either spread-spectrum or filtering works to cut the tail.But contrary to that:- I, at least, would run the update rate at the PWM period. Tack on a register update to the overflow interrupt. Easy synchronous updates, no glitching*. Or divide it with a counter (hardware or software) and use a lower, but still synchronous, update rate.- So the 10kHz sample rate is noise to me. What does it actually mean? Is the acquisition system sampling at 10k?- Is the acquisition system in-house made? Granted, a whole GUI interface plus signal analysis would be disproportionately featureful given the questioned asked, but I don't know the OP and their capabilities.- I'm aware of but not familiar with Analog Discovery, maybe that's what's being used here? It doesn't say. I also don't know if one can assign a fixed sample rate on it (or if/why one would want to when doing the kind of acquisition shown here).*Assuming a minimum delay between overflow and compare events, or a buffered system. Many timers have buffering these days, very easy to use; but there are also timers with toggle logic i.e. if you glitch/race past one compare event, the output phase becomes inverted(!). So, since the platform isn't defined, I wouldn't want to assume I know how it works.I've seen more than enough things 1. different from the way I would do it, and 2. different from what I assume at first glance, to assume the above interpretation, hence the need for clarification.Tim

author: bdunham7
author_rank: Super Contributor
timestamp: May 10, 2023, 05:28:05 am
content: Quote from: T3sl4co1l on May 10, 2023, 05:08:50 amYes, it could be explained more clearly, but here's what I get.He has as a source a (something) which generates a variable output from 0 to FS using PWM @ 188kHz. So it's just a rectangle wave of unknown amplitude with a duty cycle of 0-100% in lieu of an analog level.In the first example he is setting his (unstated device) to update the PWM output level every 100µs with a pseudorandom number or something like that to simulate random noise. In the second example he sets it to update the PWM the same way except he also randomizes the update intervals as well.His spectrum display is generated by (some FFT device) with two channels and a 400kSa/s sample rate. One channel is just the raw output, the other is run through a low-pass filter of unspecified parameters.Or at least that is what it looks like to me.

author: gf
author_rank: Super Contributor
timestamp: May 10, 2023, 05:32:46 am
content: Quote from: prosper on May 09, 2023, 09:28:51 pmI do not see any problem. A step signal (0-th order hold) with steps of fixed width (100µs in your case) and random height simplyhasa spectrum with sinc shaped envelope.[ Edit: Additionally, the PWM "garbles" the spectrum, but as I understand, this was not your primary concern. ]

author: prosper
author_rank: Regular Contributor
timestamp: May 10, 2023, 02:47:12 pm
content: Quote from: bdunham7 on May 10, 2023, 05:28:05 amYes, exactly right, thank you for explaining. It's a microcontroller, set up to spit out random values onto an 3.3V, 8-bit, 188kHz PWM, updating it's pulsewidth to a random width every 100us (not synchronized in any way to the PWM frequency; though I did want at least 10 or so PWM pulses per sample). It's being measured by my analog discovery 2 spectrum analyzer, currently running at 400kSPS. One channel is reading the raw digital fullscale PWM pulses, and the other channel is reading the same signal after it's passed through a lowpass filter.Thinking it through, the results do make sense now. Thanks to all who commented with their input; everything is much clearer nowCheers!