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
% 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
title('Input signal','FontWeight','b','FontSize',15)
grid on
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])
    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]); 
    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.OuterPosition=[0 0 1 1]; %Open Current Figure in Maximized Mode

I have also attached a snap and highlighted relevant lines enter image description here

  • $\begingroup$ Yes I agree with you; it appears the author has mistakenly swapped comments of line 15 and 16 $\endgroup$ Commented Mar 13, 2023 at 12:51

2 Answers 2


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.

  • $\begingroup$ please explain your point(3). Do you meant to say that it can be replace Line 15 of code?? $\endgroup$
    – DSP_CS
    Commented Mar 13, 2023 at 14:03
  • $\begingroup$ yes. It replaces 14 and 15. $\endgroup$
    – Hilmar
    Commented Mar 13, 2023 at 14:35
  • $\begingroup$ 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? $\endgroup$
    – DSP_CS
    Commented Mar 13, 2023 at 14:41
  • 1
    $\begingroup$ Both are needed. Maybe you should consider basic Matlab class to get started? No offense, but that seems like a very basic question. $\endgroup$
    – Hilmar
    Commented Mar 13, 2023 at 14:46
  • $\begingroup$ h = t./t doesn’t work where h is 0. $\endgroup$ Commented Mar 15, 2023 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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.