# Different vector length on plotting convolution of Hanning signal with Dirac Delta signal

4 views (last 30 days)
Meng Fei Wong on 30 Nov 2021
Commented: Meng Fei Wong on 7 Jan 2022
Hello all,I would like to ask a question about different vector length on plotting convolution of Hanning signal with Dirac Delta signal
I have created a Cosine signal (HANNING) signal which looks like below: Also, I have created a Dirac Delta signal which looks like below: I would like to take the Dirac Delta time length to show my convolution result. However, it prompts me "Vectors must be the same length" error. At the end, I have to use Hanning signal time length to show my convolution result which looks like below: I believe this plot is not really shows the complete picture of my convolution result. Therefore, I would like to ask what can I do to fix this? Below is my code for the reference:
close all;
clear all;
clc;
dt = 0.1;
xt= -1+2:dt:1+2;
yt= -5:dt:5;
%construction of the Cosine signal (HANNING) function as x1(t)
x = 0.5*[1+cos(pi*xt)];
%construction of dirac pulse
y= dirac(yt)+dirac(yt-1)+dirac(yt-2);
idx = y == Inf; % find Infinite Value
y(idx)=1; %set Inifinite value to finite Value=1
figure;
plot(yt,y,'linewidth',2);
set(gca,'fontsize',14,'fontweight','bold');
grid on;
xlabel('t');
ylabel('x3(t)');
title('Dirac pulse x3(t)');
hold on
figure;
plot(xt,x,'linewidth',2);
set(gca,'fontsize', 14, 'fontweight', 'bold');
grid on;
xlabel('t');
ylabel('x1(t)');
title('Cosine signal (HANNING) Function x1(t)');
z = conv(x,y,'same');
hold on
figure;
plot(xt,z,'linewidth',2);
set(gca,'fontsize', 14, 'fontweight', 'bold');
grid on;
title('Convolution of x1(t)and x3(t)');
xlabel('t');
ylabel('y(t)= x1(t)*x3(t)');
hold on;

Paul on 30 Nov 2021
It's easier to make it work if the signals are defined over the same time vector. And use the full convolution.
dt = 0.1;
xt= -1:dt:5; % new
yt= -1:dt:5; % new
%construction of the Cosine signal (HANNING) function as x1(t)
x = 0.5*[1+cos(pi*xt)].*(xt>=1 & xt<=3); % define the window
%construction of dirac pulse
y= dirac(yt)+dirac(yt-1)+dirac(yt-2);
idx = y == Inf; % find Infinite Value
y(idx)=1; %set Inifinite value to finite Value=1
figure;
plot(yt,y,'linewidth',2);
set(gca,'fontsize',14,'fontweight','bold');
grid on;
xlabel('t');
ylabel('x3(t)');
title('Dirac pulse x3(t)');
hold on figure;
plot(xt,x,'linewidth',2);
set(gca,'fontsize', 14, 'fontweight', 'bold');
grid on;
xlabel('t');
ylabel('x1(t)');
title('Cosine signal (HANNING) Function x1(t)');
z = conv(x,y,'full');
hold on figure;
plot(xt(1)+yt(1)+(0:(numel(z)-1))*dt,z,'linewidth',2);
set(gca,'fontsize', 14, 'fontweight', 'bold');
grid on;
title('Convolution of x1(t)and x3(t)');
xlabel('t');
ylabel('y(t)= x1(t)*x3(t)');
hold on; The answer can also be obtained exactly using symbolic math
clear
syms x(t) y(t)
x(t) = 0.5*(1+cos(sym(pi)*t))*rectangularPulse(1,3,t);
y(t) = dirac(t) + dirac(t-1) + dirac(t-2);
syms tau
z(t) = int(x(tau)*y(t-tau),tau,-inf,inf)
z(t) = figure
fplot(z(t),[-2 10]),grid ##### 1 CommentShowHide None
Meng Fei Wong on 7 Jan 2022
Thank you very much. It works perfect now