IIR Design
An FIR realization of the narrowband filter used to identify the answer tone is relatively expensive (computationally) and relatively poor quality (poor attenuation in the stopbands). An IIR (infinite impulse response) design turns out to be much better. Using Matlab's signal processing toolbox again, we can design a filter of a type known as a Chebyshev filter using the following command:
[B, A] = cheby2(1,30,[2050,2150]/4000)
The function cheby2
is one of several filter design functions
in the Matlab signal processing toolkit.
This command specifies a filter with a passband from 2050 Hz to 2150 Hz,
but constrains it to second order (which for reasons that you don't
want to know, Matlab asks you to specify as "1").
The result is:
B = 0.0012 0.0000 -0.0012 A = 1.0000 0.1568 0.9975
These give the coefficients of a linear difference equation describing the filter. If the filter input is x and the output is y, then using these coefficients we get that for all integers n,
1.0y(n) + 0.1568y(n-1) + 0.9975y(n-2) = 0.0012x(n) + 0.0x(n-1) - 0.0012x(n-2).
You can use this difference equation to calculate an output given an input by grouping the terms as follows:
y(n) = -0.1568y(n-1) - 0.9975y(n-2) + 0.0012x(n) + 0.0x(n-1) - 0.0012x(n-2).
To use this computationally, you must assume that the input starts at some value of n (usually zero), and that y has value zero before that. Notice that the number of arithmetic operations per output sample is much smaller than for the FIR example. Specifically, this requires 5 multiplications and 4 additions, vs. 101 multiplications and 100 additions.
To apply this filter to a signal x
stored as a Matlab
vector, use
y = filter(B, A, x);The result before and after filtering can be heard below: