Author Topic: Strange "harmonic" in PWM  (Read 4587 times)

0 Members and 1 Guest are viewing this topic.

Offline Dave_PTTopic starter

  • Frequent Contributor
  • **
  • Posts: 315
  • Country: pt
    • DavidMartinsEngineering
Strange "harmonic" in PWM
« on: April 27, 2017, 08:12:17 am »
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 ...
« Last Edit: April 27, 2017, 08:31:30 am by Dave_PT »
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Strange "harmonic" in PWM
« Reply #1 on: April 27, 2017, 08:52:37 am »
How is the PWM frequency derived from the source?  47kHz doesn't divide evenly into the common X MHz clocks...

« Last Edit: April 27, 2017, 09:23:58 am by hamster_nz »
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 7695
  • Country: de
  • A qualified hobbyist ;)
Re: Strange "harmonic" in PWM
« Reply #2 on: April 27, 2017, 09:01:13 am »
What kind of clock source are you using?
 

Offline Dave_PTTopic starter

  • Frequent Contributor
  • **
  • Posts: 315
  • Country: pt
    • DavidMartinsEngineering
Re: Strange "harmonic" in PWM
« Reply #3 on: April 27, 2017, 09:08:36 am »
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.92KHz

I load the comparator with 512 to make the DC of 50%.
« Last Edit: April 27, 2017, 09:12:36 am by Dave_PT »
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3452
  • Country: it
Re: Strange "harmonic" in PWM
« Reply #4 on: April 27, 2017, 10:05:46 am »
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
« Last Edit: April 27, 2017, 10:07:53 am by JPortici »
 
The following users thanked this post: Dave_PT

Offline Dave_PTTopic starter

  • Frequent Contributor
  • **
  • Posts: 315
  • Country: pt
    • DavidMartinsEngineering
Re: Strange "harmonic" in PWM
« Reply #5 on: April 27, 2017, 10:23:44 am »
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
Ok, makes a lot of sense!
In the case of the last images it is even a mistake with the sampling of the oscilloscope.

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 / .. )
Can 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.
 

Offline vealmike

  • Regular Contributor
  • *
  • Posts: 192
  • Country: gb
Re: Strange "harmonic" in PWM
« Reply #6 on: April 27, 2017, 10:31:06 am »
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 EDIT

I 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.
 

Offline Dave_PTTopic starter

  • Frequent Contributor
  • **
  • Posts: 315
  • Country: pt
    • DavidMartinsEngineering
Re: Strange "harmonic" in PWM
« Reply #7 on: April 27, 2017, 10:46:45 am »
* 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.
I'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.

* 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.
It can not do that, I think. But perhaps by enabling persistence I can see that there are deviations in the pulses ...

* 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.
Yes, I already did these 2 tests. With or without PWM running, nothing on the power rail comes up close to these frequencies.

Apologies if this is teaching you to suck eggs.
I appreciate it!!!  :-+ :-+
 

Offline alexanderbrevig

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Strange "harmonic" in PWM
« Reply #8 on: April 27, 2017, 10:56:00 am »
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? :)
 

Offline vealmike

  • Regular Contributor
  • *
  • Posts: 192
  • Country: gb
Re: Strange "harmonic" in PWM
« Reply #9 on: April 27, 2017, 11:02:38 am »
It can not do that, I think. But perhaps by enabling persistence I can see that there are deviations in the pulses ...
Good idea, but you'll need to divide the PWM clock by 15 and use that as a 'scope trigger.
 

Offline Dave_PTTopic starter

  • Frequent Contributor
  • **
  • Posts: 315
  • Country: pt
    • DavidMartinsEngineering
Re: Strange "harmonic" in PWM
« Reply #10 on: April 27, 2017, 11:05:28 am »
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? :)
I do not know if the buzz will be at 3KHz. My ear spectrometer does not work very well  :-DD.
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.
 

Offline alexanderbrevig

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Strange "harmonic" in PWM
« Reply #11 on: April 27, 2017, 11:11:29 am »
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 headphones :)
If it's not detectable in this setup, I would not spend more time on it - unless of course you find it fun and/or educational  :-+

EDIT: typos...
 

Offline Dave_PTTopic starter

  • Frequent Contributor
  • **
  • Posts: 315
  • Country: pt
    • DavidMartinsEngineering
Re: Strange "harmonic" in PWM
« Reply #12 on: April 27, 2017, 01:26:33 pm »
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 speaker

I 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.
« Last Edit: April 27, 2017, 01:51:11 pm by Dave_PT »
 

Offline alexanderbrevig

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Strange "harmonic" in PWM
« Reply #13 on: April 27, 2017, 01:48:16 pm »
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 VCC :)

At 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?
 

Offline Dave_PTTopic starter

  • Frequent Contributor
  • **
  • Posts: 315
  • Country: pt
    • DavidMartinsEngineering
Re: Strange "harmonic" in PWM
« Reply #14 on: April 27, 2017, 06:26:42 pm »
Where is your filter set, at which frequency?
The filter cuts close to 5KHz and should have this response.


Is your PSU quiet? For fun, show us an FFT of your VCC :)
In 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!


Is it still annoying and audible to you?
Seems like it's introduced at the speaker? Or am I reading this wrong?
The 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.
 

Offline Dave_PTTopic starter

  • Frequent Contributor
  • **
  • Posts: 315
  • Country: pt
    • DavidMartinsEngineering
Re: Strange "harmonic" in PWM
« Reply #15 on: April 27, 2017, 06:50:47 pm »
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.
 

Offline vealmike

  • Regular Contributor
  • *
  • Posts: 192
  • Country: gb
Re: Strange "harmonic" in PWM
« Reply #16 on: April 28, 2017, 06:27:40 am »
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.
Did 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.

 

Online Someone

  • Super Contributor
  • ***
  • Posts: 4510
  • Country: au
    • send complaints here
Re: Strange "harmonic" in PWM
« Reply #17 on: April 28, 2017, 07:27:15 am »
Probe the power supply to the micro controller, what noise is on there?
 

Offline Dave_PTTopic starter

  • Frequent Contributor
  • **
  • Posts: 315
  • Country: pt
    • DavidMartinsEngineering
Re: Strange "harmonic" in PWM
« Reply #18 on: April 28, 2017, 08:32:43 am »
Probe the power supply to the micro controller, what noise is on there?

THIS image is right next to the power pins of the MCU.
 

Offline Dave_PTTopic starter

  • Frequent Contributor
  • **
  • Posts: 315
  • Country: pt
    • DavidMartinsEngineering
Re: Strange "harmonic" in PWM
« Reply #19 on: April 28, 2017, 08:42:53 am »
Did 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.

But 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...
 

Offline Kleinstein

  • Super Contributor
  • ***
  • Posts: 14076
  • Country: de
Re: Strange "harmonic" in PWM
« Reply #20 on: April 28, 2017, 09:48:24 am »
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.
 

Offline Dave_PTTopic starter

  • Frequent Contributor
  • **
  • Posts: 315
  • Country: pt
    • DavidMartinsEngineering
Re: Strange "harmonic" in PWM
« Reply #21 on: April 28, 2017, 10:13:08 am »
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:


« Last Edit: April 28, 2017, 10:18:07 am by Dave_PT »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf