Maker Pro
Maker Pro

PWM -> Audio

S

Silvar Beitel

Jan 1, 1970
0
Recently there were some comments in another thread about
microcontroller audio output techniques. (I can't find them now -
having to use Google Groups sucks - my otherwise reliable ISP
discontinued supporting a dedicated news server), and it leads me to
ponder a bit on what the best way to do this is.

On a prototype, using 8-bit audio (voice) data sampled at 8 kHz to
drive a PWM output, I just brute-forced the audio output path: A
multistage active LP filter (4 kHz corner freq) and a simple PA to
drive the earphones/speaker. I'd like to do something smarter/better/
cheaper on the next iteration.

Some options:

1) Multiply the PWM rate by N, reusing the data N times (or perhaps
interpolating), cut down on the active filtering required. However,
significantly upping the rate chews up processor cycles and increases
power. Enough to matter? I dunno yet.

2) Make a charge pump out of a tristateable output and internal
counters, drive it using differential data, and feed an external
integrator. The sampling frequency component at the output would be
significantly less compared to the PWM output and thus could result in
savings in the filtering circuitry.

3) Implement the "traditional" class-D LC filter output.

4) Just try to get the PWM frequency as high as the rest of the system
will allow and then drive the earphones/speaker directly, using the
speaker/headphones as part of the filter. Certainly cheap, but what
about emissions, especially out of probably non-shielded headphone
cords?

I haven't thought this all out, but am asking the group for input
because I'm sure some of you have already done this thinking for
me :) So, what have you done / would you do?

(P.S. The National LM48511 looks interesting. Have any of you used
it?)
 
V

Vladimir Vassilevsky

Jan 1, 1970
0
Silvar said:
Recently there were some comments in another thread about
microcontroller audio output techniques.

Yes you can get a reasonable quality speech output from a PWM on a
microcontroller.
Some options:

1) Multiply the PWM rate by N, reusing the data N times (or perhaps
interpolating), cut down on the active filtering required. However,
significantly upping the rate chews up processor cycles and increases
power. Enough to matter? I dunno yet.

This improves quality a lot. Run the PWM at 32kHz sample rate. Upsample
the speech to 32kHz by the simplest linear interpolation. This doesn't
take much computation. Use the double edge PWM rather then single edge.
2) Make a charge pump out of a tristateable output and internal
counters, drive it using differential data, and feed an external
integrator. The sampling frequency component at the output would be
significantly less compared to the PWM output and thus could result in
savings in the filtering circuitry.

This will attenuate the carrier however the sidebands will be the same.
The carrier at 32KHz is inaudible anyway, so I wouldn't care much about
it unless there are some other concerns.
3) Implement the "traditional" class-D LC filter output.

This is a 2nd order filter no better or worse then any other filter of
the 2nd order.
4) Just try to get the PWM frequency as high as the rest of the system
will allow and then drive the earphones/speaker directly, using the
speaker/headphones as part of the filter. Certainly cheap, but what
about emissions, especially out of probably non-shielded headphone
cords?

The reasonable PWM sample rate for the audio purpose is x10...x20 of the
maximum audio frequency of interest. There is little point to go higher
then that. Emissions are the huge problem with the PWM amps.
I haven't thought this all out, but am asking the group for input
because I'm sure some of you have already done this thinking for
me :) So, what have you done / would you do?

(P.S. The National LM48511 looks interesting. Have any of you used
it?)

It depends. What exactly are you trying to accomplish?


Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com
 
A

Arlet Ottens

Jan 1, 1970
0
Silvar said:
Recently there were some comments in another thread about
microcontroller audio output techniques. (I can't find them now -
having to use Google Groups sucks - my otherwise reliable ISP
discontinued supporting a dedicated news server), and it leads me to
ponder a bit on what the best way to do this is.

On a prototype, using 8-bit audio (voice) data sampled at 8 kHz to
drive a PWM output, I just brute-forced the audio output path: A
multistage active LP filter (4 kHz corner freq) and a simple PA to
drive the earphones/speaker. I'd like to do something smarter/better/
cheaper on the next iteration.

Some options:

1) Multiply the PWM rate by N, reusing the data N times (or perhaps
interpolating), cut down on the active filtering required. However,
significantly upping the rate chews up processor cycles and increases
power. Enough to matter? I dunno yet.

2) Make a charge pump out of a tristateable output and internal
counters, drive it using differential data, and feed an external
integrator. The sampling frequency component at the output would be
significantly less compared to the PWM output and thus could result in
savings in the filtering circuitry.

3) Implement the "traditional" class-D LC filter output.

4) Just try to get the PWM frequency as high as the rest of the system
will allow and then drive the earphones/speaker directly, using the
speaker/headphones as part of the filter. Certainly cheap, but what
about emissions, especially out of probably non-shielded headphone
cords?

I haven't thought this all out, but am asking the group for input
because I'm sure some of you have already done this thinking for
me :) So, what have you done / would you do?

(P.S. The National LM48511 looks interesting. Have any of you used
it?)

5) Use a controller with real DAC

6) Use 2 PWM outputs. Use one for most significant 4 bits, other for
least significant bits, and combine outputs at 1:16 ratio. This
increases the frequency of the PWM noise.
 
M

MooseFET

Jan 1, 1970
0
Recently there were some comments in another thread about
microcontroller audio output techniques. (I can't find them now -
having to use Google Groups sucks - my otherwise reliable ISP
discontinued supporting a dedicated news server), and it leads me to
ponder a bit on what the best way to do this is.

On a prototype, using 8-bit audio (voice) data sampled at 8 kHz to
drive a PWM output, I just brute-forced the audio output path: A
multistage active LP filter (4 kHz corner freq) and a simple PA to
drive the earphones/speaker. I'd like to do something smarter/better/
cheaper on the next iteration.

Some options:

1) Multiply the PWM rate by N, reusing the data N times (or perhaps
interpolating), cut down on the active filtering required. However,
significantly upping the rate chews up processor cycles and increases
power. Enough to matter? I dunno yet.

A small increase in the PWM rate can make quite a large improvement in
the quality of the voice data. If you assume an upper limit on the
bandwidth of the audio of 4KHz, you want to sample at more than twice
the 4KHz.

The Nyquist frequency is the first frequency you can't fully
reepresent and not the last one you can. You want to suppress Nyquist
and above. When Nyquist equals the top of the band, this is
imposible.


2) Make a charge pump out of a tristateable output and internal
counters, drive it using differential data, and feed an external
integrator. The sampling frequency component at the output would be
significantly less compared to the PWM output and thus could result in
savings in the filtering circuitry.

You can stop short of that. The PWM data can have its frequency
responce adjusted to correct for the shape of the filter. This allows
a much simpler filter because the power in the signal decreases
greatly above 1KHz. Your filter could have its 3dB point at 1.5KHz and
the data could be boosted above 1.5KHz.

4) Just try to get the PWM frequency as high as the rest of the system
will allow and then drive the earphones/speaker directly, using the
speaker/headphones as part of the filter. Certainly cheap, but what
about emissions, especially out of probably non-shielded headphone
cords?

If you do this never test it to see if it passes FCC.
 
S

Silvar Beitel

Jan 1, 1970
0
Vladimir said:
Yes you can get a reasonable quality speech output from a PWM on a
microcontroller.


This improves quality a lot. Run the PWM at 32kHz sample rate. Upsample
the speech to 32kHz by the simplest linear interpolation. This doesn't
take much computation. Use the double edge PWM rather then single edge.

Yes, this seems like the first thing to do.
This will attenuate the carrier however the sidebands will be the same.
The carrier at 32KHz is inaudible anyway, so I wouldn't care much about
it unless there are some other concerns.

I presume it would make it so that the carrier and sidebands would be
suppressed in the output with simpler (read: cheaper) filtering.
This is a 2nd order filter no better or worse then any other filter of
the 2nd order.

But possibly cheaper, with lower component count, than an active
filter?
The reasonable PWM sample rate for the audio purpose is x10...x20 of the
maximum audio frequency of interest. There is little point to go higher
then that. Emissions are the huge problem with the PWM amps.

Well, that's part of what I'm asking about. Class D appeals for its
cheapness and efficiency, but emissions are tough, as you say. Note
that the LM48511 part I mentioned dithers the carrier frequency
specifically to spread the noise around so the Feds won't find it on a
spectrum analyzer :)
It depends. What exactly are you trying to accomplish?

Cheap, efficient, acceptable emissions, low power consumption voice
output from a cheap uC w/o a D/A from 4-kHz-bandlimited voice data
sampled at 8 kHz. It would be nice if the National part a) was cheap
and b) had digital input so I could just dump the voice data into it
directly without converting it to an analog signal.
Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com

Thanks for your input.
 
S

Silvar Beitel

Jan 1, 1970
0
Arlet said:
5) Use a controller with real DAC

I would if I could find one that had all the other junk I wanted on
it.
6) Use 2 PWM outputs. Use one for most significant 4 bits, other for
least significant bits, and combine outputs at 1:16 ratio. This
increases the frequency of the PWM noise.

Hmm. I'll have to think about that. I suppose I could just go the
whole hog and stick an R-2R resistor pack on it and roll my own DAC
that way, but I may not have enough pins/outputs available for that.

Thanks for your input.
 
S

Silvar Beitel

Jan 1, 1970
0
MooseFET said:
A small increase in the PWM rate can make quite a large improvement in
the quality of the voice data. If you assume an upper limit on the
bandwidth of the audio of 4KHz, you want to sample at more than twice
the 4KHz.

The Nyquist frequency is the first frequency you can't fully
reepresent and not the last one you can. You want to suppress Nyquist
and above. When Nyquist equals the top of the band, this is
imposible.

Yes. This is voice audio < 4 kHz sampled at 8 kHz.
You can stop short of that. The PWM data can have its frequency
responce adjusted to correct for the shape of the filter. This allows
a much simpler filter because the power in the signal decreases
greatly above 1KHz. Your filter could have its 3dB point at 1.5KHz and
the data could be boosted above 1.5KHz.

Interesting thought, but computationally not something I want to do in
the microcontroller! For voice data sourced from a server or burned
into ROM, sounds like a good idea.
If you do this never test it to see if it passes FCC.

I can buy stickers cheap :)

Thanks for your input.
 
J

John Larkin

Jan 1, 1970
0
Recently there were some comments in another thread about
microcontroller audio output techniques. (I can't find them now -
having to use Google Groups sucks - my otherwise reliable ISP
discontinued supporting a dedicated news server), and it leads me to
ponder a bit on what the best way to do this is.

On a prototype, using 8-bit audio (voice) data sampled at 8 kHz to
drive a PWM output, I just brute-forced the audio output path: A
multistage active LP filter (4 kHz corner freq) and a simple PA to
drive the earphones/speaker. I'd like to do something smarter/better/
cheaper on the next iteration.

Some options:

1) Multiply the PWM rate by N, reusing the data N times (or perhaps
interpolating), cut down on the active filtering required. However,
significantly upping the rate chews up processor cycles and increases
power. Enough to matter? I dunno yet.

2) Make a charge pump out of a tristateable output and internal
counters, drive it using differential data, and feed an external
integrator. The sampling frequency component at the output would be
significantly less compared to the PWM output and thus could result in
savings in the filtering circuitry.

3) Implement the "traditional" class-D LC filter output.

4) Just try to get the PWM frequency as high as the rest of the system
will allow and then drive the earphones/speaker directly, using the
speaker/headphones as part of the filter. Certainly cheap, but what
about emissions, especially out of probably non-shielded headphone
cords?

I haven't thought this all out, but am asking the group for input
because I'm sure some of you have already done this thinking for
me :) So, what have you done / would you do?

(P.S. The National LM48511 looks interesting. Have any of you used
it?)


We often use delta-sigma instead of pwm to make 1-bit dac outputs. But
that's usually in an fpga, where computation is cheap. D-S doesn't
have the constant-frequency pwm carrier frequency issue.

It would take a periodic process, doing roughly a dozen lines of
assembly code - depending on the machine - to bit-bang a port pin in
delta-sigma mode.

John
 
R

Rich Grise

Jan 1, 1970
0
Vladimir Vassilevsky wrote: ....

Cheap, efficient, acceptable emissions, low power consumption voice
output from a cheap uC w/o a D/A from 4-kHz-bandlimited voice data
sampled at 8 kHz. It would be nice if the National part a) was cheap
and b) had digital input so I could just dump the voice data into it
directly without converting it to an analog signal.

I suppose if you had a spare 8-bit output port, you wouldn't be
asking, but if you do, an R-2R ladder comes to mind...

Good Luck!
Rich
 
V

Vladimir Vassilevsky

Jan 1, 1970
0
MooseFET wrote:

A small increase in the PWM rate can make quite a large improvement in
the quality of the voice data. If you assume an upper limit on the
bandwidth of the audio of 4KHz, you want to sample at more than twice
the 4KHz.

The Nyquist frequency is the first frequency you can't fully
reepresent and not the last one you can. You want to suppress Nyquist
and above. When Nyquist equals the top of the band, this is
imposible.

PWM is the nonlinear operation, and this makes the things complicated.
Although the Nyquist condition applies for the PWM, the nonlinear
processing of the original signal is required to restore it acurately.
That is not very simple; the brute force way to deal with that is to
boost the PWM rate to somewhat x10...x20 of the maximum audio frequency
of interest.

The PWM data can have its frequency
responce adjusted to correct for the shape of the filter. This allows
a much simpler filter because the power in the signal decreases
greatly above 1KHz. Your filter could have its 3dB point at 1.5KHz and
the data could be boosted above 1.5KHz.

Furthermore, it is good to apply the preemphasis to the original voice
data and design the output PWM filter slope so it would do the
deemphasis. This improves the quality quite significantly.


Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com
 
M

MooseFET

Jan 1, 1970
0
Yes. This is voice audio < 4 kHz sampled at 8 kHz.
But how much less than 4KHz? For voice 3KHz is usually considered
the highest frequency. 3KHz is too close to 4KHz for a really cheap
filter.
Interesting thought, but computationally not something I want to do in
the microcontroller!

Wimp!

Temp = PrevSample;
PrevSample = NewSample;
NewSample = NewSample - Temp/2;

This gives a 3X boost at Nyquist. The divide is a simple shift to the
right.
For voice data sourced from a server or burned
into ROM, sounds like a good idea.

The latter was my case.
 
M

MooseFET

Jan 1, 1970
0
PWM is the nonlinear operation, and this makes the things complicated.

How do you conclude it must be non-linear? I can think of ways it can
be but it seems that it needn't be. Even if it is non-linear, since
it is before the filter, you can correct the data to make it produce
the linear result.

Furthermore, it is good to apply the preemphasis to the original voice
data and design the output PWM filter slope so it would do the
deemphasis. This improves the quality quite significantly.

In the extreme case, you can apply preemphasis and then do a one bit
output to a comparitor and still understand what is being said.
 
V

Vladimir Vassilevsky

Jan 1, 1970
0
MooseFET wrote:

How do you conclude it must be non-linear?

PCM(x) + PCM(y) == PCM(x+y)
PWM(x) + PWM(y) != PWM(x+y)
I can think of ways it can
be but it seems that it needn't be.

Imagine a DC + small sinewave on the top of it. At the PWM output, the
phase shift of the sinewave will be dependent on the DC level. With two
sinewaves you will get all sorts of harmonics and intermods.
Even if it is non-linear, since
it is before the filter, you can correct the data to make it produce
the linear result.

