DSP Convolution On Matlab
DSP Convolution On Matlab
Roll No. 26
Lab Session 3
1. What will happen if we input x(n)={0,0,1,0,0} into the above system.
↑
clear all, clc, close all;
h = [3 2 1 -2 1 0 -4 0 3];
org_h = 1;
nh = (1:length(h)) - org_h;
x = [0 0 1 0 0];
org_x = 1;
nx = (1:length(x)) - org_x;
y = conv(h, x);
ny = nh(1) + nx(1):nh(end) + nx(end);
figure;
%plotting impulse response
subplot(3, 1, 1);
stem(nh, h, 'filled', 'k');
xlabel('Time index(n)'), ylabel('Amplitude');
xlim([nh(1)-1 nh(end)+1]);
title('Impulse response h(n)'), grid;
%plotting input signal
subplot(3, 1, 2);
stem(nx, x, 'filled', 'g');
xlabel('Time index(n)'), ylabel('Amplitude');
xlim([nx(1)-1 nx(end)+1]);
title('Input signal x(n)'), grid;
%ploting output signal
subplot(3, 1, 3);
stem(ny, y, 'r', 'filled');
xlabel('Time index(n)'), ylabel('Amplitude');
xlim([ny(1)-1 ny(end)+1]);
title('Output y(n)'), grid;
DSP LAB TASKS
Roll No. 26
The system analyzed in the lab generates the following output to the input signal
x(n)={0,0,1,0,0} :
↑
y(n) = [0, 0, 3, 2, 1, −2, 1, 0, −4, 0, 3, 0, 0]
↑
2. Can you prove the commutative property of the convolution?
Matlab Code:
clear all, close all, clc;
% Impulse response
h = [2, 0, -3, 4];
% Input signal
x = [1, 2, 3, 4];
% Convolving h(n) with x(n)
y1 = conv(h, x);
subplot(2, 1, 1);
stem(y1, 'filled', 'k');
xlabel('Time index(n)'), ylabel('Amplitude');
title('y1(n) = h(n)*x(n)'), grid on;
subplot(2, 1, 2);
stem(y2, 'filled', 'k');
xlabel('Time index(n)'), ylabel('Amplitude');
title('y2(n) = x(n)*h(n) '), grid on;
DSP LAB TASKS
Roll No. 26
Comments: It can be observed that both the outputs are same whether you convolve
x(n) with h(n) or h(n) with x(n) proving the commutative property of convolution.
% Plotting
0
figure;
subplot(3, 1, 1),
-1
plot(nx1, x1, 'r'); 0 2 4 6 8 10
xlabel('Time index (n)'), Time index (n) 10 4
ylabel('Amplitude'); 0.1
audio
xlim([nx1(1) - 1, nx1(end) +
Amplitude
1]), 0
title('Shotgun'),
grid on;
-0.1
0 2 4 6 8 10
subplot(3, 1, 2), Time index (n) 10 4
plot(nx2, x2, 'g'); 5
Convolved Output
xlabel('Time index (n)'),
Amplitude
ylabel('Amplitude'); 0
xlim([nx1(1) - 1, nx1(end) +
1]); -5
title('audio'), grid on; 0 2 4 6 8 10
Time index (n) 10 4
subplot(3, 1, 3)
plot(nxz, z, 'b');
xlabel('Time index (n)'),
ylabel('Amplitude');
xlim([nx1(1) - 1, nx1(end) + 1]);
title('Convolved Output'), grid on;
% listening to the voice
sound(z, Fs2);
% Saving the convolved audio
% Normalize the signal z to the range [-1, 1]
z_normalized = z / max(abs(z));
audiowrite('D:\quick access\5th semester\DSP\conv.wav', z_normalized, Fs2);
Comments: My normal voice which was captured in my room was convolved with the
impulse response i.e. a shotgun sound. The convolved output felt like the voice was recorded in
an auditorium. Following are the plots obtained from matlab.