DSP Lab Programs
DSP Lab Programs
OBJECTIVES:
The student should be made to:
To implement Linear and Circular Convolution
To implement FIR and IIR filters
To study the architecture of DSP processor
To demonstrate Finite word length effect
OUTCOMES:
Students will be able to
Carry out simulation of DSP systems
Demonstrate their abilities towards DSP processor based implementation of DSP systems
Analyze Finite word length effect on DSP systems
Demonstrate the applications of FFT to DSP
Implement adaptive filters for various applications of DSP
ANNA UNIVERSITY, CHENNAI-600 025
B.E/ B.Tech DEGREE EXAMINATIONS, Oct/Nov 2013
Regulations-2013
Fifth Semester
B.E. ELECTRONICS AND COMMUNICATION ENGINEERING
EC 6511 DIGITAL SIGNAL PROCESSING LAB
Ex.NO:1
GENERATION OF BASIC SEQUENCES USING MATLAB
Aim:
To write MATLAB program to generate basic signals used in laboratories
Unit Impulse:
clc;
clear all;
close all;
disp('Unit Impulse Signal Generation');
N=input('Enter no of samples: ');
n=-N:1:N;
x=[zeros(1,N),1,zeros(1,N)];
stem(n,x);
xlabel('Sample');
ylabel('Amplitude');
title('Unit Impulse Signal');
UNIT RAMP
clc;
clear all;
close all;
disp('Unit Ramp Signal Generation');
N=input('Enter no of samples: ');
n=-N:1:N;
x=n;
stem(n,x);
xlabel('Sample');
ylabel('Amplitude');
title('Unit Ramp Signal');
UNIT STEP:
clc;
clear all;
close all;
disp('Unit Step Signal Generation');
N=input('Enter no of samples: ');
n=-N:1:N;
x=[zeros(1,N),ones(1,N+1)];
stem(n,x);
xlabel('Sample');
ylabel('Amplitude');
title('Unit Step Signal');
SINE WAVE:
clc;
clear all;
close all;
disp('Sinusoidal Signal generation');
N=input('Enter no of samples: ');
n=0:0.1:N;
x=sin(n);
figure, stem(n,x);
xlabel('Samples');
ylabel('Amplitude');
title('Sinusoidal Signal');
EXPONENTIALLY GROWING SIGNAL:
clc;
clear all;
close all;
disp('Exponential growing signal');
N=input('Enter no of samples: ');
a=1;
t=0:0.1:N;
x=a*exp(t);
figure,stem(t,x);
xlabel('Time');
ylabel('Amplitude');
title('Exponentially Decaying Signal');
SAWTOOTH WAVEFORM:
clc;
clear all;
n=input('Enter the no samples: ');
x=0:0.1:n;
s=sawtooth(x);
stem(x,s);
xlabel('Time');
ylabel('Amplitude');
title('Sawtooth signal');
RESULT:
Ex.NO:2
LINEAR AND CIRCULAR CONVOLUTION USING MATLAB
Aim:
To write MATLAB program to find linear and circular convolution of a
given input sequences
LINEAR CONVOLUTION:
% Program for linear convolution of two sequences
clc;
clear all;
close all;
x=input('enter the input sequence');
h=input('enter the impulse response');
y=conv(x,h);
figure;
subplot(3,1,1);
stem(x);
ylabel('Amplitude --.');
xlabel('n --');
title('Input sequence x(n)');
subplot(3,1,2);
stem(h);
ylabel('Amplitude --.');
xlabel('n --.');
title('Impulse response h(n)');
subplot(3,1,3);
stem(y);
ylabel('Amplitude --.');
xlabel('n --.');
title('output sequence y(n)');
CIRCULAR CONVOLUTION:
% Program for Computing Circular Convolution
clc;
clear;
a = input('Enter the sequence x(n) = ');
b = input('Enter the sequence h(n) = ');
n1=length(a);
n2=length(b);
N=max(n1,n2);
x = [a zeros(1,(N-n1))];
for i = 1:N
k = i;
for j = 1:n2
H(i,j)=x(k)* b(j);
k = k-1;
if (k == 0)
k = N;
end
end
end
y=zeros(1,N);
M=H';
for j = 1:N
for i = 1:n2
y(j)=M(i,j)+y(j);
end
end
disp('The output sequence is y(n)= ');
disp(y);
figure;
subplot(3,1,1);
stem(a);
ylabel('Amplitude --.');
xlabel('n --');
title('Input sequence x(n)');
subplot(3,1,2);
stem(b);
ylabel('Amplitude --.');
xlabel('n --.');
title('Impulse response h(n)');
stem(y);
title('Circular Convolution');
xlabel('n');
ylabel('y(n)');
RESULT:
Ex.No:3
SAMPLING AND ITS EFFECT OF ALIASING USING MATLAB
Aim:
Write a Matlab program to sample the given sinusoidal signal and plot the sampled
output with and without aliasing
Algorithm:
1.Generate the sinusoidal signal to be sampled
2.Generate the pulse signal having the frequency more than the signal to be sampled
3.Multiply both the signal and plot the waveforms
4.Generate the pulse signal having the frequency which will not satisfy the nyquist theorem
5.Multiply both the signal and plot the waveforms with aliasing
Program:
%program for sampling of a signal without aliasing
clc;
clear all;
close all;
t=0:0.01:2;
a=2;
fn=1.0;
sig=a*sin(2*pi*fn*t);
fs=10;
sampler=square(2*pi*fs*t);
for i=1:length(sampler)
sampler(sampler==-1)=0;
end;
sampledsig=sampler.*sig;
figure;
subplot(3,1,1);
plot(t,sig);
xlabel('time(sec)');
ylabel('Amplitude');
title('Sinusoidal signal with frequency 1.0Hz,Amplitude =2volts');
subplot(3,1,2);
plot(t,sampler,'g-');
xlabel('time(sec)');
ylabel('Amplitude');
title('Pulse signal with fs=10Hz');
subplot(3,1,3);
plot(t,sampledsig,'m-');
xlabel('time(sec)');
ylabel('Amplitude');
title('sampled signal');
grid;
%program for sampled signal with aliasing
fs=2;
sampler=square(2*pi*fs*t);
for i=1:length(sampler)
sampler(sampler==-1)=0;
end;
sampledsig=sampler.*sig;
figure;
subplot(3,1,1);
plot(t,sig);
xlabel('time(sec)');
ylabel('Amplitude');
title('Sinusoidal signal with frequency 1.0Hz,Amplitude =2volts');
subplot(3,1,2);
plot(t,sampler,'g-');
xlabel('time(sec)');
ylabel('Amplitude');
title('Pulse signal with fs=10Hz');
subplot(3,1,3);
plot(t,sampledsig,'m-');
xlabel('time(sec)');
ylabel('Amplitude');
title('sampled signal(over sampling)');
grid;
RESULT:
RESULT:
RESULT:
Ex.NO:6
FIR FILTER
Aim:
To determine the impulse response of FIR lowpass, highpass ,bandpass and bandstop
filters by fourier series method using MATLAB
Program:
%FIR lowpass Filter design
clc;
clear all;
close all;
wc=.5*pi;
N=11;
hd=zeros(1,N);
hd(1)=wc/pi;
k=1:1:((N-1)/2)+1;
hd(k+1)=(sin(wc*k))./(pi*k);
hn(k)=hd(k);
a=(N-1)/2;
w=0:pi/16:pi;
hw1=hn(1)*exp(-j*w*a);
hw2=0;
for m=1:1:a
hw3=hn(m+1)*((exp(j*w*(m-a)))+(exp(-j*w*(m+a))));
hw2=hw2+hw3;
end
hw=hw2+hw1;
HMag=abs(hw);
plot(w/pi,HMag,'k');
grid;
title('Magnitude Response');
xlabel('Normalized frequency');
ylabel('Magnitude');
%FIR highpass Filter design
clc;
clear all;
close all;
wc=.6*pi;
N=7;
hd=zeros(1,N);
hd(1)=1-(wc/pi);
k=1:1:((N-1)/2)+1;
hd(k+1)=(-sin(wc*k))./(pi*k);
hn(k)=hd(k);
a=(N-1)/2;
w=0:pi/16:pi;
hw1=hn(1)*exp(-j*w*a);
hw2=0;
for m=1:1:a
hw3=hn(m+1)*((exp(j*w*(m-a)))+(exp(-j*w*(m+a))));
hw2=hw2+hw3;
end
hw=hw2+hw1;
HMag=abs(hw);
plot(w/pi,HMag,'k');
grid;
title('Magnitude Response');
xlabel('Normalized frequency');
ylabel('Magnitude');
RESULT:
Ex.No:7
DESIGN OF FIR FILTERS USING WINDOWS
Aim:
To determine the impulse response of FIR low pass,high pass ,band pass and band
stop filters using Hamming and Hanning windows
% Program to plot the frequency response of FIR lowpass filter using Hanning window
clear all
clc
wc=.2*pi;
N=7;
hd=zeros(1,N);
a=(N-1)/2;
hna=wc/pi;
k=1:1:((N-1)/2);
n=k-1-((N-1)/2);
w_han(k)=0.5-0.5*cos(2*pi*(k-1)/(N-1));
hd(k)=(sin(wc*n))./(pi*n);
for s=1:length(k)
hn(s)=hd(s)*w_han(s);
end
hn=[hn hna];
a=(N-1)/2;
w=0:pi/16:pi;
Hw1=hna*exp(-j*w*a);
Hw2=0;
for m=1:1:a
Hw3=hn(m)*((exp(j*w*(1-m)))+(exp(-j*w*(1-m+2*a))));
Hw2=Hw2+Hw3;
end
Hw=Hw2+Hw1;
H_mag=abs(Hw);
plot(w/pi,H_mag,'k');
grid;
title('Magnitude Response');
xlabel('Normalized frequency');
ylabel('Magnitude');
for s=1:length(k)
hn(s)=hd(s)*w_han(s);
end
hn=[hn hna];
a=(N-1)/2;
w=0:pi/16:pi;
Hw1=hna*exp(-j*w*a);
Hw2=0;
for m=1:1:a
Hw3=hn(m)*((exp(j*w*(1-m)))+(exp(-j*w*(1-m+2*a))));
Hw2=Hw2+Hw3;
end
Hw=Hw2+Hw1;
H_mag=abs(Hw);
plot(w/pi,H_mag,'k');
grid;
title('Magnitude Response');
xlabel('Normalized frequency');
ylabel('Magnitude');
% Program to plot the frequency response of FIR low pass filter using Hamming
window
clear all
clc
wc=.6*pi;
N=7;
hd=zeros(1,N);
a=(N-1)/2;
hna=wc/pi;
k=1:1:((N-1)/2);
n=k-1-((N-1)/2);
w_ham(k)=0.55-0.46*cos(2*pi*(k-1)/(N-1));
hd(k)=(sin(wc*n))./(pi*n);
for s=1:length(k)
hn(s)=hd(s)*w_ham(s);
end
hn=[hn hna];
a=(N-1)/2;
w=0:pi/16:pi;
Hw1=hna*exp(-j*w*a);
Hw2=0;
for m=1:1:a
Hw3=hn(m)*((exp(j*w*(1-m)))+(exp(-j*w*(1-m+2*a))));
Hw2=Hw2+Hw3;
end
Hw=Hw2+Hw1;
H_mag=abs(Hw);
plot(w/pi,H_mag,'k');
grid;
title('Magnitude Response');
xlabel('Normalized frequency');
ylabel('Magnitude');
%Program to plot the frequency response of FIR high pass filter using
%Hamming window
clear all
clc
wc=.8*pi;
N=7;
hd=zeros(1,N);
a=(N-1)/2;
hna=1-(wc/pi);
k=1:1:((N-1)/2);
n=k-1-((N-1)/2);
w_ham(k)=0.55-0.46*cos(2*pi*(k-1)/(N-1));
hd(k)=(-sin(wc*n))./(pi*n);
for s=1:length(k)
hn(s)=hd(s)*w_ham(s);
end
hn=[hn hna];
a=(N-1)/2;
w=0:pi/16:pi;
Hw1=hna*exp(-j*w*a);
Hw2=0;
for m=1:1:a
Hw3=hn(m)*((exp(j*w*(1-m)))+(exp(-j*w*(1-m+2*a))));
Hw2=Hw2+Hw3;
end
Hw=Hw2+Hw1;
H_mag=abs(Hw);
plot(w/pi,H_mag,'k');
grid;
title('Magnitude Response');
xlabel('Normalized frequency');
ylabel('Magnitude');
% Program to plot the frequency response of FIR band pass filter using Hamming
window
clear all
clc
wc1=.4*pi;
wc2=0.65*pi;
N=7;
hd=zeros(1,N);
a=(N-1)/2;
hna=(wc2-wc1)/pi;
k=1:1:((N-1)/2);
n=k-1-((N-1)/2);
w_ham(k)=0.55-0.46*cos(2*pi*(k-1)/(N-1));
hd(k)=(sin(wc2*n)-sin(wc1*n))./(pi*n);
for s=1:length(k)
hn(s)=hd(s)*w_ham(s);
end
hn=[hn hna];
a=(N-1)/2;
w=0:pi/16:pi;
Hw1=hna*exp(-j*w*a);
Hw2=0;
for m=1:1:a
Hw3=hn(m)*((exp(j*w*(1-m)))+(exp(-j*w*(1-m+2*a))));
Hw2=Hw2+Hw3;
end
Hw=Hw2+Hw1;
H_mag=abs(Hw);
plot(w/pi,H_mag,'k');
grid;
title('Magnitude Response');
xlabel('Normalized frequency');
ylabel('Magnitude');
% Program to plot the frequency response of FIR band stop filter using
%Hamming window
clear all
clc
wc1=.4*pi;
wc2=0.65*pi;
N=7;
hd=zeros(1,N);
a=(N-1)/2;
hna=1-((wc2-wc1)/pi);
k=1:1:((N-1)/2);
n=k-1-((N-1)/2);
w_ham(k)=0.54-0.46*cos(2*pi*(k-1)/(N-1));
hd(k)=(sin(wc1*n)-sin(wc2*n))./(pi*n);
for s=1:length(k)
hn(s)=hd(s)*w_ham(s);
end
hn=[hn hna];
a=(N-1)/2;
w=0:pi/16:pi;
Hw1=hna*exp(-j*w*a);
Hw2=0;
for m=1:1:a
Hw3=hn(m)*((exp(j*w*(1-m)))+(exp(-j*w*(1-m+2*a))));
Hw2=Hw2+Hw3;
end
Hw=Hw2+Hw1;
H_mag=abs(Hw);
plot(w/pi,H_mag,'k');
grid;
title('Magnitude Response');
xlabel('Normalized frequency');
ylabel('Magnitude');
RESULT:
RESULT:
Ex.No:9
STUDY OF ADDRESSING MODES IN TMS320C5X PROCESSOR
Aim:
To study the different addressing modes used in TMS320C5X processor
Theory:
The different types of addressing modes used in TMS320C5X processor are
Immediate addressing
Direct addressing
Indirect addressing
Dedicated-register addressing
Memory-mapped register addressing
Circular addressing modes.
IMMEDIATE ADDRESSING MODE:
1. Initialize data pointer with 100H data.
2. Load the accumulator with first data.
3. Add the second data with accumulator content
DIRECT ADDRESSING MODE:
1. Initialize data pointer with 100H data.
2. Load the accumulator with first data, whose address is specified in the Instruction.
3. Add the accumulator content with second data, whose address is specified in the
instruction.
4. Store the accumulator content in specified address location.
IN-DIRECT ADDRESSING MODE:
1. Load the auxiliary register with address location of first data.
2. The auxiliary register (AR0) is modified indirectly as # symbol.
3. Load the second data into accumulator and perform addition operation.
4. Store the result.
DEDICATED-REGISTER ADDRESSING
The dedicated-registered addressing mode operates like the long immediate addressing
mode, except that the address comes from one of two special-purpose memory-
mapped registers in the CPU: the block move address register (BMAR) and the
dynamic bit manipulation register (DBMR).
The advantage of this addressing mode is that the address of the block of memory to
be acted upon can be changed during execution of the program.
MEMORY-MAPPED REGISTER ADDRESSING
With memory-mapped register addressing, you can modify the memory mapped
registers without affecting the current data page pointer value
The memory-mapped register addressing mode operates like the direct addressing
mode, except that the 9 MSBs of the address are forced to 0 instead of being loaded
with the contents of the DP.
CIRCULAR ADDRESSING
Many algorithms such as convolution, correlation, and finite impulse response (FIR)
filters can use circular buffers in memory to implement a sliding window, which
contains the most recent data to be processed.
The ’C5x supports two concurrent circular buffers operating via the ARs. The registers
which control the circular buffer operation are
CBSR1 — Circular buffer 1 start register
CBSR2 — Circular buffer 2 start register
CBER1 — Circular buffer 1 end register
CBER2 — Circular buffer 2 end register
CBCR — Circular buffer control register
Program for immediate addressing mode
.MMREGS
.TEXT
START: LDP #100H
LACC #1241H
ADD #1200H
SACL 2H
HTL: END
Program for direct addressing mode
.MMREGS
.TEXT
START: LDP #100H
LACC 0H
ADD 1H
SACL 2H
HTL: END
Program for adding two numbers with indirect addressing mode.
.MMREGS
.TEXT
START: LAR AR0,#8000H
MAR *,AR0
LACC *+,0 ;WITH ZERO SHIFT
ADD *+
SACL *+
HTL: END
RESULT:
Ex.No:10
LINEAR AND CIRCULAR CONVOLUTION USING TMS320C50
Aim:
To implement linear and circular convolution algorithms using TMS320C50
processor
Algorithm:
.mmregs
.text
START:
LDP #02H
LAR AR1,#8100H ; x(n) datas
lar ar0,#08200H ;h(n) datas
LAR AR3,#8300H ;y(n) starting
LAR AR4,#0007 ;N1+N2-1
;to fold the h(n) values
;************************
lar ar0,#8203H ; data mem 8200 to program mem c100(tblw)
lacc #0c100h
mar *,ar0
rpt #3
tblw *- ;to move 8203- 8200 to c100- c103
;padding of zerros for x(n) values
;**********************************
lar ar6,#8104h
mar *,ar6
lacc #0h
rpt #3h
sacl *+
;convalution operation starts
;******************************
LOP: MAR *,AR1
LACC *+
SACL 050H ;starting of the scope of multiplication
LAR AR2,#0153H ; end of the array, to be multiplied with h(n) {150+N1-1}
MAR *,AR2
ZAP
RPT #03H ;N1-1 times so that N1 times
MACD 0C100H,*-
APAC ;to accmulate the final product sample
MAR *,AR3
SACL *+
MAR *,AR4
BANZ LOP,*-
H: B H
RESULT:
Ex.No:11
Waveform Generation using TMS320C50 Processor
Aim:
To generate various waveforms using TMS320C50 processor
Program:
SAWTOOTH WAVEFORM:
.MMREGS
.TEXT
START:
LDP #120H
LACC #0H ;change lower amplitude
SACL 0
LOOP: LACC 0
OUT 0,04H
ADD #05h ;change frequency
SACL 0
SUB #0FFFh ;change upper amplitude
BCND LOOP,LEQ
B START
.END
SINEWAVE GENERATION.
FREQ .set 1
LENGTH .SET 360
AMPLITUDE .set 5
TEMP .set 0
TEMP1 .set 1
.mmregs
.text
START:
LDP #100H
SPLK #0C100H,TEMP ;load start address of table
lar AR2,#LENGTH
CONT:
LACC TEMP ;load address of sine value
TBLR TEMP1 ;read sine data to TEMP1
LT TEMP1
MPY #AMPLITUDE
PAC
SACL TEMP1
OUT TEMP1,4 ;send sine data to DAC
LACC TEMP
ADD #FREQ ;increase table value
SACL TEMP ;store it
MAR *,AR2
BANZ CONT,*-
b START
.end
LAR AR2,#FREQ
CONTx:
OUT TEMP,4
LACC TEMP
SUB #AMPLITUDE
SACL TEMP
MAR *,AR2
BANZ CONTx
B CONT1
.end
RESULT:
Ex.No:12
Waveform Generation using TMS320C50 Processor
Aim:
Implementation of FFT algorithms using TMS320C50 Processor
Program:
*********************************************************
; FFT(4 POINT)
;*********************************************************
IN .set 8010H
BITREV .set 8020H
REAL .set 8040H
IMG .set 8050H
.MMREGS
.TEXT
LDP #100H
LAR AR1,#IN
LAR AR2,#BITREV
SPLK #2H,05H
LMMR INDX,#8005H
MAR *,AR2
RPT #3H
BLDD #IN,*BR0+
LAR AR2,#BITREV
LAR AR3,#8030H
LAR AR0,#1H
FFT1: MAR *,AR2
LACC *+
SACB
LT *+
MPY #1H
APAC
MAR *,AR3
SACL *+
LACB
SPAC
SACL *+,AR0
BANZ FFT1,*-
LAR AR3,#8030H
LAR AR4,#REAL
LAR AR5,#IMG
MAR *,AR3
LACC *
SACB
ADRK #2H
LT *-
MPY #1H
APAC
MAR *,AR4
SACL *
ADRK #2H
LACC #0H
MAR *,AR5
SACL *
ADRK #2H
LACB
SPAC
MAR *,AR4
SACL *-
LACC #0H
MAR *,AR5
SACL *-,AR3
LACC *,AR4
SACL *
ADRK #2H
SACL *,AR3
ADRK #2H
LT *
MPY #0FFFFH
MAR *,AR5
SPL *,AR3
LT *
MPY #1H
MAR *,AR5
ADRK #2H
SPL *
H: B H
;INPUT:
; 8010-0001
; 8011-0001
; 8012-0000
; 8013-0000
;BIT_REV:
; 8020-0001
; 8021-0000
; 8022-0001
; 8023-0000
;FFT1:
; 8030-0001
; 8031-0001
; 8032-0001
; 8033-0001
;REAL:
; 8040-0002
; 8041-0001
; 8042-0000
; 8043-0001
;IMG:
; 8050-0000
; 8051-FFFF
; 8052-0000
; 8053-0001
RESULT:
Ex.No:13
Finite Impulse Response Filters using TMS320C50 Processor
Aim:
Implementation of FIR filter using TMS320C50 Processor
00000000
* Approximation type: Window design - Rectangular Window
* Filter type: Lowpass filter
* Filter Order: 52
* Cutoff frequency in KHz = 4.000000
.mmregs
.text
B START
CTABLE:
.word 0196H
.word 017EH
.word 0EBH
.word 00H
.word 0FEFFH
.word 0FE37H
.word 0FDEDH
.word 0FE44H
.word 0FF35H
.word 083H
.word 01D3H
.word 02B9H
.word 02DEH
.word 0218H
.word 07EH
.word 0FE6CH
.word 0FC72H
.word 0FB36H
.word 0FB4CH
.word 0FD0FH
.word 084H
.word 0552H
.word 0ACBH
.word 0100CH
.word 0142FH
.word 01675H
.word 01675H
.word 0142FH
.word 0100CH
.word 0ACBH
.word 0552H
.word 084H
.word 0FD0FH
.word 0FB4CH
.word 0FB36H
.word 0FC72H
.word 0FE6CH
.word 07EH
.word 0218H
.word 02DEH
.word 02B9H
.word 01D3H
.word 083H
.word 0FF35H
.word 0FE44H
.word 0FDEDH
.word 0FE37H
.word 0FEFFH
.word 00H
.word 0EBH
.word 017EH
.word 0196H
* Move the Filter coefficients
* from program memory to data memory
START:
LAR AR0,#0200H
MAR *,AR0
RPT #33H
BLKP CTABLE,*+
SETC CNF
* Input data and perform convolution
ISR: LDP #0AH
IN 0,6H
IN 0,4H
NOP
NOP
NOP
NOP
LAR AR1,#0300H
LACC 0
AND #0FFFH
SUB #800H
MAR *,AR1
SACL *
LAR AR1,#333H
ZAP
RPT #33H
MACD 0FF00H,*-
APAC
LAR AR1,#0300H
SACH * ;give as sach *,1 incase of overflow
LACC *
ADD #800H
SFR ;remove if o/p is less amplitude
SACL *
OUT *,4
NOP
B ISR
.end
RESULT: