DSS Exp5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Department of Electronics & Computer Science

Discrete Signals and Systems Lab (ECSP204)

2023-24

Experiment 5 (2023-24)

Submitted By:- Kaustubh Kulurkar

Roll No:- 65

Section:- A

Subject:- Discrete Signals and Systems

Lab and Assignment No:- “5”

Title:- Convolution of and co-relation of DTS

Date:- 26/10/2023

1
Department of Electronics & Computer Science

Discrete Signals and Systems Lab (ECSP204)

2023-24

Linear Convolution :

Lab5 a) Write matlab program to perform convolution operation on two given signals.
Given x(n) input , h(n) impulse response of system and y(n) o/p of the system
Part A : Using direct inbuilt function : use matlab help
clc
clear all
clf
n1=0:1:3;
x=[1 2 3 1];
n2=-1:1:2;
h=[1 2 1 -1];
y=conv(x,h);
display(y);
% plot results properly
n3=min(n1)+min(n2); n4=max(n1)+max(n2);
r=n3:1:n4;
subplot (3,1,1); stem(n1,x); axis([-10 10 -1 5])
subplot (3,1,2); stem(n2,h); axis([-10 10 -3 5]);
subplot (3,1,3); stem(r,y); axis([-10 10 -5 10]);

2
Department of Electronics & Computer Science

Discrete Signals and Systems Lab (ECSP204)

2023-24

Part B : Create a user defined function to perform convolution operation


clc
clear all
% step1 define input
% use bellow commented lines for creating function
%% Directly using conv function in matlab
%n1= input('enter the range/length of sequence x(n)');
%x=input('Enter the first input sequence/x(n)');
%n2= input('enter the range/length of sequence h(n)');
% h=input('Enter the impulse response h(n)/ second input sequence x(n)/h(n)');
% y=conv(x,h)
%%-----------
n1=0:1:3; x=[1 2 3 1];
n2=-1:1:2; h=[1 2 1 -1];
% step 2 Perform convolution
%use an empty numeric array (i.e. []) to indicate that an input argument % is undefined, %
which allows further input arguments to be % specified

z=[]; for i=1:length(x)


g=h.*x(i); h(n n1=0:1:3;
z=[z;g]; x(n) 1 2 1 -1 x=[1 2 3 1];
end 1 1 2 1 -1 n2=-1:1:2;
% step3 Matrix method logic Diagonal addition of 2 2 4 2 -2 h=[1 2 1 -1];
element to calculate results 3 3 6 3 -3
[r c]=size(z); k=r+c; t=2; 1 1 2 1 -1
y=[]; sum=0;
while(t<=k)
1 4 8 8 3 -2 -1
for i=1:r
for j=1:c
if((i+j)==t)
sum=sum+z(i,j);
end
end
end
t=t+1;
y=[y sum];
sum=0;
end
display(y);

3
Department of Electronics & Computer Science

Discrete Signals and Systems Lab (ECSP204)

2023-24
% Step4 Plotting the result
n3=min(n1)+min(n2);
n4=max(n1)+max(n2);
p=n3:1:n4;
subplot (3,1,1);
stem(n1,x);
axis([-10 10 -2 5])
subplot (3,1,2);
stem(n2,h);
axis([-10 10 -3 5]);
subplot (3,1,3);
stem(p,y);
axis([-10 10 -5 10]);

Use labels, title as per requirements.

4
Department of Electronics & Computer Science

Discrete Signals and Systems Lab (ECSP204)

2023-24
Lab 5 d) Use above program
to perform following convolution
X(n)=u(n)-u(n-10) and h(n)=0.9n u(n) you are not allowed to use conv or filter function.
Plot x(n), h(n) and o/p of the system.

Hint: Provide length of x and h


n1= input('Enter the length of sequence x(n) array');
n2=n1; % keep same h(n)
x=input('Enter the input sequence x(n) array');
h=input('Enter the impulse response h(n) array');
1) 'Enter the length of sequence x(n) array -5:1:50
2) input sequence x(n) array (n1>=0)-(n1>=10)
3) Enter the impulse response h(n) array (0.9.^n1).* (n1>=0)

Solution:
clc
clear all

n1 = -5:1:50;
n2 = n1;

x = zeros(size(n1));
x(n1 >= 0 & n1 <= 10) = 1;

h = (0.9 .^ n1) .* (n1 >= 0);

y = zeros(1, length(n1));
for i = 1:length(n1)
for j = 1:length(n2)
if (i - j >= 0)
y(i) = y(i) + x(i - j + 1) * h(j);
end
end
end

figure('Position', [100, 100, 800, 600]);

subplot(3, 1, 1);
stem(n1, x);
title('x(n) = u(n) - u(n-10)');

5
Department of Electronics & Computer Science

Discrete Signals and Systems Lab (ECSP204)

2023-24
xlabel('n');
ylabel('x(n)');

subplot(3, 1, 2);
stem(n2, h);
title('h(n) = 0.9^n * u(n)');
xlabel('n');
ylabel('h(n)');

subplot(3, 1, 3);
stem(n1, y);
title('Output y(n) = x(n) * h(n)');
xlabel('n');
ylabel('y(n)');

axis([-10 60 -0.5 7]);

6
Department of Electronics & Computer Science

Discrete Signals and Systems Lab (ECSP204)

2023-24

Part e : Auto -correlation Matrix Method

clc
clear all
close all
x=input('Enter the sequence');
n1=input('Enter the time sample range:');
h=fliplr(x);
n2=-fliplr(n1);
z=[];
for i=1:length(x)
g=h.*x(i);
z=[z;g];
end
[r c]=size(z);
k=r+c;
t=2;
y=[];

7
Department of Electronics & Computer Science

Discrete Signals and Systems Lab (ECSP204)

2023-24
cd=0;
while(t<=k)
for i=1:r
for j=1:c
if((i+j)==t)
cd=cd+z(i,j);
end
end
end
t=t+1;
y=[y cd];
cd=0;
end
nl=min(n1)+min(n2);
nh=max(n1)+max(n2);
t=nl:1:nh;
display(y);
subplot(3,1,1)
stem(n1,x);
axis([-5 5 -1 5]);
subplot(3,1,2)
stem(n2,h);
axis([-5 5 -1 5]);
subplot(3,1,3)
stem(t,y);
axis([-5 5 -5 30]);

8
Department of Electronics & Computer Science

Discrete Signals and Systems Lab (ECSP204)

2023-24

Part f Questions

a) Describe 5 practical real life applications of convolution


Soln: Practical Real-Life Applications of Convolution:

1. Image Processing: Convolution is extensively used in image processing for tasks like edge
detection, blurring, and feature extraction.
2. Signal Processing: In audio and speech processing, convolution is used for filtering, noise
reduction, and effects like reverb.
3. Radar and Sonar Systems: Convolution is applied in radar and sonar systems to analyze
echo signals, locate objects, and determine their characteristics.
4. Neural Networks: In deep learning and artificial neural networks, convolutional layers are
used for feature extraction and pattern recognition.
5. Communication Systems: Convolution is used in digital communication systems for
channel equalization, error correction, and modulation/demodulation.

9
Department of Electronics & Computer Science

Discrete Signals and Systems Lab (ECSP204)

2023-24

b) Write 3 practical real life applications of co-relation and auto correlation


Soln:
1. Speech Recognition: Cross-correlation is used in speech recognition systems to match
spoken words with reference templates.
2. Image Alignment: Cross-correlation is applied in image registration to align images from
different sources, such as medical images for diagnosis.
3. Radar and Sonar: Cross-correlation is used for detecting and tracking moving objects,
including aircraft and submarines.

c) Modify and develop matlab function/program for finding cross- corelation between 2
signal
Soln:
function crossCorrelation = cross_corr(x, h)
x = fliplr(x); % Flip x to perform cross-correlation
crossCorrelation = conv(x, h);
end
d) Applications of cross correlation
Soln:
1. Time Delay Estimation: In audio and signal processing, cross-correlation is used to
estimate the time delay between two signals.
2. Pattern Matching: Cross-correlation is used in pattern recognition to identify patterns or
objects within an image or a signal.
3. Geophysics: Cross-correlation of seismograph data is used to locate the epicenter of an
earthquake.
4. Radar: Cross-correlation is applied to radar signals for target detection and tracking.
5. Bioinformatics: Cross-correlation is used to analyze DNA sequences for sequence
alignment and identification of motifs.

10
Department of Electronics & Computer Science

Discrete Signals and Systems Lab (ECSP204)

2023-24

e) Calculation of Energy of follwing sequence using Auto-Correlation


Modify program and do the needful

Soln:
function energy = calculateEnergy(x)
autocorr_result = xcorr(x, 'biased'); % Calculate the auto-correlation
energy = sum(autocorr_result);
end

Example 1:
Code:
X1 = [0, 1, 2, 3, 4];

energy1 = calculateEnergy(X1);
disp(['Energy of Example 1: ', num2str(energy1)]);

Output:
Energy of Example 1: 20

Example 2:

Code:
n2 = -4:4;

X2 = 2 .^ n2;
energy2 = calculateEnergy(X2);
disp(['Energy of Example 2: ', num2str(energy2)]);

Output:
Energy of Example 2: 113.3338

11

You might also like