# Confusion regarding MATLAB code of convolution animation?

I am trying to understand a MATLAB code of convolution animation.

Please check above link especially code in lines 15 and 16. I think author has mistakenly swapped comments of line 15 and 16. Please correct me if i am wrong

I am also placing the code here

close all
clear all
clc
% Initialization
t = -400:400; % Time range
X = zeros(1,length(t)); % Input signal
x1 = -150; x2 = 150; % Range of Input Signal
H = zeros(1,length(t)); % Impulse Response
h1 = -50; h2 = 50; % Range of Impulse Response Signal
Y = zeros(1,length(t)); xh1 = x1+h1; xh2 = x2+h2; % Convolution output

% Input Signal Generation
x = sin(pi*t/10)./(pi*t/10); x(t==0)=1; % Generate Input Sinc Signal
h = t./t; h(t==0)=1; % Generate Impulse Response
H(t>=h1&t<=h2) = h(t>=h1&t<=h2); % Fit the input signal within range
X(t>=x1&t<=x2) = x(t>=x1&t<=x2); % Fit the impulse response within range

% Plot Input Signal and Impulse Response
subplot(3,2,1)
plot(t,X,'b','LineWidth',3)
title('Input signal','FontWeight','b','FontSize',15)
grid on
subplot(3,2,2)
plot(t,H,'r','LineWidth',3)
title('Impulse Response','FontWeight','b','FontSize',15)
grid on

for n = xh1:xh2 % Convolution limits
% Convolution steps
f = fliplr(X);           % Flip
Xm = circshift(f,[0,n]); % Shift
m = Xm.*H;               % Multiply
Y(t==n) = sum(m);  % Sum

% Live Convolution Process
subplot(3,2,[3 4])
plot(t,H,'r',t,circshift(fliplr(X),[0,n]),'b','LineWidth',3)
title('Convolution Process: 1. Flip, 2. Shift, 3. Multiply 4.Sum','FontWeight','b','FontSize',15)
grid on;
% Result of convolution live
subplot(3,2,[5 6]);
plot(t,Y,'k','LineWidth',3)
axis tight
grid on
title('Convolution Output','FontWeight','b','FontSize',15)
pause(0.01); % Pause between Update
sgtitle('Illustration of 1D Convolution','FontWeight','b','FontSize',20)
fig=gcf;
fig.Units='normalized';
fig.OuterPosition=[0 0 1 1]; %Open Current Figure in Maximized Mode
end

I have also attached a snap and highlighted relevant lines

• Yes I agree with you; it appears the author has mistakenly swapped comments of line 15 and 16 Mar 13 at 12:51

Personally I found this code to be a little awkward but that's maybe just a matter of personal preference.

1. Yes, the comments are swapped. Keep in mind though that convolution is commutative, i.e. $$x*h = h*x$$ and it doesn't matter which signal we call the impulse response or the input signal.
2. h = t./t would be more readable as h = ones(size(t));
3. Instead of copying the content, it feels more intuitive to explicitly set the non-signals areas to 0, i.e. H = h; H(t < h1 | t > h2) = 0;

Then again, that's just my personal preference.

• please explain your point(3). Do you meant to say that it can be replace Line 15 of code?? Mar 13 at 14:03
• yes. It replaces 14 and 15. Mar 13 at 14:35
• you have written two command in your point (3). H = h; and H(t < h1 | t > h2) = 0; which of these two commands replaces line 15 of code?or they both need to be used? Mar 13 at 14:41
• Both are needed. Maybe you should consider basic Matlab class to get started? No offense, but that seems like a very basic question. Mar 13 at 14:46
• h = t./t doesn’t work where h is 0. Mar 15 at 21:23

You may also see the Convolution Animation script from Matlab FileExchange. It has comments to make it easier to understand the process and flexibility to change the functions.