Functions > Signal Processing > Digital Filtering > Example: Filter Gain
Example: Filter Gain
The gain function returns the gain at the single frequency. If you use a vector of frequencies, the function returns a vector of gains (the transfer function). This is useful for plotting.
Gain of a Lowpass Filter
1. Use iirlow function to get the coefficients of a 2nd order, analog Butterworth lowpass IIR filter with cutoff frequency f.
Matrix A contains the filter coefficients, so the transfer function is:
2. Use the gain function to calculate the filter's gain at frequency x.
3. Plot the magnitude of the gain over frequencies ranging from 0 to 0.5 the sampling frequency.
The gain at the cutoff frequency drops to 0.707.
4. Scale the frequency so that the sampling frequency is represented by 2π, and then divide the frequency argument to the gain function by 2π.
The cutoff frequency at 0.2 of the full x scale now occurs at 0.4 of the full ω scale or 0.4 π.
Higher Order Lowpass Filters
1. Use the iirlow function to get the coefficients of a 6th order, analog Butterworth lowpass IIR filter with cutoff frequency f.
2. Use the gain function to calculate the filter's gain at frequency x.
3. Plot the magnitude of the gain over frequencies ranging from 0 to 0.5 the sampling frequency.
4. Compare the response of the two filters by plotting both responses on the same graph.
The response of the sixth-order filter falls off much more rapidly than the second-order filter.
Both filters have the same gain at the cut off frequency of 0.2.
Compute the Gain of an FIR Filter
Compute the gain for an FIR filter designed using the bandpass function.
1. Compute the coefficients for a length 51 bandpass filter using a Blackman window with passband between f_low and f_high.
F is an array of 51 elements.
2. Calculate the gain of this filter.
3. Plot the gain in dB.
The gain is maximum between the low and high cutoff frequencies.
Function Definition
You can compute gains directly from the definition of the transfer function using the summation operator.
Calculate the gain of filter F at a frequency x.
This is the transfer function evaluated at z = 2πx.
gain is faster than the summation operator, so it is useful for computing the gain at a large number of frequencies. For example use gain for plotting the frequency response. For very long filters, even gain will take a while to compute, so choose a fairly coarse grid (for example, 0.01) for plotting.
FIR Filter Phase
The phase of the complex gain represents the phase shift of the filter.
1. Set the FIR filter cutoff frequency.
2. Compute coefficients for a lowpass filter using a Hanning window (last argument of 4) in the lowpass function.
3. Plot the phase change using the arg function. The function returns the principal argument of the complex number z, between -π and π, including π.
The phase is linear in the passband (below the cuttoff frequency), but notice the effect of the filter delay.
4. Compute the complex gain relative to the delayed signal by dividing the transfer function of the delay D.
where
For this length 37 filter the delay D is 18, so the relative phase shift is:
When the gain is real and negative, roundoff errors in the gain computation can leave a tiny positive or negative imaginary part, so the phase may jump arbitrarily between π and -π.
To make sure these values get handled uniformly, redefine arg as follows:
5. Plot the magnitude of the gain in decibels.
6. Plot the phase shift relative to the delayed signal.