DSP Lab Record
DSP Lab Record
ID - 2022UEC1660
Batch - EC-3
Semester - IV
Digital Signal Processing Lab
Malviya National Institute of Technology
MATLAB Code:-
clc;
clear all;
close all;
x=input("matrix x=");
lx=length(x);
ix=input("starting index of x=");
n1=-1*ix:(lx-1)-ix; %x-axis defined for input signal
h=input("matrix h=");
lh=length(h);
ih=input("starting index of h=");
n2=-1*ih:(lh-1)-ih; %x-axis defined for impulse response
subplot(3,1,1);
stem(n1,x);
xlabel('n');
ylabel('x(n)');
title('input signal');
subplot(3,1,2);
stem(n2,h);
xlabel('n');
ylabel('h(n)');
title('impulse response');
X=conv(x,h);
n3=-1*(ix+ih):(lx-ix-1)+(lh-ih-1);
subplot(3,1,3);
stem(n3,X);
xlabel('n');
ylabel('X(n)');
title('linear convolved signal using in-built command');
Output:-
Result:- To find linear convolution of two signals and plot them using inbuilt
functions.
Experiment-2
Aim:- To find circular convolution of two signals and plot them using in-built
command.
MATLAB Code:-
clc;
clear all;
close all;
%input signal
x=input("matrix x=");
lx=length(x);
n1=0:lx-1; %x-axis defined for input signal
%impulse response
h=input("matrix h=");
lh=length(h);
n2=0:lh-1; %x-axis defined for impulse respone
subplot(3,1,1);
stem(n1,x);
xlabel('n');
ylabel('x(n)');
title('input signal');
subplot(3,1,2);
stem(n2,h);
xlabel('n');
ylabel('h(n)');
title('impulse response');
m=max(lx,lh);
n3=0:lx-1; %x-axis defined for convolved signal
y=cconv(x,h,m);
subplot(3,1,3);
stem(n3,y);
xlabel('n');
ylabel('y(n)');
title('circular convolved signal using in-built command');
Output:-
Result:- To find circular convolution of two signals and plot them using in-built
command.
Experiment-3
Aim:- To find linear convolution of two signals and plot them using loops.
MATLAB Code:-
clc;
clear all;
close all;
%input signal
x=input("matrix x=");
lx=length(x);
ix=input("starting index of x="); %give ix by taking index of 1st element=0
n1=-1*ix:(lx-1)-ix; %x-axis defined for input signal
%impulse response
h=input("matrix h=");
lh=length(h);
ih=input("starting index of h="); %give ix by taking index of 1st element=0
n2=-1*ih:(lh-1)-ih; %x-axis defined for impulse response
subplot(3,1,1);
stem(n1,x);
xlabel('n');
ylabel('x(n)');
title('input signal');
subplot(3,1,2);
stem(n2,h);
xlabel('n');
ylabel('h(n)');
title('impulse response');
X=[x,zeros(1,lh-1)];
H=[h,zeros(1,lx-1)];
y=zeros(1,lx+lh-1);
%convolving x with h
for n=1:lx+lh-1
y(n)=0;
for k=1:n
y(n)=y(n)+X(k)*H(n-k+1);
end
end
Output-
Result- We have performed linear convolution of two signals.
Experiment-4
Aim:-To find linear convolution using circular convolution of two signals .
MATLAB Code:-
clc;
clear all;
close all;
x=input('enter input signal=');
m=length(x);
h=input('enter the signal=');
n=length(h);
l=m+n-1;
X=[x,zeros(1,l-m)];
H=[h,zeros(1,l-n)];
y=zeros(l);
for n=1:l
y(n)=0;
for k=1:l
y(n)=y(n)+(X(k).*H(mod(n-k,l)+1));
end
end
subplot(3,1,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('Input Signal');
subplot(3,1,2);
stem(h);
xlabel('n');
ylabel('h(n)');
title('Impulse Response');
subplot(3,1,3);
stem(y);
xlabel('n');
ylabel('y(n)');
title('Linear convolution using Circular convolution');
Output:-
Result:- We have obtained linear convolution using circular convolution.
Experiment-5
Aim:-To find circular convolution of two signals using loop.
MATLAB Code:-
clc;
clear all;
close all;
%input signal
x=input("matrix x=");
lx=length(x); %length of x starts from 1
n1=0:lx-1; %x-axis defined for input signal
%impulse response
h=input("matrix h=");
lh=length(h); %length of h start from 1
n2=0:lh-1; %x-axis defined for impulse respone
subplot(3,1,1);
stem(n1,x);
xlabel('n');
ylabel('x(n)');
title('input signal');
subplot(3,1,2);
stem(n2,h);
xlabel('n');
ylabel('h(n)');
title('impulse response');
m=max(lx,lh);
X=[x,zeros(1,m-lx)];
H=[h,zeros(1,m-lh)];
y=zeros(1,m);
%convolving x with h
for n=1:m
y(n)=0;
for k=1:m
y(n)=y(n)+X(k)*H(mod(n-k,lx)+1);
end
end
Output:-
Result:-We have performed circular convolution of given signals using loops.
Experiment-6
Aim:- To obtain auto-correlation of given signal using loops and in-built command.
MATLAB Code:-
clc;
clear all;
close all;
%input signal
x=input("matrix x=");
lx=length(x);
subplot(3,1,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('input discrete signal');
yi=xcorr(x); %autocorrelation using in-built command
X=[x,zeros(1,lx-1)];
y=zeros(1,2*lx-1);
for k=-1*(lx-1):lx-1
for n=1:lx
if(n+k>0)
y(k+lx)=y(k+lx)+X(n)*X(n+k);
end
end
end
n1=-1*(lx-1):lx-1;
subplot(3,1,2);
stem(n1,y);
xlabel('delay (k)');
ylabel('Rxx(k)');
title('autocorrelation by loop');
subplot(3,1,3);
stem(n1,yi);
xlabel('delay (k)');
ylabel('Rxx(k)');
title('autocorrelation by in-built function');
Output:-
Aim:-To perform cross-correlation of two signals using linear convolution and in-built
command.
MATLAB Code:-
clc;
clear all;
close all;
%input signal
x=input("matrix x=");
lx=length(x);
%impulse response
h=input("matrix h=");
lh=length(h);
subplot(4,1,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('input signal');
subplot(4,1,2);
stem(h);
xlabel('n');
ylabel('h(n)');
title('impulse response');
H1=flip(h);
X=[x,zeros(1,lh-1)];
H=[H1,zeros(1,lx-1)];
y=zeros(1,lx+lh-1);
%convolving x with h
for n=1:lx+lh-1
y(n)=0;
for k=1:lx
if(n-k+1>0)
y(n)=y(n)+X(k)*H(n-k+1);
end
end
end
subplot(4,1,3);
stem(y);
xlabel('n');
ylabel('y(n)');
title('cross-correlation using loop');
Output:-
Result:- Hence we have obtained the correlation of a given Discrete Signal with
its noisy version .
Experiment-9
Aim:-To obtain Discrete Fourier Transform of given Discrete Signal using loops and in-
built command and verify the output.
MATLAB Code:-
clc;
clear all;
close all;
x=input('enter discrete signal values=');
N=length(x);
for k=1:N
y(k)=0;
for n=1:N
y(k)=y(k)+x(n).*exp((-1j*2*pi*(k-1)*(n-1))/N);
end
end
subplot(3,1,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('input signal');
subplot(3,1,2);
stem(abs(y));
xlabel('k');
ylabel('Magnitude of X(k)');
title('DFT using loop abs');
subplot(3,1,3);
stem(angle(y));
xlabel('k');
ylabel('Phase of X(k)');
title('DFT using loop phase');
yi=fft(x);
figure;
subplot(2,1,1);
stem(abs(yi));
xlabel('k');
ylabel('Magnitude of X(k)');
title('FT using fft abs');
subplot(2,1,2);
stem(angle(yi));
xlabel('k');
ylabel('Phase of X(k)');
title('FT using fft phase');
%taking IDFT
for n=1:N
Iy(n)=0;
for k=1:N
Iy(n)=Iy(n)+(1/N)*y(k).*exp((1j*2*pi*(k-1)*(n-1))/N);
end
end
figure;
subplot(2,1,1);
stem(Iy);
xlabel('n');
ylabel('x(n)');
title('IDFT using loop');
Iyi=ifft(y);
subplot(2,1,2);
stem(Iyi);
xlabel('n');
ylabel('x(n)');
title('IDFT using in-built command');
Output:-
Result:-We have obtained the Discrete Fourier Transform of a given signal using
both loops and in-built command and verified the output.
Experiment-10
Aim:-To obtain axis adjusted Fourier transform of sine signal.
MATLAB Code-
clc;
clear all;
close all;
f=20;
t=-0.5:0.01:0.5;
x=sin(2*pi*f*t);
subplot(4,1,1);
plot(t,x);
xlabel('time (t)');
ylabel('x(t)');
title('Input Signal');
X=fft(x);
subplot(4,1,2);
plot(abs(X));
xlabel('frequency(f)');
ylabel('|X(f)|');
title('Fourier transform (Magnitude)');
subplot(4,1,3);
plot(angle(X));
xlabel('frequency(f)');
ylabel('phase(X(f))');
title('Fourier transform (Phase)');
%shifting signal
Xf=fftshift(X);
lXf=length(Xf);
a=-(lXf-1)/2:(lXf-1)/2;
subplot(4,1,4);
plot(a,abs(Xf));
xlabel('frequency(f)');
ylabel('|X(f)|');
title('Axis adjusted Fourier transform');
Output:-
Result:- We have obtained correct frequency axis graph for Fourier Transform
of sine signal.
Experiment-11
Aim:- To obtain a Histogram and Cumulative distribution of a grayscale image and
then plot its Histogram equalised image.
MATLAB Code:-
clc;
clear all;
close all;
a = imread('cameraman.tif');
imshow(a);
title('Input Image');
[m,n] =size(a);
H=zeros(1,256);
for k = 1:256
count = 0;
for i = 1:m
for j = 1:n
if(a(i,j)==k)
count=count+1;
end
end
end
H(1,k)=count;
end
figure;
subplot(2,1,1);
stem(H);
xlabel('codes of gray shades');
ylabel('frequency');
title('Frequency distribution of input image');
C=cumsum(H);
subplot(2,1,2);
stem(C);
xlabel('codes of gray shades');
ylabel('cumulative sum');
title('Cumulative distribution of input image');
d=C/(m*n);
r=d*255; %normalize the cumulative distribution
v=round(r);
new=zeros(m,n);
for k=1:256
for i=1:m
for j=1:n
if(a(i,j)==k)
new(i,j)=v(1, k);
end
end
end
end
figure;
subplot(2,1,1);
imshow(uint8(new));
title('Reconstructed image');
NH=imhist(uint8(new));
subplot(2,1,2);
stem(NH);
xlabel('codes of gray shades');
ylabel('frequency');
title('Histogram of new image');
Output:-
Result:- Hence we have obtained a Histogram and Cumulative distribution of a
grayscale image and then plotted its Histogram equalised image.
Experiment-12
Aim:- To obtain 2-D Fourier Transformed Image of a grayscale image then revert the
original image back.
MATLAB Code:-
clc;
clear all;
close all;
a=imread('cameraman.tif');
subplot(1,2,1);
imshow(a);
title('Input Image');
[m,n]=size(a);
H=fft2(a);
subplot(1,2,2);
imshow(uint8(H));
title('2-D fft image');
IH=ifft2(H);
figure;
imshow(uint8(IH));
title('2-D ifft image (retrieved)');
Output:-
Result:- We have obtained 2-D Fourier Transformed Image of a grayscale image
then reverted the original image back.
Experiment-13
Aim:- To perform Discrete Wavelet Transform of a grayscale image then revert the
original image back.
MATLAB Code:-
clc;
clear all;
close all;
img=imread('cameraman.tif');
subplot(3,2,1);
imshow(img);
title('Original Image');
[ca,ch,cv,cd] = dwt2(img, 'Haar');
subplot(3,2,2);
imshow(uint8(ca));
title('Approximation');
subplot(3,2,3);
imshow(uint8(ch));
title('Horizontal part of the image');
subplot(3,2,4);
imshow(uint8(cv));
title('Vertical part of the image');
subplot(3,2,5);
imshow(uint8(cd));
title('Diagonal part of the image');
new = idwt2(ca, ch, cv, cd, 'Haar');
subplot(3,2,6);
imshow(uint8(new));
title('Inverse Fourier Transform of the Image');
Output:-