



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Signals and Systems; Subject: Electrical & Computer Engr; University: Utah State University; Term: Unknown 1989;
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!




In this programming assignment you will design both IIR and FIR filters. The IIR filter is “designed” by pole placement. The FIR filter is designed with the help of a Matlab design tool. You will then implement the filters in C++. You will test the filter response by providing sinusoidal inputs at a variety of frequencies and comparing the actual magnitude response with the theoretical magnitude response. The signals you are to deal with are sampled at a rate of Fs = 8000 samples/sec. One of the algorithms you will use for the filter design uses the Matlab routine remez. (Note: most recent versions of Matlab call this function firpm. However, since there may be older versions of Matlab still in use, we will refer to the function as remez. You should check the help function in Matlab.) The remez routine is an implementation of the algorithm known as the Parks-McClellan algorithm for digital filter design, after its inventors. (The name “remez” comes from a particular numerical algorithm, known as the “Remez exchange” which forms part of the Parks-McClellan algorithm.) The Matlab manual pages for the remez function are provided with this assignment. (For example, do help remez in Matlab. To use the remez routine, you specify the lower and upper frequencies and the desired magnitude response at those frequencies. The frequencies are given as a fraction of the Nyquist frequency for the system (the maximum frequency representable in the sampled system). For example, if you are sampling at Fs = 8000 samples/sec, the Nyquist frequency is 4000 Hz. The continuous-time frequency frequency of 4000 Hz. corresponds in the remez algorithm, to a digital frequency of 1.
Design of the filters
(e) Convert the pole-zero representation to a rational transfer function representation (the routine zp2tf might be helpful). (f) Save the filter coefficients to a file you can use.
Implementation Write a C++ class that implements digital filtering. The class might have the following form:
class filter { // private data double *numcoeff; // pointer to numerator coefficients double *dencoeff; // pointer to denominator coefficients int type; // IIR or FIR? public: // public interface filter(int n, double *numcoeff); // FIR constructor filter(int n, double *numcoeff, int m, double *dencoeff); // IIR constructor ~filter(); // destructor -- make sure you de-allocate memory double filt(double input); // the actual filter function // whatever other functions you might want };
Your constructor functions need to allocate space for the filter coefficients and the filter storage, copy the filter coefficients passed in into their appropriate storage arrays. Your filter function should do filtering as appropriate, whether IIR or FIR. (To be really fancy, you might want to use some sort of virtual function to select whether you are doing FIR or IIR filtering, but this level of sophistication is not really necessary.) You could use this filter class in your program as follows:
#include "filter.h" // <-- you write this #include <math.h> ... // other stuff
main( ... ) { // ...
int n = 100; // number of FIR filter coefficients // ... // read the FIR filter coefficients into an array called y // ...
filter FIR(n,y); // instantiate the FIR filter // ...
// Here is how to use it: x = ... // x = the input function y = FIR.filt(x); // "pass" x into the filter, then y is the output
// read the IIR coefficients into a numerator array called num, and a // denominator array called den. int n = ... // number of numerator coefficients int m = ... // number of denominator coefficients
// determine the amplitude of the output signal (think carefully!) // ... outamplitude = ...; } } // end for t ratio = outamplitude/inamplitude; // write the frequency F and the ratio to a file // ... } // end for t
// close the output file // ... } // end main
To plot the results, you will need to get into Matlab, plot the desired magnitude response with freqz, load in your actual results using the load command, then plot these on the same axes. For example, you might have the following:
% Let a and b represent the numerator and denominator polynomials % of the designed filter [h,f] = freqz(a,b); clf; % clear current figure (remove existing plots) subplot(2,1,1); % plot in part of the available window plot(f/(2pi),20log10(abs(h))); % plot the magnitude response in dB hold on; % make it so that the plot can be overlaid xlabel(’discrete frequency’) % label the axes ylabel(’magnitude response, dB’)
% read in your data, say into the arrays fa and ha % ...
% Now plot your results on top of the theoretical results plot(fa,20*log10(ha),’:’); % plot your computed response with % dotted line
Turn In the following:
The remez function is invoked as
h = remez(n,f,m)
(See the help remez in Matlab.) The following information comes from the manual for The Student Edition of Matlab, v. 4.
h = remez(n,f,m,w)
then w is a weight vector, which uses the weights to fit in each frequency band. The length of w is half the length of f or m, so there is exactly one weight per band.
Here is an example of a bandpass filter: We desire a filter to pass signals between f = 0.4 and f = 0. 6 (normalized frequency) with an amplitude of 1, and to attenuate other signals. A plot of the desired frequency response is
f = [0 0.3 0.4 .6 0.7 1]; m = [0 0 1 1 0 0]; % bands: 0-0.3 is a stop band (at 0) % 0.3-0.4 is a transition band % 0.5-0.6 is the pass band % 0.6-0.7 is a transition band % 0.7-1 is a stop band (at 0) clf; % clear previous plot, if any plot(f,m,’:’); % a plot of the desired frequency response h = remez(17,f,m); % create a filter with 18 coefficients [response,freq] = freqz(h,1,512); % compute the frequency response hold on; % overlay the plots plot(freq/pi,abs(response)); % freq/pi converts to this normalized % freq. scale % abs(response) is the magnitude response xlabel(’f (normalized)’); ylabel(’Magnitude response’); legend(’Desired frequency response’,’Actual frequency response, 18 coefs.’)
The plot below shows the results: