Lab4Introduction In this lab you will study sample by sample processing methods for FIR filters and implement them on the TMS320C6713 processor. Once you know how to implement a multiple delay on a sample by sample basis, it becomes straightforward to implement FIR and IIR filters. Multiple delays are also the key component in various digital audio effects, such as reverb. Delays can be implemented using linear or circular buffers, the latter being more efficient, especially for audio effects. The theory behind this lab is developed in Ch. 4 of the text [1] for FIR filters, and used in Ch. 8 for audio effects. FIR1 FIR filter design (using the window method) function B = FIR1(N,Wn) designs an N'th order lowpass FIR digital filter and returns the filter coefficients in length N+1 vector B. The cut-off frequency Wn must be between 0 < Wn < 1.0, with 1.0 corresponding to half the sample rate. The filter B is real and has linear phase, i.e., even symmetric coefficients obeying B(k) = B(N+2-k), k = 1,2,...,N+1. If Wn is a two-element vector, Wn = [W1 W2], FIR1 returns an order N bandpass filter with passband W1 < W < W2. B = FIR1(N,Wn,'high') designs a highpass filter. B = FIR1(N,Wn,'stop') is a bandstop filter if Wn = [W1 W2]. If Wn is a multi-element vector, Wn = [W1 W2 W3 W4 W5 ... WN], FIR1 returns an order N multiband filter with bands 0 < W < W1, W1 < W < W2, ..., WN < W < 1. B = FIR1(N,Wn,'DC-1') makes the first band a passband. B = FIR1(N,Wn,'DC-0') makes the first band a stopband. For filters with a passband near Fs/2, e.g., highpass and bandstop filters, N must be even. By default FIR1 uses a Hamming window. Other available windows, including Boxcar, Hanning, Bartlett, Blackman, Kaiser and Chebwin can be specified with an optional trailing argument. For example, B = FIR1(N,Wn,kaiser(N+1,4)) uses a Kaiser window with beta=4. B = FIR1(N,Wn,'high',chebwin(N+1,R)) uses a Chebyshev window. By default, the filter is scaled so the center of the first pass band has magnitude exactly one after windowing. Use a trailing 'noscale' Lab report 4 /************************************************************************************ Band stop filter *****************************************************************************/ Matlab source code clc close all clear all Fs=8e3; Ts=1/Fs; Ns=512; t=[0:Ts:Ts*(Ns-1)]; f1=500; f2=1800; f3=2000; f4=3200; x1=sin(2*pi*f1*t); x2=sin(2*pi*f2*t); x3=sin(2*pi*f3*t); x4=sin(2*pi*f4*t); x=x1+x2+x3+x4; grid on; N=16; W=[0.4 0.6]; B=FIR1(N,W,'DC-1'); %B=32767.*C B A=1; freqz(B,A); %pause; figure; subplot(2,1,1); Npts=200; plot(t(1:Npts),x(1:Npts)); xlabel('time (s)'); ylabel('Filtered Sig'); %pause; y=filter(B,A,x); plot(t(1:Npts),y(1:Npts)); %figure; subplot(2,1,2); xfftmag=(abs(fft(x,Ns))); xfftmagh=xfftmag(1:length(xfftmag)/2); f=[1:1:length(xfftmagh)]*Fs/Ns; plot(f,xfftmagh); title('Input and Output Spectra'); xlabel('freq (Hz)'); ylabel('Input spectrum'); subplot(2,1,2); yfftmag=(abs(fft(y,Ns))); yfftmagh=yfftmag(1:length(yfftmag)/2); plot(f,yfftmagh); xlabel('freq(Hz)'); ylabel('Filt Sig Spectrum'); A) Coefficients 0218 0.0821 0.0000 -0.0000 -0.1625 0.0038 0.B= Columns 1 through 7 -0.0821 0.1625 .0000 0.0000 -0.0000 0.0000 Columns 15 through 17 0.0038 0.0218 Figure 0.0000 0.0000 Columns 8 through 14 0.8031 0. 0000 0.B) 1) coefficients B= Columns 1 through 7 0.1130 -0.0042 Figure 0.0158 -0.1184 -0.0050 0.2147 -0.2147 Columns 8 through 14 0.0000 .0158 0.1130 0.1184 Columns 15 through 17 -0.0082 -0.0042 0.0050 0.0082 0.4052 0. 0000 Columns 15 through 17 0.0760 -0.0000 -0.2 2) coefficients B= Columns 1 through 7 -0.5000 -0.0232 .3072 -0.0000 0.0000 0.0000 Columns 8 through 14 -0.0052 -0.0000 -0.0232 -0.3072 0.0760 -0. h> #include <Codec. 612 Digital Signal Processing Laboratory #include <stdlib.h> #include <Intr.h> #include <Common.h> #include <stdio.h> #include <string.0000 A) C source code /* Floating Point FIR Digital Filter Implementation */ /* Appl.h> */ .0000 Figure 0.h> #include <Mcbsp.h> #include <Board.-0.h> #include <Mcbspdrv.0052 -0. Phys. 0000 0.0000.#include <mathf.0000 Columns 8 through 14 0.0038.6 kHz and */ coeff[4] = -0.0000. /* x array holds past input samples */ void hookint(void). interrupt void McBSPRcvISR(void).0038 0.0000.1625. */ coeff[5] = 0. /* to a 16th-order FIR Band Stop FIR filter */ coeff[2] = 0.0000 0.0821 0.Actual_Sampling_Rate.0821 0.0000 -0. Mcbsp_config mcbspConfig.0218 0.0000 .1625 0. coeff[7] = 0.8031 0.0038 coeff[0] = -0.8031. coeff[14] = 0.0000 -0. coeff[9] = 0. /* Fs = 8 kHz. coeff[13] = 0.0000 0.4 kHz. coeff[8] = 0.h> float coeff[20]. /* 2.1625.0000. coeff[6] = 0. .0218.0821. coeff[10] = 0.0000. /* coeff array holds FIR filter coeffs */ int Norder. /* Band-Stop Filter Coefficients Cut From MATLAB B= Columns 1 through 7 -0.0218.x[20].0000 -0. /* FIR filter obtained using MATLAB FIR1 */ coeff[3] = 0.0218 */ 0.1625 Columns 15 through 17 0.0000. coeff[12] =-0. int main() { Mcbsp_dev dev.0821. coeff[11] = 0. /* Here are the 17 coefficients corresponding */ coeff[1] = 0.0000 0. int sampleRate. stop band between 1. MIN_ADC_INPUT_GAIN.0dB (min) gain. mcbspConfig.clock_mode =CLK_MODE_EXT.tx. mcbspConfig.word_length1 =WORD_LENGTH_32. coeff[16] =-0. mcbspConfig.sizeof(mcbspConfig)).tx. evm_init().&mcbspConfig).0.coeff[15] = 0. } /******************************************************/ /* Configure McBSP */ /******************************************************/ memset(&mcbspConfig.0000. allocates memory for the device handles */ dev=mcbsp_open(0). /******************************************************/ /* Open MCBSP */ /******************************************************/ mcbsp_drv_init().word_length1 =WORD_LENGTH_32. /* McBSP is activated */ /******************************************************/ /* Configure CODEC */ /******************************************************/ codec_init().rx. printf("Done initializing EVM\n"). mcbspConfig. mcbsp_config(dev.FALSE.rx.LINE_SEL). sel(L/R)LINE input */ /* Set codec for stereo mode of operation */ codec_adc_control(LEFT.update =TRUE. mcbspConfig.tx.0038. /* initialize McBSP driver.frame_length1 =0. /* dev is the handle to control the McBSP */ if(dev==NULL) { printf("Error Opening MCBSP 0\n"). turn OFF 20dB mic gain. /******************************************************/ /* Initialize EVM */ /******************************************************/ printf("Initializing EVM board\n").tx.loopback =FALSE.FALSE. return(ERROR).LINE_SEL).update =TRUE. mcbspConfig.clock_mode =CLK_MODE_EXT. /* configuration adjustments */ MCBSP_ENABLE(0.MCBSP_BOTH).MIN_ADC_INPUT_GAIN. mcbspConfig. .rx.rx. codec_adc_control(RIGHT. mcbspConfig.frame_length1 =0. /* A/D 0. mcbspConfig. TRUE). /* connect ISR to the CPU_INT15 */ INTR_ENABLE(15). Placing the base address of the vector table in ISTP */ intr_map(CPU_INT15. /* set to the closest allowed rate */ printf("The actual sampling rate is = %d\n". /* map CPU_INT15 to DRR interrupt */ intr_hook(McBSPRcvISR. /* D/A 0. wait for Interrupt */ /******************************************************/ Norder = 16.0. the interrupt will branch to ISP */ intr_init().. codec_dac_control(RIGHT. while(1) { } } void hookint() { /* an interrupt is assigned to DRR event of the serial port then.FALSE).ISN_RINT0). return. hookint(). } interrupt void McBSPRcvISR(void) { /* ISR for the DRR interrupt */ /* This routine convolves the present and the Norder previous input samples with the "Norder+1" FIR coefficients h(0) through h(Norder): y(n) = h(0)*x(n) + h(1)*x(n-1) + ./* MUTE(L/R)LINE input to "DSP Bypass Path" by setting mute switch to TRUE */ codec_line_in_control(LEFT.TRUE). do not mute DAC outputs */ codec_dac_control(LEFT.0. sampleRate=8000.MIN_AUX_LINE_GAIN.0dB attenuation. codec_interrupt_enable().FALSE).TRUE). /* initialize ISTP with the address of vec_table..MIN_AUX_LINE_GAIN.CPU_INT15). Actual_Sampling_Rate = codec_change_sample_rate(sampleRate.0. codec_line_in_control(RIGHT.Actual_Sampling_Rate).0. + h(Norder)*x(n-Norder) . /* codec generates interrupts when data are received in the DRR */ /******************************************************/ /* Main Loop. INTR_GLOBAL_ENABLE(). i<=Norder. /* Convert right channel to floating point */ for(i=Norder. /* Shift result back into Right Channel position while zeroing the Left Channel position */ MCBSP_WRITE(0. where x[0] holds present sample. float floatsamp. C) Bandpass: gives the lowest attenuation at the particular frequency band .sum. i >= 0. /* Shift right channel data down to bottom 16 bits */ if(intsamp & 0x8000) intsamp = intsamp | 0xffff0000.intsamp). x[1] holds sample from 1 sample period ago.4 KHz with the sampling frequency of 8 KHz.i. intsamp=MCBSP_READ(0).i--) /* Update past input sample array. /* read from CODEC's data receive register (DRR) */ intsamp = (intsamp >>16). x[N] holds sample from N sampling periods ago*/ /* This time-consuming loop could be */ /* eliminated if circular buffering were*/ /* to be employed. } /* Perform FIR filtering (convolution) */ intsamp = (int) sum.i++) { sum=sum+x[i]*coeff[i]. /* Send to CODEC (right channel only) */ } B) C code with given limits will give the output have the characteristics band limited between 800 Hz to 2.*/ int intsamp. } x[0] = floatsamp. */ { x[i]=x[i-1]. /* Convert result back to integer form */ intsamp = intsamp << 16. Which we have observed on oscilloscope. /* Sign extend right channel data */ floatsamp = (float) intsamp. for(i=0. sum = 0. 884 3700 -8.3047 3300 0.9637 2100 -10.1153 .4201 500 -7.5997 2900 0.3595 1300 -9.3886 1700 -14.3547 2500 -3.7297 900 -7.frequency gain 100 -8. 979 -70.979 -47.959 -44.457 -80 -73.frequency 100 500 900 1300 1700 2100 2500 2900 3300 Gain 0 -73.437 -20 .457 -70. *C B . x=x1+x2+x3+x4. x2=sin(2*pi*f2*t). f1=500. x4=sin(2*pi*f4*t). %B=32767. grid on. x3=sin(2*pi*f3*t). W=[0.W.139 Table L4. x1=sin(2*pi*f1*t). Ns=512. Ts=1/Fs.1 Explanation Band pass filter Matlab source code clc close all clear all Fs=8e3.'DC-0'). f4=3200.6].4 0. B=FIR1(N. t=[0:Ts:Ts*(Ns-1)].3700 -15. N=16. f3=2000. f2=1800. ylabel('Filt Sig Spectrum'). subplot(2.1.0000 -0. plot(f.1107 0. %pause.1).0294 -0. freqz(B.2710 -0. subplot(2.2). y=filter(B. ylabel('Filtered Sig').0000 . title('Input and Output Spectra'). %figure.0000 Figure 0.0051 0.y(1:Npts)). xfftmag=(abs(fft(x. xlabel('freq(Hz)').0000 0. f=[1:1:length(xfftmagh)]*Fs/Ns.0000 -0.2193 -0. yfftmag=(abs(fft(y. %pause.2). plot(t(1:Npts).0000 -0. Npts=200. figure.0051 -0.Ns))).x).Ns))).0000 Columns 15 through 17 -0. plot(t(1:Npts).A).1.2193 Columns 8 through 14 -0. subplot(2. xfftmagh=xfftmag(1:length(xfftmag)/2).0294 0.yfftmagh).1. plot(f.A.1107 -0. xlabel('freq (Hz)'). ylabel('Input spectrum').x(1:Npts)). Coefficient B= Columns 1 through 7 0.A=1.0000 0.xfftmagh). xlabel('time (s)'). yfftmagh=yfftmag(1:length(yfftmag)/2). . Ts=1/Fs.C source code Table L4. t=[0:Ts:Ts*(Ns-1)]. x3=sin(2*pi*f3*t). x2=sin(2*pi*f2*t). f2=1800. Ns=512. f4=3200. f3=2000.2 Explanation High pass filter Matlab source code clc close all clear all Fs=8e3. f1=500. x1=sin(2*pi*f1*t). freqz(B.0051 -0. xlabel('freq (Hz)'). Npts=200. plot(f.yfftmagh).*C B A=1.A). ylabel('Filtered Sig'). B=FIR1(N. %pause.0190 -0. figure. f=[1:1:length(xfftmagh)]*Fs/Ns.xfftmagh).x). yfftmag=(abs(fft(y. plot(f. %pause. N=16.x(1:Npts)).y(1:Npts)). xfftmag=(abs(fft(x. x=x1+x2+x3+x4. plot(t(1:Npts).1. subplot(2.925].W.1053 .Ns))).'high').1.2). subplot(2.1). yfftmagh=yfftmag(1:length(yfftmag)/2). ylabel('Filt Sig Spectrum'). %figure. y=filter(B. plot(t(1:Npts).0831 0.A.0361 Columns 8 through 14 0.Ns))). xlabel('time (s)'). xlabel('freq(Hz)'). W=[0. grid on.x4=sin(2*pi*f4*t). %B=32767.1.2). ylabel('Input spectrum'). xfftmagh=xfftmag(1:length(xfftmag)/2).0586 -0. coefficients B= Columns 1 through 7 0.0088 0. title('Input and Output Spectra'). subplot(2. 1208 0.0361 . f1=500. Ns=512.-0.0190 -0.0586 -0.1264 -0.0831 0.3 Explanation Low pass filter Matlab source code clc close all clear all Fs=8e3.0051 C source code Table L4.1208 Columns 15 through 17 0. 0.0088 Figure 0. Ts=1/Fs. t=[0:Ts:Ts*(Ns-1)].1053 -0. N=16. %B=32767.Ns))). Npts=200. f=[1:1:length(xfftmagh)]*Fs/Ns. yfftmag=(abs(fft(y. ylabel('Input spectrum'). x2=sin(2*pi*f2*t).1. x=x1+x2+x3+x4.925]. f4=3200.x). xfftmagh=xfftmag(1:length(xfftmag)/2). grid on. title('Input and Output Spectra').2).yfftmagh). W=[0. x1=sin(2*pi*f1*t). %figure.1.x(1:Npts)). subplot(2. subplot(2.f2=1800. yfftmagh=yfftmag(1:length(yfftmag)/2). freqz(B. plot(f. xlabel('time (s)').*C B A=1.1. subplot(2. ylabel('Filt Sig Spectrum').'low').2). xfftmag=(abs(fft(x.xfftmagh).A. f3=2000. figure. B=FIR1(N.W. x4=sin(2*pi*f4*t). x3=sin(2*pi*f3*t). %pause.A).y(1:Npts)). Coefficients B= Columns 1 through 7 .Ns))).1). y=filter(B. plot(f. plot(t(1:Npts). xlabel('freq(Hz)'). xlabel('freq (Hz)'). ylabel('Filtered Sig'). plot(t(1:Npts). %pause. 0495 -0.0349 0.0052 -0.-0.0719 0.0030 C source code Table L4.0113 0.0052 -0.0215 -0.0113 Figure 0. 0.0215 .0349 0.0719 -0.9276 0.0030 0.4 Explanation Fixed point FIR filter clc close all clear all Fs=8e3. Ns=512.0495 -0. Ts=1/Fs.0627 Columns 15 through 17 -0.0627 Columns 8 through 14 0. 1.925]. x=x1+x2+x3+x4. B=32767. x1=sin(2*pi*f1*t). xfftmag=(abs(fft(x. ylabel('Filtered Sig'). y=filter(B.x). f1=500. xlabel('time (s)').1. plot(f.1. x3=sin(2*pi*f3*t). %figure.'high'). x2=sin(2*pi*f2*t). subplot(2. f4=3200. plot(f. figure. %pause.x(1:Npts)).A). plot(t(1:Npts). %for Fixed point FIR filter multiply each coefficient by B % 0x7fff this will convert these orignal floating point coefficint A=1. yfftmag=(abs(fft(y. W=[0.2). ylabel('Filt Sig Spectrum').Ns))). xlabel('freq (Hz)'). plot(t(1:Npts). xlabel('freq(Hz)').*C. grid on. ylabel('Input spectrum').Ns))). f=[1:1:length(xfftmagh)]*Fs/Ns. yfftmagh=yfftmag(1:length(yfftmag)/2). C=FIR1(N. Npts=200.xfftmagh).t=[0:Ts:Ts*(Ns-1)]. subplot(2. f3=2000.y(1:Npts)). f2=1800. title('Input and Output Spectra').A. Coefficients . x4=sin(2*pi*f4*t).W.2).1).yfftmagh). %pause. xfftmagh=xfftmag(1:length(xfftmag)/2). subplot(2. % into sign 16-bit integer freqz(B. N=16. 1819 1.4519 Columns 8 through 14 -3.9591 4.2879 figure C source code Table L4.4519 -2.6212 -0.1410 -3.7243 1.7243 3.5 Explanation 0.B= 1.9591 Columns 15 through 17 0.1671 3.0e+003 * Columns 1 through 7 0.1671 -0.9195 -2.9195 -1.6212 -1.1819 .2879 0.