Subject : Digital Signal Processing
(ETEC-356)
Faculty Name : Mrs. Kanika Aggarwal Student name: Gursharan Kaur
Roll No.: 44614802818
Semester: 6th
Group:E8
Maharaja Agrasen Institute Of Technology
Plot No 1 Rohini, Sector 22, PSP Area, Delhi, 110086
INDEX
Sno. Experiment Name Date Of Date Of Remarks
Experiment Submission
1. Generation of basic signals sine,
cosine, ramp, step, impulse and
exponential in continuous and discrete
domains using user defined functions.
2. To find linear and circular
convolution.
3. To find linear and circular cross
convolution.
4. Perform linear convolution using
circular convolution and vice versa.
5. To find N point DFT and plot its
magnitude and phase spectra.
6. Perform the following properties of
DFT-
a) Circular shift of a sequence.
b) Circular fold of a sequence.
7. Design FIR Low pass filter using
a) Rectangular window
b) Hanning window
c) Hamming window
d) Bartlett window
8. Implement a Low pass / High pass /
Band pass / Band stop IIR Filter using
Butterworth approximation.
Experiment – 1
Aim : Generation of basic signals sine, cosine, ramp, step, impulse and exponential in
continuous and discrete domains using user defined functions.
Code:
//Continuous Waveform
clc;
clear all;
close;
function y=ramp(t)
m=length(t);
for i=1:m
if t(i)>=0 then
y(i)=t(i);
else
y(i)=0;
end
end
return
endfunction
function y=impulse(t)
m=length(t);
for i=1:m
if t(i)==0 then
y(i)=1;
else
y(i)=0;
end
end
return
endfunction
function y=unit(t)
m=length(t);
for i=1:m
if t(i)>=0 then
y(i)=1;
else
y(i)=0;
end
end
return
endfunction
function y=cosine(t)
y=0.5*(exp(%i*w*t)+exp(-1*%i*w*t));
return
endfunction
function y=sine(t)
y=0.5*-1*%i*(exp(%i*w*t)-exp(-1*%i*w*t));
return
endfunction
function y=exponential(b, t)
y=exp(b*t)
return
endfunction
t1=-5:0.1:20;
t2=-5:0.001:30;
t3=0:0.1:10;
figure(1);
subplot(2,3,1);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d(t2,impulse(t2));
a.children.children.foreground=5
xtitle('continuous impulse function','time','amplitude');
figure(1);
subplot(2,3,2);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d(t2,unit(t2));
a.children.children.foreground=5
xtitle('continuous unit step function','time','amplitude');
figure(1);
subplot(2,3,3);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d(t2,ramp(t2));
a.children.children.foreground=5
xtitle('continuouse ramp function','time','amplitude');
figure(1);
subplot(2,3,4);
a=gca();
a.x_location="origin";
a.y_location="origin";
w=%pi/10;
plot2d(t2,cosine(t2));
a.children.children.foreground=3
xtitle('continuous cosine function','time','amplitude');
figure(1);
subplot(2,3,5);
a=gca();
a.x_location="origin";
a.y_location="origin";
w=%pi/10;
plot2d(t2,sine(t2));
a.children.children.foreground=3
xtitle('continuous sine function','time','amplitude');
figure(1);
subplot(2,3,6);
a=gca();
a.x_location="origin";
a.y_location="origin";
w=%pi/10;
b=2
plot2d(t3,exponential(b,t3));
a.children.children.foreground=3
xtitle('continuous exponential function','time','amplitude');
Output:
///Discrete waveform
clc;
clear all;
close;
function y=ramp(n)
m=length(n);
for i=1:m
if n(i)>=0 then
y(i)=n(i);
else
y(i)=0;
end
end
return
endfunction
function y=impulse(n)
m=length(n);
for i=1:m
if n(i)==0 then
y(i)=1;
else
y(i)=0;
end
end
return
endfunction
function y=unit(n)
m=length(n);
for i=1:m
if n(i)>=0 then
y(i)=1;
else
y(i)=0;
end
end
return
endfunction
function y=cosine(n)
y=0.5*(exp(%i*w*n)+exp(-1*%i*w*n));
return
endfunction
function y=sine(n)
y=0.5*-1*%i*(exp(%i*w*n)-exp(-1*%i*w*n));
return
endfunction
function y=exponential(b, n)
y=b.^(n)
return
endfunction
n1=-5:0.1:20;
n2=-5:30;
n3=0:10;
figure(1);
subplot(2,3,1);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n2,impulse(n2));
a.children.children.foreground=5
xtitle('discrete impulse function','time','amplitude');
figure(1);
subplot(2,3,2);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n2,unit(n2));
a.children.children.foreground=5
xtitle('discrete unit step function','time','amplitude');
figure(1);
subplot(2,3,3);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n2,ramp(n2));
a.children.children.foreground=5
xtitle('discrete ramp function','time','amplitude');
figure(1);
subplot(2,3,4);
a=gca();
a.x_location="origin";
a.y_location="origin";
w=%pi/10;
plot2d3(n2,cosine(n2));
a.children.children.foreground=3
xtitle('discrete cosine function','time','amplitude');
figure(1);
subplot(2,3,5);
a=gca();
a.x_location="origin";
a.y_location="origin";
w=%pi/10;
plot2d3(n2,sine(n2));
a.children.children.foreground=3
xtitle('discrete sine function','time','amplitude');
figure(1);
subplot(2,3,6);
a=gca();
a.x_location="origin";
a.y_location="origin";
w=%pi/10;
b=2
plot2d3(n3,exponential(b,n3));
a.children.children.foreground=3
xtitle('discrete exponential function','time','amplitude');
Output:
Experiment – 2
Aim : To find linear and circular convolution.
Code:
clc;
clear all;
close;
//linear convolution
x=[1,2,3,4,5]; // First Signal
h=[1,2,1,2]; // Second Signal
nx=length(x);
nh=length(h);
N=nx+nh-1;
n1=0:nx-1;
n2=0:nh-1;
n=0:nx+nh-2;
X_lin=[x,zeros(1,nh-1)];
//append zeros to make length equal to nx+nh-1
H_lin=[h,zeros(1,(nx-1))];
//append zeros to make length equal to nx+nh-1
// Formation of Toeplitz Matrix
for i=1:N
for j=1:N
if (i<j) then
X2_lin(i,j)=0;
else
X2_lin(i,j)=X_lin(i-j+1);
end
end
end
Y_lin=X2_lin*H_lin';// matrix multiplication to obtain linear convolution
disp('The first signal x(n) is ');
disp(x);
disp('The second signal h(n) is ');
disp(h);
disp('The toeplitz matrix of x(n) to calculate linear convolution is ');
disp(X2_lin);
disp('The linear convolution for x(n)and h(n) is ');
disp(Y_lin');
//Plotting of the signals
figure(1);
subplot(2,2,1);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n1,x);
a.children.children.foreground=5;
xtitle('input signal x(n)','n','input');
subplot(2,2,2);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n2,h);
a.children.children.foreground=5;
xtitle('input signal h(n)','n','input');
subplot(2,2,3);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n,Y_lin);
a.children.children.foreground=5;
xtitle('Linearly convolved signal','n','Y(n)');
//Circular Convolution
M=max(nx,nh);
X_circ=[x,zeros(1,M-nx)];
H_circ=[h,zeros(1,M-nh)];
//Formation of toeplitz matrix
for i=1:M
for j=1:M
if (i-j+1)>0 &(i-j+1)<M+1 then
X2_circ(i,j)=X_circ(i-j+1);
elseif (i<j) then
X2_circ(i,j)=X_circ(i-j+1+M)
end
end
end
Y_circ=X2_circ*H_circ';
// Matrix multiplication to obtain circular convolution
disp('The toeplitz matrix of x(n) to calculate Circular convolution is
');
disp(X2_circ);
disp('The Circular convolution for x(n)and h(n) is ');
disp(Y_circ');
//Plotting of the signals
m=0:M-1;
subplot(2,2,4);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(m,Y_circ);
a.children.children.foreground=5;
xtitle('Circilarly convolved signal','n','Y(n)');
Output:
Experiment – 3
Aim : To find linear and circular cross convolution.
Code:
clc;
clear all;
close;
//linear convolution
x=[1,2,3,4,5];// First Signal
y=[1,2,1,2];// Second Signal
y=mtlb_fliplr(y);
nx=length(x);
ny=length(y);
N=nx+ny-1;
n1=0:nx-1;
n2=0:ny-1;
n=0:nx+ny-2;
X_lin=[x,zeros(1,ny-1)];
//append zeros to make length equal to nx+nh-1
Y_lin=[y,zeros(1,(nx-1))];
//append zeros to make length equal to nx+nh-1
// Formation of Toeplitz Matrix
for i=1:N
for j=1:N
if (i<j) then
Y2_lin(i,j)=0;
else
Y2_lin(i,j)=Y_lin(i-j+1);
end
end
end
rxy_lin=Y2_lin*X_lin';
// matrix multiplication to obtain linear correlation
disp('The first signal x(n) is ');
disp(x);
disp('The second signal y(n) is ');
disp(y);
disp('The toeplitz matrix of y(n)to calculate linear correlation is ');
disp(Y2_lin);
disp('The linear correlation for x(n)and y(n) is ');
disp(rxy_lin');
//Plotting of the signals
figure(1);
subplot(2,2,1);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n1,x);
a.children.children.foreground=5;
xtitle('input signal x(n)','n','input');
subplot(2,2,2); a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n2,y);
a.children.children.foreground=5;
xtitle('input signal y(n)','n','input');
subplot(2,2,3);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n,rxy_lin);
a.children.children.foreground=5;
xtitle('Linearly correlated signal','n','rxy(l)');
//Circular Correlation
M=max(nx,ny);
if nx<M then
X_circ=[x,zeros(1,M-nx)];
else
X_circ=x;
end
if ny<M then
Y_circ=[y,zeros(1,M-ny)];
else
Y_circ=y;
end
//Formation of toeplitz matrix
for i=1:M
for j=1:M
if (i-j+1)>0 &(i-j+1)<M+1 then
Y2_circ(i,j)=Y_circ(i-j+1);
elseif (i<j) then
Y2_circ(i,j)=Y_circ(i-j+1+M)
end
end
end
rxy_circ=Y2_circ*X_circ';
// Matrix multiplication to obtain circular correlation
disp('The toeplitz matrix of y(n) to calculate Circular correlation is ');
disp(Y2_circ);
disp('The Circular correlation for x(n)and y(n) is ');
disp(rxy_circ');
//Plotting of the signals
m=0:M-1;
subplot(2,2,4);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(m,Y_circ);
a.children.children.foreground=5;
xtitle('Circilarly convolved signal','n','Y(n)');
Output
Experiment – 4
Aim: To perform linear convolution from circular convolution and vice versa
Code:
clc;
clear all;
close;
//input sequences
x=[1,2,0,3,4,1];
h=[2,0,1,3,1];
//find length of input sequences
nx=length(x);
nh=length(h);
N=nx+nh-1; // Length of linear convolution
M=max(nx,nh); // Length of circular convolution
Y1=conv(x,h);//Linear convolution
//Circular convolution
x_len=[x,zeros(1,M-nx)];
h_len=[h,zeros(1,M-nh)];
DFT_x=fft(x_len);
DFT_h=fft(h_len);
DFT_xh=DFT_x.*DFT_h;
circ_xh=ifft(DFT_xh);
//Circular Convolution from linear convolution
for i=1:M
if (i<=(N-M)) then
Y2 (i)=Y1(i)+Y1(i+M);
else
Y2(i)=Y1(i);
end
end
//linear cnvolution from circular convolution
X=[x,zeros(1,nh-1)];
H=[h,zeros(1,nx-1)];
x_dft=fft(X);
h_dft=fft(H);
xh_dft=x_dft.*h_dft;
Y3= ifft(xh_dft);
//Display various sequences on console window
disp(x,"Input Sequence x");
disp(h,"Input Sequence h");
disp(Y1,"Linearly Convolved sequence using conv function");
disp(circ_xh,"Circularly Convolved sequence using fft
function");
disp(Y3," Linear Convolution from Circular Convolution")
disp(Y2',"Circular Convolution from Linear Convolution")
//Plot various sequences
subplot(2,2,1);
xlabel("Time");
ylabel("Amplitude");
title("Input Sequence x(n)")
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(x);
subplot(2,2,2);
xlabel("Time");
ylabel("Amplitude");
title("Input Sequence h(n)")
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(h);
subplot(2,2,3);
xlabel("Time");
ylabel("Amplitude");
title("Circular Convolution from Linear Convolution")
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(Y2);
subplot(2,2,4);
xlabel("Time");
ylabel("Amplitude");
title("Linear Convolution from Circular Convolution")
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(Y3);
Output:
Experiment – 5
Aim : To find N point DFT and plot its magnitude and phase spectra.
Code:
//To calculate DFT of the sequence:
clear all;
clc;
close;
x=input(" Input sequence x :- ")
nl=input("length of input sequence :- ")
if length(x)<nl then
x=[x zeros(1,nl-length(x))]
end
n=0:nl-1;
k=0:nl-1;
//dft
Xdft= x*exp(-%i*((2*%pi)/nl)* k'*n); //dft formula
Xdft1=fft(x); // direct Command
//Idft
Xidft = (Xdft*exp(%i*((2*%pi)/nl)* k'*n))/nl; //idft formula
Xidft1=ifft(X); /// direct command
figure(1);
//input sequence
subplot(2,2,1);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n,x)
ylabel("x[n]");
xlabel("n");
title("Input sequence");
//DFT sequence 'Xdft'
subplot(2,2,2);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(k,Xdft)
ylabel("Y[k]");
xlabel("k");
title("DFT sequence");
//IDFT sequence 'Xidft'
subplot(2,2,3);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n,Xidft)
ylabel("X[n]");
xlabel("n");
title("IDFT sequence");
disp("Input Sequence x")
disp (x');
disp("DFT Sequence Xdft")
disp ( round(Xdft)');
disp("IDFT Sequence Xidft")
disp ( round(Xidft)');
Output:
Experiment – 6
Aim : Perform the following properties of DFT-
a) Circular shift of a sequence.
b) Circular fold of a sequence.
Code:
//Circular Shift of a sequence
clc;
clear all;
close;
//Circular time shifting property
x=input('Enter the input signal');
l=input('Enter the units of shift');
nx=length(x);
n1=[0:1:nx-1];
n2=pmodulo(n1-l,nx);
y=x(n2+1);
disp('Circularly time shifted signal==>');
disp(y);
Yk=zeros(1,nx);
Xk=fft(x);
for j=0:nx-1
Yk(j+1)=Xk(j+1)*exp((-1*%i*2*%pi*j*l)/nx)
End
disp('DFT of Circularly time shifted signal using the
property==>');
disp(round(Yk));
Yk1=fft(y);
disp('DFT of Circularly time shifted signal is ==>');
disp((Yk1));
Output:
Code:
clc;
clear all;
close;
//Circular time reversal property
x=[1 2 3 4 6 8];
disp('Input signal x(n)==>');
disp(x);
Xk=fft(x);
disp('DFT of x(n)==>');
disp(Xk);
nx=length(x);
n1=[0:1:nx-1];
n2=pmodulo(-n1,nx);
y=x(n2+1);
disp('Circularly time reversed signal x(-n)==>');
disp(y);
//DFT of x(-n) is X(-k)
N=length(x);
k=n1;
k1=pmodulo(-k,N);
Yk=Xk(k1+1);
disp('DFT of Circularly time reversed signal using
property is Yk==>');
disp(Yk);
Yk1=fft(y);
disp('DFT of Circularly time reversed signal is ==>');
disp(Yk1);
Output:
Experiment – 7
Aim : Design FIR Low pass filter using
a) Rectangular window
b) Hanning window
c) Hamming window
d) Bartlett window
Code:
Output:
Experiment – 8
Aim: Implement a Low pass / High pass / Band pass / Band stop IIR Filter
using Butterworth approximation.
Code:
Output: