Strange "harmonic" in PWM - Page 1
Forum section: electronics / projects

author: Dave_PT
author_rank: Frequent Contributor
timestamp: April 27, 2017, 08:12:17 am
content: Hello.I've been doing some experiments using PWM to generate audio.After I was able to execute everything successfully, I noticed a small hum.I passed the oscilloscope to FFT and tried to analyze the problem.When measuring several points I realized that even in the PWM pin this signal (of hum) continued to appear. Then I disconnected everything leaving only an RC filter attached to the pin.I changed the code so that the MCU only do the following: set the clock, configure the PWM channel (Fpwm = 47KHz), configure duty cycle to 50%, terminate.The truth is that there is a peak of 3KHz that I can not identify the source.This is the image of PWM (always at 50%):This is the FFT of the PWM signal:Signal detail of 3KHz - this is the most worries me because it directly integrates in the audio (other peaks: 9KHz, 15KHz):Signal detail of 3KHz - after low pass filter (fc = 17KHz - Only this part was welded, because the next circuit together with this RC would improve the cutoff frequency):Can someone tell me why this is happening?I've been stuck with this problem for some time but I can not solve it.I think it's not harmonic of the PWM because in that case it would be the 15th harmonic ... and it would be much more attenuated.If I look to the signal on the oscilloscope, I see no apparent ripple, other than the "triangular wave" resulting from the filter ...

author: hamster_nz
author_rank: Super Contributor
timestamp: April 27, 2017, 08:52:37 am
content: How is the PWM frequency derived from the source? 47kHz doesn't divide evenly into the common X MHz clocks...

author: madires
author_rank: Super Contributor
timestamp: April 27, 2017, 09:01:13 am
content: What kind of clock source are you using?

author: Dave_PT
author_rank: Frequent Contributor
timestamp: April 27, 2017, 09:08:36 am
content: I'm using an STM32F030, with internal clock at 48MHz (8MHz + PLL).I'm using timer 1 to generate PWM at 10bit.48MHz/1023 = 46.92KHzI load the comparator with 512 to make the DC of 50%.

author: JPortici
author_rank: Super Contributor
timestamp: April 27, 2017, 10:05:46 am
content: It seems that somewhere along the chain there's aliasing. Notice the double peaks in the valleys in image #2. I think it's an incorrect setup in the scope (sample rate / window / .. )in image #3 it appears the samplerate is 50 KS/s. given your PWM carrier frequency is 47KHz a peak at 3 KHz is something i would expect to happen

author: Dave_PT
author_rank: Frequent Contributor
timestamp: April 27, 2017, 10:23:44 am
content: Quote from: JPortici on April 27, 2017, 10:05:46 amOk, makes a lot of sense!In the case of the last images it is even a mistake with the sampling of the oscilloscope.Quote from: JPortici on April 27, 2017, 10:05:46 amCan you explain it better, please?In image 2 the sample rate is 250KS/s, so there should be no problem in sampling the 47KHz. But there are many peaks...PS: Is there any way to apply 'oversampling' on this type of low end oscilloscope?I'll investigate this.

