Tuesday, October 6, 2020

Nyquist and reconstruction filters in practice!

I assume you've heard about the Nyquist sampling theorem? It basically says that you have to sample a signal at twice the sample rate of the highest frequency you want to represent, that's why CDs have a sample rate of 44kHz - to be able to reproduce signals around 20kHz.

You may even have heard that doing so, you are not only able to reproduce an approximation of the original signal - you can reproduce an exact copy.

But have you actually seen this happen in practice? Let me show you! 


Sampling

A/D and D/A conversion is fairly simple in its basic form. Use an ADC and check the signal amplitude/level at set intervals. DAC is the reverse, set the amplitude of the output to whatever you got from the ADC (There is more to it than that for high quality signal reproduction, but for the sake of the discussion, and for the level of accuracy I need this suffices). 

Try this at home, looking at the output on an Oscilloscope and you'll quickly realise that the output looks nothing like the input. It has jagged edges, you can clearly see the height and length of the individual samples as output from the DAC. 

This is because there is one (two) more very important element(s) needed. To prevent aliasing - a higher frequency signal (outside of the audible frequency range) masquerading as a lower frequency one, we need to use a low pass filter before the ADC to remove frequencies that cannot properly be captured, e.g. frequencies above 1/2 of the sampling frequency.

When playing back the signal we need to do the same - as the DAC outputs a stepped signal it will generate lots of higher frequencies as well. By using a filter with the same cutoff - this time called a reconstruction filter - those are removed (once again, this is more complicated in practice for a high quality signal, but let's forget about that for a minute).


Ok, so that all sounds great on paper. Output a stepped signal and apply some magic, and you should get the original signal. But it can't be that easy, right?


Let's look at some photos:


DAC output (top) vs output after reconstruction filter
DAC output (top) vs output after reconstruction filter

Here we see the output of a sampled sine wave (top) vs the output after the 20kHz reconstruction filter. It looks amazing! It works for other waveshapes as well of course, though if we were to zoom in on the edges of the triangle wave we would see that it's slightly rounded - this is because the frequencies required to reproduce it faithfully have been removed by the two filters. This is of course not a big deal as we wouldn't hear them anyway. 

Sampled and reconstructed triangle wave, tips are rounded by filtering.


The effect is even more pronounced with square waves which get a clear oscillation at the edges



Increasing the frequency of the input shows that the DAC output no longer resembles the input as closely - it has very few samples per cycle of the sine wave:




Now things start getting strange. Increasing the frequency even further "distorts" the DAC output even more. Remember, Nyquist says that it should be possible to reproduce a sine wave with only TWO samples per cycle. It sounds incredibly strange, but it actually works! Here is a 15kHz signal, filtered at 20kHz. The DAC output looks nothing like the sine wave it is supposed to represent:


Seeing this for the first time blew my mind. How is it even possible?! Well, read up on the theory behind it in Steven W. Smiths awesome book "The Scientist and Engineer's Guide to Digital Signal Processing" (available for free as separate pdfs from his web page) if you want the theory.


Effects of filtering


Now, there are some effects of filtering still present. First, we get some phase distortion, which increases as we approach the cutoff frequency of the filters. Looking at the input and output sines we can see that they don't line up properly:


As far as I know, this is not audible on its own. It WILL however be audible if the signal is mixed back with the original signal (which incidentally is how phasers are made). (PS: The photo is slightly misleading. Some of the apparent phase shift is only a delay between the input and output due to the time it takes for the DSP chain to process the signal. But the phase shift definitely increases as we approach the filter cutoff)

The second effect is that the amplitude (volume) of the output will decrease as we approach the filter cutoff. This is nothing special to this circuit, it's just how filters work - they do not have an absolute cutoff point, instead they gradually attenuate more and more. Having higher order filters with more stages will help - I'm using six poles in mine. High quality reconstruction filters however, do this differently. As we know how the filter affects the amplitude, we can do the reverse in advance - increase the volume of those frequencies. This cancels out the effect of the filter, keeping the volume constant for much longer. I have not bothered with this in my circuit as we're talking about fairly high frequencies anyway.