Convolution function and shifting
50 views (last 30 days)
Show older comments
Hello, I am having a weird issue and don't know if maybe I am going a little crazy. I am testing something in matlab using a simple convolution. I want to convolve a shifted rectangular function with itself. a rect conv rect with no shift is a triangle centered at the center of the rects. So when I shift by some amount I expect the same triangle but shifted by the same amount the rects were shifted. Here is some code:
functionU = @(x,t0) rectangularPulse(x-t0);
t = -5:0.01:5;
test = conv(functionU(t,2),functionU(t,2),'same').*0.01;
figure;plot(t,test);
when I plot that the triangle is centered at 4, not at 2 like you would expect. I then set the shift of one of the functions to 0 and I get what i expect. Why is this the behavior of the function?
0 Comments
Answers (3)
John BG
on 9 Mar 2017
Edited: John Kelly
on 9 Mar 2017
Hi thatguy14
I have fixed your script, it didn't have a time reference.
MATLAB functions conv and conv2 do not have time reference. It's done on purpose to avoid conv and conv2 having to handle time resolution problems.
clear all;close all
Pspan=2
f1 = @(P,t0,v) rectangularPulse(-t0-P/2,-t0+P/2,v);
dt=.01
t = -5:dt:5;
nt=1/dt*t
tdelay1=0
y1=f1(Pspan,tdelay1,t)
figure(1);plot(t,y1)
nt1=1/dt*t
tdelay2=-2
y2=f1(Pspan,tdelay2,t)
figure(2);plot(t,y2)
nt2=1/dt*t
nyv=nt1(1)+nt2(1)
nye=nt1(length(y1))+nt2(length(y2))
ny=[nyv:nye]
ty=ny*dt
y3=conv(y1,y2);
figure(3);plot(ty,y3)
this is a simplified version of another convolution function WITH time reference that assumes the time vectors of y1 and y2 are equal in amount and start stop moments, your vector t for y1 y2 is just that, a good reference to then generate nt1 and nt2.
Note that I have used the variable dt right on the definition of t.
The key point for this simplified version to work is to amplify to so that you get the numerals
dt=.01
t = -5:dt:5;
nt1 and nt2 have to be vector indices.
Also, I learnt signal processing with negative time delays meaning shift scope left and positive time delays meaning shift scope right. If you want it reversed the function has to be redefined
f1 = @(P,t0,v) rectangularPulse(t0-P/2,t0+P/2,v);
Appreciating time and attention, thanks in advance
To any other reader, please if you find this answer of any help solving your question, please click on the thumbs-up vote link, thanks too
John BG
3 Comments
John BG
on 9 Mar 2017
thanks for accepting my answer
the literature reference Signal Processing with MATLAB by Ingle Proakis, recommends to use signal + time reference as often possible.
processing signals without reference vectors may cause the odd alias you mention in the question.
John BG
on 9 Mar 2017
Edited: John BG
on 9 Mar 2017
fixed it, thanks for pointing out that the result was not aligned.
Please have a look and let me know if now this deserves an Accepted Answer. Please not that it works as long as td1 td2 small, I have tested for
td1=-1; td2=-2;
td1=-1; td2=2;
td1=1; td2=-2;
td1=1; td2=2;
and it works ok, but if for instance td1=11;td2=2 the result is off scope
clear all;close all
Pspan=2
f1 = @(P,t0,v) rectangularPulse(-t0-P/2,-t0+P/2,v);
dt=.01
t = -5:dt:5;
nt=1/dt*t
td1=-1
td2=-2
tdelay1=td1
y1=f1(Pspan,tdelay1,t)
figure(1);
subplot(3,1,1);plot(t,y1)
nt1=1/dt*t
tdelay2=td2
y2=f1(Pspan,tdelay2,t)
% figure(2);
subplot(3,1,2);plot(t,y2)
nt2=1/dt*t
nyv=nt1(1)+nt2(1)
nye=nt1(length(y1))+nt2(length(y2))
ny=[nyv:nye]
ty=ny*dt
y3=conv(y1,y2);
if sign(td1*td2)>0
ty2=ty+(td2-td1);
else
ty2=flip([ty-(td2-td1)]);
end
% figure(3);
subplot(3,1,3);ax3=plot(ty2,y3);
hax3=ax3.Parent;
hax3.XTick=[-15:1:15]
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
0 Comments
thatguy14
on 9 Mar 2017
1 Comment
John BG
on 10 Mar 2017
Edited: John BG
on 12 Mar 2017
to any other reader: in this question operator * means convolution.
Thatguy14
Thanks for this clarification, but your delta reasoning is correct EXCEPT for your last line of comment 20:30 Mar 9th, let me explain:
1.
where you wrote:
h(t-t01-t02)
it should be
h(t-(t02-t01))
Although f(t) * g(t) = g(t) * f(t)
convolution looses the time references that f and g have to reference axis t, the result only takes into account
t1-t2
see it this way, f and g are f(t) g(t), but h=f*g is not h(t), but h(tau).
As example, 2 deltas delayed with different delays,
d(t-t1) * d(t-t2)
their correlation doesn't take place at the sum of delays, does it? it takes place right on min(t1-t2,t2-t1), doesn't it
Regards
John BG
See Also
Categories
Find more on Digital Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!