author: vealmike
author_rank: Regular Contributor
timestamp: April 27, 2017, 10:31:06 am
content: Took me a while to verify that your first FFT showed a series of peaks 3KHz apart. What use is a spectral analysis without a scale? Grrrr (not your fault OP!)Anyroadooplike, yes it does appear to be real. EDIT (doh! just read JPortici's post above and yes aliasing could do this.) END EDITI agree 1/15th of your fundamental, 15*3.13=46.98KHz.I have no idea of the architecture of your driver uP. But, some things I would try:* Change the fundamental of the PWM. Does the unwanted harmonic follow the 1/15th ratio? This is critical, tells you what clock tree your aggressor is running from. If the aggressor doesn't change frequency when you adjust the PWM frequency, start adjusting other clocks in your uP to find the issue.* If your 'scope can do it, plot time interval error (jitter) and look at the waveform for clues. TIE is a plot of the difference between each rising edge of your signal and a (software) PLL locked to the signal. It has a vertical units of UI, or time and horizontal units of time.* Included for completeness, usually I'd ac couple the 'scope on the power rail, turn off the PWM and take another FFT to see if there was an aggressor on the power rail. In this case your unwanted signal is so closely linked to the fundamental that I'm not sure it's worth bothering. If you do this without turning off the PWM, the PWM will act as an aggressor for the power rail and you'll chase your tail.It's a pity about the 1/15 ratio. A 1/16 ratio would suggest a 4 bit counter somewhere in your PWM, clocked at the fundamental & introducing a small amount of jitter...Apologies if this is teaching you to suck eggs.

author: Dave_PT
author_rank: Frequent Contributor
timestamp: April 27, 2017, 10:46:45 am
content: Quote from: vealmike on April 27, 2017, 10:31:06 amI'll do it again.I already did that and "aggressor frequency" went from 3KHz to another frequency. I did not write the relationship between the frequencies, I just did this to isolate the problem and know if that is caused by the PWM.Quote from: vealmike on April 27, 2017, 10:31:06 amIt can not do that, I think. But perhaps by enabling persistence I can see that there are deviations in the pulses ...Quote from: vealmike on April 27, 2017, 10:31:06 amYes, I already did these 2 tests. With or without PWM running, nothing on the power rail comes up close to these frequencies.Quote from: vealmike on April 27, 2017, 10:31:06 amI appreciate it!!!

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: April 27, 2017, 10:56:00 am
content: If you heard the 3khz before seeing it, I guess that's an argument for the harmonic not to be a probing error. So then the question is; did you?

author: vealmike
author_rank: Regular Contributor
timestamp: April 27, 2017, 11:02:38 am
content: Quote from: Dave_PT on April 27, 2017, 10:46:45 amGood idea, but you'll need to divide the PWM clock by 15 and use that as a 'scope trigger.

author: Dave_PT
author_rank: Frequent Contributor
timestamp: April 27, 2017, 11:05:28 am
content: Quote from: alexanderbrevig on April 27, 2017, 10:56:00 amI do not know if the buzz will be at 3KHz. My ear spectrometer does not work very well.I'm going to try to use a spectrum application with the microphone on my smartphone and see if I can get any results...I'm going to play a fixed tone at 1KHz and see if I can register another tone effectively.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: April 27, 2017, 11:11:29 am
content: If you have a signal gen that can sweep, you could try a slow one from say 1khz to 5khz and see if you can detect a peak in volume with your eyes closed. Listen on low volume with a good pair of headphonesIf it's not detectable in this setup, I would not spend more time on it - unless of course you find it fun and/or educationalEDIT: typos...

author: Dave_PT
author_rank: Frequent Contributor
timestamp: April 27, 2017, 01:26:33 pm
content: In my lunch hour I jumped home and did some quick tests.I turned the filter circuit back on. The tests were performed with the following assembly:MCU - PWM - 3 order low pass filter - audio amp (LM386) - 8R 4W speakerI had in mind the sampling frequency of the oscilloscope so that the FFT did not mislead me.Measurement after the filter with freq of 1KHz:Measurement after filter with freq of 2.5KHz:Measurement to speaker terminals with 1KHz freq:Measurement to speaker terminals with 2.5KHz freq:Measurement to speaker terminals with PWM = 50%:So far there is nothing that I find very strange ...Then I used an application on my smartphone to better translate what I hear.Reference - Nothing connected, just the application:Circuit on, but no audio to play - PWM 50%:1KHz Tone:2.5KHz Tone:It seems that the noise is audible, but it does not appear in scope FFT, at the audio amp output.It looks like the problem is with the loudspeaker, right?But I've switched to another speaker and the noise is still slightly audible.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: April 27, 2017, 01:48:16 pm
content: Where is your filter set, at which frequency? Does your speaker handle high mid freqs?Is your PSU quiet? For fun, show us an FFT of your VCCAt 40db down in the spectrum (not sure it can be trusted much coming from a smartphone) it's quite subtle then - but definitely audible. At least when you're listening for it.Is it still annoying and audible to you?Seems like it's introduced at the speaker? Or am I reading this wrong?

author: Dave_PT
author_rank: Frequent Contributor
timestamp: April 27, 2017, 06:26:42 pm
content: Quote from: alexanderbrevig on April 27, 2017, 01:48:16 pmThe filter cuts close to 5KHz and should have this response.Quote from: alexanderbrevig on April 27, 2017, 01:48:16 pmIn my circuit I have 2 power rails. A + 9V rail for the LM386 and a + 3.3V rail for the MCU.Measurements were made as close to the power pins as possible.FFT of +3.3V:FFT of +9V - here comes a peak of, guess what, 3KHz, but very attenuated!Quote from: alexanderbrevig on April 27, 2017, 01:48:16 pmThe sound seems like rain in the distance.It is not annoying when there is audio playing, but when there is no audio the sound becomes a bit annoying yes.

author: Dave_PT
author_rank: Frequent Contributor
timestamp: April 27, 2017, 06:50:47 pm
content: I enabled the persistence and set the scope to trigger only after 320uS (15 pulses of 47KHz).I did not observe any abnormal variation in period or duty cycle.

author: vealmike
author_rank: Regular Contributor
timestamp: April 28, 2017, 06:27:40 am
content: Quote from: Dave_PT on April 27, 2017, 06:50:47 pmDid you hang a by 15 divider on the 47KHz and use that as the trigger?Lets make a guess. It's likely wrong, but the goal is to understand how the aggressor is modulating the signal, so wrong doesn't matter.Possibly something in the uP is altering the mark space on one cycle every 15. If you trigger on any old edge then you will struggle to see this. On the first trigger it might be the 15th edge that moves a smidge. On the second trigger the 2nd edge. With persistence on, all the edges get a bit fatter and you see nothing.Whereas, if you feed the 47KHz into some /15 logic, then you know that you are triggering at the same edge (relative to your aggressor waveform).Could be a complete waste of effort, debug is like that.

author: Someone
author_rank: Super Contributor
timestamp: April 28, 2017, 07:27:15 am
content: Probe the power supply to the micro controller, what noise is on there?

author: Dave_PT
author_rank: Frequent Contributor
timestamp: April 28, 2017, 08:32:43 am
content: Quote from: Someone on April 28, 2017, 07:27:15 amTHISimage is right next to the power pins of the MCU.

author: Dave_PT
author_rank: Frequent Contributor
timestamp: April 28, 2017, 08:42:53 am
content: Quote from: vealmike on April 28, 2017, 06:27:40 amBut if I trigger on all the rising edges and enable persistence eventually I should start seeing the edges to "get fat" ...So far I do not observe this...

author: Kleinstein
author_rank: Super Contributor
timestamp: April 28, 2017, 09:48:24 am
content: The extra peaks very much look like aliasing. The 47 kHz PWM signal contains significant harmonics. So even with 250 kHz sampling there would be be aliasing to something like 5 times the 47 kHz. So it is mainly a fault of the scope, in not having a suitable filter when using a low sampling rate.One should see if it is aliasing by changing the PWM frequency slightly (e.g. change upper limit to something 1000 instead of 1023). This should shift the frequencies in the signal by a certain factor. Peaks from aliasing are shifting more and some of them even in the other direction as they are at a frequency that related to the signal (and thus shifting) minus a fixed one (related to sampling).The later FFTs done after the amplifier still show a little noise background at something like -70 dB. This is not much, but can still be audible. It is very unlikely to have noise from the speakers. So it is more like the amplifier or DWM with filter than can be at fault. Noise on the supply can be an issue. The noise may also change with PWM active - switching can case RF range ringing and this can result in audible noise too.

author: Dave_PT
author_rank: Frequent Contributor
timestamp: April 28, 2017, 10:13:08 am
content: Maybe it's from my imagination, but I found some similarity between these two images ...Measurement after filter with freq of 2.5KHz:2.5KHz Tone: