# Audio using PWM, what is the principle behind it?

I have seen a schematic of a PIC board which uses filtered PWM to provide audio output signal to an audio jack. It shows PWM output filtered using 3 stages of passive RC filter followed by an LM386 stage. I have the following questions:

1. Usually an audio signal would have multiple frequencies summed up simultaneously. How does PWM do that?
2. Is the audio quality as good as using PCM with DAC, filter and amplifier?
3. Since this technique looks and is so convenient, why don't all audio devices use this to save money and cost, including sound cards in computers?
• You might want to read the wikipedia article about class D amps Commented Jun 6, 2016 at 13:47
• If you're using a lm386 as your amplifier then the audio quality is gonna be poor regardless Commented Jun 6, 2016 at 14:00
• @PlasmaHH ... or this Maxim app note on Class D amplifiers. Commented Jun 6, 2016 at 16:02
• For a non-constant frequency square wave system, look up delta-sigma or sigma-delta modulation. It works for ADCs and DACs and can be a nearly-completely-digital system for implementing a DAC. Commented Jun 6, 2016 at 19:33
• 1A) How can a single analog signal contain multiple frequencies summed up simultaneously? 1B) How can PWM approximate a single analog signal? Commented Jun 6, 2016 at 20:30

Usually an audio signal would have multiple frequencies summed up simultaneously. How does PWM do that?

The audio signal that contains a spectrum of multiple frequencies is still just an audio signal that can be sampled by and ADC and recreated by a DAC. Providing the sampling rate used is higher than twice the highest audio frequency then all is good. A DAC that uses PWM techniques is no different. In any one cycle of the PWM waveform, the ratio of mark-to-space must accurately "represent" the instantaneous analogue signal and a single PWM cycle must be shorter in time than half the period of the highest audio signal: -

The above is a simple representation of 3 DC levels using PWM. Clearly if the PWM frequency is "high" those three levels can be regarded as part of a complex AC waveform. Hopefully you can see that controlling the PWM mark-space ratio accurately is really fundamental to obtaining low audio distortion.

Is the audio quality as good as using PCM with DAC, filter and amplifier?

Traditionally no, but it's getting better.

Since this technique looks and is so convenient, why don't all audio devices use this to save money and cost, including sound cards in computers?

Controlling PWM ratio accuracy is quite difficult to get really good hi-fi quality and with class D amplifiers power supply rejection is still a pretty difficult challenge. See the embedded picture above - if the 5V power rail doubled then the gain also doubles - now imagine that instead of it simply doubling, you had a load of crappy noise on that rail - this would directly modulate your audio signal and create some very noticeable effects.

• Duty cycle controls amplitude and pwm instantaneous frequency is equal to signal instantaneous frequency, correct? Commented Jun 6, 2016 at 14:49
• PWM switching frequency MUST be higher than twice the highest audio frequency present to prevent aliasing (as per nyquist rate sampling): en.wikipedia.org/wiki/Nyquist_rate and en.wikipedia.org/wiki/Aliasing and cs.cf.ac.uk/Dave/Multimedia/node149.html Commented Jun 6, 2016 at 14:52
• "instantaneous frequency" is not a thing that makes sense. Output modulation of PWM is done by having a fixed very high PWM frequency and varying the duty cycle to match the desired analog output level at each sample time period. Commented Jun 6, 2016 at 15:00
• So how fast we vary the duty cycle of the fixed frequency pwm, shall generate a signal with amplitude that varies proportionally and thus, the final signal frequency component is controlled by how fast we change the pwm signal duty cycle? AWESOME!!! Commented Jun 6, 2016 at 15:05
• @vaxquis I disagree. Any one cycle of PWM can have a mark space ratio of whatever precision depth it is designed for irrespective of the speed of the analogue signal. It's just like a conventional DAC - a signal may have sparse sampling but the bit depth (aka duty cycle resolution) is unaffected. Maybe you haven't explained yourself very well? Commented Jun 7, 2016 at 7:20

PCM with DAC, filter and amplifier

This depends on how your DAC is built internally. Most sound card DACs will be using sigma-delta modulation, which resembles PWM in that it's a one-bit signal turned on and off at high speed through a filter, but using a smarter algorithm to ensure the correct output level and slew rate.

This example sound card codec datasheet has a nice diagram on the first page.

You can get quite decent sound out of pure PWM if your PWM is fast enough. It needs to have a PWM frequency much higher than the highest audio frequency you want, into the MHz region.

• The pwm duty cycle is directly proportional to signal amplitude, how is audio signal frequency represented? Commented Jun 6, 2016 at 14:51
• Yes, PWM duty cycle gives you a signal level, so if you treat each 1/40000s period as a "sample" and adjust the PWM level at that rate you can pretend that you've output an analog level at each point in time. Again, the PWM frequency has to be much faster than the frequency of audio samples for playback. Commented Jun 6, 2016 at 14:57
• @quantum231: Forget PWM for a moment and consider a digital encoding where 1 means increase voltage and 0 means decrease voltage. It's easy to imagine drawing an arbitrary waveform by stringing together 1s and 0s. It won't be accurate but good enough. It will suck for absolute silence since it can't really encode "no voltage change" but works well with most audio waveforms. Commented Jun 7, 2016 at 3:27
• @slebetman How is what you're describing different from DSD / Pulse Density Modulation? It uses nothin' but 0 (no output) and 1 (full output) but CD-level quality requires megabits (plural) of throughput. If you thought PWM required a very high sample rate to get close to traditional 16-bit PCM, that's going to take even more. Commented Feb 12, 2020 at 16:06
• @Meower68 I'm describing delta encoding. A simpler form of differential encoding from delta-sigma encoding used in DSD. Yes, basically I'm describing DSD. But PWM coupled with a capacitor works the same way. The OP is asking how PWM period is converted into voltage - I'm merely describing the mechanism behind it. Technically DSD is a little bit different from pure PWM encoding Commented Feb 12, 2020 at 16:35