Simple One Pole and Band Pass Filters

In this section two simple filters are presented. Both use feedback of the previous output samples in the calculation of new samples. When samples are averaged, high frequencies are diminished. When samples are differentiated, low frequencies are diminished.

One Pole Low Pass

The first is a one-pole low pass filter. The new output sample can clearly be seen as a crossfade between the input and the previous output.

out = in * (1 - c) + out1 * c;
out1 = out;

The amount of in/out “crossfade” is the filter coefficient c. You can get an intuitive feeling for the filter coefficient c by observing that each output sample is an mix between the previous output and the input. As the coefficient c is increased, this mix contains mostly previous output, and this averaging between the mix of previous samples and the current sample smooths out the waveform – it reduces the high frequencies.

As the coefficient c is lowered, the filtering is reduced, and when c is 0.0, the input is copied to the output unfiltered.

This type of filter is also known as a exponentially weighted moving average filter. You can find the derivation for the exact coefficient c for a given filter frequency elsewhere, but the following approximation is often used.

c = exp(-2 * PI * frequency/samplingRate);

You can obtain a high-pass by subtracting the low-pass output from the input.

hpout = new - out;

Simple Band Pass Filter

The following filter was used in Max Mathew’s filter opcode in Music IV and a derivation is used in Barry Vercoe’s reson opcode in Csound.

out = in + c1 * out1 - c2 * out2;
out2 = out1;
out1 = out;

This filter is somewhat similar to the moving average filter above, and when c2 is zero, it is very similar. When c2 increases, the output 2 samples previous is removed from the moving average. This diminishes the low frequency component and gives the entire filter a bandpass characteristic. In Max Mathew’s implementation, the two coefficients c1 and c2 are calculated for any given bandwidth and frequency.

c1 = 2.0 * exp(-1.0 * PI * bandwidth/samplingRate) * cos(2 * PI * frequency/samplingRate);
c2 = exp(-2 * PI * bandwidth/samplingRate);

Coefficient calculation and sound processing

In these two filters there is one section of code to calculate the coefficients from a given frequency and bandwidth (or more detailed parameters) and another section for filtering the audio (using those coefficients and saving the internal state in one or more state variables). It’s typical for the coefficient calculation to be more CPU intensive than the filter calculation. This is because coefficient calculation involves trigonometric functions. For many instances the coefficients can be precalculated for the range of frequencies and bandwidths used.

Leave a Reply

Your email address will not be published.