You may have noticed that in our discussion of frequency domain and time domain filters, we didn’t mention how we got the filters – we just had them and applied them. In the case of an FIR filter, the filter is represented in the coefficients $$\left [ a_{0},a_{1},a_{2},\cdots ,a_{N-1} \right ]$$. In the case of the IIR filter, the filter resides in coefficients $$\left [ a_{0},a_{1},a_{2},\cdots ,a_{N-1} \right ]$$ and $$\left [ b_{0},b_{1},b_{2},\cdots ,b_{M} \right ]$$.
Without explaining in detail the mathematics of filter creation, we can show you algorithms for creating low-pass, high-pass, bandpass, and bandstop filters when they are given the appropriate parameters as input. Low-pass filters allow only frequencies below a cutoff frequency $$f_{c}$$ to pass through. Thus, Algorithm 7.2 takes $$f_{c}$$ as input and outputs an N-element array constituting a low-pass filter. Similarly, Algorithm 7.3 takes $$f_{c}$$ as input and yields a high-pass filter, and Algorithm 7.4 and Algorithm 7.5 take $$f_{1}$$ and $$f_{2}$$ as input to yield bandpass and bandstop filters. These algorithms yield time-domain filters. If you’re interested in how these algorithms were derived, see (Ifeachor and Jervis 1993), (Steiglitz 1996), or (Burg 2008).
[equation class=”algorithm” caption=”Algorithm 7.2 Low-pass filter”]
algorithm FIR_low_pass filter
/*
Input:
f_c, the cutoff frequency for the low-pass filter, in Hz
f_samp, sampling frequency of the audio signal to be filtered, in Hz
N, the order of the filter; assume N is odd
Output:
h, a low-pass FIR filter in the form of an N-element array */
{
//Normalize f_c and ω _c so that pi is equal to the Nyquist angular frequency
f_c = f_c/f_samp
ω_c = 2*pi*f_c
middle = N/2 /*Integer division, dropping remainder*/
for i = −N/2 to N/2
if (i == 0) h(middle) = 2*f_c
else h(i + middle) = sin(ω_c*i)/(pi*i)
}
[/equation]
[equation class=”algorithm” caption=”Algorithm 7.3 High-pass filter”]
algorithm FIR_high_pass filter
/*
Input:
f_c, the cutoff frequency for the high pass filter, in Hz
f_samp, sampling frequency of the audio signal to be filtered, in Hz
N, the order of the filter; assume N is odd
Output:
h, a high-pass FIR filter in the form of an N-element array */
{
//Normalize f_c and ω _c so that pi is equal to the Nyquist angular frequency
f_c = f_c/f_samp
ω_c = 2*pi*f_c
middle = N/2 /*Integer division, dropping remainder*/
for i = −N/2 to N/2
if (i == 0) h(middle) = 1 – 2*f_c
else h(i + middle) = -sin(ω_c*i)/(pi*i)
}
[/equation]
[equation class=”algorithm” caption=”Algorithm 7.4 Bandpass filter”]
algorithm FIR_bandpass filter
/*
Input:
f1, the lowest frequency to be included, in Hz
f2, the highest frequency to be included, in Hz
f_samp, sampling frequency of the audio signal to be filtered, in Hz
N, the order of the filter; assume N is odd
Output:
h, a bandpass FIR filter in the form of an N-element array */
{
//Normalize f_c and ω _c so that pi is equal to the Nyquist angular frequency
f1_c = f1/f_samp
f2_c = f2/f_samp
ω1_c = 2*pi*f1_c
ω2_c = 2*pi*f2_c
middle = N/2 /*Integer division, dropping remainder*/
for i = −N/2 to N/2
if (i == 0) h(middle) = 2*(f2_c – f1_c)
else
h(i + middle) = sin(ω2_c*i)/(pi*i) – sin(ω1_c*i)/(pi*i)
}
[/equation]
[equation class=”algorithm” caption=”Algorithm 7.5 Bandstop filter”]
algorithm FIR_bandstop filter
/*
Input:
f1, the highest frequency to be included in the bottom band, in Hz
f2, the lowest frequency to be included in the top band, in Hz
Everything from f1 to f2 will be filtered out
f_samp, sampling frequency of the audio signal to be filtered, in Hz
N, the order of the filter; assume N is odd
Output:
h, a bandstop FIR filter in the form of an N-element array */
{
//Normalize f_c and ω _c so that pi is equal to the Nyquist angular frequency
f1_c = f1/f_samp
f2_c = f2/f_samp
ω1_c = 2*pi*f1_c
ω2_c = 2*pi*f2_c
middle = N/2 /*Integer division, dropping remainder*/
for i = −N/2 to N/2
if (i == 0) h(middle) = 1 – 2*(f2_c – f1_c)
else
h(i + middle) = sin(ω1_c*i)/(p*i) – sin(ω2_c*i)/(pi*i)
}
[/equation]
[wpfilebase tag=file id=163 tpl=supplement /]
[wpfilebase tag=file id=165 tpl=supplement /]
As an exercise, you can try implementing these algorithms in C++, Java, or MATLAB and see if they actually work. In Section 7.3.8, we’ll show you some higher level tools in MATLAB’s digital signal processing toolkit that create these types of filters for you.