MATLAB Answers

0

Convolution function and shifting

Asked by thatguy14 on 8 Mar 2017
Latest activity Edited by John BG
on 12 Mar 2017
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

Sign in to comment.

3 Answers

Answer by John BG
on 9 Mar 2017
Edited by 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

Hi thanks for the detailed response. I think I understand your solution though the technicals I will need to think about. I guess my signal processing background ins't nearly as strong as I thought it was. Do you have any recommendations as to a book or reference that could help bolster my background knowledge?
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.
Something actually doesn't make sense about your answer. It actually doesn't seem to "solve" the issue rather than just adds some steps. Maybe I didn't phrase my question correctly.
In my original post I said that if I have 2 shifted functions (two rects centered at t = 2units) I should get a triangle centered at 2units also (it's the same as if you were to do two rects centered at 0). However, when I shift both the functions I get it at the shift*2 (using my original code if I instead set the time delay from 2 to 3 for both functions it is now centered at 6).
All the other steps you include I don't think are necessary. My code does the exact same as yours if I define my second function to have no time delay i.e.,
functionU = @(x,t0) rectangularPulse(x-t0);
t = -5:0.01:5;
test = conv(functionU(t,2),functionU(t, *0*),'same').*0.01;
figure;plot(t,test);
where I have bolded the change.
I did a bit more investigation and it seems to be an implementation thing as you said. I looked at the wikipedia on convolutions and the relevent part is under Definition>Notation that describes the engineering convention. You can see:
f(t)*g(t-t0) is equivalent to f*g(t-t0) but f(t-t0)*g(t-t0) is in fact equivalent to f*g(t-2t0).
I am not sure how to interpret this in this context but it is obviously related. If I could get some input at this point that would be great.

Sign in to comment.


Answer by John BG
on 9 Mar 2017
Edited by 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

Sign in to comment.


Answer by thatguy14 on 9 Mar 2017

I actually answered my own question and it has nothing to do with the code involved (my code does everything the other answer's code did but in fewer lines. This is actually due to the math behind convolutions. For anyone else that may stumble upon this question see the following.
if I have a function f(x) and g(x) and their convolution is h(x):
if I shift f(x) by x0
f(x-x0) * g(x) using the sifting property of delta functions i.e., f(x-x0) = f(x) * delta(x-x0) Also using the associative property of convolutions:
[f(x) * delta(x-x0)] * g(x) = [f(x) * g(x)] * delta(x-x0) = h(x) * delta(x-x0) = h(x-x0)
it's easy to see that if you were to apply a shift to BOTH (lets say t01 and t02) you would get:
h(x-t01-t02) which is what I was seeing when I was applying a shift to both terms.

  1 Comment

John BG
on 10 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

Sign in to comment.