Rensselaer Polytechnic Institute ECSE-4760 Computer Applications Laboratory DIGITAL FILTER DESIGN Number of Sessions – 4 INTRODUCTION This lab demonstrates the use of digital filters on a PC or DSP. It consists of two parts, the first being the design of some filters on the PC using MATLAB and QEDesign, and the second, implementing those filters, as well as some others on the PC and a DSP system. The first part involves the design of some fourth order and higher filters using MATLAB and a filter design program called QEDesign. To help you towards this end, Appendix A provides a list of MATLAB commands useful for filter design. Further details may be found in the MATLAB manual, Appendix B includes instructions for using the Momentum Data Systems QEDesign digital filter design system. The second part involves the implementation and analysis of these filters on the PC. Two methods of implementation will be evaluated. In order to gain some familiarity with digital filter design, you are required to solve the following problem prior to the first lab session: Convert the analog low-pass filter: H (s) 1 1 sRC to an equivalent digital low-pass filter. The filter should have a cut-off frequency of 143 rad/sec. Use the impulse invariant transformation with a sampling period (T) of 7 msec. Your answer should be in the Z domain, and should include an associated block diagram. The program on the PC is set up to implement two different fourth order filters: H z A Bz 1 Cz2 F Gz1 Hz 2 1 Dz 1 Ez2 1 Iz 1 Jz2 H z A Bz 1 Cz2 F Gz1 Hz 2 1 Dz 1 Ez2 1 Iz 1 Jz2 and Block diagrams of the two filter forms are shown in Figure 1 and Figure 2. To run the program, go to the C:\CSTUDIO\CAL_LAB\FILTER directory and type digfilte at the DOS prompt. The 1 program is entirely menu driven. One of the two implementation forms may be chosen from the main menu. Subsequently, the filter coefficients A through J may be entered. Pressing the END key starts the execution of the current filter. Note that A/D input 0 is used for the incoming analog signal (whose range must not exceed 10 volts). D/A output 0 is used for the outgoing filtered signal. y(k) F+Gz-1+Hz-2 1+Iz-1+Jz-2 y(k) FIGURE 1. Filter Type I. 2 F+Gz-1+Hz-2 1+Iz-1+Jz-2 y(k) y(k) H FIGURE 2. Filter Type II. BACKGROUND THE APPROXIMATION PROBLEM The approximation problem is one of finding a match between the idealized frequency response desired, and the various responses possible. The ideal low-pass filter response is as shown in Figure 3a. The filter has a gain equal to 1 for |f| < fc, and a gain equal to 0 for |f| > fc. This response is practically and theoretically unrealizable. Consider the inverse Fourier transform of this filter. It is a sinc pulse centered at t = 0 (Figure 3b), which is a non-causal output. A time delay can be added to the filter and the response is now as in Figure 3c. For a large enough delay, h(t) will be negligible for t < 0, and can be approximated by a realizable filter. 3 There are three main types of low-pass filter approximations. They are the Butterworth (or maximally flat), the Chebyshev (two versions), and the elliptic approximations. The Butterworth low-pass filter of order n has an amplitude ratio given by 1 f 2n 2 H f 1 B This filter, whose Bode plot is shown in Figure 4, has a gain which decreases monotonically from unity at f = 0 Hz. As n (the filter order) is increased, the rate of fall off of the filter at its cutoff frequency is increased. This is not without a penalty, because as the filter degree increases, the phase shift gets worse, and the impulse response does not follow the sinc pulse very closely. The Chebyshev filter (Type I) has a ripple in the pass-band, and decreases monotonically in the stop-band. The Type II filter reverses these bands. A typical frequency response is shown in Figure 5. This filter has the advantage of a faster rate of fall off, and a more linear phase shift. In the passband, the magnitude of the frequency response fluctuates between 1 and 1/(1 + e2)1/2. For a larger e, the ripple is larger but the fall off is faster. There is a design trade-off between the ripple size and the fall off for a given filter order. The elliptic filter allows ripples in both the pass and stop-bands, as shown in Figure 6. This has the fastest fall off rate of the three filter types but has a large phase shift. This filter again has a trade off between ripple size and fall-off rate. For further details on analog filter types see reference [3]. BAND-PASS FILTER DESIGN A band-pass filter with center pass-band frequency w0 can be derived from a low-pass filter by using the low-pass to band-pass transformation. A pole-zero pattern and frequency response curve for a typical low-pass filter is shown in Figure 7a. To make a band-pass filter you might try to make the substitution s s - jw0 to move the poles up to jw0. This would not work because any circuit built with real elements must have all complex poles and zeros in complex conjugate pairs. A substitution that does work is the replacement of the Laplace domain variable s in the lowpass filter H(s) by sb j0 sb j0 2sb where sb is the Laplace variable of the transformed band-pass filter. Then, for frequencies of operation close to the center frequency w0 (i.e. sb is approx equal to jw0), the transformed low-pass filter becomes s j 0 sb j 0 H j2sb H s H b H ( j) 2sb 2sb where D is the deviation from w0. Thus, the shape and bandwidth of the low-pass filter are preserved. This transformation leads to complex conjugate poles and zeros as shown in Figure 7b, and is therefore realizable. 4 FIGURE 3a. Ideal Low-Pass. FIGURE 3b. Impulse Response of a Low-Pass filter. FIGURE 3c. Delayed Response of a filter. 5 FIGURE 4a. Butterworth Low-Pass Filter (1st Order). FIGURE 4b. Butterworth Low-Pass Filter (2nd Order). FIGURE 4c. Butterworth Low-Pass Filter (3rd Order). 6 1 1 2 FIGURE 5. Chebyshev Low-Pass Filter. FIGURE 6. Elliptic Low-Pass Filter. FIGURE 7a. Low-Pass Filter. 7 FIGURE 7b. Transformed Band-Pass Filter. DISCRETE TIME SYSTEMS A discrete signal is an ordered sequence of numbers. If you sampled a continuous signal x(t) every T seconds, your output would be a function (x(kT): k = 0, 1, 2, ...), which is a discrete signal (i.e., a series of values occurring every T seconds). A discrete system is one in which all the variables are discrete signals. A discrete system is analogous to a continuous system in many ways. The output of the system at any future time is known if you know the system's present state and the input. A state variable equation can be written as y(kT) = S[q0 : x(kT)] k ≥ k0, where x(kT) is the input, q0 is the initial state at k = k0, and y(kT) is the output. A fixed, linear discrete system will obey the principles of decomposability, superposition, and time invariance (see reference [2]). Discrete systems are described by difference equations in the same way that continuous systems are described by differential equations. The block diagram elements of a discrete system are unit delay elements and scalars. a. Time-Delay Element. b. Scalar. FIGURE 8. Simulation Diagram Elements. All systems involving digital computers for signal processing are discrete time systems. To work on a signal, it must first be coded into some binary representation. This analog to digital conversion takes some finite amount of time. Therefore, there is some maximum sampling frequency. If the signal is to be processed in real time, the amount of time taken to perform calculations and output results must be added to this conversion time. This reduces the maximum possible sampling frequency. To simplify the manipulation of continuous systems, the Laplace transform is used. An analogous tool for the discrete system is the Z-transform. The Z-transform of v(k) is defined as the infinite summation 8 V z vkz k k 0 which is sometimes expressed as V(z) = Z{v(k)}, or by the transform pair v(k) V(z). The Z-transform converts a difficult to solve finite difference equation into an easy to solve algebraic equation in z. There are many techniques for designing digital filters. The method used in this lab is to design an analog filter for the desired response, and then to transform the H(s) into an H(z) by one of three methods. The first two methods used are impulse and step invariance. These two techniques set the response of the digital filter to a particular input to be equal to the response of the analog filter to the same input. To get the impulse invariant filter, it is necessary to obtain the time domain impulse response h(t) of the desired analog filter. This is then sampled giving the values h(0), h(1),... etc. The corresponding Z-transform of the impulse invariant digital filter is thus h(1) h(2) 2 ... z z Alternately, if H(s) is the transfer function of the analog filter, then H z h0 H z poles of H(s) 1 residuesH s sT 1 1 e z At this point it should be noted that the DC or zero frequency gains of H(s) and H(z) will not be the same. Thus a scaling factor is needed for H(z), DC gain h(i) i 0 For H(s), DC gain h(t)dt 0 However, h(t)dt T h(i) i 0 0 Therefore, H(z) must be multiplied by T prior to its implementation. With the step invariant transformation, the output of the digital filter is to be equal to the sampled outputs of the corresponding analog filter. An example of this is the step invariant Butterworth second order low-pass filter shown in Figure 9. As can be seen, the digital response is identical to the analog response every T seconds. This technique guarantees the output for a step input, but in turn says nothing about the impulse response of the digital filter. To get the step invariant filter, it is necessary to get the time domain response of the analog filter to a step input. Then t is converted to kT and the Z-transform of this is obtained. This is divided by z/(z-1), the Z-transform of the unit step, to get the desired step invariant filter. No correction factor is needed. 9 FIGURE 9. Step Response of a Butterworth Low-Pass Filter. The other technique used for the experiment is the bilinear transformation. This is accomplished by replacing s by (z–1)/(z+1) in the transform of the analog filter HA(s). Rearranging the new transfer function gives the desired HD(z). The magnitude and the phase plots of HD(z) obtained are guaranteed to have the same general shape as those corresponding to HA(s), but with distorted frequency scales. For example, consider the response of the filter to a sinusoidal input of radian frequency 0. The transfer function is given by z 1 H D (z) z e j 0T H A z 1 z e j 0T j T T z 1 e 0 1 j A j tan 0 2 z 1 z e j 0T e j 0T 1 Hence, T A tan 0 2 This warping is illustrated for a band-pass filter in Figure 10. This transformation is used mostly where you have stop and pass-bands and can compensate for the frequency distortion. The advantage of the bilinear transformation is related to fold-over or alias problems. Fold-over is the situation which occurs if the frequency response is not band-limited to half the sampling frequency. Then, because the frequency response repeats every fS, the characteristic has an overlap, i.e., an aliasing problem. The bilinear transformation maps the entire s-plane into a strip bounded by s = +jπfs and s = -jπfs; then mapping this into the z-plane results in no aliasing foldover problem since there are no frequency components past s = jπfs. FILTER TRANSFER FUNCTIONS A. IMPULSE INVARIANT BUTTERWORTH The impulse invariant Butterworth transfer function is derived from the magnitude squared function H(j)2 = 1/(1 + B()2n), where n is the order of the filter. To get a cut-off frequency at 0, it is 10 necessary that H(j 0)2 = 1/2 at 0. Thus, 1 + B( 0)2n = 2. For example, if 0 = 143 rad/sec, and n = 4, then B = (143)-8. To get H(s), set H sH s H j 2 2 s 2 and replace 2 by -s2. All the left-half plane poles are then associated with H(s) and all the right-half plane poles are associated with H(-s). Doing this for the above fourth order filter gives 143 H s s 143112.5s 143247.5s 143157.5s 143202.5 4 This is then expanded into partial fractions in order to get the impulse invariant Z-transform. A useful formula can be used to facilitate the transformation from a second order pair of complex conjugate s-plane poles to the z-plane. If for example, then, H s x jy x jy s a180 s a180 H s x jy x jy s a180 s a180 H z Z{H (s)} x jy x jy 1 z 1eb180 1 z 1eb180 FIGURE 10. Warping of the Frequency axis caused by the Bilinear Transformation. 11 where b = aT or H z b cos180 2x z 1 2e x cosbsin(180 ) ysinbsin(180 ) b cos180 1 z 1 2e cosbsin(180 ) z 2 e 2b cos180 The results then for the above fourth order example for T = 0.007 seconds are: H z 132.114 z 1 1.37226 132.114 z 1 24.5272 1 z 1.736522 z 2 .157589 1 z 1.822155 z 2 .465161 The implementation on the computer is such that the above H(z) is multiplied by T so that the pass-band response is of the order z (i.e. the input and output have the same magnitude). B. STEP INVARIANT BUTTERWORTH For the step invariant Butterworth low-pass, it is first necessary to use the low-pass Butterworth transfer function derived in the preceding part. Then, H(s) is multiplied by 1/s to get the step response of the filter. This response is then converted to a time function y(t), and the corresponding Z-transform Y(z) is taken. This is then divided by the Z-transform of the unit step function to get the step invariant Butterworth low-pass Z-transform which yields H(z). For the preceding example, Y z and H z 1.70709 z 1.375603 .707108 z 1.675496 1 1 z 1.736522 z 2 .157589 1 z 1.822155 z 2 .465161 1 z 1 1.20709 z 11.71443 z 2 (.296808 ) 1 z 1.736522 z 2 .157589 1.207108 z 11.79368 z 2 (.908076 ) 1 z 1.822155 z 2 .465161 C. ELLIPTIC FILTERS The elliptic filter is derived from an analog low-pass elliptic filter using the unscaled bilinear transformation s = (z-1)/(z+1). Use of the bilinear transformation requires that the analog filter be prewarped. Consider a sinusoidal input of radian frequency 0. Then we have s z 1 z 1 e j 0T 1 T j analog j T j tan 0 0 2 e 1 0T analog tan 2 The analog elliptic filter used here was designed for a pass-band ripple of 3 dB and stop band attenuation of 23 dB using the theory presented in [4]. For the digital filter to have a cutoff frequency of 143 rad/sec, the analog filter cutoff is given by analog tan 143 /sec.007 sec tan1 .55 rad/sec 2 2 12 The analog elliptic filter for the warped specifications is given by H s .11624 s 3.07658 s .00966 j.57887 s .00966 j.57887 s .13674 j.33993 s .13674 j.33993 s .014965 j.53943 s .014965 j.53943 H(s) was broken into two additive terms of the form H1s As B s Cs D 2 Applying the bilinear transformation to H1(s) gives 2B 2 B A A B z 1 z 1 C D 1 C D 1 C D H1z 2D 2 2 1 C D 1 z 1 z 1 C D 1 C D Thus, for the above transfer function, .078985 z 1.183462 z 2 .104477 .028022 z 1.032195 z 2 .0047312 H z 1 z 11.2300 z 2 .61146 1 z 11.0730 z 2 .95469 D. BAND-PASS FILTER The band-pass filter is derived from a low-pass filter by using the low-pass to band-pass transformation followed by the bilinear transformation. When the bilinear transform is applied to an analog filter to produce a digital filter, the bandwidth and center frequency must be warped appropriately using T analog tan 0 2 If the desired center frequency of a band-pass filter with bandwidth 14.3 rad/sec is 286 rad/sec and the sampling frequency is 143 Hz, then the center frequency of the analog filter must be analog tan 286 /sec.007 sec tan 1 1.5574 2 rad/sec Bandwidth conversion is done as follows: filter d filter d 1 cos 2 T T 2 2 1 filter .007 sec / 214.3 rad/sec .171 rad/sec cos 2 1 The second order Butterworth low-pass filter for these warped specifications is H s 2a 2 .0599 s 2as 2a 2 s 2 .346 s .0599 2 where a is the bandwidth. Applying the band-pass transformation, 13 s yields H s s 2 02 2s s.01907 .4459 s(.01907 ) .32548 2 s s.2254 2.0719 s s.2638 2.8386 Applying the unscaled bilinear transformation, we get .092927 z1.19742 z2 .10449 .10404 z1.21739 z2 (.11334 ) H z 1 z 1.65018 z 2 .86326 1 z 1 .89635 z 2 .87136 EXPERIMENTAL PROCEDURE PART I For this section, you will be required to use MATLAB or QEDesign, to help with filter design calculations. MATLAB and SPW are available on RCS and QEDesign is on the CAL Microsoft Windows PCs. Some functions to aid you in designing the filters in MATLAB are listed in Appendix A and Appendix B of this handout has is a brief manual on the QEDesign package along with instructions for using the DSP system. The MATLAB designs can only be run on the PC while QEDesign filters may be run on either the PC directly or the DSP (digital signal processor) board linked to the PC. PC implementations are restricted to 4 th order filters with a minimum T = 1.1 msec while the DSP can handle higher than 20th order filters with a minimum T = 20.833 microseconds but limits them to cascade form (Type II). For each of the following questions, be sure to include the filter coefficients in your report along with a z-plane analysis. Do not include any MATLAB diary files, m-files, etc. Computer generated frequency response plots, however, are needed since they can be compared to your lab results. 1) Pick a sampling frequency for a filter to be designed. For PC filter designs, use a sampling period range of T = .0011 to T = .007 sec. The ADSP 21020 (Analog Devices' DSP board) filters must use a T corresponding to one of the standard sample frequencies listed in the table on page 10 of Appendix B. (The actual period corresponding to the sample frequency value entered into the PC program should however be confirmed in the lab. This can be done by running a simple all-pass filter with A = F = 1 and all other coefficients set to 0 and measuring the sample period.) 2) Using the sampling frequency from step 1, design several filters using MATLAB or QEDesign in the parallel and cascade forms. A few suggestions follow, with the first being required: A low-pass filter with cutoff of 10 Hz. Use MATLAB to design this filter and implement it as both a parallel and cascade filter on the PC showing all work. A band-stop notch filter at 15 Hz. A high-pass filter with cutoff at 15 Hz. A band-pass filter from 1.0 kHz to 2.5 kHz and T = .0000625 sec. (Use the ADSP 21020 and analyze with your own voice using a microphone.) A band-stop filter from 1.0 kHz to 2.5 kHz and T = .0000625 sec. (Use the ADSP 21020 and analyze with your own voice using a microphone.) At least two more filters of your own choice. Remember that for PC implementations, you are limited to fourth order here. You should adjust the filter parameters for the optimum design. Note that the MATLAB design will result in a 14 composite 4th order transfer function. Use the MATLAB function “root” or “residue” to get the component blocks for the cascade or parallel form. Note: the selected filter implementation (cascade or parallel) will be highlighted in gray by the PC DIGFILTE program. Gather as much information about the filters as is required for the write-up. Remember to mix the approximation types. 3) Make a thorough report of your results, comparing the approximation types, and showing why you would choose one type over another. Plot the filter on the z-plane and analyze the filter using the z-plane plot. 4) Record the coefficients generated by MATLAB, or QEDesign for use in the implementation. 5) Implement the filters on the PC or ADSP 21020 board, in the parallel or cascade mode (as in Figure 1 and 2) and compare with the results obtained from MATLAB simulations of the same filters. Explain any differences. PART II Note that the following designs are all of Type I (as in Figure 1). A. IMPULSE INVARIANT BUTTERWORTH LOW-PASS FILTER Recall from before the Butterworth parallel fourth order example: 143 H s s 143112.5s 143247.5s 143157.5s 143202.5 4 H z .923876 z 1 .00959623 .923876 z 1 .171519 1 z 1.736522 z 2 .157589 1 z 1.822155 z 2 .465161 1) Implement this filter on the PC, choosing the parallel implementation. 2) Take the data to plot the frequency response of this filter from f = 5 Hz to f = 200 Hz. This can be done simply by varying the frequency of the sine wave generator and comparing the amplitude of the input signal and the amplitude of the steady state output signal. 3) Measure the rise time and overshoot for a square wave input of 4 Hz and explain why it occurs. 4) Is the frequency response different from what you would expect from the analog filter. How? Why? B. STEP INVARIANT BUTTERWORTH LOW-PASS FILTER For this filter, H z 1) 2) 3) 1.20709 z 11.71443 z 2 (.296808 ) 1.207108 z 11.79368 z 2 (.908076 ) 1 z 1.736522 z 2 .157589 1 z 1.822155 z 2 .465161 Implement this filter on the PC choosing the parallel implementation. Sketch the frequency response of this filter up to about 50 Hz. Is it different from that of the Impulse Invariant filter? How? Measure the rise time and overshoot for a square wave input of 4 Hz. How does this compare to that of the Impulse Invariant filter? Is there more or less distortion of the wave shape? Why? 15 C. ELLIPTIC LOW-PASS FILTER (BILINEAR TRANSFORMATION) The filter transfer function is given by .11624 s 3.07658 s .00966 j.57887 s .00966 j.57887 H s s .13674 j.33993 s .13674 j.33993 s .014965 j.53943 s .014965 j.53943 H z 1) 2) 3) .078985 z 1.183462 z 2 (.104477 ) .028022 z 1 .032195 z 2 (.0047312 ) 1 z 11.2300 z 2 .61146 1 z 1 1.0730 x2 .95469 Implement this filter on the PC choosing the parallel implementation. Sketch the frequency response of this filter up to about 50 Hz. How does this compare with that of the Impulse Invariant Butterworth filter? Measure the rise time and overshoot for a square wave input of 4 Hz. How does this compare to that of the Impulse Invariant filter? Which has the best response? Why? D. BAND-PASS FILTER 1) This filter is a bilinear transformation of a band-pass filter that was designed using the lowpass to band-pass transformation. Since the bilinear transformation was used, the frequency axis had to be prewarped. The original low-pass filter was a second order Butterworth. The discrete transfer function for this band-pass filter is H z .092927 z1.19742 z 2 .10449 .10404 z1.21739 z 2 (.11334 ) 1 z 1 (.65018 ) z 2 (.86326 ) 1 z 1.89635 z 2 .87136 Implement this filter on the PC choosing the parallel implementation. 2) Sketch the frequency response of this filter up to about 150 Hz. How is this response different from that of an analog band-pass filter? Why is it different? E. NOISE AND ALIASING 1) Set up the Impulse Invariant Butterworth low-pass filter on the PC. 2) Using the analog low-pass filter provided, filter square waves whose frequencies lie between 25 and 35 Hz and connect this to the computer A/D input. Observe the output and input with and without the analog low-pass filter on the D/A output. (The analog filter has fc ≈ 30 Hz). 3) Implement the filter described in the preparation problem. 4) Feed in a triangular wave at 20 Hz with a noise source attached. Observe the output waveform. By changing the cutoff frequency of your digital filter, attempt to get the best output waveshape possible, with the least distortion. Note your final choice of parameters and what cutoff frequency this represents. Give an explanation as to why this cut-off frequency worked best. REFERENCES 1. 2. 3. 4. Cadzow, J. A., and Martens, H. R., Discrete Time and Computer Control Systems. Frederick, D. K., and Carlson, A. B., Linear Systems in Communications and Control. Oppenheim, A. V., and Schafer, R. W., Digital Signal Processing. Gold, B., and Rader, C. M., Digital Processing of Signals. 16 FACULTY RESOURCE J. W. Woods, W. Pearlman 17 APPENDIX A This appendix describes some of the functions provided by MATLAB for designing and analyzing filters. Note that these functions are described in the MATLAB manual, a copy of which is available on-line. The HELP command in MATLAB should also provide sufficient detail. ANGLE, UNWRAP angle(h) returns the phase angle in radians, of the elements of the complex matrix. These angles will lie between + and –. unwrap(p) corrects the phase angles in vector by adding multiples of +2 or -2, to smooth the transitions across branch cuts. The phase must be in radians. BILINEAR [zd,pd,kd]=bilinear(z,p,k,fs) converts the s-domain transfer function specified by zeros, poles and gain into a discrete equivalent. Inputs z and p are column vectors containing the zeros and poles, and k is a scalar gain factor. fs is the sample frequency in Hz. The discrete equivalent is returned in column vectors zd, pd and scalar kd. [numd,dend]=bilinear(num,den,fs) converts an s-domain transfer function to a discrete equivalent. The function is num(s) num(1)s nn ... num(nn 1) den(s) den(1)s nd ... num(nd 1) fs is the sample frequency in Hz. The discrete equivalent is returned in row vectors numd and dend in descending powers of z. BUTTAP [z,p,k]=buttap(n) returns the zeros, poles and gain of an order n normalized Butterworth analog low-pass filter prototype. The poles are returned in length n column vector p, the gain in k, and z is an empty matrix, as there are no zeros. The transfer function is H ( s) z ( s) k p( s) ( s p(1))( s p(2))...( s p(n)) BUTTER [b,a]=butter(n,wn) designs an order n low-pass digital Butterworth filter with cutoff frequency wn and returns the filter coefficients in length n+1 row vectors b and a. H ( z) B( z ) b(1) b(2) z 1 ... b(n 1) z n A( z ) 1 a(1) z 1 .. a(n 1) z n 18 wn must be between 0 and 1, where 1 corresponds to half the sample frequency. If wn = [w1 w2] is a two element vector, an order 2n band-pass filter is designed with pass-band w1 < w < w2. [b,a]=butter(n,wn,'high') designs a highn. [b,a]=butter(n, n,'stop') designs an order 2n bandn is a two element vector. The stop-band is w1 < w < w2. BUTTERORD [n,wn]=butterord(wp,ws,rp,rs) returns the order n of the lowest order Butterworth filter that loses no more than rp dB in the pass-band and has at least rs dB of attenuation in the stop-band. The pass-band runs from 0 to wp and the stop-band runs from ws to 1, the Nyquist frequency. wn, the natural frequency to use with butter is also returned. CHEB1AP [z,p,k]=cheb1ap(n,rp) returns the zeros, poles and gain of an order n normalized Chebyshev type I analog low-pass filter prototype with rp decibels of ripple in the pass-band. The poles are returned in length n column vector p, the gain in scalar k and z is an empty matrix. H ( s) z ( s) k p( s) ( s p(1))( s p(2))...( s p(n)) The poles are evenly spaced about an ellipse in the left half plane. CHEB1ORD [n,wn]=cheb1ord(wp,ws,rp,rs) returns n, the order of the lowest order Chebyshev filter that loses no more than rp dB in the pass-band and has at least rp dB of attenuation in the stop-band. The pass-band runs from 0 to wp and the stop-band extends from ws to 1, the Nyquist frequency. wn, the Chebyshev normalized frequency to be used with cheby1, is also returned. CHEB2AP [z,p,k]=cheb2ap(n,rs) returns the zeros, poles and gain of an order n normalized Chebyshev type II analog low-pass filter prototype with stop-band ripple rs dB down from the peak value in the pass-band. The zeros and poles are returned in column vectors z and p, and the gain is in scalar k: H ( s) s z (1)...s z(n) z (s) p( s) ( s p(1))...( s p(n)) The Chebyshev cutoff frequency w0 is set to 1 for a normalized result. CHEB2ORD 19 [n,wn]=cheb2ord(wp,ws,rp,rs) returns n, the order of the lowest order Chebyshev filter that loses no more than rp dB in the pass-band and has at least rs dB of attenuation in the stop-band. The pass-band runs from 0 to wp and the stop-band extends from ws to 1, the Nyquist frequency. wn, the natural frequency to be used with cheby2 is also returned. CHEBY1 CHEBY2 [b,a]=cheby1(n,rp,wn) designs an order n low-pass digital Chebyshev filter with cutoff frequency wn and rp dB of ripple in the pass-band. n+1 long row vectors a and b contain the filter coefficients. H ( z) B( z ) b(1) b(2) z 1 ... b(n 1) z n A( z ) 1 a(2) z 1 .. a(n 1) z n n, the cutoff frequency must be between 0 and 1, where 1 corresponds to half the sample frequency. If wn = [w1 w2] is a two element vector, a band-pass filter of order 2n is designed with w1 < < w2. [b,a]=cheby1(n,rp,wn,'high') designs a high-pass filter with cutoff frequency wn. [b,a]=cheby1(n,rp,wn,'stop') designs an order 2n stop-band filter if wn is a two element vector. cheby2 accepts the same parameters as cheby1, but designs a type II Chebyshev filter. ELLIP [b,a]=ellip(n,rp,rs,wn) designs an order n low-pass digital elliptic filter with cutoff frequency wn, rp dB of ripple in the pass-band, and a stop-band rs dB down from the peak value in the pass-band. The filter coefficients are returned in n+1 long row vectors a and b. If wn = [w1 w2] is a two element vector, a band-pass filter of order 2n is designed with w1 < < w2. [b,a]=ellip(n,rp,rs,wn,'high') designs a high-pass filter with cutoff frequency wn. [b,a]=ellip(n,rp,rs,wn,'stop') designs an order 2n stop-band filter if wn is a two element vector. OTHER FUNCTIONS MATLAB has two toolboxes of useful functions and two demonstrations related to polynomial math and filters. Use the “help” command to get more information on any of these items. Toolboxes: polyfun, signal Demonstrations: filtdemo, filters (You may wish to copy listings of these M-files to your directory for your own use and modification.) Functions: conv, deconv, roots, poly, residue, freqs, pzmap, dimpulse, residuez, freqz (conv is also used for polynomial multiplication.) 20 APPENDIX B DIGITAL FILTERING USING THE PC and DSP BOARD The process of digital filtering will be carried out in two steps. First, the coefficients for the filter will be created by QEDesign 1000. Because the filtering program on the PC is limited to a 4th order filter, we will limit the filter order to 4 either by relaxing the constraints on the filter, or by allowing QEDesign to force the filter order to 4. After creating the filter and printing out the coefficients we will implement the filter using a PC. Creating 4th Order Filters on QEDesign I. After starting windows, open the C:\Program Files\MDS\QED1000 folder. Start QEDesign by "double clicking" the QED1000 icon or selecting QED1000 from the Start All Programs MDS QED1000 menu . A parallel port hardware key is required by QEDesign to access its full set of features, including saving and viewing filter design coefficients. Ask the TA for the block to attach to the PC before running the program. II. Pull down the Design menu and choose IIR Design …. If making the filter with cascaded second order sections, choose Bilinear Transformation under the IIR Design menu in order to force that method for the continuous to discrete conversion. If making a parallel implemented filter select Impulse Invariant transformation under the IIR Design ... menu. Under Filter Realization Method select either Cascaded Second Order Sections for a series implementation and Sum of Second Order Sections for a parallel implementation. Select a filter type, and frequency mode. Do not use phase equalization or filter compensation. Select Next and enter the filter specifications. III. At this point a window should pop up showing the predicted order(s) of the filter. Choose one of the analog prototypes from which the digital filter will be modeled. If the predicted order is higher than 4, go back to the design window by pressing Prev in the window. In order to lower the filter order, relax one or more of the constraints (make transition band wider, raise allowable ripple values, etc.). Make sure the order is less than or equal to 4 for implementation with the PC DIGFILTE program. NOTE: If the predicted order is higher than 4, and the desired order is set to 4, QEDesign will automatically relax one or more of the constraints (usually the stopband ripple). IV. Next, go to the Output Quantization … menu and choose On for Switch, fixed for Type, and 24 for Number of Bits: under Quantization General Control and select Accept. These settings will allow the filters created to be used with both the PC and the EZ-LAB ADSP-21020 Analog Devices Evaluation Board. V. After the filter is designed, examine the plots for accuracy. If the filter information is what was desired, save the information to a text file by pulling down the Output menu. From the file menu choose Create Filter Coefficient File … and enter a file name (and directory/drive if needed) to save the file under. You may also use View IIR Quantized Z Domain Data … to observe parameters such as the pole/zero locations and quantization noise figures. VI. This concludes the QEDesign portion of the digital design. 21 Retrieving Coefficients I. Print out the text file created by QEDesign. II. The coefficient file contains all the parameters for the analog filter prototype, and the quantized coefficients labeled b0, b1, b2, a1, a2, (B0, B1, B2, A1, and A2 by the program) and overall gain. These coefficients are for the discrete transfer function of the following form: (overall gain) b0 z 2 b1 z b2 z 2 a1 z a2 or (overall gain) B0z 2 B1z B2 z 2 A1z A2 Implementing Digital Filters on the PC I. The PC to be used for signal processing should have a signal processing card and associated input and output ports installed. In order to test the filter, the following setup should be used: FIGURE 11. Equipment Setup for Filtering with the PC. The output of the signal generator goes to both the channel 1 input of the oscilloscope and the A/D converter on the PC. The output of the signal processor (D/A port) goes through a filter to the channel 2 input of the oscilloscope. If the oscilloscope is triggered on channel 1, the unprocessed signal can be compared directly with the processed signal (filtered or unfiltered). II. To start the digital filtering program, turn on the computer and go to the C:\CAL_LAB\FILTER directory. Start the filtering program by typing DIGFILTE<enter> at the DOS prompt. After the entry screen disappears, there will be a choice of cascade or parallel implementation for the filter. Choose the appropriate implementation by moving the white bar over the implementation type and press <enter>. III. The first line of entry for the filtering program will be the sample frequency. This number should be entered in kHz (e.g. 500 Hz sample rate = .5). The next 10 lines are the coefficients for the digital filter. These coefficients satisfy the following digital transfer function: Az 2 Bz C Fz 2 Gz H 2 z 2 Dz E z Iz J 22 for the cascade implementation and: Az 2 Bz C Fz 2 Gz H 2 z 2 Dz E z Iz J for the parallel implementation. These coefficients can be entered from the QEDesign coefficients file using the following conversions for the cascade configuration: A overall gain B0 (if B0 is not given, it is assumed to be 1) B overall gain B1 C overall gain B2 D A1 E A2 where the Ai and Bi values are from section (stage) 1 F overall gain B0 (if B0 is not given it is assumed to be 1) G = overall gain B1 H overall gain B2 I A1 J A2 where the Ai and Bi values are from section (stage) 2 and for the parallel configuration: A B0 (if B0 is not given it is assumed to be 1) B B1 C B2 D A1 E A2 where the Ai and Bi values are from section (stage) 1 F B0 (If B0 is not given it is assumed to be 1) G B1 H B2 I A1 J A2 IV. To start the filter press the <end> key. The filtering can be halted at any time by pressing <ctrl><break>. Examples Illustrating Procedure Steps In this section two low pass Tschebyscheff filters will be designed using the QEDesign program. Filter Type I will be a parallel form and filter Type II will be a cascade form. The steps needed to generate and implement these filters for both the CAL PCs and directly from QEDesign, using the EZ-LAB ADSP 21020 board (Type II filters only, 24 bit quantization) will also be explained. The coefficients generated by QEDesign are related to the CAL PCs as shown in section III of Implementing Digital Filters on the PC and are referenced below. 23 Cascade (Type II): H( z) Az 2 Bz C Fz 2 Gz H 2 z 2 Dz E z Iz J H( z) Az 2 Bz C Fz 2 Gz H 2 z 2 Dz E z Iz J Parallel (Type I): Section 1, cascade A overall gain B0 (if B0 is not given, it is assumed to be 1) B overall gain B1 C overall gain B2 D A1 E A2 Section 2, cascade F overall gain B0 (if B0 is not given it is assumed to be 1) G = overall gain B1 H overall gain B2 I A1 J A2 In order to generate the desired filter the user must first understand the format that QEDesign uses for its input parameters. The names of these parameters are self-explanatory and are as follows: Sampling Frequency, Passband Frequency, Stopband Frequency, Passband ripple (dB), Stopband Ripple (dB). These parameters are graphically illustrated below and it should be noted how the Passband and Stopband ripples are related to one another, this relation is true for all filter type in QEDesign (see Figure 12). Passband Ripple (dB) 100 10-1 Stopband Ripple (dB) 10-2 10-3 10-4 0 10 20 30 40 50 Passband Freq (Hz) Stopband Freq (Hz) FIGURE FIG.12. 12.Typical TypicalFilter filter Response. response. 24 60 70 With the input parameters explained, two step-by-step filter designs will now be conducted. If QEDesign is not already running type the following command at the DOS prompt C:\WINDOWS\WIN then choose the QEDesign icon in the QEDesign 1000 folder to invoke the program. Example # 1 Cascade (Type II) Low-Pass Tschebyscheff Desired Filter Specifications Sampling Freq. 900 Hz Passband Freq. 200 Hz Stopband Freq. 300 Hz Passband Ripple 1 dB Stopband Ripple 35 dB I. Select Quantized Coefficients under the Options menu (this will allow a coefficient file to be generated which will be needed). This will initiate the Set Quantization Screen, the parameters for this example are as follows: Number of Significant Bits - (24), Quantization Type - (Floating Point Quantization), and Realization Type - (Cascaded Second Order Section (Floating Point)). With these selected, click on the OK icon. Also select Bilinear Transformation, Maximum gain of 1.0, and No Filter Compensation if not already selected under the Options menu NOTE: The number of quantized bits is only critical when using the EZ-LAB board. For that it must be set to 24 bits but when using the CAL PCs 16 bits is sufficient. II. Select the Design menu window from the main menu. From this, pick IIR Design and then Lowpass. III. The Lowpass Filter Input Screen will now be present, at this point enter the desired filter specifications as listed above. Once the values are entered click on the Start icon to advance to the next step. IV. The Estimated Filter Order/Select Analog Filter Type screen will now be visible. This screen is self-explanatory. For this example choose Tschebyscheff. The order will be 4 as per the design specifications for this example. Clicking on the OK icon will start the design process. V. Various performance graphs, including poles and zeros, phase and magnitude, etc., can be selected through the Window menu in the main menu. Tile will display all selected graphs simultaneously on the screen; this is suggested for ease of viewing. VI. Creating Filter Specifications and Filter Coefficient Files is accomplished by clicking on the File icon in the main menu. Choose the Save As option at this point. In the Save As menu choose the following: Save Problem Specifications and Create Quantized Coefficients File. Clicking on OK brings up the two sub-menus which allows the naming of the files VII. The Coefficient file is the only one needed in hard copy form since it restates the filter specs as part of its format. This file can be printed with the use of the File Manager in windows or DOS commands once out of the QEDesign program. (See File 1 and File 2 for sample Coefficient file) VIII. Conversion of QEDesign coefficients for implementation on the CAL PCs as follows: Section 1 25 A = .35832741E-1 x 1.0 = 0.18929538 B = .35832741E-1 x 2.0 = 0.37859076 C = .35832741E-1 x 1.0 = 0.18929538 D = -(.91175234) = -.91175234 E = -(-.35832381) = 0.35832381 Section 2 F = .35832741E-1 x 1.0 = 0.18929538 G = .35832741E-1 x 2.0 = 0.37859076 H = .35832741E-1 x 1.0 = 0.18929538 I = -(.31669492) = -.31669492 J = -(-.75718105) = 0.75718105 Example #2 Low-Pass Tschebyscheff Parallel (Type I) Desired Filter Specifications Sampling Freq. 900 Hz Passband Freq. 100 Hz Stopband Freq. 200 Hz Passband Ripple 1 dB Stopband Ripple 30 dB I. Select Options Menu from the Main menu. Select Quantized Coefficients (this will allow a coefficient file to be generated which will be needed). This will initiate the Set Quantization Screen, the parameters for this example are as follows: Number of Significant Bits - (16), Quantization Type - (Floating Point Quantization), and Realization Type - (Parallel Transposed Second Order Section (Floating Point)). With these selected, click on the OK icon. This will once again bring you to the Options Menu, at this point select Impulse Invariant, Maximum gain of 1 dB, and No Filter Compensation if not already selected. II. - VIII. These steps are the same as in Example # 1 Section 1 A = -.92521667E-01 = -.09252167 B = -.39561272E-01 = -.03956127 C = .00000000 = 0.0 D = -(1.4032898) = -1.4032898 E = -(-.82296753) = 0.82296753 26 Section 2 F = .92521667E-01 = 0.09252167 G = .37019730E-01 = 0.03701973 H = .00000000 = 0.0 I = -(1.5173645) = -1.5173645 J = -(-.62477112) = 0.62477112 With these conversions completed either filter can be easily implemented on the CAL PCs by following the procedure outlined in sections I - IV of Implementing Digital Filters on the PC. Possible implementation problems can be avoided by following these several steps. 1) Make sure the A/D/A converter is plugged into the PC correctly. The 50-pin plug is not keyed and therefore may have been installed incorrectly. 2) Before implementing the filter make sure the amplitude of the input signal supplied by the signal generator is much less then 10 volts. If a large signal is initially supplied, the PC will crash and have to be rebooted to continue. 3) Check entered coefficient values or reenter them if filter does not work. The input format on the CAL digital filter program makes correcting coefficients rather difficult so care should be used when first entering these values. EZ-LAB ADSP-21020 Implementation via QEDesign: Implementing Type II filters on the EZ-LAB ADSP-21020 board via QEDesign directly is all file manipulation rather then a conversion process as needed for the CAL PCs. As stated earlier the number of quantized bits must be 24, this bit length is a requirement of the EZGEN21K compiler program and as such will not generate code unless this requirement is satisfied. The hardware should be set up as follows: J7 IN-L J5 OUT-L RS232 to PC EZ-LAB ADSP 21020 FIGURE 13. Equipment Setup for Filtering with the DSP. 27 The steps needed for implementation are as follows: I. Copy the coefficient file generated by QEDesign into the ADSP21K directory. When in the QEDesign directory type the following DOS commands COPY <file_name.flt> C:\DSPWORKS\ADSP21K II. Change directories once more by typing CD C:\DSPWORKS\ADSP21K III. At the C: prompt type EZGEN21K, this will bring up a list of available files, enter the file number of interest. After a period of time a completion screen will appear. You may then return to the main menu to exit the code generator or select another coefficient file. At this point, a bug in some versions of the design package requires an extra step before proceeding to step IV. EZGEN21K takes file_name.FLT and creates file_name.ASM. The file file_name.ASM must be slightly edited before it can be assembled. Using any text editor (e.g. EDIT), open file_name.ASM and change the line: #include "body1.asm" to: #include "body1.h" save the changes and exit the editor. IV. After exiting the code generator program type the following at the C: prompt: ASM <file_name>. (No extension is needed.) V. Type LNKIIR <file_name> at next prompt. VI. Type LAB21K when the linking is done. A screen will now appear with various options that are self-explanatory. Choose Option 0 (Download to ADSP 21020 Memory) and enter the desired file name at the prompt. Pick Option 1 to run the filter. Possible Problems: Before using the EZ-LAB ADSP 21020 board the manual should be referenced for its proper operation. Special attention should be given to page 5-11 of the manual (Change setup for the AD1849) summarized below. This page covers the operation of the mode switches. The manual states that these switches should not be pressed too quickly. This fact cannot be overstated; the only problem associated with the operation of the board while preparing this document was with pressing these switches too fast. There are three program parameters that may be adjusted by the user. They are: 1. Input signal source; toggles between Line and Microphone. The program defaults to Microphone but the Line will be used in parts of the experiment. 2. Input gain; 8 levels are available. The default gain will probably be too small but setting it too high will cause distortion and numerical overflow errors. 3. Sample rate; 16 standard sample frequencies from 5.5125 kHz to 48 kHz are available with the default being 16 kHz. (See the table below for available frequencies.) The following steps are performed to change the values of the three system parameters. Using the FLAG0 and IRQ2 interrupt push buttons, these parameters may be changed from their default values. Do not press the IRQ2 button too fast when changing the sample rate. The 2111 reboots and sets up the AD1849 each time the button is pressed, and will get confused if the button is pressed while this is happening. INITIALIZATION: FLAG0 must be setup as an input. IRQ2 must be enabled. ustat1 bits 0 & 1 are reserved to tell the IRQ2 if it is in setup. 28 OPERATION: 1. Press and hold <FLAG0> button 2. Press and release <RESET> button (SLOWLY) 3. Release <FLAG0> button to enter the setup routine to adjust parameter values 4. Push <IRQ2> button to toggle between Line and Microphone input 5. Push <FLAG0> button to go to the next state (Step 6.) 6. Push <IRQ2> button to change the input gain (circles continuously through all 8 values, going to the next value with each press) 7. Push <FLAG0> button to go to the next state (Step 8.) 8. Push <IRQ2> button to change the sample rate (circles continuously through all 16 values, going to the next value with each press) 9. Push <FLAG0> button to exit the setup routine. Microphone Input Gain Formula: ex) Default level = 4 gain = 20 dB + (level)(3) dB default microphone gain = 20 dB + (4)(3) dB = 32 dB Line input Gain Formula: ex) Default level = 0 gain = (level)(3) dB default line gain = (0)(3) dB = 0 dB Sample Frequency Table level sample frequency 0 8.0 kHz 1 16.0 kHz 2 27.42857 kHz 3 32.0 kHz 4 N/A 5 N/A 6 48.0 kHz 7 9.6 kHz 8 5.5125 kHz 9 11.025 kHz 10 18.9 kHz 11 22.05 kHz 12 37.8 kHz 13 44.1 kHz 14 33.075 kHz 15 6.615 kHz (default) 29 File 1 FILTER COEFFICIENT FILE IIR DESIGN FILTER TYPE LOW PASS ANALOG FILTER TYPE TSCHEBYSCHEFF PASSBAND RIPPLE IN -dB -1.0000 STOPBAND RIPPLE IN -dB -35.0000 PASSBAND CUTOFF FREQUENCY .200000E+03 HERTZ STOPBAND CUTOFF FREQUENCY .300000E+03 HERTZ SAMPLING FREQUENCY .900000E+03 HERTZ FILTER DESIGN METHOD: BILINEAR TRANSFORMATION FILTER ORDER 4 0004h NUMBER OF SECTIONS 2 0002h NO. OF QUANTIZED BITS 24 0018h QUANTIZATION TYPE - FLOATING POINT COEFFICIENTS SCALED FOR FLOATING POINT IMPLEMENTATION .35832741E-01 /* overall gain 2.0000000 /* section 1 coefficient B1 1.0000000 /* section 1 coefficient B2 .91175234 /* section 1 coefficient A1 -.35832381 /* section 1 coefficient A2 2.0000000 /* section 2 coefficient B1 1.0000000 /* section 2 coefficient B2 .31669492 /* section 2 coefficient A1 -.75718105 /* section 2 coefficient A2 */ */ */ */ */ */ */ */ */ File 2 FILTER COEFFICIENT FILE IIR DESIGN FILTER TYPE LOW PASS ANALOG FILTER TYPE TSCHEBYSCHEFF PASSBAND RIPPLE IN -dB -1.0000 STOPBAND RIPPLE IN -dB -30.0000 PASSBAND CUTOFF FREQUENCY .100000E+03 HERTZ STOPBAND CUTOFF FREQUENCY .200000E+03 HERTZ SAMPLING FREQUENCY .900000E+03 HERTZ FILTER DESIGN METHOD: INVARIANT IMPULSE FILTER ORDER 4 0004h NUMBER OF SECTIONS 2 0002h NO. OF QUANTIZED BITS 16 0010h QUANTIZATION TYPE - FLOATING POINT COEFFICIENTS SCALED FOR FLOATING POINT IMPLEMENTATION .00000000 /* overall gain -.92521667E-01 /* section 1 coefficient B0 -.39561272E-01 /* section 1 coefficient B1 .00000000 /* section 1 coefficient B2 1.4032898 /* section 1 coefficient A1 -.82296753 /* section 1 coefficient A2 .92521667E-01 /* section 2 coefficient B0 .37019730E-01 /* section 2 coefficient B1 .00000000 /* section 2 coefficient B2 1.5173645 /* section 2 coefficient A1 -.62477112 /* section 2 coefficient A2 30 */ */ */ */ */ */ */ */ */ */ */ 31