Hi All, I am using the PWM unit in a PIC18F6720 to generate a number of tones using DDS. All seemed to be well until I hooked up a spectrum analyser. The 2nd harmonic distortion of a 400Hz tone was sitting at about -26dB. At first I thought it was a problem with the single quadrant lookup table and after a lot of testing and making of .WAV files I came to the conclusion that it had to be somthing to do with the PWM process. To confirm this I setup an 8 bit R-2R network and output the data to this simple DAC and voila! The 2nd harmonic distorion was now -45dB below the fundamental. I thought maybe there is something dodgy about the PWM linearity, so I checked the output m/s ratio for codes from 0x00 to 0xFF and it was beatifully linear. The PRF is running at 14400Hz, sample rate at 7200Hz. Is there something I am missing about using PWM to synthesise audio frequency tones? Regards Robert
PWM as DAC problems
Started by ●September 10, 2003
Reply by ●September 10, 20032003-09-10
Robert Gush wrote:> Hi All, > > I am using the PWM unit in a PIC18F6720 to generate a number of tones > using DDS. All seemed to be well until I hooked up a spectrum > analyser. The 2nd harmonic distortion of a 400Hz tone was sitting at > about -26dB. At first I thought it was a problem with the single > quadrant lookup table and after a lot of testing and making of .WAV > files I came to the conclusion that it had to be somthing to do with > the PWM process. > > To confirm this I setup an 8 bit R-2R network and output the data to > this simple DAC and voila! The 2nd harmonic distorion was now -45dB > below the fundamental. > > I thought maybe there is something dodgy about the PWM linearity, so I > checked the output m/s ratio for codes from 0x00 to 0xFF and it was > beatifully linear. > The PRF is running at 14400Hz, sample rate at 7200Hz. > > Is there something I am missing about using PWM to synthesise audio > frequency tones?I take it you have a low-pass filter after the PWM and before sampling? Ben -- I'm not just a number. To many, I'm known as a String...
Reply by ●September 10, 20032003-09-10
Robert Gush wrote:> > I am using the PWM unit in a PIC18F6720 to generate a number of tones > using DDS. All seemed to be well until I hooked up a spectrum > analyser. The 2nd harmonic distortion of a 400Hz tone was sitting at > about -26dB.[...]> Is there something I am missing about using PWM to synthesise audio > frequency tones?1. You need to have a good LPF after a PWM modulator. 2. The sidebands of the PWM spectrum are falling into the audio range. The PWM sample frequency should be at least 10 times higher then audio. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
Reply by ●September 10, 20032003-09-10
In comp.dsp, robert@suesound.co.za (Robert Gush) wrote:>Hi All, > >I am using the PWM unit in a PIC18F6720 to generate a number of tones >using DDS. All seemed to be well until I hooked up a spectrum >analyser. The 2nd harmonic distortion of a 400Hz tone was sitting at >about -26dB. At first I thought it was a problem with the single >quadrant lookup table and after a lot of testing and making of .WAV >files I came to the conclusion that it had to be somthing to do with >the PWM process.What's your 'modulation index', or what percentage of the full PWM range does the peak-to-peak 400Hz wave take up? If you lower the modulation level and make up for it with more gain on the analog side, you will have less distortion. The tradeoff is that you get more noise (the absolute noise level is the same, but it's higher in relation to the lowered signal level). I've done PWM at 9600Hz, and generated DTMF and 300 baud mark-space modem tones with it. I used a several-pole LPF AND a twin-t r-c notch filter at 9600Hz on the PWM output, and got good (well within acceptable/legal limits for the phone line) results. I didn't go through the math of exactly what PWM does, but with an analyzer I did notice the distortion products going up disproportionally as the modulation was increased and saw that this was a property of PWM. There are (at least) two ways of generating PWM. One is where one edge always occurs at the same time (in relation to the generating frequency) and the opposite edge varies. This is analogous to the output of a comparator driven by [the input signal and] a sawtooth wave. The other varies both edges complementarily, like a comparator driven by a triangle wave. The PWM I generated only varied one edge, but I believe the one that varies both edges generates less distortion. This distortion appears to be a dirty secret about PWM that is rarely discussed and not well known about. I don't know where the math is that describes it, but I'm pretty sure it's not simple. If someone has a good reference for it, that would (maybe!) be appreciated.>To confirm this I setup an 8 bit R-2R network and output the data to >this simple DAC and voila! The 2nd harmonic distorion was now -45dB >below the fundamental. > >I thought maybe there is something dodgy about the PWM linearity, so I >checked the output m/s ratio for codes from 0x00 to 0xFF and it was >beatifully linear. >The PRF is running at 14400Hz, sample rate at 7200Hz. > >Is there something I am missing about using PWM to synthesise audio >frequency tones? > >Regards >Robert
Reply by ●September 10, 20032003-09-10
"Ben Bradley" <ben_nospam_bradley@mindspring.com> wrote in message news:3f5fac4d.11198835@newsgroups.bellsouth.net...> In comp.dsp, robert@suesound.co.za (Robert Gush) wrote:> >I am using the PWM unit in a PIC18F6720 to generate a number of tones > >using DDS. All seemed to be well until I hooked up a spectrum > >analyser. The 2nd harmonic distortion of a 400Hz tone was sitting at > >about -26dB. At first I thought it was a problem with the single > >quadrant lookup table and after a lot of testing and making of .WAV > >files I came to the conclusion that it had to be somthing to do with > >the PWM process.(snip)> There are (at least) two ways of generating PWM. One is where one > edge always occurs at the same time (in relation to the generating > frequency) and the opposite edge varies. This is analogous to the > output of a comparator driven by [the input signal and] a sawtooth > wave. The other varies both edges complementarily, like a comparator > driven by a triangle wave. The PWM I generated only varied one edge, > but I believe the one that varies both edges generates less > distortion.Without actually trying to do the calculation, it does seem like that would generate second harmonic distortion. Anything that is non-linear between +x and -x will generate second harmonics. That is, not symmetric around zero. If it is symmetric around zero you should get only odd harmonics. The result is an amplitude dependent phase shift, which means that a second harmonic that would otherwise cancel out, doesn't cancel. -- glen
Reply by ●September 11, 20032003-09-11
"Glen Herrmannsfeldt" <gah@ugcs.caltech.edu> wrote in message news:<YwR7b.415582$uu5.74781@sccrnsc04>...> "Ben Bradley" <ben_nospam_bradley@mindspring.com> wrote in message > news:3f5fac4d.11198835@newsgroups.bellsouth.net... > > In comp.dsp, robert@suesound.co.za (Robert Gush) wrote: > > > >I am using the PWM unit in a PIC18F6720 to generate a number of tones > > >using DDS. All seemed to be well until I hooked up a spectrum > > >analyser. The 2nd harmonic distortion of a 400Hz tone was sitting at > > >about -26dB. At first I thought it was a problem with the single > > >quadrant lookup table and after a lot of testing and making of .WAV > > >files I came to the conclusion that it had to be somthing to do with > > >the PWM process. > > (snip) > > > There are (at least) two ways of generating PWM. One is where one > > edge always occurs at the same time (in relation to the generating > > frequency) and the opposite edge varies. This is analogous to the > > output of a comparator driven by [the input signal and] a sawtooth > > wave. The other varies both edges complementarily, like a comparator > > driven by a triangle wave. The PWM I generated only varied one edge, > > but I believe the one that varies both edges generates less > > distortion. > > Without actually trying to do the calculation, it does seem like that would > generate second harmonic distortion. Anything that is non-linear between > +x and -x will generate second harmonics. That is, not symmetric around > zero. If it is symmetric around zero you should get only odd harmonics. > > The result is an amplitude dependent phase shift, which means that a second > harmonic that would otherwise cancel out, doesn't cancel. >Thanks Glen & Ben, I doubled the PRF to 28800Hz, left the sample rate at 7200Hz. The 2nd harmonic has dropped by 6 dB and is now within spec. I don't suppose anyone has got a simple method of correcting this distortion? What about deliberately introducing an asymetrical distorion that is proportional to frequency? It would be fairly easy to do with the hardware multiplier as I am already doing amplitude modulation... Any thanks for the answer about the phase shift. Regards Robert
Reply by ●September 11, 20032003-09-11
Reply by ●September 11, 20032003-09-11
"Robert Gush" <robert@suesound.co.za> wrote in message news:a0f35ea5.0309102238.1c5a421f@posting.google.com...> "Glen Herrmannsfeldt" <gah@ugcs.caltech.edu> wrote in messagenews:<YwR7b.415582$uu5.74781@sccrnsc04>...> > "Ben Bradley" <ben_nospam_bradley@mindspring.com> wrote in message > > news:3f5fac4d.11198835@newsgroups.bellsouth.net... > > > In comp.dsp, robert@suesound.co.za (Robert Gush) wrote:(snip)> > (snip) > > > > > There are (at least) two ways of generating PWM. One is where one > > > edge always occurs at the same time (in relation to the generating > > > frequency) and the opposite edge varies. This is analogous to the > > > output of a comparator driven by [the input signal and] a sawtooth > > > wave. The other varies both edges complementarily, like a comparator > > > driven by a triangle wave. The PWM I generated only varied one edge, > > > but I believe the one that varies both edges generates less > > > distortion. > > > > Without actually trying to do the calculation, it does seem like thatwould> > generate second harmonic distortion. Anything that is non-linearbetween> > +x and -x will generate second harmonics. That is, not symmetric around > > zero. If it is symmetric around zero you should get only odd harmonics. > > > > The result is an amplitude dependent phase shift, which means that asecond> > harmonic that would otherwise cancel out, doesn't cancel. > > > > Thanks Glen & Ben, > > I doubled the PRF to 28800Hz, left the sample rate at 7200Hz. The 2nd > harmonic has dropped by 6 dB and is now within spec. > I don't suppose anyone has got a simple method of correcting this > distortion? > What about deliberately introducing an asymetrical distorion that is > proportional to frequency? It would be fairly easy to do with the > hardware multiplier as I am already doing amplitude modulation...If you can double the rate, I would think it wouldn't be too hard to make symmetric pulses. If you can't make symmetric pulses, alternating from going high at the beginning of the interval, and at the end of the interval, should correct that problem. Increasing the PRF by a factor of two decreases the phase shift by a factor of two, or 6dB. It will, though, I believe increase the second harmonic as the frequency increases, unless you can make them symmetric in either of the ways indicated. -- glen
Reply by ●September 11, 20032003-09-11
Ben Bradley wrote:>...> There are (at least) two ways of generating PWM. One is where one > edge always occurs at the same time (in relation to the generating > frequency) and the opposite edge varies. This is analogous to the > output of a comparator driven by [the input signal and] a sawtooth > wave. The other varies both edges complementarily, like a comparator > driven by a triangle wave. The PWM I generated only varied one edge, > but I believe the one that varies both edges generates less > distortion.... When the modulator output is steady, both methods yield waveforms that can be distinguished only by a timing reference. It's easy to see that this amounts to a phase shift. Varying only one edge superposes amplitude and phase modulations. At small duty-cycle changes -- you call that modulation index -- they have the same spectra, but the relative distortion increases as the swing does. Your conjecture is correct. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Reply by ●September 11, 20032003-09-11
ben_nospam_bradley@mindspring.com (Ben Bradley) wrote in message news:<3f5fac4d.11198835@newsgroups.bellsouth.net>...> There are (at least) two ways of generating PWM. One is where one > edge always occurs at the same time (in relation to the generating > frequency) and the opposite edge varies. This is analogous to the > output of a comparator driven by [the input signal and] a sawtooth > wave. The other varies both edges complementarily, like a comparator > driven by a triangle wave. The PWM I generated only varied one edge, > but I believe the one that varies both edges generates less > distortion.The distortion in "double-edge" PWM is smaller than in "single-edge" PWM. Performance is better if the locations of the two edges are determined by different sample values of the modulator input (asymmetric double-edge PWM) than by a single value (symmetric double-edge PWM).> This distortion appears to be a dirty secret about PWM that is > rarely discussed and not well known about. I don't know where the math > is that describes it, but I'm pretty sure it's not simple. If someone > has a good reference for it, that would (maybe!) be appreciated.Ummm... not a dirty secret, just a topic that is unfamiliar to many readers of comp.dsp. The power electronics people are very familiar with this type of stuff; just look in the IEEE Transactions on Power Electronics. Robert Gush (robert@suesound.co.za) wrote>I doubled the PRF to 28800Hz, left the sample rate at 7200Hz. The 2nd >harmonic has dropped by 6 dBThis is precisely the behavior predicted by theory. A paper describing in gory detail the time-domain and frequency representations of the various forms of PWM signals is scheduled to appear in the October 2003 issue of the European journal titled Signal Processing. If you cannot wait that long, a preprint can be downloaded from http://www.ifp.uiuc.edu/~sarwate/spectral.ps>I don't suppose anyone has got a simple method of correcting this >distortion? >What about deliberately introducing an asymetrical distorion that is >proportional to frequency? It would be fairly easy to do with the >hardware multiplier as I am already doing amplitude modulation...All the methods being discussed here are termed "uniform-sampling" PWM because the pulse widths are proportional to uniformly-spaced -in-time sample values of the modulating signal that is, the standard bread-and-butter discrete-time signal universally beloved in the DSP community. Converting such a signal to PWM is relatively easy but has problems with harmonic distortion. Now, it has long been a folk theorem in the PWM community that the "natural-sampling" form of PWM has **NO** harmonic distortion. A formal proof of this, applicable to all signals (not just the single-tone and two-tone signals that provide the basis for the folk theorem) is given in the paper cited above. Natural-sampling PWM is relatively easy to create with analog electronics when the modulating signal is available as a continuous-time signal. Natural-sampling PWM is **NOT** as easy to generate when the modulating signal is available only as a sequence of (uniformly spaced) samples. Some DSP-oriented natural sampling PWM schemes are described in a paper by Pascual et al. in the January 2003 issue of the IEEE Transactions on Power Electronics (preprint mss available at http://www.ifp.uiuc.edu/~sarwate/PWM_PEL.pdf). This paper describes a hi-fi audio amplifier putting more than 50W into an 8 ohm load with an efficiency of 82% -- far better than any analog linear amplifier -- and a THD of 0.022%. Vladimir Vassilevsky (vlv@abvolt.com) pointed out that>The sidebands of the PWM spectrum are falling into the audio rangeThis is true for both uniform-sampling PWM and natural-sampling PWM. In the former case, the harmonic distortion is overwhelmingly larger that the distortion caused by the sidebands. In the latter case, this sideband distortion is the *only* distortion when the PWM signal is demodulated by passing it through a low-pass filter. Thus, making the pulse repetition frequency as large as possible is the right thing to do (as Vladimir suggested) because it puts the sidebands far away and because it reduces harmonic distortion in uniform-sampling PWM. Curiously, these offending **passband** PWM signals consist of the modulating signal **perfectly** phase-modulated onto the carrier (pulse repetition) frequency and its harmonics; no need for varactor diodes or VCOs etc. This fact might be of more interest to the RFians reading this newsgroup... --Dilip Sarwate






