Case Studies: (1) Digital FIR filter design and implementation with MATLAB and DSP56002 processor (2) Digital IIR filter design and implementation with MATLAB and DSP56002 processor Professor E C Ifeachor 11 March 2003. 1 Case study 1: Digital FIR filter design and implementation with MATLAB and DSP56002 processor 1. Introduction This case study is intended to provide a practical guide to students in the design and implementation of FIR filters using MATLAB and a fixed point DSP processor (DSP56002 in particular). The case study pulls together various concepts that we have covered in laboratory exercises and lectures. Specific learning objectives are to provide an understanding and practical guide on: (1) how to use MATLAB to calculate the coefficients and plot the frequency response of optimal FIR filters for a given set of specifications. investigate the effects of coefficient quantization on frequency response (2) how to implement FIR filters using a fixed point DSP processor 2. The problem A requirement exists for a linear phase FIR filter to remove a 1 kHz interference from an audio signal. The filter should meet the following specifications: passband ripple stop band attenuation pass band stop band edge frequencies sampling frequency 0.5 dB 25 dB 900 – 1100 Hz 990 Hz and 1010 Hz 8 kHz Design and implement the filter using MATLAB and the Motorola DSP56002 fixed point DSP processor. Solution As in most design problems, a good approach here is to first break the task into sub-tasks. In this case, specific sub-tasks are: (1) Calculate the coefficients of a suitable FIR filter using MATLAB and the optimal method. NB: The MATLAB function remezord will be used to estimate the order of the filter, N, remez to calculate the filter coefficients, and freqz to compute and plot the frequency response of the filter. The following equations are useful for converting pass band and stop band ripples from dB to ordinary units (the remezord and remez functions do not accept values in dB): R p 20 log 10 where 1 p 1 p - pass band ripple in dB; Rs 20 log 10 s 1 p - stopband attenuation in dB p is passband ripple and s is stopband ripple. The filter length (or order) may be obtained using the following commands: Rp;0.5; Rs=25; dp=(1-10^(-Rp/20))/(1+10^(-Rp/20)); % convert Rp to ordinary units ds=10^(-Rs/20))*(dp+1); % convert Rs to ordinary units [N, F0, M0, W] = remezord([900 990 1010 1100], [1 0 1], [ds, dp, ds], 8000); 2 The above commands returned a value for N of 106. Having determined N, the remez command is then used to obtain the cofficients of the FIR filter as follows: B = remez(N, F0, M0, W); freqz(B, [1], 200, 8000) The filter coefficients and the filter response are depicted in Table 1 and Figure 1, respectively. Comments: (i) The general syntax for remezord is: [N,F0,M0,W] = remezord(F, M, DEV, Fs) In our case F, M and DEV are vectors. In practice, it is best not to enter the values of the vectors directly as we have done (we done so here for ease of comprehension), but instead to have them pre-defined so that the commands can be re-used (see later). (ii) Again, for the remez command we have used specific parameter values for the problem in the arguments for simplicity and to aid understanding here. A general form is preferred. (2) Determine a suitable coefficient word length for the filter by carrying out an analysis of the effects of quantizing the coefficients to 4, 8, 16, and 24-bits on the frequency response of the filter. NB: to quantise the filter coefficients to B-bit, two's complement, fixed point numbers, we simply multiply each B 1 coefficient by 2 and then round to the nearest integer. The difference between the actual coefficients and the fixed point representation gives a measure of coefficient quantisation error. The MATLAB m-file is given in Appendix 1 and the associated results are shown in Figure 2. 3 Table 1 Filter coefficients (NB: only one half of coefficients are listed because of symmetry). k h(k) 0 0.00116584619411 1 0.01975815182230 2 0.00330847539025 3 0.00004835831475 4 0.00481818823492 5 0.00729292239187 6 0.00550995544411 7 0.00000679279742 8 0.006232372711 9 0.00934969594846 10 0.00699660586715 11 0.00000114854211 12 0.00777209131110 13 0.01155949476867 14 0.00857764406256 15 0.00000139583919 16 0.00939939512564 17 0.01387085344160 18 0.01022430003039 19 0.00000237458747 20 0.01105628814338 21 0.01622523306074 22 0.01188363203294 23 0.00000391048761 24 0.01270392307412 25 0.01853642365879 26 0.01351144439539 Quantized coefficients k 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 h(k) 0.00000815480843 0.01428029150806 0.02073819342841 0.01503923635205 0.00000109653069 0.01575296056380 0.02276078998661 0.01641997374020 0.00000629689036 0.01705535532922 0.02452396027161 0.01762128567316 0.00000679473857 0.01812302720587 0.02597299815988 0.01857095697618 0.00000994460442 0.01895835721894 0.02705553261321 0.01926285060899 0.00001225324307 0.01950240211186 0.02768945331773 0.01965774523850 0.00000468761533 0.01973425842113 0.97206655183857 Quantized coefficients Figure 1 4 24-bit Frequency Plot > B24(1:15)' 0.01827049255371 0.00294506549835 -0.00095856189728 -0.00549638271332 -0.00782871246338 -0.00612306594849 -0.00087237358093 0.00507891178131 0.00807225704193 0.00586605072021 -0.00074934959412 -0.00812005996704 -0.01170027256012 -0.00885510444641 -0.00068557262421 16-bit Frequency Plot > B16(1:15)' 0.01824951171875 0.00292968750000 -0.00097656250000 -0.00552368164063 -0.00784301757813 -0.00613403320313 -0.00088500976563 0.00506591796875 0.00805664062500 0.00585937500000 -0.00076293945313 -0.00814819335938 -0.01171875000000 -0.00888061523438 -0.00070190429688 5 8-bit Frequency Plot > B8(1:15)' 0.01562500000000 0.0 -0.00781250000000 -0.00781250000000 -0.01562500000000 -0.00781250000000 -0.00781250000000 0.0 0.00781250000000 0.0 -0.00781250000000 -0.01562500000000 -0.01562500000000 -0.01562500000000 -0.00781250000000 4-bit Frequency Plot > B4(1:15)' 0.0 0.0 -0.12500000000000 -0.12500000000000 -0.12500000000000 -0.12500000000000 -0.12500000000000 0.0 0.0 0.0 -0.12500000000000 -0.12500000000000 -0.12500000000000 -0.12500000000000 -0.12500000000000 Figure 2 Effects of coefficient quantization on frequency response of FIR filters. 6 (3) Implement the filter on the DSP56002 evaluation module and verify that the filter is operating correctly. NB: As an introduction to FIR digital filtering using the DSP56002, consider the simple 3-point FIR filter depicted in Figure 2a. The FIR filter is characterised by the following equation: y (n) h(0) x(n) h(1) x(n 1) h(2) x(n 2) Figure 2b gives the initial memory map for the coefficient and data for the filter. Registers r0 and r4 are used as pointers to the coefficient and data memories, respectively. Circular addressing is used. Table 2 gives the DSP56002 code for implementing the simple filter. The comment column explains the operation of the program. Figure 2 (a) A simple three-point FIR filter, and (b) associated memory map. move move mpy mac mac x: (r0)+, x0 ; copy h(2) into register x0 and increment pointer r0. y: (r4)+, y0 ; copy x(n-1) into register y0 and increment pointer r4 x0, y0, a x: (r0)+, x0 y: (r4)+, y0 ; multiply h(2) and x(n-2) and store in accumulator a ; copy h(1) into register x0 and increment r0. ; copy x(n-1) into register y0 and increment r4. x0, y0, a x: (r0)+, x0 y: (r4)+, y0 ; multiply h(1) and x(n-1) and add to the accumulator a ; Copy a0 into register x0 and increment r0. Copy ; Copy x(n) into register y0 and increment r4 x0, y0, a y1, y: (r4)+ ; multiply h(0) and x(n) and add to the accumulator a ; Accumulator a contains the output sample y(n). ; x(n-2), x(n-1) and x(n) shall now be referred to as ; x(n-3), x(n-2) and x(n-1). Overwrite x(n-3) with the ; next input sample stored in y(1). Program 1 DSP56002 Code for a simple 3-point FIR filter. For an FIR filter with many coefficients, it is more efficient to use the DSP56002 repeat instruction together with the mac instruction. A fragment of a DSP56000 code is given below (See the appendix for more detailed listing of the DSP codes): ; FIR filtering using the repeat instruction clr rep mac macr move a #N-1 x0, y0, a x0, y0, a a, y:Output x(r0) +, x0 y(r4)+, y0 x(r0) +, x0 (r0)- y(r4)+, y0 The quantised filter coefficients need to be inserted into the program in the appendix and run on the DSP56002 EVM to verify that it does what is required. 7 Case study 2: Digital IIR filter design and implementation with MATLAB and DSP56002 processor Introduction This case study follows on from the last case study on FIR filter design. The purpose is to provide a practical guide in the design and implementation of IIR filters using MATLAB and fixed point DSP processors (DSP56002 in particular). The case study brings together the key concepts already covered in the laboratory sessions and lectures. In addition, we will introduce new concepts on the effects of using fixed point processors to implement IIR filters. The solution is tutorial in nature and deliberately detailed to make it easier to follow. Specific learning objectives are to provide an understanding and practical guide on: (1) how to use MATLAB to plot the frequency response of IIR filters for a given set of coefficients (2) how to represent IIR filters using direct form I and direct form II (canonic) structures (3) how to represent IIR filter coefficients in fixed point format (4) how to implement IIR filters using high level language and a fixed point DSP processor. Second order IIR filters will be used as the main vehicle in the case study. 1. The Problem Design and implement a second order IIR notch filter that meets the specifications given below. The coefficients of the filter should be obtained using the pole-zero placement method. The filter should be implemented on the Motorola DSP56002 DSP fixed point processor using the canonic structure. Notch frequency 3 dB notch bandwidth Sampling frequency - 1.6 kHz 50Hz 16 kHz Solution As in most design problems, we will break the problem into sub-tasks: Coefficient calculation using the pole-zero placement method and verification of the frequency response using MATLAB. Representation of filter using the canonic structure. Representation of filter coefficients in fixed point and analysis of finite word length effects. Implementation of filter In the next several sections, we will go through the sub-tasks. 1.1 Coefficient calculation and frequency response verification We have come across a very similar problem before in Semester 1. However, the solution isrepeated here for convenience. Now, MATLAB does not support the pole-placement method of coefficient calculation explicitly. To reject the component at 1.60 kHz, place a pair of complex conjugate zeros at 8 points on the unit circle equivalent to 1.6kHz , i.e. at angles of (360 1600) / 16000 36 . To achieve a sharp notch and improved amplitude response a pair of complex conjugate poles are o placed at 36 and radius, r: o r 1 bandwidth Fs 1 = 100 0.980365 16000 From the pole-zero diagram (Figure 1), the transfer function, H(z), is given by ( z e j 36 )( z e j 36 ) o H ( z) o (1) ( z 0.980365e j 36 )( z 0.980365e j 36 ) o = H ( z) o z 2 1.618034 z 1 z 2 1.586264 z 0.961116 NB: we have made use of the well known relation: e j cos j sin Figure 1 Conceptual pole-zero diagram and magnitude frequency response of filter. To make the filter realizable, we divide the top and bottom of H(z) by the highest power of z, i.e. H ( z) z2: 1 1.618034 z 1 z 2 1 1.586264 z 1 0.961116 z 2 The general second order transfer function has the form: H ( z) b0 b1 z 1 b2 z 2 1 a1 z 1 a 2 z 2 (2) Comparing Equation 2 with the transfer function of the filter, the coefficients are: b0 1 , b1 1.618034 , b2 1 ; a1 1.586264 , a2 0.961116 To verify that the transfer function above will lead to the desired frequency-selective filter, its frequency response is obtained using the following MATLAB commands (see Figure 2): 9 We can verify the frequency response of the filter in (2) by using the coefficients directly in MATLAB.: b = [1 – 1.618034 1]; a = [1 - 1.586264 0.961116]; freqz(b, a, 512, 16000) % filter coefficients % plot frequency response Figure 2 Frequency response of filter. In the MATLAB code above, we have used the coefficients directly. As explained previously it is often better to use the general form of the transfer function in MATLAB. From Equation 1, the general form of the second order filter with zeros at 1 , and poles at r is: H ( z) ( z e j )( z e j ) ( z re j )( z re j ) = 1 2 cos( ) z 1 z 2 1 2r cos( ) z 1 r 2 z 2 (3) where in this case, r 0.980365 2 and 36 . We have come across Equation (3) before in the lab (Introduction to digital filter design with MATLAB). The MATLAB code for this is given in the Appendix. o 2.2 Representation of filter using the canonic structure. In practice, IIR filters are often implemented using the second order canonic section (i.e. direct form II) and direct form I structures. The direct form I is dealt with in the Problems section. 10 The canonic structure is characterised by the following equations: y(n) b0 w(n) b1 w(n 1) b2 w(n 2) = w(n) 1.618034w(n 1) w(n 1) (4a) w(n) x(n) a1w(n 1) a2 w(n 2) = x(n) 1.586264w(n 1) 0.961116w(n 2) b0 b1 z 1 b2 z 2 H ( z) 1 a1 z 1 a 2 z 2 (4b) (4c) where b0 1 b1 1.618034 ; b2 1 ; a1 1.586264 a2 0.961116 Equations 4a and 4b are the two-step difference equation which are used to perform IIR filtering. Equation 4c is the transfer function of the filter. For higher order filters (see later), several second order canonic sections (or the direct form I sections) are connected in cascade or parallel. Figure 3 Second order canonic (direct form II) structure DSP56000 Implementation (1) Fixed point representation of filter coefficients 11 The coefficients of the IIR filter, a k and bk , obtained in section 2.1 above are represented as decimal numbers. When an IIR filter is implemented in a fixed point processor, the coefficients must be represented in fixed point format. B 1 In practice, to quantise the coefficients, we simply multiply each coefficient by 2 . This automatically allows 1 bit for the sign. However, in some cases one or more of the filter coefficients will be greater than 1 (i.e. exceeds the permissible range) and an allowance should be made for this. Returning to the design problem, our filter is characterised by the following transfer function: H ( z) 1 b1 z 1 z 2 1 a1 z 1 a 2 z 2 - transfer function. Where b1 1.618034 , a1 1.586264 ; a2 0.961116 We will quantise the coefficients to 24 bits (the word length of the DSP56000). As some of the coefficients are greater than unity, we could assign 1 bit as the sign bit and 1 bit for the integer part and 22 bits for the fractional part of the coefficients. Quantizing the coefficients in this way leads to: b1' 1.618034 2 22 6786527 ; a1' 1.586264 2 22 6653273 a2' 0.961116 2 22 4031213 . An alternative quantization approach is to divide each coefficient by 2 to bring them to within the B 1 permissible range and then multiplying each by 2 . During filtering, the results of MAC operations will need to be shifted left at the appropriate point to compensate for this. (2) DSP56000 code for filter. Constraints in the processor. Finite word length effects in fixed point DSP systems (chapter 13 – third lecture). 12 Appendix 1 - Codes for FIR filter design and implementation (prepared by Brahim). A1.1 MatLab Code FreqPass1 = 900; FreqStop1 = 990; FreqStop2 = 1010; FreqPass2 = 1100; PassBandRipple = 0.5; StopBandRipple = 25; SamplingFreq = 8000; a = 10^(-PassBandRipple / 20); PassBandDeviation = (1 - a) / (1 + a); StopBandDeviation = (1 + PassBandDeviation) * (10^(-StopBandRipple / 20)); CutOffFreq = [FreqPass1 FreqStop1 FreqStop2 FreqPass2]; Magnitude = [1 0 1]; Deviation = [PassBandDeviation StopBandDeviation PassBandDeviation]; [N, Fo, Mo, W] = remezord(CutOffFreq, Magnitude, Deviation, SamplingFreq); B = remez(N, Fo, Mo, W); figure(1); freqz(B); title('Floating Point Response'); B24 = quantise(B, 24); figure(2); freqz(B24); title('24-bit FixedPoint Response'); B16 = quantise(B, 16); figure(3); freqz(B16); title('16-bit FixedPoint Response'); B8 = quantise(B, 8); figure(4); freqz(B8); title('8-bit FixedPoint Response'); B4 = quantise(B, 4); figure(5); freqz(B4); title('4-bit FixedPoint Response'); 13 A1.2 DSP56000 Code for FIR filtering DSP Code to initialize the filter ;============================================================ ; initialisation of the filter here move move move move #Samples,r0 #Coeffs,r4 #N-1,m0 #N-1,m4 ; ; ; ; r0 points on the Sample data r4 points on the Coeffs data Memory Access Modulo N-1 Memory Access Modulo N-1 DSP Code for the FIR filter move x:RX_BUFF_BASE,x0 ; Get new sample Left ;============================================================ ; Code of the FIR filter here ... move x0,x:(r0) ; Input sample in memory clr rep mac macr move move channel a x:(r0)+,x0 #N-1 x0,y0,a x:(r0)+,x0 x0,y0,a (r0)a,x0 x0,x:TX_BUFF_BASE y:(r4)+,y0 y:(r4)+,y0 ; Filtered output ; Put value in Left 14 Appendix 2 Codes for IIR filter design MATLAB codes for the general notch filter b=[1 -1.618034 1]; a=[1 -1.586264 0.961116]; freqz(b, a, 512, 16000) zplane(b,a) % filter coefficients % plot frequency response % plot the pole-zero diagram ============= Fs=16000; r=1-(100*pi)/Fs; angle=2*pi*(36/360); a=[1 -2*r*cos(angle) r*r]; b=[1 -2*cos(angle) 1]; freqz(b,a,512,Fs) % sampling frequency % pole position % filter coefficients b = 1.00000000000000 -1.61803398874989 1.00000000000000 a = 1.00000000000000 -1.58626389138079 0.96111553322500 ============== Fs=16000; r=1-(100*pi)/Fs; angle=2*pi*(36/360); a=[1 -2*r*cos(angle) r*r]; b=[1 0 0]; h=freqz(b,a,512,Fs); mag=abs(h); s1=max(mag); s1 =43.49085605400499 plot(mag) % sampling frequency % pole position % filter coefficients % magnitude frequency response ================ a1=-1.586264; a2=0.961116; s2=1/(1-a2*a2-(a1*a1*(1-a2)/(1+a2))); s2= 37.92842449708963 b1=-1.618034; b11=b1*(2^22) b11 = -6.786526478336000e+006 a11=a1*(2^22) a11 = -6.653273440256000e+006 a22=a2*(2^22) a22 = 4.031212683264000e+006 ss2=sqrt(s2) ss2 = 6.15860572671198 15