real time audio signal processing tms320c54x assembly code and report
TRANSCRIPT
EE-256 Semester Project Report
This report covers all the aspects of the project. It explains how things were executed and accomplished. The layout of the report is as follows:
1. MATLAB Filter Design and Analysis
2. Filter Coefficients
3. MATLAB Code
4. C5416 DSK Program
5. Results Using Simulated audio signal
6. Conclusion and Comments
Filter Design and Analysis
FIR Low Pass Filter
1
0 500 1000 1500 2000 2500 3000 3500 4000-6000
-4000
-2000
0
Frequency (Hz)
Pha
se (
degr
ees)
0 500 1000 1500 2000 2500 3000 3500 4000-100
-50
0
50
Frequency (Hz)
Mag
nitu
de (
dB)
Lowpass Filter with Cutoff Frequency 2400Hz
0 100 200 300 400 500 60047.5
48
48.5
49
49.5Group Delay of FIR Filter
samples
2
-16 -14 -12 -10 -8 -6 -4 -2 0
-6
-4
-2
0
2
4
6
97
Real Part
Imag
inar
y P
art
Pole-Zero Plot of FIR Filter
IIR Notch Filter
3
0 500 1000 1500 2000 2500 3000 3500 4000-100
-50
0
50
100
Frequency (Hz)
Pha
se (
degr
ees)
0 500 1000 1500 2000 2500 3000 3500 4000-60
-40
-20
0
Frequency (Hz)
Mag
nitu
de (
dB)
Notch Filter with Notch Frequency 1200Hz
0 100 200 300 400 500 6000
1
2
3
4
5
6Group Delay of IIR Filter
samples
4
-1 -0.5 0 0.5 1
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Real Part
Imag
inar
y P
art
Pole-Zero Plot of IIR Filter
Filter CoefficientsThe filter coefficients of our desired filters designed in MATLAB were converted to Q.15 using MATLAB. The function to convert them into Q15 Hex numbers, which were written down in the ‘fir_coeff.dat’ file. These coefficients are listed below.
FIR Coefficients• 98 coefficients of FIR filter in Q.15 form
.word -13.word -203.word 43.word 44.word -76.word 8.word 82
.word -75
.word -36
.word 116
.word -50
.word -94
.word 131
.word 5
.word -154
.word 116
.word 85
.word -200
.word 60
.word 181
.word -210
.word -40
.word 271
.word -167
.word -178
.word 331
.word -57
.word -333
.word 328
.word 124
.word -476
.word 234
.word 367
.word -564
.word 21
.word 650
.word -548
.word -338
.word 942
5
.word -356
.word -889
.word 1207
.word 148
.word -1795
.word 1407
.word 1508
.word -4109
.word 1515
.word 17251
.word 17251
.word 1515
.word -4109
.word 1508
.word 1407
.word -1795
.word 148
.word 1207
.word -889
.word -356
.word 942
.word -338
.word -548
.word 650
.word 21
.word -564
.word 367
.word 234
.word -476
.word 124
.word 328
.word -333
.word -57
.word 331
.word -178
.word -167
.word 271
.word -40
.word -210
.word 181
.word 60
.word -200
.word 85
.word 116
.word -154
.word 5
.word 131
.word -94
.word -50
.word 116
.word -36
.word -75
.word 82
.word 8
.word -76
.word 44
.word 43
.word -203
.word -13
6
IIR Filter Coefficient The IIR coefficients were converted to Q15 hex numbers and then converted to decimal format that was given to the main file. The decimal numbers were represented in signed 16-bit numbers.
The coefficients are shown below in all the formats.
; IIR Filter coefficients (sequential locations)b0 .word 9598 ;257Eh ;b0 = 0.2929b1 .word -11282 ;D3EEh ;b1 = -0.3343b2 .word 9598 ;257Eh ;b2 = 0.2929a1 .word -11282 ;D3EEh ;a1 = -0.3343a2 .word -13572 ;CAFCh ;a2 = -0.4412
MATLAB Code
This is the code that I used to design the 2400Hz cutoff frequency low pass FIR Filter and 1200Hz Notch IIR filter. The magnitude and phase response, group delay and Pole-zero plot were seen for both the designed filters.
An audio file named ‘male.wav’ is passed to the system and two single frequency tones are superimposed on it. These frequencies are 3500Hz in order to test the Low Pass filter and 1200Hz in order to test the IIR filter. The frequency domain analysis was done for the system.
% ****************************************************% Design & Analysis of Filters% ****************************************************
% *******************% FIR Filter% ******************* Fs = 8000; % Sampling frequencyf = [0.3*Fs 0.32*Fs]; % Cutoff frequenciesa = [1 0]; % Desired amplitudesdev = [0.01 0.01]; % deviations in the pass & stop bands %Parks-McClellan optimal FIR filter order estimation[n,fo,ao] = firpmord(f,a,dev,Fs)b = firpm(n,fo,ao); % Filter Coeeficients % Ploting the Low Pass FIR Filter figure;freqz(b,1,512,Fs);title('Lowpass Filter with Cutoff Frequency 2400Hz'); % Group Delay
figure; plot(grpdelay(b,a));title('Group Delay of FIR Filter');xlabel('samples'); % Pole-Zero Plotfigure;zplane(b,1);title('Pole-Zero Plot of FIR Filter');
% *******************% IIR Filter% ******************* wo=0.15*Fs/(0.5*Fs); % Notch Frequencybw=wo/.4;[bi,ai] = iirnotch(wo,bw); % Ploting the Low Pass FIR Filter figure;freqz(bi,ai,512,Fs);title('Notch Filter with Notch Frequency 1200Hz'); % Group Delayfigure;plot(grpdelay(bi,ai));title('Group Delay of IIR Filter');xlabel('samples'); % Pole-Zero Plotfigure;zplane(bi,ai);title('Pole-Zero Plot of IIR Filter');
% *******************************************************% Creating Input Signals% *******************************************************w = [1:(3*Fs)];[input,fs]=wavread('male.wav'); % sound file passedn=[1:length(input)]; % Add sinosoids with frequency f1=1200Hz and f2=2800Hz input=input'+0.04*sin(2*pi*.35*n)+0.05*sin(2*pi*n*.1507);% input=awgn(input,25);fft_input = abs((fft(input))); % Plot Inputfigure;plot((w(1:1.5*Fs)*(Fs/2))/(1.5*Fs),fft_input(1:1.5*Fs));axis([1 4000 1 100]);title('Input Signal');xlabel('Frequency (Hz)');ylabel('Magnitude') % Play Inputinput = input(1:(3*Fs)); sound(input,Fs);
% ***********************************************************% Filtering of the input % *********************************************************** input = input(1:(3*Fs)); % just pick 3 seconds of the signal
% *******************% LPF Output % ******************* % Generate Outputfir_output = filter(b,1,input);fft_fir_output = abs((fft(fir_output))); % Plot Outputfigure;plot((w(1:1.5*Fs)*(Fs/2))/(1.5*Fs),fft_fir_output(1:1.5*Fs));axis([1 4000 1 100]);title('Output of FIR Filter (Low-Pass)');xlabel('Frequency (Hz)');ylabel('Magnitude') % Play Outputpause(3)sound(fir_output,Fs);
% ****************************% Notch Filter Output % **************************** % Generate Outputiir_output = filter(bi,ai,fir_output);fft_iir_output = abs((fft(iir_output))); % Plot Outputfigure;plot((w(1:1.5*Fs)*(Fs/2))/(1.5*Fs),fft_iir_output(1:1.5*Fs));axis([1 4000 1 100]);title('Output of IIR Filter (Notch)');xlabel('Frequency (Hz)');ylabel('Magnitude') % Play Outputpause(3)sound(iir_output,Fs);
C5416.m FileThis code was used to record the sound coming out of the C5416 DSK board. The frequency response of the unprocessed input signal, the FIR output signal and the final output after the IIR filter were plotted using this code.
% ********************************************% C5416 Sound Output Analysis% ******************************************** Fs=8000; input_board = wavrecord(3*Fs,Fs); fir_output_board = wavrecord(3*Fs,Fs);
iir_output_board = wavrecord(3*Fs,Fs);
% ********************************************************% Frequency Response Using a Simulated Signal% ******************************************************** % Generate FFt of input_board fft_input_board = abs((fft(input_board))); % Plot input_boardfigure;plot((w(1:1.5*Fs)*(Fs/2))/(1.5*Fs),fft_input_board(1:1.5*Fs),'r');axis([1 4000 1 100]);title('input board Signal');xlabel('Frequency (Hz)');ylabel('Magnitude') % Play Inputsound(input_board,Fs)
% *******************% LPF Output % *******************
% Generate FFT of Outputfft_fir_output_board = abs((fft(fir_output_board))); % Plot Outputfigure;plot((w(1:1.5*Fs)*(Fs/2))/(1.5*Fs),fft_fir_output_board(1:1.5*Fs),'r');axis([1 4000 1 100]);title('Output of FIR Filter (Low-Pass)');xlabel('Frequency (Hz)');ylabel('Magnitude') % Play Outputpause(3)sound(fir_output_board,Fs);
% ****************************% Notch Filter Output % **************************** % Generate FFT of Outputfft_iir_output_board = abs((fft(iir_output_board))); % Plot Outputfigure;plot((w(1:1.5*Fs)*(Fs/2))/(1.5*Fs),fft_iir_output_board(1:1.5*Fs),'r');axis([1 4000 1 100]);title('Output of IIR Filter (Notch)');xlabel('Frequency (Hz)');ylabel('Magnitude')
% Play Outputpause(3)sound(iir_output_board,Fs);
C5416 DSK Program
Following is the C5416 program used to implement the required system on the DSk board. It takes the audio input through ‘Line In’ port on the board. It stores the signal in the form of sample because of Codec that is set to sample at 8KHz, using the following value on the Codec Clock Register in the initPCM3002.asm file: ; Codec clock Register (0000 1010) ; Bit7-4 = 0000: Reserved ; Bit3 = 1: Clock divisor selected ; 0: No divisor, 48 KHz sampling rate ; Bit2 = 0: Clock enabled ; Bit1-0 = 00: Clock divisor for 24 KHz sampling rate ; 01: Clock divisor for 12 KHz sampling rate ; 10: Clock divisor for 8 KHz sampling rate ; 11: Clock divisor for 6 KHz sampling rate VAL_CLK_REG .set 12h
The samples received and transmitted are 16-bits each. This is achieved by changing the values of the transmit and receive control registers (RCR1, RCR2, XCR1, XCR2) in the file initMcBSP.asm:
; Receive Control Register 1 (0000 0000 0100 0000) ; Bit15 = 0: Reserved ; Bit14-8 = 0000000: 1 word per frame ; Bit7-5 = 010: 16 bit receive word ; Bit4-0 = 00000: Reserved VAL_RCR1 .set 0040h ; Receive Control Register 2 (0000 0000 0100 0001) ; Bit15 = 0: Single phase frame ; Bit14-8 = 00h: 1 word per frame ; Bit7-5 = 010: 16 bit receive word ; Bit4-3 = 00: No companding ; Bit2 = 0: Receive frame sync pulses not ignored ; Bit1-0 = 01: 1-bit data delay VAL_RCR2 .set 0041h
; Transmit Control Register 1 (0000 0000 0100 0000) ; Bit15 = 0: Reserved
; Bit14-8 = 00h: 1 word per frame ; Bit7-5 = 010: 16 bit transmit word ; Bit4-0 = 0h: Reserved VAL_XCR1 .set 0040h ; Transmit Control Register 2 (0000 0000 0100 0000) ; Bit15 = 0: Single phase frame ; Bit14-8 = 00h: 1 word per frame ; Bit7-5 = 010: 16 bit transmit word ; Bit4-3 = 00: No companding ; Bit2 = 0: Transmit frame sync pulses not ignored ; Bit1-0 = 00: 0-bit data delay VAL_XCR2 .set 0040h
The main file of the project is as shown below. It waits for the receive interrupt and when a sample is received it calls an FIR filter whose coefficients are already loaded in a circular buffer. After the FIR filter the sample is in Register A. It then calls the IIR filter and the sample is processed upon.
************************************************************************** This program reads an input signal from the ADC , Passes it through a Lowpass FIR *filter and then that output through a IIR Notch Filter. It then writes the sample to* the DAC on the DSK5416 board. *************************************************************************
.include "regs.asm" .ref initC5416 .ref initMcBSP2 .ref initPCM3002 .def _c_int00 .def brint2_isr .def sample_receive .def sample_transmit VAL_SP .set 0x0500 ; initial stack address OutSamples .bss y,1,1 ; Allocate space for y(n)s
.bss CoefBuf, 99, 1 ; Memory for coeff circular buffer .bss SampleBuf, 99, 1 ; Memory for sample circular buffer .sect "FIRcoeff" ; Filter coeff (seq locations)FIRcoeff .include "coff_fir.dat"
Nm1 .set 98 ; Number of filter taps N - 1
; Intermediate variables (sequential locations)wn .word 0 ;initial w(n)wnm1 .word 0 ;initial w(n-1) =0wnm2 .word 0 ;initial w(n-2)=0
; IIR Filter coefficients (sequential locations)b0 .word 9598 ;b0 = 0.2929
b1 .word -11282 ;b1 = -0.3343b2 .word 9598 ;b2 = 0.2929a1 .word -11282 ;a1 = -0.3343a2 .word -13572 ;a2 = -0.4412
.text
* The entry-point for the program
_c_int00: stm #VAL_SP, SP ; Define the stack ssbx INTM ; Disable all interrupts call initC5416 ; Init the DSP processor call initMcBSP2 ; Init the McBSP2 port call initPCM3002 ; Init the DSK CODEC
stm #SPCR1, MCBSP2_SPSA ; Enable McBSP2 receiver orm #0001h, MCBSP2_SPSD
stm #SPCR2, MCBSP2_SPSA ; Enable McBSP2 transmitter orm #0001h, MCBSP2_SPSD
stm #0FFFFh, IFR ; Clear pending interrupts orm #040h, IMR ; Unmask McBSP2 RX int STM #OutSamples, AR6 ; Clear output sample buffer
ST #0, *AR6 ; AR6 points to OutSample buffer CALL fir_init ; Init for filter calculations ssbx SXM ; Select sign extension mode rsbx INTM ; Enable all interrupts
wait_main: idle 1 ; Wait for an RX interrupt b wait_main nop nop nop
* Interrupt service routine for McBSP2 Receiver
brint2_isr: call sample_receive ; Receive the sample
call fir_filter ; Call FIR filter routine, STH A,1,*AR6 ; Store filtered sample
(integer) call iir_filter ; Call IIR filter routine STH A,1,*AR6 ; Store filtered sample (integer)
call sample_transmit ; Transmit the sample rete
* This procedure receives a 16-bit value from the ADC* Return with A (LSBs) = 16 bit received sample
sample_receive: ld #0, A ldm MCBSP2_DRR1, A ; Retrieve 16 bits ret
* This procedure sends a 16-bit value in A (LSBs) to the DAC.
sample_transmit: stlm A, MCBSP2_DXR1 ; Transmit 16 bits ret * ----------------------------------------------------------------------------------* FIR Filter Initialization Routine* This routine sets AR2 as the pointer for the sample circular buffer, and* AR3 as the pointer for coefficient circular buffer.* BK = Number of filter taps - 1.* AR0 = 1 = circular buffer pointer increment* ----------------------------------------------------------------------------------
fir_init: ST #CoefBuf,AR3 ; AR3 is the CB Coeff Pointer ST #SampleBuf,AR2 ; AR2 is the CB sample pointer STM #Nm1,BK ; BK = number of filter taps RPT #Nm1 MVPD #FIRcoeff, *AR3+% ; Place coeff in circular buffer RPT #Nm1 - 1 ; Clear circular sample buffer ST #0h,*AR2+% STM #1,AR0 ; AR0 = 1 = CB pointer increment ret nop nop nop * --------------------------------------------------------------------------------* FIR Filter Routine* Enter with A = the current sample x(n) - an integer,* AR2 pointing to the location for the current sample x(n),* and AR3 pointing to the q15 coefficient h(N-1). * Exit with A = y(n) as q15 number.* --------------------------------------------------------------------------------
fir_filter: STL A, *AR2+0% ; Place x(n)in the sample buffer
RPTZ A, #Nm1 - 1 ; A = 0 MAC *AR3+0%,*AR2+0%,A ; A = filtered sum (q15) ret nop nop nop ;---------------------------------------------------------------------------------; IIR Filter Subroutine; Enter with A = x(n) as q15 number; Exit with A = y(n) as q15 number
; Uses AR2 and AR3;---------------------------------------------------------------------------------
iir_filter: pshm AR2
pshm AR3
;w(n)=x(n)+ a1.w(n-1)+ a2.w(n-2)
STM #a2,AR2 ; AR2 points to a2 STM #wnm2, AR3 ; AR3 points to w(n-2) MAC *AR2-,*AR3-,A ; A = x(n)+ a2.w(n-2)
; AR2 points to a1 & AR3 to w(n-1) MAC *AR2-,*AR3-,A ; A = x(n)+ a1.w(n-1)+ a2.w(n-2) ; AR2 points to b2 & AR3 to w(n) STH A,1,*AR3 ; Save w(n)
;y(n)=b0.w(n)+ b1.w(n-1)+ b2.w(n-2)
LD #0,A ; A = 0 STM #wnm2,AR3 ; AR3 points to w(n-2)
MAC *AR2-,*AR3-,A ; A = b2.w(n-2) ; AR2 points to b1 & AR3 to w(n-1) DELAY *AR3 ; w(n-1) -> w(n-2)
MAC *AR2-,*AR3-,A ; A = b1.w(n-1)+ b2.w(n-2) ; AR2 points to b0 & AR3 to w(n) DELAY *AR3 ; w(n) -> w(n-1)
MAC *AR2,*AR3,A ; A = b0.w(n)+ b1.w(n-1)+ b2.w(n-2) popm AR3 popm AR2
RET ; Return .end
Frequency Response of a Simulated Signal
This section shows the results of the project. The input signal, low pass filter output and notch filter output are all shown in the frequency domain.
The plots in red color are the ones obtained by processing upon the signal obtained from the DSK board.
MATLAB Results
0 500 1000 1500 2000 2500 3000 3500 40000
100
200
300
400
500
600
700
800
900
1000Input Signal
Frequency (Hz)
Mag
nitu
de
0 500 1000 1500 2000 2500 3000 3500 40000
100
200
300
400
500
600Output of FIR Filter (Low-Pass)
Frequency (Hz)
Mag
nitu
de
0 500 1000 1500 2000 2500 3000 3500 40000
5
10
15
20
25
30
35
40Output of IIR Filter (Notch)
Frequency (Hz)
Mag
nitu
de
C5416 DSK Board Output
0 500 1000 1500 2000 2500 3000 3500 40000
10
20
30
40
50
60input board Signal
Frequency (Hz)
Mag
nitu
de
0 500 1000 1500 2000 2500 3000 3500 40000
10
20
30
40
50
60Output of FIR Filter (Low-Pass)
Frequency (Hz)
Mag
nitu
de
0 500 1000 1500 2000 2500 3000 3500 40000
10
20
30
40
50
60
70
80Output of IIR Filter (Notch)
Frequency (Hz)
Mag
nitu
de
Comments and Conclusions
The project was successfully implemented and desired results were achieved. The FIR filter was a high order filter as expected, but since DSP processor are fast in implementing filter operations, this was not an issue. More important thing was to get the desired frequency response.
The output in MATLAB using AWGN in the signal was also seen. The results were similar so the plots are not included.
The outputs of MATLAB simulation and actual implementation were quite similar. This is evident from the correlation of the two plots and from the audio heard during the demonstration. The difference in amplitudes of the magnitude responses can be because different strength tones were used during implementation as compared to simulation. The simulated signals were created in MATLAB while ‘NCH Tone Generator’ was used to generate tones for the input to board. Moreover, the output signal of the board was acquired for analysis by bringing the speaker to the laptop mic. Hence, there could be loss of amplitude.
Overall, the results were satisfactory and the objective of the project was achieved.