It doesn't matter before or after filter. This is a dynamic nonlinearity
problem; it is fairly difficult to correct.

Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com
 
M

MooseFET

Jan 1, 1970
0
PCM(x) + PCM(y) == PCM(x+y)
PWM(x) + PWM(y) != PWM(x+y)

That is not enough to make it non-linear from our point of view. You
need some of the differences to be within the band of interest.

Imagine a DC + small sinewave on the top of it. At the PWM output, the
phase shift of the sinewave will be dependent on the DC level.

This depends on how the PWM is done. If only one edge of the PWM is
modulated, it is true but if both edges of the PWM are modulated, it
need not be.

With modulation of both edges in PWM, the phase will be constant as
the DC level is changed.
With two
sinewaves you will get all sorts of harmonics and intermods.

This is voice not music, so it matters a lot less.
 
V

Vladimir Vassilevsky

Jan 1, 1970
0
MooseFET said:
That is not enough to make it non-linear from our point of view. You
need some of the differences to be within the band of interest.

This is enough. The order of the difference is infinite.
This depends on how the PWM is done. If only one edge of the PWM is
modulated, it is true but if both edges of the PWM are modulated, it
need not be.

With modulation of both edges in PWM, the phase will be constant as
the DC level is changed.

Yes, the double sided PWM has lower distortion, then the single sided
PWM. However the double sided PWM is not free from distortion either.
This is voice not music, so it matters a lot less.

The PWM artifacts should be of the same order of magnitude as all other
artifacts (quantization, nonlinearity and such). This requires the PWM
rate to be at least at x10 of the max. frequency of interest, unless the
nonlinear correction is employed.

....but so much for the boring PWM stuff. Once you mentioned that you
made an invertor using the switching rectifier on the secondary side.
How did you control the cross conduction? For the proper operation, the
current should be switched from one side to another precisely. The
actual switching time depends on temperature, voltage and current. Did
you just use snubbers or did you have to adjust the dead time dynamically?


Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com
 
M

MooseFET

Jan 1, 1970
0
This is enough. The order of the difference is infinite.

I disagree. Merely saying that there is a difference doesn't mean
that there is distortion within the band of interest. Consider the
example of:

F(X) = X + sin(1E9 * t);

F(A+B) != F(A) + F(B)

Since all of the sin(1E9 * t) is well above the audio range, it
couldn't be considered as distortion in the OP's product. There is
likely to be some proof about the distortion subject and if I found it
more interesting than the subject below, I would continue with this
subject.


...but so much for the boring PWM stuff. Once you mentioned that you
made an invertor using the switching rectifier on the secondary side.
How did you control the cross conduction? For the proper operation, the
current should be switched from one side to another precisely. The
actual switching time depends on temperature, voltage and current. Did
you just use snubbers or did you have to adjust the dead time dynamically?

I did, sort of, neither. First I will fill any who may be reading
along in on what the heck we are talking about.

I built a 12V DC in to 120V AC at 60Hz out DC to AC converter that
made a good sine wave on its output.

The basic design has a push pull drive to a tranformer and an array of
MOSFETs on the secondary side. After the MOSFETs was a LC filter to
smoothen out the sine wave.


ASCII ART:
L2
GND--[SW]-- ---[SW]----+----)))))----+----
) !! ( ! !
L1 ) !! ( ! ---
---))))------+ !! +--GND ! ---
) !! ( ! !
) !! ( ! GND
GND--[SW]-- ---[SW]----

The secondary side used pairs of MOSFETs connected source to source so
that the body diodes would not conduct when the switch was supposed to
be off.

The gates of the secondary MOSFETs were driven by a transformer so
their switching was not quite as fast as the primary ones. As a
result, the switching situation on the two sides were slightly
different.

The primary side MOSFETs swithed very quickly. The capacitance in the
tranformer would normally have made a current spike during the
switching. L1 blocks this spike and absorbs energy. L1 was connected
to a "snubbing circuit" sort of like this:

L1
----+----(((((----+------
! !
! ---
! ---
! ! L3
----!<-------+---))))---- GND

L3 is mechanically quite small because it only needs to store a
smallish amount of energy. The nice thing about this circuit is that
the energy it put back onto the input power and not turned into heat
as would normally be the case.

The practical effect of this primary side was that the secondary had a
small nearly constant step on its switching edges. Whichever
secondary side switch was on at that time would conduct the edge to
the L2 inductor. Since the frequencies in this edge were way above
60Hz, the LC filter removed them. Variations in the edge with load
etc are a different matter that I will get to later.

The secondary side switches are a bit slower. There is a short time
during which the secondary is open circuited so there is a voltage
spike. IIRC the MOSFETs I used were IRF6N60 or the like and didn't
like drain spikes. Adding just a couple of capacitors on the
secondary would decrease the peak of the spikes at the cost of making
some very ugly ringing. A small resistance in series with the
capacitance was needed to damp this. The capacitance caused a spike
on the L3 and I am fairly sure that most of the energy in the
secondary side switching ended up in the input side.

The phase relationship between the primary and secondary side
switching is what determined the output voltages. This phase was slid
back and forth at a 60Hz rate to construct my output side sinewave.

Running as an open loop system, the output wave form was quite a good
sine wave but its amplitude varied with the input voltage. When I
switched to using a PID like controller for the feedback, this problem
was largely solved for reasonable changes in the input.

The controller also supressed any distortion up to several times the
cut off frequency of the LC filter, but there was a bit of an issue
with getting enough phase margin at all posible gain crossover
frequencies. The "D" part of the controller ended up with an extra RC
in it somewhere.

The phase shifting circuit ran at 2x the switching frequency. It
determined how late in the alternation, the secondary side switches
got updated with the current state of the primary side switches. This
means that give or take some cross talk, the secondary duty cycle
didn't depend on the accuracy of the analog parts.

The linearity of the phase shifter was determined largely by how
linear of a ramp could be made.
 
J

JosephKK

Jan 1, 1970
0
Silvar Beitel [email protected] posted to
sci.electronics.design:
I would if I could find one that had all the other junk I wanted on
it.


Hmm. I'll have to think about that. I suppose I could just go the
whole hog and stick an R-2R resistor pack on it and roll my own DAC
that way, but I may not have enough pins/outputs available for that.

Thanks for your input.

Hmmm. Perhaps a high speed serial interface to a real DAC.
 
J

JosephKK

Jan 1, 1970
0
MooseFET [email protected] posted to sci.electronics.design:
How do you conclude it must be non-linear? I can think of ways it
can
be but it seems that it needn't be. Even if it is non-linear, since
it is before the filter, you can correct the data to make it produce
the linear result.



In the extreme case, you can apply preemphasis and then do a one bit
output to a comparitor and still understand what is being said.

Alternatively you can transcode it and use a telephone type CVSD.
 
J

JosephKK

Jan 1, 1970
0
John Larkin [email protected] posted to
sci.electronics.design:
We often use delta-sigma instead of pwm to make 1-bit dac outputs.
But that's usually in an fpga, where computation is cheap. D-S
doesn't have the constant-frequency pwm carrier frequency issue.

It would take a periodic process, doing roughly a dozen lines of
assembly code - depending on the machine - to bit-bang a port pin in
delta-sigma mode.

John

You have me curious here, are you feeding a traditional CVSD chip with
that or are you somehow using it directly (just shoved into a
filter)? In either case what is the bit bang rate, and the effective
audio bandwidth.
 
M

MooseFET

Jan 1, 1970
0
Hmmm. Perhaps a high speed serial interface to a real DAC.

Depending on the micro, a parallel port to a DAC may be better, but a
real DAC sounds like a good idea. A voltage output DAC feeding a very
simple low pass filter would give good performance with a low number
of parts.
 
Top