0% found this document useful (0 votes)
54 views

Convolution: MATLAB Script For Convolution of A Discrete Signal With Impulse Response

The document provides MATLAB scripts for convolution and correlation of discrete sequences and signals. Convolution involves multiplying and summing corresponding samples of two sequences to produce a convoluted sequence. Correlation involves multiplying and summing corresponding samples of one sequence with the reversed second sequence to detect similarities and find patterns. The scripts zero pad the sequences, perform the multiplication and summation operations in loops, and plot the original, filtered, and output sequences for comparison.

Uploaded by

Rishi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Convolution: MATLAB Script For Convolution of A Discrete Signal With Impulse Response

The document provides MATLAB scripts for convolution and correlation of discrete sequences and signals. Convolution involves multiplying and summing corresponding samples of two sequences to produce a convoluted sequence. Correlation involves multiplying and summing corresponding samples of one sequence with the reversed second sequence to detect similarities and find patterns. The scripts zero pad the sequences, perform the multiplication and summation operations in loops, and plot the original, filtered, and output sequences for comparison.

Uploaded by

Rishi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Convolution

MATLAB Script for Convolution of a discrete signal with impulse response :

x = input('Enter 1st sequence\n');

h = input('Enter 2nd sequence\n');

m = length(x);

n = length(h);

X = [x,zeros(1,n)]; // zero padding

H = [h,zeros(1,m)]; // zero padding

for i = 1:n+m-1

y(i) = 0;

for j = 1:m

if (i-j+1>0)

y(i) = y(i)+X(j)*H(i-j+1); // convolution rotates the filter matrix by 180 degrees

end

end

end

disp('Convoluted sequence is')

disp(y)

suptitle('Convolution of 2 sequences');

subplot(3,1,1); //plot in 3*1 matrix at (1,1) position

stem(x); // for plotting of discrete function

xlabel('time');

ylabel('1st sequence');
subplot(3,1,2); // plot in 3*1 matrix at (2,1) position

stem(h);

xlabel('time');

ylabel('2nd sequence');

subplot(3,1,3); // plot in 3*1 matrix at (3,1) position

stem(y);

xlabel('time');

ylabel('Convoluted sequence');

Example:

Enter 1st sequence

[3 8 0 8 6 4 6 8 3 8 4 8 6]

Enter 2nd sequence

[3 5 3 9 3 3]

Convoluted sequence is

9 39 49 75 139 99 152 144 145 147 163 137 175 123 126 90 42 18
MATLAB Script for Convolution of a signal with impulse response :

x = input('Enter 1st sequence\n');


Y = sin(x);
h = input('Enter 2nd sequence\n');
H = cos(h);
m = length(x);
n = length(h);
X = [x,zeros(1,n)];
H = [h,zeros(1,m)];
for i = 1:n+m-1
y(i) = 0;
for j = 1:m
if (i-j+1>0)
y(i) = y(i)+Y(j)*H(i-j+1);
end
end
end
suptitle('Convolution of 2 signals');
subplot(3,1,1);
stem(Y);
xlabel('time');
ylabel('1st signal');
subplot(3,1,2);
stem(H);
xlabel('time');
ylabel('2nd signal');
subplot(3,1,3);
stem(y);
xlabel('time');ylabel('Convoluted sequence');
Correlation
Correlation is a way to detect a known waveform in a noisy background.

MATLAB Script for Correlation of two input discrete sequences:

x = input('Enter 1st sequence\n');

h = fliplr(input('Enter 2nd sequence\n'));

m = length(x);

n = length(h);

X = [x,zeros(1,n)]; // zero padding

H = [h,zeros(1,m)]; // zero padding

for i = 1:n+m-1

y(i) = 0;

for j = 1:m

if (i-j+1>0)

y(i) = y(i)+X(j)*H(i-j+1); // correlation

end

end

end

disp('Correlated sequence is')

disp(y)

suptitle('Correlation of 2 sequences');

subplot(3,1,1); //plot in 3*1 matrix at (1,1) position

stem(x);

xlabel('time');

ylabel('1st sequence');
subplot(3,1,2); //plot in 3*1 matrix at (2,1) position

stem(h);

xlabel('time');

ylabel('2nd sequence');

subplot(3,1,3); //plot in 3*1 matrix at (3,1) position

stem(y);

xlabel('time');

ylabel('Correlated sequence');

Example:

Enter 1st sequence

[1 2 3 4 5]

Enter 2nd sequence

[4 0 9 4]

Correlated sequence is

4 17 30 47 64 57 16 20

Enter 1st sequence

[1 2 9 4 5 7 3 8 7 4 9 0 5 0 6 9 6 3 8 0]

Enter 2nd sequence

[3 4 7 6 8 8 7 4]

Correlated sequence is

Columns 1 through 21

4 15 58 103 142 186 205 247 264 273 283 263 255 221 198 218 213 236 229
239 211

Columns 22 through 27

178 120 86 41 24 0
MATLAB Script for Correlation of two input signals:

x = input('Enter 1st sequence\n');

Y = sin(x);

h = fliplr(input('Enter 2nd sequence\n'));

H = cos(h);

m = length(x);

n = length(h);

X = [x,zeros(1,n)];

H = [h,zeros(1,m)];

for i = 1:n+m-1

y(i) = 0;

for j = 1:m

if (i-j+1>0)

y(i) = y(i)+Y(j)*H(i-j+1);

end

end

end
disp('Correlated sequence is')

disp(y);

suptitle('Correlation of 2 sequences');

subplot(3,1,1);

plot(Y);

xlabel('time');

ylabel('1st sequence');

subplot(3,1,2);

plot(H);

xlabel('time');

ylabel('2nd sequence');

subplot(3,1,3);

plot(y);

xlabel('time');

ylabel('Correlated sequence');

Enter 1st sequence

[0:0.01:pi]

Enter 2nd sequence

[0:0.01:pi]
Difference b/w Convolution and Correlation:
Correlation is measuring how similar two signals are to each other, and convolution is a filtering
operation

You might also like