How can I plot convolution of rect(t/2)*sgn(t)

조회 수: 13 (최근 30일)
courtney
courtney 2012년 8월 31일
The exercise I am trying to complete requires me to: Compute and plot the following convolution integral by using "conv.m" in MATLAB. rect(t/2)*sgn(t)
CODE 1 t=-5:0.01:5; 2 x=abs(t)<1; 3 h=sign(t); 4 y=conv(x,h); 5 plot(t,y);
ERRORS Error using conv2 First and second arguments must be single or double.
Error in conv (line 39) c = conv2(a(:),b(:),shape);
Error in three (line 4) y = conv(x,h);
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 31일
what is
2 x=abs(t)<1
courtney
courtney 2012년 9월 1일
x=abs(t)<1 gives the equivalent of rect(t/2) where: rect(t) = [ 1, abs(t) < 0.5 & [ 0, abs(t) > 0.5 as piecewise fucntion.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2012년 9월 1일
You have to make the small adjustment of plotting every other value of y (since y is about twice the length of t), but if you want to plot y as a function of t, this will work:
t = -5:0.01:5;
x = double(abs(t) < 1)); % Convert logical to double
h = sign(t);
y = conv(x,h);
figure(1)
plot(t,y(1:2:end)) % Plot every other value of y
  댓글 수: 1
courtney
courtney 2012년 9월 2일
I ended up getting the correct answer using this code but your method worked as well Strider. Cheers for the help!
(the rect(t) here is referring to a function out file)
t = -6:0.01:6;
x = rect(t);
y = sign(t);
z = conv(x,y)*0.001;
plot(-12:0.01:12,z);

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 31일
t=-5:0.01:5;
x=abs(t);
h=sign(t);
[tt,y]=conv(x,h);
plot(y); % y and t have'nt the same length
  댓글 수: 2
courtney
courtney 2012년 9월 1일
편집: courtney 2012년 9월 1일
If I try to run this I get the following errors.
Error using conv Too many output arguments.
Error in test [tt,y]=conv(x,y);
Instead of using 'x=abs<1' for the equivalent of rect(t/2) I created a function out file called rect.m with the following code
function[out]=rect(t);
for i=1:length(t);
if(abs(t(i))<1)
out(i)=1;
else;
out(i)=0;
end
end
Referring to this code in my convolution file gives me the perfect rect(t/2) plot but I still get the following error.
Error using plot
Vectors must be the same lengths.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 1일
beacause y and t have'nt the same length. x , h and t have the same length but not with y=conv(x,h) . then instead of plot(t,y) which is false, use plot(y)

댓글을 달려면 로그인하십시오.


daltonicox
daltonicox 2013년 9월 13일
how can i plot this: g(t) = rect(t/2) * [δ(t+2) - δ(t+1)]. As matter of fact, i want to plot the convolution of this functions. But i'm having trouble creating the rect function. If anyone can help me, i would appreciate it very much.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by