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.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Matrix A contains the filter coefficients, so the transfer function is:
Click to copy this expression
2. Use the gain function to calculate the filter's gain at frequency x.
Click to copy this expression
3. Plot the magnitude of the gain over frequencies ranging from 0 to 0.5 the sampling frequency.
Click to copy this expression
Click to copy this expression
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π.
Click to copy this expression
Click to copy this expression
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.
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Use the gain function to calculate the filter's gain at frequency x.
Click to copy this expression
3. Plot the magnitude of the gain over frequencies ranging from 0 to 0.5 the sampling frequency.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
4. Compare the response of the two filters by plotting both responses on the same graph.
Click to copy this expression
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.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
F is an array of 51 elements.
2. Calculate the gain of this filter.
Click to copy this expression
3. Plot the gain in dB.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
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.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
This is the transfer function evaluated at z = 2πx.
Click to copy this expression
Click to copy this expression
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.
Click to copy this expression
2. Compute coefficients for a lowpass filter using a Hanning window (last argument of 4) in the lowpass function.
Click to copy this expression
3. Plot the phase change using the arg function. The function returns the principal argument of the complex number z, between -π and π, including π.
Click to copy this expression
Click to copy this expression
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.
Click to copy this expression
where
Click to copy this expression
Click to copy this expression
For this length 37 filter the delay D is 18, so the relative phase shift is:
Click to copy this expression
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:
Click to copy this expression
5. Plot the magnitude of the gain in decibels.
Click to copy this expression
Click to copy this expression
6. Plot the phase shift relative to the delayed signal.
Click to copy this expression
Click to copy this expression