# How does a digital filter work?

To introduce my situation: I'm developing a digital synthesizer in a form of a C++ library, working with low level APIs like WASAPI, ASIO, ALSA etc. It's probably not very practical and I'm mostly "reinventing the wheel" but my intention is to learn about digital synthesis in depth. So far I have successfully implemented basic concepts like oscillators and modulation of their properties. The next logical step is a filter.

So my question is: How does a digital filter work on this low level? How exactly does it modify the individual samples?

I understand, that this involves a lot of math. That's not a problem for me. I only need a good starting point (some sources to learn) and an intuitive explanation as all I was able to find were either analogue explanations or just formulas explained with a lot of advanced terminology that I'm not familiar with.

• I'm voting to close this question as off-topic because the question is both too broad and not about music practice, performance, or history.
– David Bowling
Sep 4, 2018 at 22:18
• The low-level workings of digital filters does not seem at all like music theory to me; rather this is the domain of digital signal processing. Maybe this would be a better fit at SE Electrical Engineering, or at one of the programming-related sites.
– David Bowling
Sep 4, 2018 at 22:29
• I have just stumbled upon the Signal Processing Stack Exchange. I don't know why I haven't found it earlier, but it may be actually a better place for this question.
– McSim
Sep 4, 2018 at 22:52
• Welcome! Please check the answers to this question, and also browse the site a bit. Any remaining question can be formulated as a new question on this site. Sep 5, 2018 at 7:39
• The best way to start with digital filters is to read up a bit. I suggest ccrma.stanford.edu/~jos/filters and dspguide.com/pdfbook.htm . Both are free . Sep 5, 2018 at 11:21

Consider a moving average over N samples- this is a simple FIR filter where each new output is the average of the past N samples. It is easy to see how high frequency noise can be filtered out (so is a low pass filter), and the longer time duration we include in the averaging window the lower will be the frequency cut off (just compare a stock market 30 day moving average to a 1 day moving average).

A moving average is a poor low pass filter, having a frequency response that approaches a Sinc function, which rolls off relatively slowly in frequency. By doing a weighted moving average where different samples are given different weights in the averaging process, we can significantly improve the frequency response - and coming up with the correct weights is the science of digital filter design.

IIR filters are similar except we are performing the average with previous outputs instead of past inputs.

• In the mean time, I've decided to attend a course at my university on signal processing, so I now have some understanding of basic signal processing including IIR and FIR filters. But as I remember my confusion with filters when I was asking this quetion, your explanation would have been exactly what I needed - the idea of using previous samples or previous filtered (output) samples.
– egst
Dec 30, 2019 at 15:53
• Oh sorry I didn’t see it sooner! I teach continuing ed courses in the Boston area for the IEEE on DSP and Python that are focused on providing more intuitive insight into the underlying operations and mathematics with practical applications. Dec 30, 2019 at 15:55

What you probably want is what's called a finite impulse response filter, or FIR. I know that this answer might be rejected for not being specific enough, but honestly you need a good grasp of DSP. However, on the bright side, there could possibly be ready to go software that will generate a filter for you. This shit is not easy. Really you need know the math behind it.

• For synths, IIRs tend to be much more useful than FIRs. Sep 4, 2018 at 22:45
• Interesting. because IIRs can be unstable. I just had though that synths use FIRs for that reason. I learn something new everyday.
– Larry Troxler
Sep 4, 2018 at 22:49
• The main advantage or IIRs is that their parameters can be interactively tweaked without latency. And, yes, badly designed IIRs can indeed be unstable – but so can an analogue state variable filter! Properly thought through design can prevent unrecoverable instability. Driving a filter into the almost-unstable resonant region is actually an effective way of making screaming-aggressive lead synth sounds though. This too is hard to do with FIRs (which are incapable of modelling nonlinear response. Sep 4, 2018 at 22:58
• Well... a digitized & sampled IIR is just a specially-tweaked FIR when it comes right down to it. Sep 5, 2018 at 11:57

I understand, that this involves a lot of math.

Not so much, in principle. The basic idea behind linear (or nonlinear) filtering is to remplace a inaccurate or noisy sample $$s[n]$$ by a combination of other samples, assuming that their values or location is somehow close to $$s[n]$$ (cf. local vs non-local filters).

At a low level, when the filter is both local (around the current signal) and linear, then you replace the current sample $$s[n]$$ by the estimate $$\hat{s}[n] = \cdots + a_3\hat{s}[n-3] +a_2\hat{s}[n-2] + a_{1}\hat{s}[n-1] + a_{0}\hat{s}[n] + a_{-1}\hat{s}[n+1] +a_{-2}\hat{s}[n+2] + a_{-3}\hat{s}[n+3] +\cdots$$

The above operations can de generalized.