MATLAB Digital Signal Processing
MATLAB Digital Signal Processing
PRITHIKA PRIYA
2018105578
1
S.NO DATE NAME OF THE EXPERIMENT PAGE MARKS REMARK
NO
1 12/09/2020 Generation of Basic signals 3
3 12/09/2020 Convolution 18
5 14/09/2020 Correlation 47
2
Exp : 1
Generation of basic signals
AIM:
To generate basic signals like unit impulse, unit step, unit ramp, rising
exponential, decaying exponential sequence and their time shifted form using
matlab code.
ALGORITHM:
Open Matlab.
Enter the code to generate basic signals and their time shifted form.
Run the code
Display the output plot.
MATLAB CODE:
clc;
clear all;
close all;
t=-5:1:5;
imp=t==0;
unit=t>=0;
ramp= t.*unit;
a=input('Enter Amplitude:');
z=exp(a*t);
h=exp(-a*t);
p=input('Enter Shifting factor:');
q=p+t;
subplot(5,2,1);
stem(t,imp,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('Impulse');
subplot(5,2,2);
stem(q,imp,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('Impulse signal After Shifting');
3
subplot(5,2,3);
stem(t,unit,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('Unit step');
subplot(5,2,4);
stem(q,unit,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('Unit step signal After Shifting');
subplot(5,2,5);
stem(t,ramp,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('ramp');
subplot(5,2,6);
stem(q,ramp,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('Ramp signal After Shifting');
subplot(5,2,7);
stem(t,z,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('Rising exponential');
subplot(5,2,8);
stem(q,z,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('Rising exponential signal After Shifting');
subplot(5,2,9);
stem(t,h,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('Decaying exponential');
4
subplot(5,2,10);
stem(q,h,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('Decaying exponential signal After Shifting');
OUTPUT:
5
6
MODEL GRAPH:
7
8
RESULT:
Thus the basics signals and their time shifted form is generated using
matlab code and their output is viewed.
9
Exp : 2 DFT Pair
AIM:
To perform DFT and IDFT for the given input sequence using matlab
code.
ALGORITHM:
Open Matlab software
Enter the code to perform DFT and IDFT
Enter the input sequence
Perform required calculations
Display the output plot
MATLAB CODE:
DFT:
clc;
clear all;
close all;
xn=input('Enter the sequence x(n):');
l=length(xn);
xk=zeros(1,l);
i=sqrt(-1);
for k=0:l-1
for n=0:l-1
xk(k+1)=xk(k+1)+(xn(n+1)*exp(((-i)*2*pi*k*n)/l));
end
end
disp(xk);
t=0:l-1;
subplot(2,3,1);
stem(t,xn,'b');
grid on;
xlabel('time');
ylabel('Amplitude of x(n)');
title('Input sequence x(n)');
mag=abs(xk);
for k=0:l-1
a(k+1)=(2*180.*k./l);
10
end
disp(a);
subplot(2,3,2);
stem(a,mag,'b');
grid on;
xlabel('frequency');
ylabel('|X(k)|');
title('Magnitude of X(k)');
phase=rad2deg(angle(xk));
t=0:l-1;
subplot(2,3,3);
stem(a,phase,'b');
grid on;
xlabel('frequency');
ylabel('Phase(X(k))');
title('Phase of X(k)');
IDFT:
xf=input('Enter the DFT coefficients X(k):');
l=length(xf);
x=zeros(1,l);
i=sqrt(-1);
for n=0:l-1
for k=0:l-1
x(n+1)=x(n+1)+(xf(k+1)*exp(((i)*2*pi*k*n)/l));
end
q=1/l;
x(n+1)=q*x(n+1);
end
disp(x);
t=0:l-1;
subplot(2,3,4);
stem(t,x,'b');
grid on;
xlabel('time');
ylabel('x(n)');
title('IDFT of X(k)');
mag=abs(x);
t=0:l-1;
subplot(2,3,5);
stem(t,mag,'b');
grid on;
xlabel('time');
ylabel('|x(n)|');
title('Magnitude of x(n)');
phase=deg2rad(angle(x));
t=0:l-1;
11
subplot(2,3,6);
stem(t,phase,'b');
grid on;
xlabel('time');
ylabel('phase(x(n))');
title('phase of x(n)');
OUTPUT:
DFT:
12
IDFT:
13
14
MODEL CALCULATION:
15
16
RESULT:
Thus the DFT and IDFT of the given input sequence is performed using
matlab code and the output is viewed.
17
EXP : 3 CONVOLUTION
AIM:
To perform circular and linear convolution by matrix method and DFT
IDFT method and linear convolution using circular convolution using matlab
code.
ALGORITHM:
Open matlab software.
Get the input sequences x(n) and h(n).
Calculate the length of n1, n2 for the sequences x(n) h(n) respectively.
Add zeros to the input sequences such that the length of the input
sequences is equal to n1+n2-1 for linear convolution.
Add zeros to the input sequences such that the length of the input
sequences is equal to maximum of n1 and n2 for circular convolution.
Perform the convolution operation such that the length of output is
equal to n1+n2-1 for linear convolution by matrix method.
Perform the convolution operation such that the length of output is
equal to maximum of n1 and n2 for circular convolution by matrix
method.
Use DFT and IDFT formula to perform linear and circular convolution
using DFT and IDFT method such that the output length for linear
convolution is n1+n2-1 and circular convolution is maximum of n1 ad n2.
Display the output plot.
MATLAB CODE:
clc;
clear all;
close all;
disp('1.Linear Convolution by matrix method');
disp('2.Circular Convolution by matrix method');
disp('3.Linear COnvolution using circular convolution');
disp('4.Linear Convolution with DFT');
disp('5.Circular Convolution with DFT');
i=input('Enter Choice');
x=input('Enter the sequence x(n)');
h=input('Enter the sequence h(n)');
18
n1=length(x);
n2=length(h);
switch i
case 1
p=[];
for i=1:n1
q=h.*x(i);
p=[p;q];
end
[row,column]=size(p);
total=row+column;
s=2;
z=[];
for s=2:total
sum=0;
for i=1:row
for j=1:column
if((i+j)==s)
sum=sum+p(i,j);
end
end
end
z=[z sum];
end
n=n1+n2-1;
a=0:n1-1;
subplot(311);
stem(a,x);
grid on;
xlabel('time');
ylabel('Amplitude of x(n)')
title('Input sequence x(n)');
b=0:n2-1;
subplot(312);
stem(b,h)
grid on;
xlabel('time');
ylabel('Amplitude of h(n)')
title('Input sequence h(n)');
t=0:n-1;
subplot(313);
stem(t,z);
grid on;
xlabel('time');
19
ylabel('Amplitude of y(n)')
title('Linearly convolved sequence y(n) by matrix
method');
case 2
n=max(n1,n2);
if n~=n1
x1=[x zeros(1,n-n1)]
else
x1=x;
end
if n~=n2
h1=[h zeros(1,n-n2)];
else
h1=h;
end
y1=[];
y1=[y1 h1];
z1=y1;
for i=1:n-1
y2=y1;
y2=circshift(y2,1);
z1=[z1;y2];
y1=y2;
end
a=transpose(z1);
b=transpose(x1);
c=a*b;
d=transpose(c);
t=0:n-1;
subplot(311);
stem(t,x1);
grid on;
xlabel('Time');
ylabel('Amplitude of x(n)');
title('Input sequence x(n)');
subplot(312);
stem(t,h1);
grid on;
xlabel('Time');
ylabel('Amplitude of h(n)');
title('Input sequence h(n)');
subplot(313);
stem(t,d);
grid on;
20
xlabel('Time');
ylabel('Amplitude of y(n)');
title('Circularly convolved sequence y(n) by matrix
method');
case 3
n=max(n1,n2);
l=n1+n2-1;
x1=[x zeros(1,l-n1)]
h1=[h zeros(1,l-n2)];
y1=[];
y1=[y1 h1];
z1=y1;
for i=1:n-1
y2=y1;
y2=circshift(y2,1);
z1=[z1;y2];
y1=y2;
end
a=transpose(z1);
if n~=n1
x2=[x zeros(1,n-n1)]
else
x2=x;
end
b=transpose(x2);
c=a*b;
d=transpose(c);
t=0:l-1;
subplot(311);
stem(t,x1);
grid on;
xlabel('Time');
ylabel('Amplitude of x(n)');
title('Input sequence x(n)');
subplot(312);
stem(t,h1);
grid on;
xlabel('Time');
ylabel('Amplitude of h(n)');
title('Input sequence h(n)');
subplot(313);
stem(t,d);
grid on;
xlabel('Time');
21
ylabel('Amplitude of y(n)');
title('Linearly convolved sequence y(n) using circular
convolution');
case 4
l1=length(x);
l2=length(h);
l=l1+l2-1;
xn=[x zeros(1,l-l1)];
disp(xn);
hn=[h zeros(1,l-l2)];
disp(hn);
t=0:l-1;
xk=zeros(1,l);
i=sqrt(-1);
for k=0:l-1
for n=0:l-1
xk(k+1)=xk(k+1)+(xn(n+1)*exp(((-i)*2*pi*k*n)/l));
end
end
hk=zeros(1,l);
i=sqrt(-1);
for k=0:l-1
for n=0:l-1
hk(k+1)=hk(k+1)+(hn(n+1)*exp(((-i)*2*pi*k*n)/l));
end
end
yk=(xk.*hk);
yn=zeros(1,l);
i=sqrt(-1);
for n=0:l-1
for k=0:l-1
yn(n+1)=yn(n+1)+(yk(k+1)*exp(((i)*2*pi*k*n)/l));
end
q=1/l;
yn(n+1)=q*yn(n+1);
end
disp(yn);
t=0:l-1;
subplot(2,2,1);
stem(t,xn,'b');
grid on;
xlabel('time');
22
ylabel('Amplitude');
title('x(n)');
subplot(2,2,2);
stem(t,hn,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('h(n)');
subplot(2,2,3);
stem(t,yn,'b');
grid on;
xlabel('time');
ylabel('y(n)');
title('Linear convolution of x(n) and h(n) using DFT
IDFT');
case 5
l1=length(x);
l2=length(h);
l=max(l1,l2)
xn=[x zeros(1,l-l1)];
disp(xn);
hn=[h zeros(1,l-l2)];
disp(hn);
t=0:l-1;
xk=zeros(1,l);
i=sqrt(-1);
for k=0:l-1
for n=0:l-1
xk(k+1)=xk(k+1)+(xn(n+1)*exp(((-i)*2*pi*k*n)/l));
end
end
hk=zeros(1,l);
i=sqrt(-1);
for k=0:l-1
for n=0:l-1
hk(k+1)=hk(k+1)+(hn(n+1)*exp(((-i)*2*pi*k*n)/l));
end
end
yk=(xk.*hk);
yn=zeros(1,l);
i=sqrt(-1);
for n=0:l-1
23
for k=0:l-1
yn(n+1)=yn(n+1)+(yk(k+1)*exp(((i)*2*pi*k*n)/l));
end
q=1/l;
yn(n+1)=q*yn(n+1);
end
disp(yn);
t=0:l-1;
subplot(2,2,1);
stem(t,xn,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('x(n)');
subplot(2,2,2);
stem(t,hn,'b');
grid on;
xlabel('time');
ylabel('Amplitude');
title('h(n)');
subplot(2,2,3);
stem(t,yn,'b');
grid on;
xlabel('time');
ylabel('y(n)');
title('Circular convolution of x(n) and h(n) using DFT
IDFT');
end
OUTPUT:
24
CIRCULAR CONVOLUTION BY MATRIX METHOD:
25
LINEAR CONVOLUTION BY CIRCULAR CONVOLUTION METHOD:
26
LINEAR CONVOLUTION BY DFT IDFT METHOD:
27
CIRCULAR CONVOLUTION USING DFT IDFT METHOD:
28
MODEL CALCULATION:
29
30
31
32
33
MODEL GRAPH:
34
RESULT:
Thus the circular and linear convolution by matrix method and DFT
IDFT method is performed using matlab code and the output is viewed.
35
EXP : 4 BLOCK CONVOLUTION
AIM:
To perform block convolution for the given input sequences using matlab
code and to view the output.
ALGORITHM:
Open Matlab software.
Get the input sequences x(n) and h(n).
Calculate the length l, m for the sequences x(n) h(n) respectively.
Split the input sequences into separate blocks by adding m elements
from x(n) and m-1 zeros to each of the separate blocks for overlap add
method.
Add zeros if necessary to the last block to make its length 2m-1.
Split the input sequences into separate blocks by adding last m-1
elements and m elements from x(n) from the previous blocks for
overlap save method. Initially, m-1 zeros followed by m elements from
x(n) is added for the first block.
Add zeros if necessary to the last block to make its length 2m-1.
Perform circular convolution to each of the blocks with h(n)
Get the resultant sequence by adding elements and discarding
unwanted elements for overlap add method and overlap save method
respectively.
MATLAB CODE:
clc;
clear all;
close all;
disp('1.Convolution using overlap add method');
disp('2.Convolution using overlap save method');
in=input('Enter choice:');
x=input("Enter x(n): ")
h=input("Enter h(n): ")
switch in
36
OVERLAP ADD METHOD:
case 1
l=length(x);
m=length(h);
n=l+m-1;
h1=[h zeros(1,m-1)];
n1=length(h1);
H=fft(h1);
x1=[zeros(1,m-1) x zeros(1,n-n1-m)]
s=[0:1:n-1];
s1=[0:1:l-1];
subplot(2,2,1)
stem(s1,x)
grid on;
title("Input sequence x(n)")
xlabel("time")
ylabel("Amplitude of x(n)")
s2=[0:1:m-1];
subplot(2,2,2)
stem(s2,h)
grid on;
title("Input sequence h(n)")
xlabel("time")
ylabel("Amplitude of h(n)")
x2=[x zeros(1,m)];
Y=zeros(1,n);
c=1;
for i=1:m:n
x3=x2(i:(i+m-1));
x4=[x3 zeros(1,m-1)];
y5=fft(x4);
y6=y5.*H;
y7=round(ifft(y6));
if i==1
Y(i:i+n1-1)=y7(1:n1);
elseif i>1 && i<=n-m
Y((i):(i+n1-m-1))=Y((i):(i+n1-m-1))+y7(1:n1-m);
Y((i+n1-m):(i+n1-1))=y7(n1-m+1:n1);
elseif i>n-m
Y((i):(i+n1-m-1))=Y((i):(i+n1-m-1))+y7(1:n1-m);
Y(n)=y7(n1-m+1);
end
end
disp(Y);
subplot(2,2,3)
stem(s,Y)
37
grid on
title("y(n) using overlap add method")
xlabel("time")
ylabel("Amplitude of y(n)")
case 2
l=length(x);
m=length(h);
n=l+m-1;
h1=[h zeros(1,m-1)];
n1=length(h1);
H=fft(h1);
x1=[zeros(1,m-1) x zeros(1,n-n1-m)];
y=zeros(1,n);
for i=1:m:n
y1=x1(i:i+(2*(n1-m)));
y2=fft(y1);
y3=y2.*H;
y4=round(ifft(y3));
if i<n
y(i:(i+n1-m))=y4(m:n1);
else
y(i:(i+n1-m-1))=y4(m:n1-1);
end
end
disp(y);
s=[0:1:n-1];
s1=[0:1:l-1];
subplot(2,2,1)
stem(s1,x)
grid on;
title("Input sequence x(n)")
xlabel("time")
ylabel("Amplitude of x(n)")
s2=[0:1:m-1];
subplot(2,2,2)
stem(s2,h)
grid on;
title("Input sequence h(n)")
xlabel("time")
ylabel("Amplitude of h(n)")
subplot(2,2,3)
stem(s,y)
grid on
title("y(n) using overlap save method")
38
xlabel("time")
ylabel("Amplitude of y(n)")
end
OUTPUT:
Enter choice: 1
Enter x(n): [3 -1 0 1 3 2 0 1 2 1]
x=
3 -1 0 1 3 2 0 1 2 1
Enter h(n): [1 1 1]
h=
1 1 1
3 2 2 0 4 6 5 3 3 4 3 1
39
OVERLAP SAVE METHOD:
1.Convolution using overlap add method
40
x=
3 -1 0 1 3 2 0 1 2 1
Enter h(n): [1 1 1]
h=
1 1 1
3 2 2 0 4 6 5 3 3 4 3 1
41
42
MODEL CALCULATION:
43
44
45
RESULT:
Thus the block convolution for the given input sequences is performed
using matlab code and their output is viewed
46
EXP : 5 CORRELATION
AIM:
To perform auto correlation and cross correlation for the given input
sequences and to view their output.
ALGORITHM:
Open Matlab software.
Get the input sequences x(n) and y(n).
Calculate the length n1, n2 for the input sequences x(n) and y(n)
respectively.
Append n1-1 zeros before after the input sequence x(n) to get the
sequence a.
Append n2-1 zeros before after the input sequence y(n) to get the
sequence b.
For auto correlation of x perform correlation operation with x(n) and the
sequence a.
For auto correlation of y perform correlation operation with the
sequence y(n) and the sequence b.
For cross correlation of x and y perform correlation operation with the
sequence y(n) and the sequence a.
For cross correlation of y and x perform correlation operation with the
sequence x(n) and the sequence b.
Display the output plot.
MATLAB CODE:
clc;
clear all;
close all;
x=input('Enter the input sequence x(n)');
y=input('Enter the input sequence y(n)');
n1=length(x);
n2=length(y);
a=[zeros(1,n1-1),x,zeros(1,n1-1)];
b=[zeros(1,n2-1),y,zeros(1,n2-1)];
47
disp('1.Auto correlation of x 2.Auto correlation of y
3.Cross correlation of x and y 4.Cross correlation of y
and x');
r=input('Select operation');
switch r
AUTO CORRELATION OF X:
case 1
for k=1:2.*n1-1
Rxx(k)=0;
for i=1:n1
Rx(k)=x(i)*a(i+(k-1));
Rxx(k)=Rxx(k)+Rx(k);
end
end
l=0:n1-1;
subplot(211);
stem(l,x);
grid on;
title('Input signal x(n)');
xlabel('time');
ylabel('Amplitude of x(n)');
subplot(212);
n2=-(n1-1):(n1-1);
stem(n2,Rxx);
grid on;
title('Auto correlation of x(n)');
xlabel('time');
ylabel('Amplitude of Rxx(k)');
AUTO CORRELATION OF Y:
case 2
for k=1:2.*n2-1
Ryy(k)=0;
for i=1:n1
Ry(k)=y(i)*b(i+(k-1));
Ryy(k)=Ryy(k)+Ry(k);
end
end
l=0:n2-1;
subplot(211);
stem(l,y);
grid on;
title('Input signal y(n)');
xlabel('time');
48
ylabel('Amplitude of y(n)');
subplot(212);
n1=-(n2-1):(n2-1);
stem(n1,Ryy);
grid on;
title('Auto correlation of y(n)');
xlabel('time');
ylabel('Amplitude of Ryy(k)');
case 3
for k=1:2.*n1-1
Rxy(k)=0;
for i=1:n2
Rx(k)=y(i)*a(i+(k-1));
Rxy(k)=Rxy(k)+Rx(k);
end
end
subplot(311);
l=0:n1-1;
stem(l,x);
title('Input signal x(n)');
xlabel('time');
ylabel('Amplitude of x(n)');
subplot(312);
stem(l,y);
grid on;
title('Input signal y(n)');
xlabel('time');
ylabel('Amplitude of y(n)');
subplot(313);
p=-(n1-1):(n1-1);
stem(p,Rxy);
grid on;
title('Cross correlation of x and y');
xlabel('time');
ylabel('Amplitude of Rxy(k)');
case 4
for k=1:2.*n2-1
Ryx(k)=0;
for i=1:n1
Ry(k)=x(i)*b(i+(k-1));
Ryx(k)=Ryx(k)+Ry(k);
49
end
end
subplot(411);
l=0:n1-1;
stem(l,x);
grid on;
title('Input signal x(n)');
xlabel('time');
ylabel('Amplitude of x(n)');
subplot(412);
stem(l,y);
grid on;
title('Input signal y(n)');
xlabel('time');
ylabel('Amplitude of y(n)');
subplot(413);
p=-(n2-1):(n2-1);
stem(p,Ryx);
title('Cross correlation of y and x');
xlabel('time');
ylabel('Amplitude of Ryx(k)');
end
OUTPUT:
CASE 1:
Enter the input sequence x(n) [0 1 2 3]
Enter the input sequence y(n) [1 0 -1 0]
1.Auto correlation of x 2.Auto correlation of y 3.Cross correlation of x and y 4.Cross
correlation of y and x
Select operation 1
0 3 8 14 8 3 0
CASE 2:
Enter the input sequence x(n) [0 1 2 3]
Enter the input sequence y(n) [1 0 -1 0]
1.Auto correlation of x 2.Auto correlation of y 3.Cross correlation of x and y 4.Cross
correlation of y and x
Select operation 2
0 -1 0 2 0 -1 0
CASE 3:
Enter the input sequence x(n) [0 1 2 3]
Enter the input sequence y(n) [1 0 -1 0]
1.Auto correlation of x 2.Auto correlation of y 3.Cross correlation of x and y 4.Cross
correlation of y and x
50
Select operation 3
0 0 -1 -2 -2 2 3
CASE 4:
Enter the input sequence x(n) [0 1 2 3]
Enter the input sequence y(n) [1 0 -1 0]
1.Auto correlation of x 2.Auto correlation of y 3.Cross correlation of x and y 4.Cross
correlation of y and x
Select operation 4
3 2 -2 -2 -1 0 0
AUTO CORRELATION OF X:
AUTO CORRELATION OF Y:
51
CROSS CORRELATION OF X AND Y:
52
CROSS CORRELATION OF Y AND X:
53
MODEL CALCULATION AND MODEL GRAPH:
54
55
56
RESULT:
Thus the auto correlation and cross correlation of the given input
sequences are performed using Matlab code and the output is viewed.
57
EXP : 6 DECIMATION AND INTERPOLATION
AIM:
To perform decimation and interpolation for the given input sequences
using matlab code and to view the output.
ALGORITHM:
Open the Matlab software.
Get the input sequence x.
Compute the length N for the input sequence.
Get the interpolation and Decimation factor.
In interpolation, expand the input sequence to correct length by
inserting zeros between the original data values.
In decimation, the input sequence is shortened by decimation factor.
View the output plot.
MATLAB CODE:
clc;
clear all;
close all;
x=input('Input sequence:');
I=input('Interpolation factor:');
D=input('Decimation factor:');
N=length(x);
n=0:1:N-1;
subplot(311);
stem(n,x,'b');
grid on;
title('Input Sequence x(n)');
xlabel('time');
ylabel('Amplitude of x(n)');
y=[zeros(1,I*N)];
k=0;
for i=1:1:I*N
if mod(i-1,I)==0
k=k+1;
y(i)=x(k);
else
y(i)=0;
58
end
end
j=0:1:(I*N)-1;
subplot(312);
stem(j,y,'b');
grid on;
title('Interpolated Sequence');
xlabel('time');
ylabel('Amplitude of x(n)');
p=0;
for i=1:1:N
if mod(i-1,D)==0
p=p+1;
z(p)=x(i);
end
end
if mod((N/D),1)==0
m=(N/D)-1
else
m=floor(N/D);
end
n1=0:1:m;
subplot(313);
stem(n1,z,'b');
grid on;
title('Decimated Sequence');
xlabel('time');
ylabel('Amplitude of x(n)');
OUTPUT:
Input sequence: [1 3 2 1]
Interpolation factor:2
Decimation factor:2
59
INTERPOLATION:
DECIMATION:
60
MODEL CALCULATION AND MODEL GRAPH:
61
RESULT:
Thus the given input sequence is interpolated and decimated using
matlab code and their output is viewed.
62
EXP : 7 SPECTRUM OF FFT
AIM:
To generate the spectrum of fast fourier transform using matlab code
and to view the output.
ALGORITHM:
Open Matlab code.
Get the frequency components of the signal.
Plot the signal in time domain.
Identify the new input length that is the power of 2 from original signal
length which will pad the signal with trialing zeros in order to improve
the performance of fft.
Covert the obtained signal to frequency domain by performing fft.
Define the frequency domain and plot the unique frequencies.
Obtain the phase of the spectrum and plot it.
View the output.
MATLAB CODE:
clc;
clear all;
close all;
Fs=input('Sampling Frequency');
t=0:1/Fs:Fs;
disp('1.Signal with Single Frequency Component');
disp('2.Signal with two Frequency components');
r=input('Choose an option');
switch r
case 1
f=input('Enter the frequency component of the
signal "x"');
x=sin(2*pi*f*t);
subplot(311);
title('Input signal');
xlabel('Frequency in Hz');
ylabel('Amplitude');
plot(t(1:Fs),x(1:Fs));
d=length(x);
NFFT=2^nextpow2(d);
63
x=(fft(x,NFFT))/1;
y=fftshift(x);
f=Fs/2*linspace(-1,1,NFFT);
subplot(312);
plot((Fs)*abs(y(1:NFFT)));
title('Single spectrum');
xlabel('k');
ylabel('Amplitude');
d=angle(y);
z=rad2deg(d);
subplot(313);
plot(f,z);
title('Phase Spectrum');
xlabel('k');
ylabel('Deg');
case 2
f1=input('Enter the frequency component of the
first signal "x1"');
x1=sin(2*pi*f1*t);
t=0:1/Fs:Fs;
f2=input('Enter the frequency component of the
second signal "x2"');
x2=sin(2*pi*f2*t);
y=x1+x2;
d=length(y);
NFFT=2^nextpow2(d);
y=(fft(y,NFFT))/1;
y=fftshift(y);
f=Fs/2*linspace(-1,1,NFFT);
subplot(311);
title('Input signal');
xlabel('Frequency in hz');
ylabel('Amplitude');
plot(t(1:Fs),y(1:Fs));
subplot(312);
plot((Fs)*abs(y(1:NFFT)));
title('Single Spectrum');
xlabel('k');
ylabel('Amplitude');
d=angle(y);
z=rad2deg(d);
subplot(313);
plot(f,z);
title('Phase Spectrum');
xlabel('k'); ylabel('Deg');
end
64
OUTPUT:
CASE 1:
Sampling Frequency 1000
Choose an option 1
Enter the frequency component of the signal "x" 200
CASE 2:
Sampling Frequency 1000
Choose an option 2
Enter the frequency component of the first signal "x1" 100
Enter the frequency component of the second signal "x2" 200
65
SIGNAL WITH SINGLE FREQUENCY COMPONENT:
CASE 1:
66
SIGNAL WITH TWO FREQUENCY COMPONENTS:
CASE 2:
67
MODEL GRAPH:
68
RESULT:
Thus the spectrum of fft is generated using matlab code and its output
is viewed.
69
EXP : 8 FREQUENCY RESPONSE AND STABILITY ANALYSIS
AIM:
To perform Frequency response and Stability analysis and to display bode
plot, pole zero plot, magnetic response using matlab code and view the output.
ALGORITHM:
Open Matlab software.
Get the numerator and denominator coefficients.
Calculate the transfer function.
Obtain the bode plot using bode function.
Plot the pole-zero map for I/O pairs using iopzplot function.
Obtain the complex frequency response and angular frequency using
freqz function.
Compute the magnitude and phase value of the obtained complex
frequency response.
Compute the roots of numerator and denominator and display it.
Display “System is stable” if the magnitude of the roots of numerator is
greater than or equal to 1.
Display “System is unstable” if the magnitude of the roots of numerator
is less than 1.
Plot the magnetic response.
View the output.
MATLAB CODE:
clc;
clear all;
close all;
num=input('Numerator Coefficient:');
den=input('Denominator Coefficient:');
h=tf(num,den,0.1,'variable','z^-1');
figure(1);
bode(h);
figure(2);
iopzplot(h);
f=0:100;
[h,om]=freqz(den,num,f);
m=abs(h);
an=angle(h);
disp(h);h
p=roots(num);
disp(p);p
z=roots(den);
70
disp(z);z
if abs(p)>=1
disp('System is unstable');
else
disp('System is stable');
end
figure(3);
plot(om,m);
xlabel('f');
ylabel('Gain');
title('Magnetic Response');
OUTPUT:
Numerator Coefficient:
[1 0.8]
Denominator Coefficient:
[1 -0.25]
Columns 1 through 10
Columns 11 through 20
Columns 21 through 30
Columns 31 through 40
71
0.4183 + 0.0982i 0.4581 + 0.4897i
Columns 41 through 50
Columns 51 through 60
Columns 61 through 70
Columns 71 through 80
Columns 81 through 90
Column 101
0.4220 - 0.1761i
h =
72
Columns 1 through 10
Columns 11 through 20
Columns 21 through 30
Columns 31 through 40
Columns 41 through 50
Columns 51 through 60
Columns 61 through 70
Columns 71 through 80
73
0.4328 + 0.3063i 0.5500 + 0.8717i 2.9006 + 2.8844i 0.8550 - 1.5378i
0.4671 - 0.5402i 0.4196 - 0.1307i 0.4236 + 0.2009i 0.4923 + 0.6599i
1.2269 + 2.0174i 1.4881 - 2.2588i
Columns 81 through 90
Column 101
0.4220 - 0.1761i
-0.8000
p =
-0.8000
0.2500
z =
0.2500
System is stable
74
BODE PLOT:
MAGNITUDE RESPONSE:
75
OUTPUT:
Numerator Coefficient:
[1 -1]
Denominator Coefficient:
[1 -2 4]
1.0e+02 *
Columns 1 through 10
Columns 11 through 20
Columns 21 through 30
Columns 31 through 40
Columns 41 through 50
76
0.0217 + 0.0231i 0.0345 - 0.0051i 0.0110 - 0.0268i -0.0272 - 0.0052i
-0.0450 - 1.6939i -0.0260 + 0.0071i 0.0123 + 0.0266i 0.0347 + 0.0040i
0.0206 - 0.0237i -0.0170 - 0.0177i
Columns 51 through 60
Columns 61 through 70
Columns 71 through 80
Columns 81 through 90
Column 101
-0.0395 + 0.0349i
77
h =
1.0e+02 *
Columns 1 through 10
Columns 11 through 20
Columns 21 through 30
Columns 31 through 40
Columns 41 through 50
Columns 51 through 60
Columns 61 through 70
78
0.0331 - 0.0099i 0.0053 - 0.0271i -0.0319 + 0.0044i -0.0444 - 0.1713i
-0.0207 + 0.0141i 0.0175 + 0.0251i 0.0350 - 0.0009i 0.0157 - 0.0258i -
0.0226 - 0.0119i -0.0447 + 0.2559i
Columns 71 through 80
Columns 81 through 90
Column 101
-0.0395 + 0.0349i
p =
1.0000 + 1.7321i
1.0000 - 1.7321i
z =
1.0000 + 1.7321i
1.0000 - 1.7321i
System is unstable
79
BODE PLOT:
MAGNITUDE RESPONSE:
80
MODEL CALCULATION:
81
MODEL GRAPH:
82
83
RESULT:
Thus the frequency response and stability analysis is performed for the
given transfer function coefficients and their output is viewed
84
EXP : 9 DESIGN OF IIR FILTERS
AIM:
To design and implement low pass IIR filter, high pass IIR filterr, band
pass IIR filter, band stop IIR filter using
Butterworth filter
Chebyshev type 1 filter
and to display Magnitude spectra of the filtered signal and its Frequency
response curve.
ALGORITHM:
Open Matlab software
Get the sampling frequency and frequency components.
Get the pass band and stop band ripple.
Determine the order and cut off frequency of the filter using buttord
function for butterworth filter and cheb1ord for Chebyshev filter.
Compute the transfer function coefficients for the obtained order and
normalized cut off frequency using butter function for butterworth and
cheby1 for Chebyshev filter.
Specify the type of filter high for high pass, low for low pass, bandpass
for band pass, stop for band stop filters
Perform zero phase digital filtering by processing the data input x using
filtfilt function to obtain filter with zero phase distortion, transfer
function equal to the squared magnitude of the original filter transfer
function and order double the order of the filter specified by b and a.
Compute DFT of the original and filtered signal and its magnitude.
Plot the magnitude spectrum of the input and filtered signal.
Obtain the frequency response of the original and filtered signal using
freqz function.
View the output.
MATLAB CODE:
clc;
clear all;
85
close all;
fs=input('Enter sampling frequency');
t=0:1/fs:10;
f1=input('Enter the frequency 1 of input signal:');
f2=input('Enter the frequency 2 of input signal:');
f3=input('Enter the frequency 3 of input signal:');
x=sin(2.*pi.*f1.*t)+sin(2.*pi.*f2.*t)+sin(2.*pi.*f3.*t);
disp('1.BUTTERWORTH 2.CHEBYSHEV');
sw1=input('Select Filter:');
switch sw1
case 1
86
freqz(b,a);
case 2
end
87
OUTPUT:
0.3265
88
LOW PASS BUTTERWORTH FILTER:
MAGNITUDE SPECTRUM:
89
FREQUENCY RESPONSE:
90
OUTPUT:
0.3000
91
LOW PASS CHEBYSHEV TYPE 1 FILTER:
MAGNITUDE SPECTRA:
92
FREQUENCY RESPONSE:
93
MATLAB CODE:
clc;
clear all;
close all;
fs=input('Enter sampling frequency');
t=0:1/fs:10;
f1=input('Enter the frequency 1 of input signal:');
f2=input('Enter the frequency 2 of input signal:');
f3=input('Enter the frequency 3 of input signal:');
x=sin(2.*pi.*f1.*t)+sin(2.*pi.*f2.*t)+sin(2.*pi.*f3.*t);
disp('1.BUTTERWORTH 2.CHEBYSHEV');
sw1=input('Select Filter:');
switch sw1
case 1
94
xlabel('freq(hz)');
ylabel('gain(db)');
title('Magnitude spectra of filtered signal');
figure();
freqz(b,a);
case 2
end
95
OUTPUT:
1.BUTTERWORTH 2.CHEBYSHEV
Select Filter:1
Enter passband ripple:4
Enter stopband ripple:20
Enter the passband frequency:2500
0.4683
96
HIGH PASS BUTTERWOTH FILTER:
MAGNITUDE SPECTRA:
97
FREQUENCY RESPONSE:
98
OUTPUT:
1.BUTTERWORTH 2.CHEBYSHEV
Select Filter:2
Enter passband ripple:4
Enter stopband ripple:20
Enter the passband frequency:2500
0.5000
99
HIGH PASS CHEBYSHEV TYPE 1 FILTER:
MAGNITUDE SPECTRUM:
100
FREQUENCY RESPONSE:
101
MATLAB CODE:
clc;
clear all;
close all;
fs=input('Enter sampling frequency');
t=0:1/fs:10;
f1=input('Enter the frequency 1 of input signal:');
f2=input('Enter the frequency 2 of input signal:');
f3=input('Enter the frequency 3 of input signal:');
x=sin(2.*pi.*f1.*t)+sin(2.*pi.*f2.*t)+sin(2.*pi.*f3.*t);
disp('1.BUTTERWORTH 2.CHEBYSHEV');
sw1=input('Select Filter:');
switch sw1
case 1
102
stem(w3,m2);
xlabel('freq(hz)');
ylabel('gain(db)');
title('Magnitude spectra of filtered signal');
figure();
freqz(b,a);
case 2
end
103
OUTPUT:
0.1940 0.3085
104
BAND PASS BUTTERWORTH FILTER:
MAGNITUDE SPECTRA:
105
FREQUENCY RESPONSE:
106
OUTPUT:
1.BUTTERWORTH 2.CHEBYSHEV
Select Filter:2
Enter passband ripple:3
Enter stopband ripple:15
Enter the first passband frequency:1000
0.2000 0.3000
107
BAND PASS CHEBYSHEV TYPE 1 FILTER:
MAGNITUDE SPECTRUM:
108
FREQUENCY RESPONSE:
109
MATLAB CODE:
clc;
clear all;
close all;
fs=input('Enter sampling frequency');
t=0:1/fs:10;
f1=input('Enter the frequency 1 of input signal:');
f2=input('Enter the frequency 2 of input signal:');
f3=input('Enter the frequency 3 of input signal:');
x=sin(2.*pi.*f1.*t)+sin(2.*pi.*f2.*t)+sin(2.*pi.*f3.*t);
disp('1.BUTTERWORTH 2.CHEBYSHEV');
sw1=input('Select Filter:');
switch sw1
case 1
110
stem(w3,m2);
xlabel('freq(hz)');
ylabel('gain(db)');
title('Magnitude spectra of filtered signal');
figure();
freqz(b,a);
case 2
end
111
OUTPUT:
1.BUTTERWORTH 2.CHEBYSHEV
Select Filter:1
0.1524 0.3794
112
BAND STOP BUTTERWORTH FILTER
MAGNITUDE SPECTRA:
113
FREQUENCY RESPONSE:
114
OUTPUT:
1.BUTTERWORTH 2.CHEBYSHEV
Select Filter:2
0.1000 0.4000
115
BAND STOP CHEBYSHEV TYPE 1 FILTER:
MAGNITUDE SPECTRA:
116
FREQUENCY RESPONSE:
117
MODEL CALCULATION:
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
MODEL GRAPH:
134
135
136
RESULT:
Thus the implementation low pass IIR, high pass IIR, band pass IIR and
band stop IIR filters are performed using Butterworth and Chebyshev filters
and their output is viewed using matlab code.
137
EXP : 10 DESIGN OF FIR FILTERS
AIM:
To design and implement the low pass, high pass, bandpass and band
stop FIR filters using
Blackman window
Hamming window
Hanning window
Rectangular window
and to display Magnitude spectra of the filtered signal and its Frequency
response curve.
ALGORITHM:
Open Matlab software.
Get the sampling frequency and the frequency components and the
cutoff frequency.
Get the order of the filter.
Obtain a N point blackman window using blackman function.
Perform least squares approximation to compute filter coefficients b and
to smooth the impulse response with the window using fir1 function.
Specify the type of filter.
Compute DFT of the input signal and find its magnitude.
Filter the input data x with the given rational transfer function defined
by the numerator coefficient b and denominator coefficient a using
filter function. Since FIR denominator coefficient a is 1.
Compute DFT for the obtained filtered signal and find its magnitude.
Plot the magnitude spectra of the original and filtered signal.
Plot the frequency response of the original and filtered signal.
View the output.
MATLAB CODE:
clc;
clear all;
close all;
fs=input('Enter sampling frequency');
138
t=0:1/fs:10;
f1=input('Enter the first frequency of input signal');
f2=input('Enter the second frequency of input signal');
f3=input('Enter the third frequency of input signal');
x=sin(2*pi*f1*t)+sin(2*pi*f2*t)+sin(2*pi*f3*t);
disp('1.Blackman 2.Hanning 3.Hamming 4.Rectangular');
in=input('Enter choice:');
switch in
case 1
139
LOW PASS FIR FILTER USING HANNING WINDOW:
case 2
case 3
140
wp=input('Enter cutoff frequency');
wc=(2.*wp)/fs;
b=fir1(N,wc,'low',y);
w=0:(pi/100):pi;
X=fft(x);
z=filter(b,1,x);
disp(b);
Z=fft(z);
m1=abs(X);
m2=abs(Z);
subplot(121);
w0=[(0:length(m1)-1)/length(m1)-1]*fs;
stem(w0,m1);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of input signal');
subplot(122);
w3=[(0:length(m2)-1)/length(m2)-1]*fs;
stem(w3,m2);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of filtered signal');
figure();
freqz(b);
case 4
141
w0=[(0:length(m1)-1)/length(m1)-1]*fs;
stem(w0,m1);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of input signal');
subplot(122);
w3=[(0:length(m2)-1)/length(m2)-1]*fs;
stem(w3,m2);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of filtered signal');
figure();
freqz(b);
end
OUTPUT:
Enter choice: 1
Columns 1 through 11
Columns 12 through 15
142
LOW PASS FIR FILTER USING BLACKMAN WINDOW:
MAGNITUDE SPECTRA:
143
FREQUENCY RESPONSE:
144
OUTPUT:
0.0010 0.0074 -0.0000 -0.0380 -0.0432 0.0801 0.2921 0.4012 0.2921 0.0801
-0.0432
Columns 12 through 15
145
LOW PASS FIR FILTER USING HANNING WINDOW:
MAGNITUDE SPECTRA:
146
FREQUENCY RESPONSE:
147
OUTPUT:
Enter choice: 3
Enter the order of the filter: 15
Enter cutoff frequency 2000
Columns 1 through 11
0.0021 0.0063 -0.0000 -0.0330 -0.0399 0.0771 0.2880 0.3987 0.2880 0.0771
-0.0399
Columns 12 through 15
148
LOW PASS FIR FILTER USING HAMMING WINDOW:
MAGNITUDE SPECTRA:
149
FREQUENCY RESPONSE:
150
OUTPUT:
0.0250 0.0471 -0.0000 -0.0707 -0.0582 0.0874 0.2827 0.3735 0.2827 0.0874
-0.0582
Columns 12 through 15
151
LOW PASS FIR FILTER USING RECTANGULAR WINDOW:
MAGNITUDE SPECTRA:
152
FREQUENCY RESPONSE:
153
MATLAB CODE:
clc;
clear all;
close all;
fs=input('Enter sampling frequency');
t=0:1/fs:10;
f1=input('Enter the first frequency of input signal');
f2=input('Enter the second frequency of input signal');
f3=input('Enter the third frequency of input signal');
x=sin(2*pi*f1*t)+sin(2*pi*f2*t)+sin(2*pi*f3*t);
disp('1.Blackman 2.Hanning 3.Hamming 4.Rectangular');
in=input('Enter choice:');
switch in
case 1
154
title('Magnitude spectrum of filtered signal');
figure();
freqz(b);
case 2
case 3
155
if(rem(N,2)~=0)
N1=N;
N=N-1;
end
y=hamming(N1);
wp=input('Enter cutoff frequency');
wc=(2.*wp)/fs;
b=fir1(N,wc,'high',y);
w=0:(pi/100):pi;
X=fft(x);
z=filter(b,1,x);
disp(b);
Z=fft(z);
m1=abs(X);
m2=abs(Z);
subplot(121);
w0=[(0:length(m1)-1)/length(m1)-1]*fs;
stem(w0,m1);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of input signal');
subplot(122);
w3=[(0:length(m2)-1)/length(m2)-1]*fs;
stem(w3,m2);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of filtered signal');
figure();
freqz(b);
case 4
N=input('Enter the order of the filter:');
N1=N+1;
if(rem(N,2)~=0)
N1=N;
N=N-1;
end
y=rectwin(N1);
wp=input('Enter cutoff frequency');
wc=(2.*wp)/fs;
b=fir1(N,wc,'high',y);
w=0:(pi/100):pi;
X=fft(x);
z=filter(b,1,x);
disp(b);
156
Z=fft(z);
m1=abs(X);
m2=abs(Z);
subplot(121);
w0=[(0:length(m1)-1)/length(m1)-1]*fs;
stem(w0,m1);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of input signal');
subplot(122);
w3=[(0:length(m2)-1)/length(m2)-1]*fs;
stem(w3,m2);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of filtered signal');
figure();
freqz(b);
end
OUTPUT:
Enter choice: 1
Columns 12 through 15
157
HIGH PASS FIR FILTER USING BLACKMAN WINDOW:
MAGNITUDE SPECTRA:
158
FREQUENCY RESPONSE:
159
OUTPUT:
-0.0010 -0.0074 0.0000 0.0379 0.0431 -0.0799 -0.2914 0.6003 -0.2914 -0.0799
0.0431
Columns 12 through 15
160
HIGH PASS FIR FILTER USING HANNING WINDOW:
MAGNITUDE SPECTRA:
161
FREQUENCY RESPONSE:
162
OUTPUT:
-0.0021 -0.0063 0.0000 0.0331 0.0400 -0.0773 -0.2887 0.5995 -0.2887 -0.0773
0.0400
Columns 12 through 15
163
HIGH PASS FILTER USING HAMMING WINDOW:
MAGNITUDE SPECTRA:
164
FREQUENCY RESPONSE:
165
OUTPUT:
-0.0268 -0.0506 0.0000 0.0759 0.0625 -0.0938 -0.3035 0.6015 -0.3035 -0.0938
0.0625
Columns 12 through 15
166
HIGH PASS FI FILTER USING RECTANGULAR WINDOW:
MAGNITUDE SPECTRUM:
167
FREQUENCY RESPONSE:
168
MATLAB CODE:
clc;
clear all;
close all;
fs=input('Enter sampling frequency');
t=0:1/fs:10;
f1=input('Enter the first frequency of input signal');
f2=input('Enter the second frequency of input signal');
f3=input('Enter the third frequency of input signal');
x=sin(2*pi*f1*t)+sin(2*pi*f2*t)+sin(2*pi*f3*t);
disp('1.Blackman 2.Hanning 3.Hamming
4.Rectangular');
in=input('Enter choice:');
switch in
case 1
169
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of filtered signal');
figure();
freqz(b);
case 2
170
if(rem(N,2)~=0)
N1=N;
N=N-1;
end
y=hamming(N1);
wp1=input('Enter first cutoff frequency');
wp2=input('Enter Second cutoff frequency')
wc=[(2.*wp1)/fs (2.*wp2)/fs];
b=fir1(N,wc,'bandpass',y);
w=0:(pi/100):pi;
X=fft(x);
z=filter(b,1,x);
disp(b);
Z=fft(z);
m1=abs(X);
m2=abs(Z);
subplot(121);
w0=[(0:length(m1)-1)/length(m1)-1]*fs;
stem(w0,m1);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of input signal');
subplot(122);
w3=[(0:length(m2)-1)/length(m2)-1]*fs;
stem(w3,m2);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of filtered signal');
figure();
freqz(b);
171
z=filter(b,1,x);
disp(b);
Z=fft(z);
m1=abs(X);
m2=abs(Z);
subplot(121);
w0=[(0:length(m1)-1)/length(m1)-1]*fs;
stem(w0,m1);
xlabel('Frequency'); ylabel('Gain(dB)');
title('Magnitude spectrum of input signal');
subplot(122);
w3=[(0:length(m2)-1)/length(m2)-1]*fs;
stem(w3,m2);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of filtered signal');
figure();
freqz(b);
end
OUTPUT:
wp2 =
3000
Columns 1 through 11
Columns 12 through 15
172
BAND PASS FIR FILTER USING BLACKMAN WINDOW:
MAGNITUDE SPECTRUM:
173
FREQUENCY RESPONSE:
174
OUTPUT:
Enter choice: 2
wp2 =
3000
Columns 1 through 11
-0.0000 -0.0211 0.0000 0.1081 -0.0000 -0.2280 -0.0000 0.2856 -0.0000 -0.2280
-0.0000
Columns 12 through 15
175
BAND PASS FIR FILTER USING HANNING WINDOW:
MAGNITUDE SPECTRUM:
176
FREQUENCY RESPONSE:
177
OUTPUT:
wp2 =
178
3000
Columns 1 through 11
-0.0000 -0.0190 0.0000 0.0993 -0.0000 -0.2319 -0.0000 0.2998 -0.0000 -0.2319
-0.0000
Columns 12 through 15
MAGNITUDE SPECTRUM:
179
FREQUENCY RESPONSE:
180
OUTPUT:
181
BAND PASS FIR FILTER USING RECTANGULAR WINDOW:
wp2 =
3000
Columns 1 through 11
-0.0000 -0.0935 0.0000 0.1403 -0.0000 -0.1734 -0.0000 0.1854 -0.0000 -0.1734
-0.0000
Columns 12 through 15
182
MAGNITUDE SPECTRUM:
FREQUENCY RESPONSE:
183
MATLAB CODE:
clc;
184
clear all;
close all;
fs=input('Enter sampling frequency');
t=0:1/fs:10;
f1=input('Enter the first frequency of input signal');
f2=input('Enter the second frequency of input signal');
f3=input('Enter the third frequency of input signal');
x=sin(2*pi*f1*t)+sin(2*pi*f2*t)+sin(2*pi*f3*t);
disp('1.Blackman 2.Hanning 3.Hamming 4.Rectangular');
in=input('Enter choice:');
switch in
case 1
case 2
case 3
186
N=N-1;
end
y=hamming(N1);
wp1=input('Enter first cutoff frequency');
wp2=input('Enter Second cutoff frequency')
wc=[(2.*wp1)/fs (2.*wp2)/fs];
b=fir1(N,wc,'stop',y);
w=0:(pi/100):pi;
X=fft(x);
z=filter(b,1,x);
disp(b);
Z=fft(z);
m1=abs(X);
m2=abs(Z);
subplot(121);
w0=[(0:length(m1)-1)/length(m1)-1]*fs;
stem(w0,m1);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of input signal');
subplot(122);
w3=[(0:length(m2)-1)/length(m2)-1]*fs;
stem(w3,m2);
xlabel('Frequency');
ylabel('Gain(dB)');
title('Magnitude spectrum of filtered signal');
figure();
freqz(b);
case 4
187
disp(b);
Z=fft(z);
m1=abs(X);
m2=abs(Z);
subplot(121);
w0=[(0:length(m1)-1)/length(m1)-1]*fs;
stem(w0,m1);
xlabel('Frequency'); ylabel('Gain(dB)');
title('Magnitude spectrum of input signal');
subplot(122);
w3=[(0:length(m2)-1)/length(m2)-1]*fs;
stem(w3,m2);
xlabel('Frequency'); ylabel('Gain(dB)');
title('Magnitude spectrum of filtered signal');
figure();
freqz(b);
end
OUTPUT:
Enter choice: 1
Columns 1 through 11
Columns 12 through 15
188
-0.0358 0.0000 0.0020 0
MAGNITUDE SPECTRA:
FREQUENCY RESPONSE:
189
OUTPUT:
190
BAND STOP FIR FILTER USING HANNING WINDOW:
Enter choice: 2
Enter the order of the filter: 15
Enter first cutoff frequency 2000
Enter Second cutoff frequency 3000
wp2 =
3000
Columns 1 through 11
0.0000 0.0148 0.0000 -0.0759 0.0000 0.1601 0.0000 0.8019 0.0000 0.1601
0.0000
Columns 12 through 15
191
MAGNITUDE SPECTRUM:
FREQUENCY RESPONSE:
192
OUTPUT:
193
BAND STOP FIR FILTER USING HAMMING WINDOW:
Enter choice:3
Enter the order of the filter: 15
Enter first cutoff frequency 2000
Enter Second cutoff frequency 3000
wp2 =
3000
Columns 1 through 11
0.0000 0.0126 0.0000 -0.0661 0.0000 0.1543 0.0000 0.7982 0.0000 0.1543
0.0000
Columns 12 through 15
194
MAGNITUDE SPECTRUM:
FREQUENCY RESPONSE:
195
OUTPUT:
196
BAND STOP FIR FILTER USING RECTANGULAR WINDOW:
Enter choice:4
Enter the order of the filter: 15
Enter first cutoff frequency 2000
Enter Second cutoff frequency 3000
wp2 =
3000
Columns 1 through 11
0.0000 0.0940 0.0000 -0.1410 0.0000 0.1743 0.0000 0.7454 0.0000 0.1743
0.0000
Columns 12 through 15
197
MAGNITUDE SPECTRUM:
FREQUENCY RESPONSE:
198
MODEL CALCULATION:
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
MODEL GRAPH:
229
230
231
232
233
RESULT:
Thus the implementation low pass FIR, high pass FIR, band pass FIR
and band stop FIR filters are performed using Blackman window, Hanning
window, Hamming window and Rectangular window and their output is
viewed using matlab code.
234
EXP : 11 STUDY OF DSP PROCESSORS
AIM:
To study architecture, overview of DSP processors TMS320C5X and
TMS320C4X.
ARCHITECTURE OF TMS320C54X:
The ’54x DSPs use an advanced, modified Harvard architecture that maximizes
processing power by maintaining one program memory bus and three data
memory buses. These processors also provide an arithmetic logic unit (ALU)
that has a high degree of parallelism, application-specific hardware logic, on-
chip memory, and additional on-chip peripherals. These DSP families also
provide a highly specialized instruction set, which is the basis of the operational
flexibility and speed of these DSPs. Separate program and data spaces allow
simultaneous access to program instructions and data, providing the high degree
of parallelism. Two reads and one write operation can be performed in a single
cycle. Instructions with parallel store and application-specific instructions can
fully utilize this architecture. In addition, data can be transferred between data
and program spaces. Such parallelism supports a powerful set of arithmetic,
logic, and bit-manipulation operations that can all be performed in a single
machine cycle. Also included are the control mechanisms to manage interrupts,
repeated operations, and function calls.
235
INTERNAL MEMORY ORGANISATION
The ¢54x memory is organised into three individually selectable spaces:
program, data and I/O space. All ¢54X devices contain both random
access memory (RAM) and read only memory (ROM). Among the
devices, two types of RAM are represented: dual-access RAM
(DARAM) and single-access RAM (SARAM). The DARAM and
SARAM may be configured either as data memory or program/data
memory. Table 10.2 shows how much ROM, DARAM and SARAM are
available on the different ¢54X devices. The ¢54X also has 26 CPU
registers plus peripheral registers that are mapped in data memory space.
1. On-ChipROM
The on-chip ROM is part of the program memory space and, in some
cases, part of the data memory space. The amount of on-chip ROM
available on each device varies, as shown in Table 10.2. On devices with
a small amount of ROM (2K words), the ROM contains a boot loader,
which is useful for booting to faster on-chip or external RAM. On
devices with larger amounts of ROM, a portion of the ROM may be
mapped into both data and program space.
2. On-Chip Dual-Access RAM (DARAM)
236
The DARAM is composed of several blocks. Because each DARAM
block can be accessed twice per machine cycle, the CPU can read from
and write to a single block of DARAM in the same cycle. The DARAM
is always mapped in data space and is primarily intended to store data
values. It can also be mapped into program space and used to store
program code.
3. On-Chip Single-Access RAM (SARAM)
The SARAM is composed of several blocks. Each block is accessible
once per machine cycle for either a read or a write. The SARAM is
always mapped in data space and is primarily intended to store data
values. It can also be mapped into program space and used to store
program code.
4. On-Chip Memory Security
The ¢54X maskable memory security option protects the contents
of on-chip memories. When this option is chosen, no externally
originating instruction can access the on-chip memory spaces.
5. Memory-Mapped Registers
The data memory space contains memory-mapped registers for the CPU
and the on-chip peripherals. These registers are located on data page 0,
simplifying access to them. The memory-mapped access provides a
convenient way to save and restore the registers for context switches and
to transfer information between the accumulators and the other registers.
237
The CPU registers are memory-mapped, enabling quick saves and
restores. Table 10.3 gives the list of memory-mapped CPU registers
and their functions are as follows:
Accumulators
The accumulators, ACCA and ACCB, store the output from the ALU or the
multiplier / adder block; the accumulators can also provide a second input to the
ALU or the multiplier / adder. The bits in each accumulator is grouped as
follows: Guard bits (bits 32–39) A high-order word (bits 16–31) A low-order
word (bits 0–15) Instructions are provided for storing the guard bits, the high-
order and the low-order accumulator words in data memory, and for
manipulating 32-bit accumulator words in or out of data memory. Also, any of
the accumulators can be used as temporary storage for the other.
Barrel Shifter
The ’54x’s barrel shifter has a 40-bit input connected to the accumulator or data
memory (CB, DB) and a 40-bit output connected to the ALU or data memory
(EB). The barrel shifter produces a left shift of 0 to 31 bits and a right shift of 0
to 16 bits on the input data. The shift requirements are defined in the shift-count
field (ASM) of ST1 or defined in the temporary register (TREG), which is
designated as a shift-count register. This shifter and the exponent detector
normalize the values in an accumulator in a single cycle. The least significant
bits (LSBs) of the output are filled with 0s and the most significant bits (MSBs)
can be either zero-filled or sign-extended, depending on the state of the sign-
extended mode bit (SXM) of ST1. Additional shift capabilities enable the
processor to perform numerical scaling, bit extraction, extended arithmetic, and
overflow prevention operations.
Multiplier/Adder
The multiplier / adder performs 17 × 17-bit 2s-complement multiplication with
a 40-bit accumulation in a single instruction cycle. The multiplier / adder block
consists of several elements: a multiplier, adder, signed/unsigned input control,
fractional control, a zero detector, a rounder (2s-complement),
overflow/saturation logic, and TREG. The multiplier has two inputs: one input
is selected from the TREG, a data-memory operand, or an accumulator; the
other is selected from the program memory, the data memory, an accumulator,
238
or an immediate value. The fast on-chip multiplier allows the ’54x to perform
operations such as convolution, correlation, and filtering efficiently. In addition,
the multiplier and ALU together execute multiply/accumulate (MAC)
computations and ALU operations in parallel in a single instruction cycle. This
function is used in determining the Euclid distance, and in implementing
symmetrical and least mean square (LMS) filters, which are required for
complex DSP algorithms.
Program control
Program control is provided by several hardware and software mechanisms:
The program controller decodes instructions, manages the pipeline, stores the
status of operations, and decodes conditional operations. Some of the hardware
elements included in the program controller are the program counter, the status
and control register, the stack, and the address-generation logic.
Some of the software mechanisms used for program control include branches,
calls, conditional instructions, a repeat instruction, reset, and interrupts.
The ’54x supports both the use of hardware and software interrupts for program
control. Interrupt service routines are vectored through a relocatable interrupt
vector table. Interrupts can be globally enabled/disabled and can be individually
masked through the interrupt mask register (IMR). Pending interrupts are
indicated in the interrupt flag register (IFR). For detailed information on the
structure of the interrupt vector table, the IMR and the IFR, see the device-
specific data sheets.
239
units (ARAUs). The primary function of the auxiliary registers is generating 16-
bit addresses for data space. However, these registers also can act as general-
purpose registers or counters.
240
Each ¢54X device has two general-purpose I/O pins: BIO and XF. BIO is an
input pin that can be used to monitor the status of external devices. XF is a
software-controlled output pin that allows you to signal external devices.
Hardware Timer
The ¢54X features a 16-bit timing circuit with a 4-bit prescaler. The timer
counter is decremented by 1 at every CLKOUT cycle. Each time the counter
decrements to 0, a timer interrupt is generated. The timer can be stopped,
restarted, reset or disabled by specific status bits.
Clock Generator
The clock generator consists of an internal oscillator and a phase-locked loop
(PLL) circuit. The clock generator can be driven internally by a crystal
resonator circuit or externally by a clock source. The PLL circuit can generate
an internal CPU clock by multiplying the clock source by a specific factor; thus,
a clock source with a lower frequency than that of the CPU should be used.
Serial Ports
The serial ports on the ¢54X vary by device, but four types of serial ports are
represented: synchronous, buffered, multichannel buffer (McBSP) and time-
241
division multiplexed (TDM). Table 10.7 gives the number of each type of serial
ports on the various ¢54X devices.
ARCHITECTURE OF TMS320C6X
242
The TMS320C6X DSPs use the VelociTITM architecture, the fi rst DSPs to use
advanced VLIW (Very Large Instruction Word) architecture to achieve high
performance through increased instruction parallelism. This makes the ¢C6X
DSPs an excellent choice for multichannel and multifunction applications.
Internal Architecture
The block diagram of TMS320C6X devices is given in Fig. 13.1. The ¢C6X
devices contains 32-bit CPU, on-chip program, data memory and on-chip
peripherals. The on-chip memory has cache either for program space or for both
program and data space. The ¢C6X devices have peripherals such as external
memory interface (EMIF), direct memory access controller (DMA), timers,
multi-channel buffered serial ports (McBSP), host port interface (HPI) and
power down logic.
CPU
The central processing unit of ¢C6X device is 32-bit size. The block diagram of
¢C6X CPU is given in Fig. 13.2. The CPU contains the following units:
a. Program fetch unit
b. Instruction dispatch unit
c. Instruction decode unit
d. Two data paths, each data path consists of four functional units
e. Register fi le for each data path
f. control registers
g. Control logic
h. Test, emulation and interrupt logic
The instruction dispatch unit receives the fetch packet and splits it into execute
packets. The instructions in the execute packet (eight instructions) are assigned
243
to the appropriate eight functional units in the data path. During the instruction
decode, the source registers, destination registers and associated paths are
decoded for the execution of the instructions in the functional units. Finally the
instructions are executed by the functional units.
Internal Memory
The internal memory confi guration varies between the different ¢C6X
processors. The TMS320C620X/ TMS320C670X family processors have
separate on-chip program and data memories. The internal program memory
can be accessed by the CPU or it can be operated as program cache. The size of
internal program memory is 64 K bytes of RAM and it can accommodate 16K
32-bit instructions. The CPU accesses this program memory space through
program memory controller. The program memory controller performs CPU
and DMA (Direct Memory Access) requests to internal program memory,
performs CPU requests to external memory through external memory interface
(EMIF) circuit and also manages the internal program memory when it is confi
gured as cache. The size of internal data memory is 64 K bytes of RAM. Both
CPU and DMA controller can access this data memory space through data
memory controller. The data memory controller connects CPU to external
memory and on-chip peripherals through EMIF and peripheral bus controller
respectively. The ¢C6202 processor has 2x128 K bytes of internal program
memory blocks out of which one 128K bytes block can be used as program
cache. The ¢C621X/¢C671X family processors have cache-based internal
memory architectures. They are provided with two level memory architecture
for internal program and data busses. The fi rst level of internal memory is with
separate level-one program (L1P) cache and data cache (L1D) each of size 4K
bytes. The program and data cache spaces are not included in the memory map
and are enabled at all times. The level-one cache memories are accessible only
by the CPU. The program cache controller interfaces the CPU to the L1P cache.
A 256 wide path is provided from to the CPU to allow a continuous stream of
eight 32-bit instructions for maximum performance. The 4K L1P cache is
organized as a 64 line direct mapped cache with a 64 byte line size. The data
cache controller provides interface between the CPU and L1D cache. The L1D
is a dualported memory. This allows simultaneous access by both paths of the
CPU (Path A and B). The L1D, 4K cache is organized as a 64 set 2-way set
associative cache with a 32 byte line size. The second level of internal memory
is 64K bytes of RAM that is shared by both program and data memory space
with L2 cache controller. The internal memories and bus connections between
the CPU and various controllers are shown in Fig. 15.7. First the L1P and L1D
caches are accessed, on a miss to either L1D or L1P; the request is passed to L2
controller. The L2 controller facilitates the following accesses ∑
244
The CPU and the enhanced direct memory access (EDMA) controller
accesses to the internal memory, and performs the necessary arbitration
The CPU data access to the EMIF
The CPU access to on-chip peripherals
Sends a request to EMIF for an L2 data miss.
ON CHIP Peripherals
Timers
The ¢C6X devices have two 32-bit general purpose timers that are used to time
events, count events, generate pulses, interrupt CPU and send synchronization
event to DMA. The timer operation can be confi gured through three memory
mapped registers namely timer control register, timer period register and timer
counter register. The ¢C6X processor on-chip timer block diagram is given in
Fig. 15.10. The timer control register (TCR) is programmed to select the
different modes of operation of timer; the timer period register contains the
number of timer input clock cycle to count and the timer counter register
increments when it is enabled to count. The timer counter register resets to 0
when the count reaches the count value in the period register.
Expansion Bus
The expansion bus is available only in ¢C6202 processor. The expansion bus is
32-bit wide bus that is used to interface different types of asynchronous
peripherals, asynchronous and synchronous FIFOs, PCI bridge chips and other
external masters. The expansion bus offers a fl exible bus arbitration scheme.
245
DMA. It also has multichannel capability compatible with the T1, E1, and
MVIP standards. The MCSP provides:
Full-duplex communication
Double-buffered data registers
Direct interface to other devices
Clock generation or an internal programmable frequency shift
clock Multichannel transmit and receive
Power-down Logic
In CMOS logic circuits, power dissipation can be reduced by decreasing the
switching from one logic state to another. By preventing some or all of the
chip’s logic from switching, signifi cant power can be reduced without losing
the data or operational context. PD1, PD2 and PD3 are three power-down
modes available to perform this function. The PD1 mode blocks the internal
clock inputs at the boundary of the CPU, preventing most of its logic from
switching. PD1 effectively shuts down the CPU. The PD2 mode halts the entire
on-chip clock structure at the output of the PLL. The PD3 mode is like PD2
mode but also disconnects the external clock source (CLKIN) from reaching
PLL. In addition to these powerdown modes, the IDLE instruction provides low
CPU power consumption by executing continuous NOPs. The IDLE instruction
terminates only upon servicing an interrupt.
246
247
248
RESULT:
Thus the architecture, overview of DSP processors TMS320C5X and
TMS320C4X is studied.
249
EXP : 12 MAC OPERATION USING VARIOUSADDRESSING MODES
AIM:
To Study the various addressing modes of TMS320C5416 XX DSP processor.
THEORY:
Addressing Modes The TMS320C67XX DSP supports three types of
addressing
modes that enable flexible access to data memory, to memory-mapped
registers, to register
bits, and to I/O space:
The absolute addressing mode allows you to reference a location by supplying
all or
part of an address as a constant in an instruction.
The direct addressing mode allows you to reference a location using an address
offset. The indirect addressing mode allows you to reference a location using a
pointer.
Each addressing mode provides one or more types of operands. An instruction
that
supports an addressing-mode operand has one of the following syntax elements
listed below.
Baddr
When an instruction contains Baddr, that instruction can access one or two bits
in an
accumulator (AC0–AC3), an auxiliary register (AR0–AR7), or a temporary register
(T0–T3).
Only the register bit test/set/clear/complement instructions support Baddr. As
you write one
of these instructions, replace Baddr with a compatible operand.
250
Cmem
When an instruction contains Cmem, that instruction can access a single word
(16
bits)of data from data memory. As you write the instruction, replace Cmem with
a compatible
operand.
Lmem
When an instruction contains Lmem, that instruction can access a long word (32
bits)
of data from data memory or from a memory-mapped registers. As you write
the instruction,
replace Lmem with a compatible operand.
Smem
When an instruction contains Smem, that instruction can access a single word
(16
bits) of data from data memory, from I/O space, or from a memory-mapped
register. As you
write the instruction, replace Smem with a compatible operand.
Xmem and Ymem
When an instruction contains Xmem and Ymem, that instruction can perform
two
simultaneous 16-bit accesses to data memory. As you write the instruction,
replace Xmem
and Ymem with compatible operands.
Absolute Addressing Modes k16 absolute
This mode uses the 7-bit register called DPH (high part of the extended data
page
251
register) and a 16-bit unsigned constant to form a 23-bit data space address.
This mode is
used to access a memory location or a memory-mapped register.
k23 absolute
This mode enables you to specify a full address as a 23-bit unsigned constant.
This
mode is used to access a memory location or a memory-mapped register.
I/O absolute
This mode enables you to specify an I/O address as a 16-bit unsigned constant.
This
mode is used to access a location in I/O space.
Direct Addressing Modes DP direct
This mode uses the main data page specified by DPH (high part of the extended
data
page register) in conjunction with the data page register (DP).This mode is used
to access a
memory location or a memory-mapped register.
SP direct
This mode uses the main data page specified by SPH (high part of the extended
stack
pointers) in conjunction with the data stack pointer (SP). This mode is used to
access stack
values in data memory.
Register-bit direct
This mode uses an offset to specify a bit address. This mode is used to access
one
register bit or two adjacent register bits.
PDP direct
252
This mode uses the peripheral data page register (PDP) and an offset to specify
an I/O
address. This mode is used to access a location in I/O space. The DP direct and
SP direct
addressing modes are mutually exclusive. The mode selected depends on the
CPL bit in
status register ST1_67: 0 DP direct addressing mode 1 SP direct addressing mode
The register-
bit and PDP direct addressing modes are independent of the CPL bit.
Indirect Addressing Modes
You may use these modes for linear addressing or circular addressing.
AR indirect
This mode uses one of eight auxiliary registers (AR0–AR7) to point to data. The
way
the CPU uses the auxiliary register to generate an address depends on whether
you are
accessing data space (memory or memory-mapped registers), individual register
bits, or I/O
space.
Dual AR indirect
This mode uses the same address-generation process as the AR indirect
addressing
mode. This mode is used with instructions that access two or more data-memory
locations.
CDP indirect
This mode uses the coefficient data pointer (CDP) to point to data. The way the
CPU
uses CDP to generate an address depends on whether you are accessing data
space (memory
253
or memory-mapped registers), individual register bits, or I/O space.
Coefficient indirect
This mode uses the same address-generation process as the CDP indirect
addressing
mode. This mode is available to support instructions that can access a coefficient
in data
memory at the same time they access two other data-memory values using the
dual AR
indirect addressing mode.
Circular Addressing
Circular addressing can be used with any of the indirect addressing modes. Each
of
the eight auxiliary registers (AR0–AR7) and the coefficient data pointer (CDP)
can be
independently configured to be linearly or circularly modified as they act as
pointers to data
or to register bits, see Table 3−10. This configuration is done with a bit (ARnLC)
in status
register ST2_67. To choose circular modification, set the bit. Each auxiliary
register ARn has
its own linear/circular configuration bit in ST2_67: 0 Linear addressing 1 Circular
addressing
The CDPLC bit in status register ST2_67 configures the DSP to use CDP for linear
addressing or circular addressing: 0 Linear addressing 1 Circular addressing You
can use the
circular addressing instruction qualifier, .CR, if you want every pointer used by
the
instruction to be modified circularly, just add .CR to the end of the instruction
mnemonic (for
254
example, ADD.CR). The circular addressing instruction qualifier overrides the
linear/circular
configuration in ST2_67.
EXAMPLES:
ADDITION:
INP1 .SET 0H
INP2 .SET 1H
OUT .SET 2H
.mmregs
.text
START:
LD #140H,DP
RSBX CPL
NOP
NOP
NOP
NOP
LD INP1,A
ADD INP2,A
STL A,OUT
HLT: B HLT
INPUT:
255
OUTPUT:
Data Memory:
A000h 0004h
A001h 0004h
Data Memory:
A002h 0008h
SUBTRACTION:
INP1 .SET 0H
INP2 .SET 1H
OUT .SET 2H
.mmregs
.text
START:
LD #140H,DP
RSBX CPL
NOP
NOP
NOP
NOP
LD INP1,A
SUB INP2,A
256
STL A,OUT
HLT: B HLT
INPUT:
Data Memory:
A000h 0004h
A001h 0002h
OUTPUT:
Data Memory:
A002h 0002h
MULTIPLICATION
.mmregs
.text
START:
STM #0140H,ST0
STM #40H,PMST
STM #0A000H,AR0
ST #1H,*AR0
LD *AR0+,T
ST #2H,*AR0
MPY *AR0+,A
STL A,*AR0
HLT: B HLT
.END
257
OUTPUT
A002H 2H
DIVISION
DIVID .SET 0H
DIVIS .SET 1H
OUT .SET 2H
.mmregs
.text
START:
STM #140H,ST0
RSBX CPL
RSBX FRCT
NOP
NOP
NOP
NOP
LD DIVID,A ;dividend to acc
RPT #0FH
SUBC DIVIS,A ;A / DIVIS -> A
STL A,OUT ;result in 9002h
HLT: B HLT
.END
258
INPUT
DATA MEMORY
A000H 000AH
A001H 0002H
OUTPUT
A002H 0005H
RESULT:
Thus, the various addressing mode of DSP processor TMS320C5416XX
was studied
259
260