AC 29-125: FIR FILTERS FOR TECHNOLOGISTS, SCIENTISTS, AND OTHER NON-PH.D.S William Blanton, East Tennessee State University Dr. Blanton is an associate professor and coordinator of the Biomedical Engineering Technology concentration at East Tennessee State University. His scholarly interests are the applications of digital signal processing to electronic instrumentation, especially medical instrumentation and medical imaging. American Society for Engineering Education, 29 Page 14.632.1
FIR Filters for Technologists, Scientists, and Other Non-PhDs ABSTRACT The digital filter used most often in digital signal processing (DSP) is the Finite Impulse Response (FIR) filter because it is the easiest to design and it is always stable. An interesting demonstration of the design and implementation of a FIR filter can be shown using MATLAB. The Remez function in MATLAB can be used to generate the filter coefficients for the lowpass, highpass, bandpass, or bandstop filter. MATLAB can be used to generate a set of sinusoidal signals that can be observed in the time domain and frequency domain. The appropriate filter can be applied to pass or block one or all the signals. In addition, MATLAB provides a graphic user interface tool, the Filter Design and Analysis Tool (fdatool), that can be used to generate the filter coefficients. Regardless of the method, the design and implementation of a FIR filter is shown to be straightforward. INTRODUCTION Human reality revolves around the analog domain where perception of events is formed by information that can take on any numeric value at any time. Unfortunately, most modern information is collected, manipulated, collated, and stored in the digital domain associated with computers where data must be a discrete value having limited values for specific times only (Figure 1). The general scheme for converting from the analog domain to the discrete (digital) domain is shown in Figure 2. Digital signal processing (DSP) generally consists of an antialiasing filter to limit the bandwidth of the analog signal, an analog-to-digital converter (ADC) that converts analog signals to discrete signals that the computer or microcontroller can use, a DSP chip that manipulates (filters) the digital signal, a digital-to-analog converter (DAC) to convert the digital Page 14.632.2
signal back to an analog signal, and a reconstruction filter to reduce the discontinuities associated with digital-to-analog conversion. The manipulation of data is demonstrated in Figure 3. Figure 1: Analog and digital functions. Figure 2: The general scheme of DSP. Page 14.632.3
Figure 3: An example of DSP. Because digital signal processing is so pervasive in modern applications, Figure 4, all students throughout the electrical engineering spectrum need a general working Figure 4. DSP applications. knowledge of digital filters. The objective is to present such knowledge in a way that technologists, scientists, and other non-phds are not frightened by the mathematical rigor of the subject. The most straightforward method of digital signal processing is the FIR filter, and the most straightforward method to mask the mathematical rigor is to use MATLAB 1. Page 14.632.4
FIR FILTERS FIR filters are defined by the following discrete equation: y N ] n_? bk x] n/ k_ (1) k? The present discrete output, y[n], is the result of the sum of a stream of weighted discrete inputs, b k x k. The equation can be demonstrated figuratively in Figure 5. An important Figure 5. FIR filter block diagram. factor associated with the FIR filter is that the output is related only to inputs and not the previous outputs. That is, there are no feedback components in a FIR filter; therefore, the FIR filter is always stable. The FIR filter response is dependent upon the weighted coefficients, b k, associated with the various inputs, x k. MATLAB can be used to generate a coefficient matrix that can be inserted into the memory of a digital signal processor to perform the indicated filtering. Page 14.632.5
A FIR FILTER EXAMPLE Consider the following example adapted from Kehtarnavaz and Keramat 2 consisting of a composite signal of three sinusoids created by the following MATLAB code and shown in Figure 6. The spectrum of the composite waveform can be derived using the fast Fourier transform function routine, Figure 7. A very simple FIR filtering method can be implemented using the Remez method 3 in MATLAB. The Remez method normalizes (sets to 1) the folding frequency (1/2 the sampling frequency). The corresponding magnitudes for the corresponding frequencies are denoted by mag and nu, respectfully, Figure 8. For a sampling frequency of 1, Hz and if the magnitude is 1 for the normalized frequencies of ( Hz) and.3 (15 Hz) and for the normalized frequencies of.35 (155 Hz) and 1 (5 Hz), a lowpass filter will be realized as shown in Figure 9. Note that the FIR filter passes the 1 khz signal while blocking the 2 khz and 3 khz signals, Figure 1. We can compare the original waveform with the filtered waveform as shown in Figure 11. Note that in the time domain, the signal has been transformed from a waveform with several frequencies into a signal with a single frequency. More importantly, the Remez algorithm generated a set of coefficients that can be downloaded into a digital signal processor memory, Figure 12. These coefficients are the weighted coefficients, b k, in equation 1. These are the coefficients that define the frequency response of the digital filter, and when implemented in the processor software allow the digital signal processor to filter the input signal. By varying the coefficients, the digital signal processor can function as a lowpass, highpass, bandpass, or bandstop filter. Page 14.632.6
Fs=1e3; %1 khz sampling frequency Ts=1/Fs; %Sampling time Ns=124; %Number of samples t= [:Ts:Ts*(Ns-1)]; f1=1; %frequncey 1 f2=2; %frequency 2 f3=3; %frequency 3 x1=sin(2*pi*f1*t); %1 khz Sinusoid x2=sin(2*pi*f2*t); %2 khz Sinusoid x3=sin(2*pi*f3*t); %3 khz Sinusoid x=x1+x2+x3; %Composite signal consisting of three sine waves plot(t,x), grid; %Plot of the composite signal axis([.8-3 3]) ; 3 2 1-1 -2-3 1 2 3 4 5 6 7 8 x 1-3 Figure 6. Composite signal consisting of 1 khz, 2kHz, and 3kHz sine waves. Page 14.632.7
function va_fft(x,ns,fs) X=fft(x,Ns); XX=(abs(X)); XXX=XX(1:length(XX)/2); y=xxx; f=[1:1:length(y)]; plot(f*fs/ns,y); grid on; 5 45 4 35 3 25 2 15 1 5 5 1 15 2 25 3 35 4 45 5 Figure 7. Spectrum of composite waveform. Page 14.632.8
%MAT33.M FIR LOWPASS WITH 33 COEFFICIENTS USING MATLAB nu=[.3.35 1]; mag=[1 1 ]; c=remez(32,nu, mag); bp33=c ; save c:\matbp33.cofbp33 -ascii; [h,w]=freqz(c, 1, 124); You can change this to whichever file you use to save data! plot(5*nu,mag,w/pi,abs(h)) %normalized frequencies %magnitude @ normalized freqs %invoke remez algorithm for 33 coefficients %coefficient vector transposed %save coefficients in ascii file (dest,source) %frequency plot with 124 points %plot ideal magnitude response Figure 8. Remez FIR method. 5 45 4 35 3 25 2 15 1 5 5 1 15 2 25 3 35 4 45 5 Figure 9. Lowpass Fir filter using Remez method. Page 14.632.9
nu=[.3.35 1]; mag=[1 1 ]; c=remez(32,nu, mag); a=1; freqz(c,a);grid on subplot(3,1,1) va_fft(x,124,1); subplot(3,1,2),grid on [h,w]=freqz(c,1,256); plot(w/(2*pi),1*log(abs(h))); subplot(3,1,3);grid on y=filter(c,a,x); va_fft(y,124,1); 6 4 2 5 1 15 2 25 3 35 4 45 5 5-5 -1.5.1.15.2.25.3.35.4.45.5 4 3 2 1 5 1 15 2 25 3 35 4 45 5 Figure 1. Filtered signal in the frequency domain. Page 14.632.1
n= 128; subplot(2,1,1); plot(t(1:n),x( 1:n)); grid on; axis([ 8e-3-3 3]); xlabel('time(s)'); ylabel('amplitude '); title('original and Filtered Signal '); subplot(2,1,2); plot(t(1:n),y(1:n)); grid on; axis([ 8e-3-3 3]); xlabel('times(s)'); ylabel('amplitude '); 3 Original and Filtered Signal Amplitude 2 1-1 -2-3 1 2 3 4 5 6 7 8 time(s) x 1-3 Amplitude 3 2 1-1 -2-3 1 2 3 4 5 6 7 8 times(s) x 1-3 Figure 11. Original and filtered signals in time domain. Page 14.632.11
3.646389e-2-2.623148e-2-3.63887e-2 6.59395e-4 1.1311239e-2-2.166658e-2-2.3917629e-2 2.2122339e-2 2.262e-2-3.8139676e-2-2.4148287e-2 5.6624944e-2 2.3685121e-2-1.258764e-1-2.512318e-2 3.1675284e-1 5.247762e-1 3.1675284e-1-2.512318e-2-1.258764e-1 2.3685121e-2 5.6624944e-2-2.4148287e-2-3.8139676e-2 2.262e-2 2.2122339e-2-2.3917629e-2-2.166658e-2 1.1311239e-2 6.59395e-4-3.63887e-2-2.623148e-2 3.646389e-2 Figure 12. Coefficients generated by Remez method. Page 14.632.12
FDATOOL EXAMPLE The fdatool command in MATLAB can also be used to design a FIR filter. Typing fdatool in the command line of MATLAB invokes the Filter Design & Analysis Tool, Figure 13. Using the previous parameters for a 33 coefficient lowpass filter, the response type can be set for lowpass, the design method can be set for window, the filter order can be set for 33, the window option can be set for rectangular, the sampling frequency can be set for 1 khz, and the cutoff frequency can be set for 15 Hz. Selecting the design button designs the filter, Figure 14. The corresponding coefficients are shown in Figure 15. Figure 13. MATLAB s Filter Design & Analysis Tool graphic user interface. Page 14.632.13
Figure 14. A 33 coefficient FIR filter design developed by MATLAB s Filter Design & Analysis Tool graphic user interface. Replacing the coefficients in variable c in MATLAB script in Figure 1 with the coefficients in Figure 15, the FIR filter once again passes the 1 khz signal while blocking the 2 khz and 3 khz signals, Figure 16. We can compare the original waveform with the filtered waveform as shown in Figure 17. Once again, the signal has been transformed from a waveform with several frequencies into a signal with a single frequency. Moreover, the techniques used in these examples can be extended to highpass filters, bandpass filters, and bandstop filters. Page 14.632.14
.29.176.188.35 -.173 -.263 -.132.146.356.289 -.74 -.496 -.66 -.137.866.216.278.278.216.866 -.137 -.66 -.496 -.74.289.356.146 -.132 -.263 -.173.35.188.176 Figure 15. Coefficients generated by MATLAB s Filter Design & Analysis Tool. Page 14.632.15
6 4 2 5 1 15 2 25 3 35 4 45 5 2-2 -4-6.5.1.15.2.25.3.35.4.45.5 4 3 2 1 5 1 15 2 25 3 35 4 45 5 Figure 16. Filtered signal in the frequency domain. 3 Original and Filtered Signal Amplitude 2 1-1 -2-3 1 2 3 4 5 6 7 8 time(s) x 1-3 Amplitude 3 2 1-1 -2-3 1 2 3 4 times(s) 5 6 7 8 x 1-3 Figure 17. Original and filtered signal in time domain. Page 14.632.16
CONCLUSIONS Once used primarily for academic research and futuristic military applications, digital signal processing has become a widely accessible commercial technology. A variety of highperformance, integrated digital siganl processors have made digital signal processing technology easier and more affordable to use. Digital signal processors are dedicated microprocessors. Because they are programmed and interfaced much like microprocessors, DSP theory provides a superb alternative for teaching microprocessor theory, especially for programs that emphasize digital communications and digital control. Also, software and development tools are more available so that students can easily become proficient in the use of DSP. Digital signal processing has been transformed from a relatively new, esoteric technology to a mature, well understood technology. It is not impossible to learn, it doesn't require a Ph.D. in mathematics, and it really can be useful. There are texts 4,5 and other resources aimed at the non-phd, and it has never been easier to experiment with signal processing. REFERENCES 1. The Mathworks, MATLAB & Simulink, R28b. Natick, MA: The Mathworks (28). 2. Kehtarnavaz, N and Keramat, M., DSP System Design Using the TMS32C6. Upper Saddle River, NJ: Prentice-Hall (21). 3. Chassaing, R., Digital Signal Processing and Applications with the C6713 and C6416 DSK. New York: Wiley, (25). 4. Lyons, R., Understanding Digital Signal Processing, 2nd Ed., Upper Saddle River, NJ: Prentice-Hall (24). 5. McClellan, J., Schafer, R., and Yoder, M., Dsp First: A Multimedia Approach. Upper Saddle River, NJ: Prentice-Hall (1998). Page 14.632.17