DSP Lab Programs Word-2
DSP Lab Programs Word-2
DSP Lab Programs Word-2
1. SAMPLING METHOD
Aim: To sample a band limited continuous time signal band limited by different sampling
frequencies.
Program:
clc;
clear all; close
all;
t=0:0.001:0.1;
fin=input('enter input frequency:');
y=2*cos(2*pi*fin*t);
fs=input('enter sampling frequency:');
ts=(1/fs); tx=0:ts:0.1;
ys=2*cos(2*pi*fin*tx);
subplot(2,2,1);
plot(t,y); xlabel('t--
>'); ylabel('amp--
>'); title('input
signal');
subplot(2,2,2);
stem(tx,ys);
xlabel('t-->');
ylabel('amp-->');
title('sampled signal');
x=abs(fft(y,100));
k=0:length(ys)-1;
subplot(2,2,3);
stem(x);
xlabel('freq-->');
ylabel('amp-->');
title('spectrum of input signal');
p=abs(fft(ys,100));
k1=0:length(ys)-1;
subplot(2,2,4);
stem(p);
xlabel('freq-->');
ylabel('amp-->');
title('spectrum of sampled signal');
Page No.
Input:
Output:
Page No.
Input:
Output:
Page No.
Input:
Output:
Result: Thus the experiment to sample a band limited continuous time signal is executed by taking
different sampling frequencies.
Page No.
Program:
clc
clear all;
close all;
n=40;
a=input('enter numerator sequence:');
b=input('enter denominator sequence:');
y=impz(a,b,n);
stem(y); xlabel('discrete
time');
ylabel('amplitude');
title('impulse response');
Input:
enter numerator sequence:[1 2 1]
enter denominator sequence:[2 1 2]
Output:
Result: Thus the experiment to find the impulse response of given system is executed.
Page No.
Program:
clc
clear all; close
all;
t=0:pi/100:2*pi;
y=sin(t);
subplot(4,4,1);
plot(t,y);
xlabel('t-->');
ylabel('amp-->');
title('sinusoidal signal');
z=cos(t);
subplot(4,4,2);
plot(t,z);
xlabel('t-->');
ylabel('amp-->');
title('cosine signal');
p=square(t);
subplot(4,4,3);
plot(t,p);
xlabel('t-->');
ylabel('amp-->');
title('square signal');
q=sawtooth(t);
subplot(4,4,4);
plot(t,q);
xlabel('t-->');
ylabel('amp-->');
title('sawtooth signal');
t=-pi:0.01:pi;
r=sinc(t);
subplot(4,4,5);
plot(t,r);
xlabel('t-->');
ylabel('amp-->');
title('sinc signal');
Page No.
t=0:0.01:2;
s=exp(t);
subplot(4,4,6);
plot(t,s);
xlabel('t-->');
ylabel('amp-->');
title('exponential signal');
t=-pi:pi/100:pi;
T=tripuls(t,2);
subplot(4,4,7);
plot(t,T); xlabel('t-->');
ylabel('amp-->');
title('triangular signal');
t=0:0.1:5;
ramp=t;
subplot(4,4,8);
plot(t,ramp);
xlabel('t-->');
ylabel('amp-->');
title('ramp signal');
t=-5:0.01:5;
step=[zeros(1,500),ones(1,501)];
subplot(4,4,9);
plot(t,step);
xlabel('t-->');
ylabel('amp-->');
title('unit step');
axis([-5 5 0 1.5]);
t=-1:0.001:1;
impulse=[zeros(1,1000),ones(1,1),zeros(1,1000)];
subplot(4,4,10);
plot(t,impulse);
xlabel('t-->');
ylabel('amp-->');
title('unit impulse');
axis([-2 2 0 2]);
Page No.
Output:
Result: Thus the various types of signals and sequences are generated using matlab.
Page No.
Program:
clc;
close all;
clear all;
b=input('coefficients of x(n):');
a=input('coefficients of y(n):');
c=input('enter intial conditions:');
n=[-5:50];
ic=filtic(b,a,c);
x=[(n>=0)]-[(n>10)];
y=filter(b,a,x,ic);
subplot(2,1,1);
stem(n,x);
title('input sequence of x(n)');
xlabel('n-->');
ylabel('x(n)');
subplot(2,1,2);
stem(n,y);
title('output sequence of y(n)');
xlabel('n-->');
ylabel('y(n)');
Input:
coefficients of x(n):[1 1 1]
coefficients of y(n):[1 2 1]
Output:
Result: Thus the difference equation with initial conditions of given system is solved using matlab.
Page No.
Program:
clc;
close all;
clear all;
b=input('coefficients of x(n)');
a=input('coefficients of y(n)');
n=[-5:50];
x=[(n>=0)]-[(n>5)];
figure(1);
subplot(2,1,1);
stem(n,x);
title('input sequence of x(n)');
xlabel('n-->');
ylabel('x(n)');
subplot(2,1,2);
y=filter(b,a,x);
stem(n,y);
title('output sequence of y(n)');
xlabel('n-->');
ylabel('y(n)');
Input:
coefficients of x(n):[1 1 1]
coefficients of y(n):[1 2 1]
Page No.
Output:
Result: Thus the difference equation without initial conditions of given systems is solved using
Matlab.
Page No.
Program:
clc;
close all;
clear all;
x1=input('enter first sequence:');
x2=input('enter second sequence:');
l1=length(x1);
l2=length(x2);
n1=0:1:l1-1;
subplot(3,1,1);
stem(n1,x1);
title('first sequence');
xlabel('n1-->');
ylabel('x1(n)');
n2=0:1:l2-1;
subplot(3,1,2);
stem(n2,x2);
title('second sequence');
xlabel('n2-->');
ylabel('x2(n)');
n3=0:1:(l1+l2-2);
y=conv(x1,x2);
subplot(3,1,3);
stem(n3,y);
title('linear convolved sequence');
disp('linear convolved sequence:');
disp(y);
xlabel('n3-->');
ylabel('y(n3)');
hold on;
grid on;
Input:
enter first sequence:[3 8 12]
Output:
12 56 130 144 72
Result: Thus the experiment to find the linear convolution of two finite length sequences is
executed.
Page No.
Program:
clc;
close all;
clear all;
p=input('enter first sequence:');
q=input('enter second sequence:');
r=length(p);
s=length(q);
N=max(r,s);
a=[p,zeros(1,N-r)];
b=[q,zeros(1,N-s)];
x=zeros(1,N);
for m=1:N
for n=1:N
i=m-n+1;
if(i<=0)
i=N+i; end
x(m)=x(m)+a(n).*b(i);
end
end
disp('first sequence');
disp(a);
disp('second sequence');
disp(b);
disp('convolved sequence');
disp(x);
subplot(3,1,1);
stem(a);
title('first sequence');
xlabel('n-->');
ylabel('a(n)');
subplot(3,1,2);
stem(b);
title('second sequence');
xlabel('n-->');
Page No.
ylabel('b(n)');
subplot(3,1,3);
stem(x);
title('circular convolved sequence');
xlabel('n-->');
ylabel('x(n)');
hold on;
grid on;
Input:
enter first sequence:[2 9 7]
enter second sequence:[1 8 3 4]
first sequence
2 9 7 0
second sequence
1 8 3 4
Output:
convolved sequence
59 53 85 91
Result: Thus the experiment to find circular convolution of given two finite length sequences is
executed
Page No.
Input:
enter first sequence:[3 8 12]
Output:
12 56 130 144 72
convolved sequence
59 53 85 91
Result: Thus the linear convolution and circular convolution outputs are compared and results are
obtained.
Page No.
Aim: To find auto correlation of the given sequence and verify its properties.
Program:
clc;
close all;
clear all;
x=input('enter the input sequence:');
l=length(x);
n=0:1:l-1;
subplot(2,1,1);
stem(n,x);
xlabel('n-->');
ylabel('x(n)');
title('input sequence');
Rx=xcorr(x,x);
disp('correlated sequence');
disp(Rx);
n1=-l+1:1:l-1;
subplot(2,1,2);
stem(n1,Rx);
xlabel('n-->');
ylabel('Rx(n1)');
title('correlated signal');
disp('energy of signal');
E=sum(x.^2);
mid=ceil(length(Rx)/2);
Rx_o=Rx(mid);
if Rx_o==E
disp('Rx(o) gives energy signal');
else
disp('Rx(o) doesnot gives energy signal');
end
Rx_R=Rx(mid:1:length(Rx));
Rx_L=Rx(mid:-1:1);
if Rx_R==Rx_L
disp('Rx is an even sequence');
end
Page No.
Input:
enter the input sequence:[1 3 5 7]
Output:
correlated sequence
7.0000 26.0000 53.0000 84.0000 53.0000 26.0000 7.0000energy of signal
Rx(o) gives energy signal
Rx is an even sequence
Result: Thus the experiment to find auto correlation of the finite length sequences and to verify its
properties are executed
Page No.
Program:
clc;
close all;
clear all;
x1=input('enter first sequence:');
l1=length(x1);
x2=input('enter second sequence:');
l2=length(x2);
y=xcorr(x1,x2);
l3=length(y);
t1=0:1:l1-1;
t2=0:1:l2-1;
t3=0:1:l3-1;
disp('cross correalated sequence');
disp(y); figure;
subplot(3,1,1);
stem(t1,x1);
xlabel('sequence');
ylabel('amplitude');
title('sequence1');
subplot(3,1,2);
stem(t2,x2);
xlabel('sequence');
ylabel('amplitude');
title('sequence2');
subplot(3,1,3);
stem(t3,y);
xlabel('sequence');
ylabel('amplitude');
title('cross correlated sequence');
mid=ceil(length(y)/2);
if y(mid)==0
disp('sequences are orthogonal');
else
disp('sequences are not orthogonal');
end
Page No.
Input:
Output:
Result: Thus the experiment to find cross correlation of given sequences and to verify its properties
is executed.
Page No.
Aim: To compute DFT of the sequence [2 3 4 5 7 8 12 14] and plot its magnitude and phase
spectrum using matlab.
Program:
clc;
clear all;
close all;
N=input('enter the N value:');
xn=input('enter the sequence of x(n):');
ln=length(xn);
xn=[xn zeros(1,N-ln)];
xk=zeros(1,N);
ixk=zeros(1,N);
for k=0:N-1
for n=0:N-1
xk(k+1)=xk(k+1)+(xn(n+1)*exp((-1i)*2*pi*k*n/N));
end
end
t=0:N-1;
subplot(3,2,1);
stem(t,xn);
ylabel('amplitude');
xlabel('n');
title('input sequence');
grid on;
magnitude=abs(xk);
disp(magnitude);
t=0:N-1;
subplot(3,2,2);
stem(t,magnitude);
ylabel('amplitude')
xlabel('k');
title('DFT magnitude response');
grid on;
phase=angle(xk);
disp('phase is');
disp(phase);
t=0:N-1;
Page No.
subplot(3,2,3);
stem(t,phase);
ylabel('phase');
xlabel('k');
title('DFT phase response');
grid on;
Input:
Output:
Result: Thus the experiment to compute DFT of the given sequence is executed and magnitude and
phase plots are obtained.
Page No.
Aim: To compute IDFT of the sequence [2 3 4 5 7 8 12 14] and plot its magnitude and phase
spectrum using matlab.
Program:
clc;
clear all;
close all;
N=input('enter the N value:');
xn=input('enter the sequence of x(n):');
ln=length(xn);
xn=[xn zeros(1,N-ln)];
xk=zeros(1,N);
ixk=zeros(1,N);
t=0:N-1;
subplot(3,2,1);
stem(t,xn);
ylabel('amplitude');
xlabel('n');
title('input sequence');
grid on;
for k=0:N-1
for n=0:N-1
xk(k+1)=xk(k+1)+(xn(n+1)*exp((-1i)*2*pi*k*n/N));
end
end
for n=0:N-1
for k=0:N-1
ixk(n+1)=ixk(n+1)+(xk(k+1)*exp(1i*2*pi*k*n/N));
end end
ixk=ixk./N;
t=0:N-1;
subplot(3,2,2);
stem(t,ixk);
disp('idft sequence=');
disp(ixk);
ylabel('amplitude');
xlabel('n');
Page No.
Input:
enter the N value:8
enter the sequence of x(n):[2 3 4 5 7 8 12 14]
Output:
Result: Thus the experiment to compute IDFT of given sequence is executed and magnitude and
phase plots are obtained.
Page No.
Program:
clc
clear all;
close all;
xn1=input('enter first sequence');
xn2=input('enter second sequence');
n1=length(xn1);
n2=length(xn2);
N=n1+n2-1;
xn1=[xn1 zeros(1,N-n1)];
xn2=[xn2 zeros(1,N-n2)];
y1=zeros(1,N);
y2=zeros(1,N);
izk=zeros(1,N);
for k=0:N-1
for n=0:N-1
y1(k+1)=y1(k+1)+(xn1(n+1)*exp((-1i)*2*pi*k*n/N));
y2(k+1)=y2(k+1)+(xn2(n+1)*exp((-1i)*2*pi*k*n/N));
end end
zk=y1.*y2;
disp('linear convolved sequence is:');
for n=0:N-1
for k=0:N-1
izk(n+1)=izk(n+1)+(zk(k+1)*exp((1i)*2*pi*k*n/N));
end end
izzk=izk./N;
disp(izzk);
n1=0:1:N-1;
subplot(3,1,1);
stem(n1,xn1);
ylabel('amp');
xlabel('n');
title('first sequence');
n2=0:1:N-1;
Page No.
subplot(3,1,2);
stem(n2,xn2);
ylabel('amp');
xlabel('n');
title('second sequence');
n=0:1:N-1;
subplot(3,1,3);
stem(n,izzk);
ylabel('amp');
xlabel('n');
title('linearly convolved sequence');
Input:
enter first sequence:[1 3 5]
Output:
2.0000 - 0.0000i 10.0000 - 0.0000i 28.0000 - 0.0000i 38.0000 + 0.0000i 30.0000 + 0.0000i
Result: Thus the experiment to find linear convolution using DFT and IDFT is executed.
Page No.
Program:
clc
clear all;
close all;
x=input('enter first sequence');
h=input('enter second sequence');
n1=length(x);
n2=length(h);
N=n1+n2-1;
x1=[x zeros(1,N-n1)];
x2=[x zeros(1,N-n2)];
a=fft(x,N);
b=fft(h,N);
c=a.*b;
d=ifft(c,N);
disp('convolved sequence');
disp(d); n1=0:1:n1-
1; subplot(3,1,1);
stem(n1,x);
ylabel('magnitude');
xlabel('n');
title('first sequence');
n2=0:1:n2-1;
subplot(3,1,2);
stem(n2,h);
ylabel('magnitude');
xlabel('n');
title('second sequence');
n=0:1:N-1;
subplot(3,1,3);
stem(n,d);
ylabel('magnitude');
xlabel('n');
title('linearly convolved sequence');
Page No.
Input:
Output:
convolved sequence
Result: Thus the experiment to find linear convolution of a sequence using FFT and IFFT is
executed.
Page No.
Aim: To find circular convolution of given sequence using DFT and IDFT.
Program:
clc
clear all;
close all;
x=input('enter first sequence');
h=input('enter second sequence');
n1=length(x);
n2=length(h);
N=max(n1,n2);
x1=[x zeros(1,N-n1)];
x2=[x zeros(1,N-n2)];
x1k=zeros(N,1);
x2k=zeros(N,1);
for k=0:N-1
for n=0:N-1
x1k(k+1)=x1k(k+1)+(x1(n+1)*exp(-1i)*2*pi*k*n/N);
x2k(k+1)=x2k(k+1)+(x2(n+1)*exp(-1i)*2*pi*k*n/N);
end end
zk=x1k.*x2k;
z=zeros(N,1);
for n=0:N-1
for k=0:N-1
z(n+1)=z(n+1)+(zk(k+1)*exp((1i)*2*pi*k*n/N));
end
end
disp('circular convolved');
disp(z);
n1=0:1:n1-1;
subplot(3,1,1);
stem(n1,x);
ylabel('amp');
xlabel('n');
title('first sequence');
n2=0:1:n2-1;
subplot(3,1,2);
Page No.
stem(n2,h);
ylabel('amp');
xlabel('n');
title('second sequence');
n=0:1:N-1;
subplot(3,1,3);
stem(n,z);
ylabel('amp');
xlabel('n');
title('circular convolved sequence');
Input:
enter first sequence[1 3 5 7 9 4]
Output:
Result: Thus the experiment to find circular convolution using DFT and IDFT is executed.
Page No.
Aim: To find circular convolution of given sequence using FFT and IFFT.
Program:
clc
clear all;
close all;
x=input('enter first sequence');
h=input('enter second sequence');
n1=length(x);
n2=length(h);
N=max(n1,n2);
x1=[x zeros(1,N-n1)];
x2=[x zeros(1,N-n2)];
a=fft(x,N);
b=fft(h,N);
c=a.*b;
d=ifft(c,N);
disp('first sequence');
disp(x);
disp('second sequence');
disp(h);
disp('convolved sequence');
disp(d);
n1=0:1:n1-1;
subplot(3,1,1);
stem(n1,x);
xlabel('n');
ylabel('x1n');
title('input');
n2=0:1:n2-1;
subplot(3,1,2);
stem(n2,h);
xlabel('n');
ylabel('x2n');
title('input');
n=0:1:N-1;
subplot(3,1,3);
stem(n,d);
Page No.
xlabel('n');
ylabel('amp');
title('circular');
Input:
enter first sequence[2 7 4 6 3 9]
first sequence
2 7 4 6 3 9
second sequence
1 3 4 5 7 6
Output:
convolved sequence
Result: Thus the experiment to find circular convolution of given sequence using FFT and IFFT is
executed.
Page No.
Aim: To design and implement FIR filter of cut-off frequency 0.5 with pass band frequency of 0.5
and stop band frequency of 70.
Program:
clear all;
close all;
ap=input('Enter passband attenuation');
as=input('Enter stopband attenuation');
wp=input('Enter passband freq');
ws=input('Enter stopband cutoff freq');
wc=input('Enter cutoff freq');
t=[ws-wp]/(2*pi);
n=8*pi/(ws-wp);
wh=hamming(n);
b=fir1(n-1,wc);
w=0:0.01:pi;
h=freqz(b,1,w);
m=20*log10(abs(h));
plot(w,m);
xlabel('freq');
ylabel('magn');
title('response of fir lpf')
Input:
Enter passband attenuation 0.5
Output:
Result: Thus the FIR filter is designed and implemented with cut off frequency of 0.5, pass band
frequency of 0.5 and stop band frequency of 70
Page No.
Program:
clear all;
close all;
ap=input('Enter passband attenuation');
as=input('Enter stopband attenuation');
wp=input('Enter passband freq');
ws=input('Enter stopband cutoff freq');
t=input('enter sampling frequency');
fs=1/t;
wp=wp/fs; ws=ws/fs;
wpp=(2/t)*tan(wp/2);
wss=(2/t)*tan(ws/2);
[N,wc]=cheb1ord(wpp,wss,ap,as,'s');
[a,b]=cheby1(N,ap,wc,'s');
[az,bz]=bilinear(a,b,fs);
k=0:0.01/pi:pi;
h=freqz(az,bz,k);
m=20*log10(abs(h));
n=angle(h);
subplot(3,1,1);
plot(k,abs(h));
xlabel('abs(h))');
ylabel('k');
title('magnitude response');
grid on;
subplot(3,1,2);
plot(k,m);
xlabel('m');
ylabel('k');
title('gain');
grid on;
subplot(3,1,3);
plot(k,n);
xlabel('n');
ylabel('k');
title('phase response');
grid on;
Page No.
Input:
Output:
Result: Thus the IIR filter of sampling frequency 4 is designed and implemented.
Page No.
Program:
clc;
close all;
clear all;
ap=input('enter pass band cutoff frequency:');
as=input('enter stop band cutoff frequency:');
fp=input('enter pass band frequency:');
fs=input('enter stop band frequency:');
w1=fp/fs;
w2=fs/fs;
[N,wn]=buttord(w1,w2,ap,as,'s');
[b,a]=butter(N,wn,'s');
[num,den]=impinvar(b,a,fs);
freqz(num,den);
title('butterworth low pass filter response');
Input:
enter pass band cutoff frequency:0.25
enter stop band cutoff frequency:50
enter pass band frequency:0.2*pi
Output:
Result: Thus the Butterworth Filter is designed and implemented using Matlab.