Using conv() function

13 views (last 30 days)
Yakov
Yakov on 6 Oct 2022
Edited: Paul on 7 Oct 2022
I have wrote the function as follows
u = @(t) 1.0*(t>=0);
t = -0.25:0.1:4;
x = @(t) 1.5.*sin(pi*t).*(u(t)-u(t-1));
h = @(t) 1.5.*(u(t)-u(t-1.5))-(u(t-2)-u(t-2.5));
y = conv(h,x,'same');
Im also looking to plot y(t) afterwards, I keep getting an error at the line where the convolution actually takes place

Answers (1)

Paul
Paul on 6 Oct 2022
h, x, and u are actually functions that are evaluated at the values of their input arguments. So need to evaluate h and x at the values in the variable t (which has nothing to do with the t in the definitions of u, x, and h.
u = @(t) 1.0*(t>=0);
t = -0.25:0.1:4;
x = @(t) 1.5.*sin(pi*t).*(u(t)-u(t-1));
h = @(t) 1.5.*(u(t)-u(t-1.5))-(u(t-2)-u(t-2.5));
y = conv(h(t),x(t),'same');
  2 Comments
Yakov
Yakov on 6 Oct 2022
Oh ok I see how would I go about plotting the convolution, plot(t,y(t)) doesnt work.
Paul
Paul on 7 Oct 2022
Edited: Paul on 7 Oct 2022
That's because y is an array, not a function.
u = @(t) 1.0*(t>=0);
t = -0.25:0.1:4;
x = @(t) 1.5.*sin(pi*t).*(u(t)-u(t-1));
h = @(t) 1.5.*(u(t)-u(t-1.5))-(u(t-2)-u(t-2.5));
y = conv(h(t),x(t),'same');
plot(t,y)
Whether or not that's the correct answer for the problem at hand is a different question.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!