So I want to do the following convolution (dirac(t+1)+2*dirac(t-1))*dirac(t-3), but I get a weird value. Normally I would think every thing is shifted with 3 units to the right but the 2*dirac(t-1) stays at t=2 instead of the expected t=4. Why is this?
%Chapter 3_a
clear all
close all
clc
%% excercise 3
t = -5:0.1:5;
x_1 = dirac(t+1);
idt = x_1 == Inf; % find Inf
x_1(idt) = 1; % set Inf to finite value
x_2 = 2*dirac(t-1);
idt = x_2 == Inf; % find Inf
x_2(idt) = 1; % set Inf to finite value
figure
x = x_1 + x_2;
stem(t,x)
hold on
figure
y = dirac(t-3);
idt = y == Inf; % find Inf
y(idt) = 1; % set Inf to finite value
stem(t,y)
figure
w = conv(x,y)
t_2 = linspace(-5,5,length(w));;
stem(t_2,w)
The problem is in your second to last line,
To understand how to fix this line, it should be first noted that
convknows nothing about the time axes. When you computew = conv(x,y), it is only assumed thatxandy.xandyare thought of as being implicitly extended with zeros to the left and to the right.The result
whas a length equal to thelength(x)+length(y)-1. This is becauseconvcomputes the output with the minimum length that ensures that all values outside that length would necessarily be zero (because of assumption 2). For example,As is seen, there is no reference to the time axes in the inputs or in the output. It does not matter, for example, if
xstarts at t=0or at t=8. So you need to keep track of the time axes separately. Specifically, if (the specified portions of)xandystart at t=aand t=brespectively, you know that the resultwwill start at t=a+b. Since you also know the length ofwand the time between samplesTs, you can define the time axis forwasa+b + (0:length(w)-1)*Ts.In your case, since
you are defining
xandywith time origint(1)=5and sample spacingt(2)-t(2)=0.1. So you need to create the time axis forwas follows (replace your second to last line):With this, you will see that the two deltas of
x, located at t=-1and t=1, appear at t=2and t=4respectively when you runstem(t_2,w).