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

DSP Convolution On Matlab

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

DSP Convolution On Matlab

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

DSP LAB TASKS

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);

% Convolving x(n) with h(n)


y2 = conv(x, h);

% Plotting the results


figure;

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.

3. Modify the code to prove Associative and Distributed properties of the


convolution.
close all, clear all; clc;
% Define three input sequences
x = [1, 2, 3, 4];
h = [1, 4, 3];
g = [1, 1, 4];
% commutative property
y1 = conv(x, h); % x * h
y2 = conv(h, x); % h * x
% associative property
ap_lhs = conv(conv(x, h), g);
ap_rhs = conv(x, conv(h, g));
% distributive property
dp_lhs = conv(x, h + g);
dp_rhs = conv(x, h) + conv(x, g);
% Plot the comparisons
figure;
% Commutative Property Comparison
subplot(2, 1, 1);
stem(y1, 'filled', 'r');
title('y(n) = x(n) * h(n)');
subplot(2, 1, 2);
stem(y2, 'filled');
title(' y(n) = h(n) * x(n)');
suptitle('Commutative Property');
% Associative Property Comparison
figure;
subplot(2, 1, 1);
stem(ap_lhs, 'filled','k');
title('y(n) = (x(n) * h(n)) * g(n)');
subplot(2, 1, 2);
stem(ap_rhs, 'filled', 'b');
title('y(n) = x(n) * (h(n) * g(n))');
suptitle('Associative Property');
% Distributive Property Comparison
figure;
subplot(2, 1, 1);
stem(dp_lhs, 'filled');
title('y(n)= x(n) * (h(n) + g(n))');
subplot(2, 1, 2);
stem(dp_rhs, 'filled', 'r');

Comments: The laws of association and


distribution were further applied to the previous
code. It can be observed that the convolution
follows the associative and distributive properties
too. The output is similar for every case.
DSP LAB TASKS
Roll No. 26

4. Convolve your recorded sound with drumloop.wav. Note your observation


a) Plot the output.
b) Listen the output.
Matlab Code:
clear all; close all; clc;
%using audioread command to access .wav files
[x1, Fs1] = audioread('D:\quick access\5th semester\DSP\kar98k.wav');
[x2, Fs2] = audioread('D:\quick access\5th semester\DSP\output44.1k.wav');
x1 = x1(:, 1);
x2 = x2(:, 1);
org_x1 = 1;
nx1 = [1:length(x1)] - org_x1;
org_x2 = 1;
nx2 = [1:length(x2)] - org_x2;
% Perform convolution
z = conv(x1, x2);
nxz = [nx1(1) + nx2(1): 1
Shotgun
nx1(end) + nx2(end)];
Amplitude

% 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.

You might also like