dsp_basics_02: filters

almost everything is a filter

In dsp a filter is anything that takes a signal in and puts a signal out. This means almost everything is a filter of some sort[1]. There are all kinds of handy math we can do to design our own filters and tune them up. For now, I want to show you how to make a simple lowpass filter and give you some intuition for how it works.

A lowpass filter takes in a signal and lets the low frequencies in the sound through by while lowering the volume of the high frequencies. Here is a really simple one[1]:


struct Lowpass1stOrderFIR {
    float previous=0;

    float next(float current){
        float out = current+previous;
        previous=current;
        return out;
    }
};

What is this thing doing? Its a data structure that holds on to a sample. It has a method that adds the current sample to the previous sample. In effect its averaging the current and the previous sample. Why does this operation shave off high frequencies?

sound is made of frequencies

When you pluck a guitar string, or blow into a flute, or sing for that matter, you create a signal that has more than one frequency. Lets take the example of the guitar string. The guitar string vibrates with a wave that corresponds to its length, 1/2 its length, 1/3 of its length, 1/4 of its length and so on. Each of these smaller waves have different volumes and decay at different rates.
The sum of the harmonics (shown in black) is a different shaped wave.

What happens when we add up a bunch of harmonics together? Lets add up a hundred of them them like so: (1/n)*sin(n*2*pi*t) where n is a number 1 through 100.

The result of adding up 100 sine waves is a decent approximation of a sawtooth wave. There are still visible wiggles on the sharp bits. If we added more waves, these would get harder to see and the wave would get sharper.

We get whats called a sawtooth wave! More important for right now, we get a shape with sharp edges. Sounds that have lots of high frequencies have sharp edges. Sharp edges mean sudden changes.

high frequencies mean sudden changes

This is the intuition behind the lowpass filter. When we average we smooth out the sharp edges. We make the sudden changes more gradual. This removes high frequency components because we now need fewer harmonics to represent the shape of the wave.

references