PDA

View Full Version : PWM as DAC problems


Robert Gush
09-10-2003, 12:10 PM
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

Ben Pope
09-10-2003, 12:39 PM
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...

Vladimir Vassilevsky
09-10-2003, 01:48 PM
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

Ben Bradley
09-11-2003, 12:35 AM
In comp.dsp, [email protected] (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

Glen Herrmannsfeldt
09-11-2003, 03:58 AM
"Ben Bradley" <[email protected]> wrote in message
news:[email protected]...
> In comp.dsp, [email protected] (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

Robert Gush
09-11-2003, 07:38 AM
"Glen Herrmannsfeldt" <[email protected]> wrote in message news:<YwR7b.415582$uu5.74781@sccrnsc04>...
> "Ben Bradley" <[email protected]> wrote in message
> news:[email protected]...
> > In comp.dsp, [email protected] (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

Robert Gush
09-11-2003, 08:48 AM
Oops....
way
Any thanks for the answer about the phase shift.
^

Regards,

Robert

Glen Herrmannsfeldt
09-11-2003, 07:47 PM
"Robert Gush" <[email protected]> wrote in message
news:[email protected] om...
> "Glen Herrmannsfeldt" <[email protected]> wrote in message
news:<YwR7b.415582$uu5.74781@sccrnsc04>...
> > "Ben Bradley" <[email protected]> wrote in message
> > news:[email protected]...
> > > In comp.dsp, [email protected] (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 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...

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

Jerry Avins
09-11-2003, 08:23 PM
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.
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

Dilip V. Sarwate
09-11-2003, 08:27 PM
[email protected] (Ben Bradley) wrote in message news:<[email protected]>...

> 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 ([email protected]) wrote

>I doubled the PRF to 28800Hz, left the sample rate at 7200Hz. The 2nd
>harmonic has dropped by 6 dB

This 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 ([email protected]) pointed out that

>The sidebands of the PWM spectrum are falling into the audio range

This 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

Al Clark
09-11-2003, 10:57 PM
I discovered the even distortion problem many years ago when I was trying
to generate a sine wave using PWM.

Most PWM circuits in microcontrollers output high followed by low at
whatever duty cycle is set.

Here is an idea. I haven't tried it and I would be interested in
comments.

Assume that you have two PWM generators operating at the same frequency.
Two PWMs are often available in microcontrollers.

Set PWM1 with the DAC value. Set PWM2 with (1 - DAC value). Feed PWM1 &
PWM2 into a balanced input low pass filter where PWM1 and PWM2 are of
opposite phase.

I think this will reduce the distortion.

_______
___ PWM1 --- + Input


___
_______ PWM2 --- - Input

--
Al Clark
Danville Signal Processing, Inc.
--------------------------------------------------------------------
Purveyors of Fine DSP Hardware and other Cool Stuff
Available at http://www.danvillesignal.com

Jerry Avins
09-11-2003, 11:37 PM
"Dilip V. Sarwate" wrote:
>
...
>
> 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).
>
...

Why does the asymmetric form have less distortion than the symmetric?
How are the separate edges separately timed?

When the analog signal is available and constant frequency isn't needed,
a comparator makes simple PWM modulator.

Jerry
--
Engineering is the art of making what you want from things you can get.
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

Dilip V. Sarwate
09-12-2003, 04:00 AM
Jerry Avins <[email protected]> asked in message news:<[email protected]>...
>
> Why does the asymmetric form have less distortion than the symmetric?

Broadly speaking in the manner espoused by Julius Kusuma, since
the asymmetric form uses two samples per pulse (thus effectively
doubling the sample rate being used), we get better performance.

\|||/ \\\\| \|||/ |////
| \ | /

(Sorry, that's the only ASCII representation I could think of that
vaguely represents a vigorous waving of hands)

> How are the separate edges separately timed?

In natural-sampling PWM (with the analog signal available), the
modulating signal is continuously compared to a double sawtooth
waveform: (I hope everyone is using a monospace font!)

/\ /\ /\
/ \ / \ / \
/ \/ \/ \

The PWM pulse goes positive when the analog signal crosses the
down-ramp and back to 0 (or negative, depending on implementation)
when the analog signal crosses the up-ramp. If the input
signal is bandlimited to f_s, then making the pulse repetition
frequency at least pi.f_s ensures that there is a unique crossing
point for each ramp (up or down). Notice that when the signal is
positive, a broad pulse is created, whereas if the signal is
negative, a narrow pulse is created.

In uniform-sampling PWM, the signal is sampled at the peaks and
troughs of the double sawtooth shown. The sample value is then
held, and compared with the ramp to determine when to turn the
pulse on or off. The details are omitted.

> When the analog signal is available and constant frequency isn't needed,
> a comparator makes simple PWM modulator.

I agree with the sentiment expressed though I am unsure what the
qualification "constant frequency isn't needed" means...

--Dilip Sarwate

Robert Gush
09-12-2003, 06:29 AM
Al Clark <[email protected]> wrote in message news:<[email protected]>...
> I discovered the even distortion problem many years ago when I was trying
> to generate a sine wave using PWM.
>
> Most PWM circuits in microcontrollers output high followed by low at
> whatever duty cycle is set.
>
> Here is an idea. I haven't tried it and I would be interested in
> comments.
>
> Assume that you have two PWM generators operating at the same frequency.
> Two PWMs are often available in microcontrollers.
>
> Set PWM1 with the DAC value. Set PWM2 with (1 - DAC value). Feed PWM1 &
> PWM2 into a balanced input low pass filter where PWM1 and PWM2 are of
> opposite phase.
>
> I think this will reduce the distortion.

I think you are right Al, A bit of a throwback to the old push-pull
tube days. Unfortunately I'm using 4 of the 5 PWM outputs, so I don't
have the luxury of being able to put them into PUSH-PULL. What I did
notice when checking to see if it was a supply problem was that when
the one supply was dropped that until asymmetrical clipping occurred
than the 2nd harmonic distortion could be 'nulled' out quite a lot.
Since it is a 'pure' sine that is being generated it maybe it could be
fixed by changing the amplitude of HALF of the generated wave by an
amount that is dependant on the peak amplitude and the frequency.

Regards

Robert