14
\$\begingroup\$

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?
\$\endgroup\$
  • 6
    \$\begingroup\$ You might want to read the wikipedia article about class D amps \$\endgroup\$ – PlasmaHH Jun 6 '16 at 13:47
  • 4
    \$\begingroup\$ If you're using a lm386 as your amplifier then the audio quality is gonna be poor regardless \$\endgroup\$ – JIm Dearden Jun 6 '16 at 14:00
  • 2
    \$\begingroup\$ @PlasmaHH ... or this Maxim app note on Class D amplifiers. \$\endgroup\$ – Nick Alexeev Jun 6 '16 at 16:02
  • \$\begingroup\$ 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. \$\endgroup\$ – user2943160 Jun 6 '16 at 19:33
  • 1
    \$\begingroup\$ 1A) How can a single analog signal contain multiple frequencies summed up simultaneously? 1B) How can PWM approximate a single analog signal? \$\endgroup\$ – user253751 Jun 6 '16 at 20:30
18
\$\begingroup\$

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: -

enter image description here

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Duty cycle controls amplitude and pwm instantaneous frequency is equal to signal instantaneous frequency, correct? \$\endgroup\$ – quantum231 Jun 6 '16 at 14:49
  • \$\begingroup\$ 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 \$\endgroup\$ – Andy aka Jun 6 '16 at 14:52
  • \$\begingroup\$ "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. \$\endgroup\$ – pjc50 Jun 6 '16 at 15:00
  • \$\begingroup\$ 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!!! \$\endgroup\$ – quantum231 Jun 6 '16 at 15:05
  • 1
    \$\begingroup\$ @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? \$\endgroup\$ – Andy aka Jun 7 '16 at 7:20
8
\$\begingroup\$

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.enter image description here

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.

See Converting PWM into an analog signal

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ The pwm duty cycle is directly proportional to signal amplitude, how is audio signal frequency represented? \$\endgroup\$ – quantum231 Jun 6 '16 at 14:51
  • \$\begingroup\$ 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. \$\endgroup\$ – pjc50 Jun 6 '16 at 14:57
  • \$\begingroup\$ @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. \$\endgroup\$ – slebetman Jun 7 '16 at 3:27
  • \$\begingroup\$ @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. \$\endgroup\$ – Meower68 Feb 12 at 16:06
  • \$\begingroup\$ @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 \$\endgroup\$ – slebetman Feb 12 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.