DSP Lab Manual 2024-25
DSP Lab Manual 2024-25
a) Signal Addition
clc;
n1=-4:1:4
a=[1 2 3 4 3 2 0 -3 -1]
n2=-2:1:3
b=[-2 1 0 4 6 -5]
subplot(2,2,1)
stem(n1,a)
subplot(2,2,2)
stem(n2,b)
u=min(min(n1),min(n2))
v=max(max(n1),max(n2))
w=u:1:v
z1=[]
temp=1;
for i=1:length(w)
if (w(i)< min(n1)||w(i)>max(n1));
z1=[z1 0];
else
z1=[z1 a(temp)]
temp=temp+1;
end
end
z2=[]
temp=1;
for i=1:length(w)
if (w(i)< min(n2)||w(i)>max(n2))
z2=[z2 0]
else
z2=[z2 b(temp)]
temp=temp+1;
end
end
z=z1+z2
subplot(2,2,3)
stem(w,z)
Out Put
b) Signal Multiplication
clc;
n1=-4:1:4
a=[1 2 3 4 3 2 0 -3 -1]
n2=-2:1:3
b=[-2 1 0 4 6 -5]
subplot(2,2,1)
stem(n1,a)
subplot(2,2,2)
stem(n2,b)
u=min(min(n1),min(n2))
v=max(max(n1),max(n2))
w=u:1:v
z1=[]
temp=1;
for i=1:length(w)
if (w(i)< min(n1)||w(i)>max(n1));
z1=[z1 0];
else
z1=[z1 a(temp)]
temp=temp+1;
end
end
z2=[]
temp=1;
for i=1:length(w)
if (w(i)< min(n2)||w(i)>max(n2))
z2=[z2 0]
else
z2=[z2 b(temp)]
temp=temp+1;
end
end
z=z1.*z2
subplot(2,2,3)
stem(w,z)
%folding Sequence
clc;
n=-4:1:4
x=[-1 2 3 1 -2 5 2 -2 5]
y=fliplr(x)
subplot(2,3,1)
stem(n,x)
subplot(2,3,2)
stem(n,y)
Output
n = -4 -3 -2 -1 0 1 2 3 4
x = -1 2 3 1 -2 5 2 -2 5
y= 5 -2 2 5 -2 1 3 2 -1
c) Amplitude Scaling
clc;
clear all;
close all;
n=-3:1:3
x=[1 -1 2 3 -2 5 -1]
a=2
x2=a.*x
subplot(2,2,1)
stem(n,x)
xlabel('n')
ylabel('amplitude')
title('original sequence')
subplot(2,2,2)
stem(n,x2)
xlabel('n')
ylabel('amplitude')
title('amplitude scaled sequence')
Output:
d) Time Shifting
clc;
n1=-2:1:3
x=[3 2 4 5 -1 3]
m=n1-2
subplot(2,2,1)
stem(n1,x)
subplot(2,2,2)
stem(m,x)
%Time scaling
a=2.5
scal_x=a.*x
subplot(2,2,3)
stem(n1,scal_x)
Output
n1 =-2 -1 0 1 2 3
x = 3 2 4 5 -1 3
m = -4 -3 -2 -1 0 1
a = 2.5000
scal_x = 7.5000 5.0000 10.0000 12.5000 -2.5000 7.5000
e) Floding
clc;
n=-2:1:2
x=[1 0 -1 2 3]
subplot(1,2,1)
stem(n,x)
xlabel('n')
ylabel('x(n)')
title('original sequence')
y=fliplr(x)
subplot(1,2,2)
stem(n,y)
xlabel('n')
ylabel('y(n)')
title('flipped sequence')
Output
Experiment No:3 Program to perform convolution of two given sequences
(without using built-in function) and display the signals.
clc;
clear all;
close all;
x1 = input('Enter Input Sequence for Signal x1(n): ');
n1 = length(x1);
x2 = input('Enter Input Sequence for Signal x2(n): ');
n2=length(x2);
N = n1+n2-1; %Length of Convolved Sequence
%Zero padding to make sequences of length N
x1=[x1 zeros(1,N-n1)];
x2=[x2 zeros(1,N-n2)];
%Initializing Output sequence of zeros.
y = zeros(1,N);
for n = 1:N
for k = 1:n
y(n)=y(n)+x1(k)*x2(n-k+1);
end
end
subplot(3,1,1);
stem(x1);
title('Input Signal x1(n)');
xlabel('n');
ylabel('x1(n)');
title(‘First Sequence’)
subplot(3,1,2);
stem(x2);
title('Input Signal x2(n)');
xlabel('n');
ylabel('x2(n)');
title(‘Second Sequence’)
subplot(3,1,3);
stem(y);
title('Convolved Signal y(n) = x1(n)*x2(n)');
xlabel('n');
ylabel('y(n)');
title(‘Convolved Sequence’)
disp('Convolved sequence:'); disp(y);
Output:
Enter Input Sequence for Signal x1(n): [1 2 1]
Enter Input Sequence for Signal x2(n): [1 2 3 4]
Convolved sequence:
1 4 8 12 11 4
Experiment No: 4
Computation of N Point DFT without using built in function
clc;
clear all;
close all;
x=input('enter the first sequence')
subplot(2,2,1)
stem(x)
xlabel('n')
ylabel('x(n)')
title('Input sequence')
N=input('enter the length')
n=0:N-1
for k=0:N-1
w=exp(-j*2*pi*k*n/N)
X(k+1)=sum(x.*w)
end
subplot(2,2,2)
stem(X)
xlabel('n')
ylabel('X(k)')
title('DFT of the input sequence')
% To plot magnitude
subplot(2,2,3)
stem(n,abs(X))
disp('the magnitude of X(k) is')
disp(abs(X))
% To plot phase spectrum
subplot(2,2,4)
stem(n,angle(X))
disp('the phase spectrum of X(k)is')
disp(angle(X))
Output:
enter the first sequence[1 2 1 0]
x=1210
enter the length4
N=4
X = 4.0000 + 0.0000i 0.0000 - 2.0000i 0.0000 + 0.0000i -0.0000 + 2.0000i
the magnitude of X(k) is
4.0000 2.0000 0 2.0000
the phase spectrum of X(k)is
0 -1.5708 0 1.5708
Experiment No:5
clc;
clear all;
close all;
x=input('enter the first sequence')
subplot(2,2,1)
stem(x)
xlabel('n')
ylabel('x(n)')
title('first sequence')
h=input('enter the second sequence')
subplot(2,2,2)
stem(h)
xlabel('n')
ylabel('h(n)')
title('second sequence')
L1=length(x)
L2=length(h)
L=L1+L2-1
x=[x zeros(1,L-L1)]
h=[h zeros(1,L-L2)]
X=fft(x)
H=fft(h)
Y=X.*H
y=ifft(Y)
subplot(2,2,3)
stem(y)
xlabel('n')
ylabel('y(n)')
title('Linear convolution sequence')
Output
enter the first sequence[1 2 3 4]
x=1234
enter the second sequence[1 2]
h=12
y = 1 4 7 10 8
Experiment No: 6
a) Linearity Property
clc;
clear all;
close all;
a=2;
b=1;
x1=input('enter the first sequence')
x2=input('enter the second sequence')
lhs=fft(a*x1+b*x2)
X1=fft(x1)
X2=fft(x2)
rhs=(a*X1+b*X2)
if lhs==rhs
disp('Linearity property is proved')
else
disp('Linearity is not proved')
end
Output
enter the first sequence[1 2 3 4]
x1 = 1 2 3 4
enter the second sequence[1 2 1 0]
x2 = 1 2 1 0
lhs = 24.0000 + 0.0000i -4.0000 + 2.0000i -4.0000 + 0.0000i -4.0000 - 2.0000i
X1 =10.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 - 2.0000i
X2 = 4.0000 + 0.0000i 0.0000 - 2.0000i 0.0000 + 0.0000i 0.0000 + 2.0000i
rhs =24.0000 + 0.0000i -4.0000 + 2.0000i -4.0000 + 0.0000i -4.0000 - 2.0000i
Output
X1=fft(x)
rhs=circshift(X1,l)
LHS1=round(lhs)
RHS1=round(rhs)
if LHS1==RHS1
disp('Circular frequency shift property is proved')
else
disp('Circular frequency shift property is not proved')
end
Output
Design and implementation of digital low pass FIR filter using a window to
meet the given specifications
clc;
clear all;
close all;
n=20;
fp=200;
fq=300;
fs=1000;
fn=2*fp/fs;
window=blackman(n+1);
b=fir1(n,fn,window);
[H W]=freqz(b,1,128);
subplot(2,1,1);
plot(W/pi,abs(H));
title('magnitude response of lpf');
ylabel('gain in db-------->');
xlabel('normalized frequency------>');
subplot(2,1,2);
plot(W/pi,angle(H));
title('phase response of lpf');
ylabel('angle-------->');
xlabel('normalized frequency------>');
Output:
Experiment No: 8
Design and implementation of digital high pass FIR filter using a window to
meet the given specifications
clc;
clear all;
close all;
n=20;
fp=300;
fq=200;
fs=1000;
fn=2*fp/fs;
window=blackman(n+1);
b=fir1(n,fn,'high',window);
[H W]=freqz(b,1,128);
subplot(2,1,1);
plot(W/pi,abs(H));
title('mag res of lpf');
ylabel('gain in db-------->');
xlabel('normalized frequency------>');
subplot(2,1,2);
plot(W/pi,angle(H));
title('phase res of lpf');
ylabel('angle-------->');
xlabel('normalized frequency------>');
OUTPUT
Experiment No: 9
Design and implementation of digital IIR Butterworth low pass filter to meet
the given specifications
clc;
clear all;
close all;
disp('enter the IIR filter design specifications');
rp=input('enter the passband ripple:');
rs=input('enter the stopband ripple:');
wp=input('enter the passband freq:');
ws=input('enter the stopband freq:');
fs=input('enter the sampling freq:');
w1=2*wp/fs;w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs,'s');
disp('Frequency response of IIR LPF is:');
[b,a]=butter(n,wn,'low','s');
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure,subplot(2,1,1);
plot(om/pi,m);
title('magnitude response of IIR filter is:');
xlabel('(a) Normalized freq. -->');
ylabel('Gain in dB-->');
subplot(2,1,2);
plot(om/pi,an);
title('phase response of IIR filter is:');
xlabel('(b) Normalized freq. -->');
ylabel('Phase in radians-->');
Output
Enter the IIR filter design specifications
Enter the passband ripple:15
Enter the stopband ripple:30
Enter the passband freq:300
Enter the stopband freq:500
Enter the sampling freq:1000
Frequency response of IIR LPF is:
:
Experiment No: 10
Design and implementation of digital IIR Butterworth high pass filter to meet
the given specifications
clc;
clear all;
close all;
disp('enter the IIR filter design specifications');
rp=input('enter the passband ripple:');
rs=input('enter the stopband ripple:');
wp=input('enter the passband freq:');
ws=input('enter the stopband freq:');
fs=input('enter the sampling freq:');
w1=2*wp/fs;w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs,'s');
disp('Frequency response of IIR LPF is:');
[b,a]=butter(n,wn,'high','s');
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure,subplot(2,1,1);
plot(om/pi,m);
title('magnitude response of IIR filter is:');
xlabel('(a) Normalized freq. -->');
ylabel('Gain in dB-->');
subplot(2,1,2);
plot(om/pi,an);
title('phase response of IIR filter is:');
xlabel('(b) Normalized freq. -->');
ylabel('Phase in radians-->');
Output
Enter the IIR filter design specifications
Enter the passband ripple:15
Enter the stopband ripple:30
Enter the passband freq:300
Enter the stopband freq:500
Enter the sampling freq:1000
Frequency response of IIR LPF is: