Can I get a "clean" audio output?

Hello there. Here the thing is: is it possible to get a "clean" PWM-ed audio output? Let me explain you what's going on.

I've made a simple stereo WAV player by using the TMRpcm library. It works pretty well, but I'm listening a clicking noise along with the audio (maybe that's a serial clock pulse of the SPI?).

Is possible to get rid of that noise, or is not that easy?

Thanks beforehand!

Schematic? Link to the library? Code?

With opamps and digital to analog converters and other analog circuits, there's a specification called PSRR, which stands for Power Supply Rejection Ratio.

For example, a DAC or opamp might have PSRR of 60 dB (values from 50 to 90 dB are common). Typically it's specified at a certain frequency, and for better devices a graph is sometimes given showing how it varies by frequency.

So if you've got a PSRR of 60 dB and you've got 50 mV of noise on your power supply, it will be attenuated by 60 dB (which is a reduction of 1000 times), so you'll get 50 uV of noise on the output. Obviously you want your power supply to have as little noise as practical, and you want as much PSRR as you can get, so the output doesn't get noise from the power.

With filtered PWM, you get the worst case in both regards. There no PSRR at all. Or maybe 6 dB if you want to play with specs and assume on average your signal will be 50% PWM duty cycle. But the point is any noise on your power supply couples directly to your output circuit, because the PWM is just turning on a transistor inside the chip which shorts the two to drive the pin high. Worse yet, that's the noisy digital power supply, not a nice analog supply which has lots of filtering and powers only analog circuits that draw (appoximately) constant current. The digital supply powers lots of other logic circuits, which are switching rapidly. Even with good decoupling capacitors, you can expect quite a lot more noise on your digital supply... which of course couples right to the output signal, since PWM has no PSRR at all.

Of course, your problem might not be related to PSRR. It might be a ground loop. There too, PWM is pretty terrible, since it produces output relative to the same digital ground that's running lots of other stuff which switches and draws pulsing currents, which of course must flow through digital ground wires. If any of those currents induce voltages relative to your amplifier, you'll heard them added to your signal.

The bottom line is PWM is pretty terrible for quality audio. You can take steps to lessen the issues, but ultimately it's very hard or pretty much impossible to get a signal that anyone who really cares about sound quality would consider "clean".

Ooff. I thought that this thread was abandoned.

polymorph: Schematic? Link to the library? Code?

Schematic? Pins 11 to 13 connected to an SD card module. 5v to the "5v" pin of the module. Pin 9 and 10 connected to a headphones stereo jack, through two 100 ohm resistors in series, a 0.1 uF ceramic capacitor in parallel to ground (per each pin, since the output is stereo); and finally, a 50K dual potentiometer as a dual voltage divider (again, stereo output). All ground points are connected together... obviously.

Link to the library? https://github.com/TMRh20/TMRpcm

Code? Only if you really want to see it. The code nearly uses 300 lines; but the core idea, is to playback a WAV file from the SD card to the PWM pins.

[quote author=Paul Stoffregen date=1461882624 link=msg=2733369] Of course, your problem might not be related to PSRR. It might be a ground loop. There too, PWM is pretty terrible, since it produces output relative to the same digital ground that's running lots of other stuff which switches and draws pulsing currents, which of course must flow through digital ground wires. If any of those currents induce voltages relative to your amplifier, you'll heard them added to your signal.

The bottom line is PWM is pretty terrible for quality audio. You can take steps to lessen the issues, but ultimately it's very hard or pretty much impossible to get a signal that anyone who really cares about sound quality would consider "clean". [/quote] Correct me if I'm wrong. Since the ground line is shared for all the digital pins, then it's practically impossible to "isolate" those "switching noises" (in my case, the "Serial Clock" pulses and the "Master Output Slave Input" pulses when writing a file).

That's fine, because I was just wondering if it's possible; is not the end of the world. Also, I already knew that PWM-ed audio is not the best quality, due to not being a true DAC.

Anyway, thank you for the answers! :D