Simple Convolution Plot Errors

조회 수: 10 (최근 30일)
Anonomous
Anonomous 2016년 2월 25일
댓글: Haribabu Kilari 2016년 9월 28일
I am trying to plot a convolution example from my text book. Somewhere in the setting up of the plot dimensions/colors etc i made an error and i cannot find it. any help would be greatly appreciated.
The current compile error is on
axis ([tau (1) tau(end) -2.0 2.5]);
thank you.
% MS2p4.m : MATLAB Session 2, Program 4
% Script M-file graphically demonstrates the convolution process.
clc
clear
figure (1) % Create figure window and make visible on screen
x = inline('1.5*sin(pi*t).*(t>=0 & t<1)');
h = inline('1.5*(t>=0&t<1.5) - (t>=2&t<2.5)');
dtau = 0.005; tau = -1:dtau:4;
ti = 0; tvec = -.25:.1:3.75;
y = NaN*zeros(1, length (tvec)); % Pre-allocate memory
for t = tvec,
ti = ti+1; % Time index
xh = x(t-tau).*h(tau); lxh = length (xh);
y(ti) = sum(xh.*dtau); % Trapezoidal approximation of interal
subplot (2,1,1), plot(tau, h(tau), 'k-', tau, x(t-tau), 'k--', t, 0, 'ok');
axis ([tau (1) tau(end) -2.0 2.5]);
patch([tau(1:end-1); tau(1:end-1); tau (2:end); tau (2:end)],...
[zeros(1,lxh-1);xh(1:end-1);xh(2:end);zeros(1,lxh-1)],...
[.8 .8 .8], 'edgecolor', 'none');
c = get(gca, 'children'); set (gca, 'children', [c(2); c(3); c(4); c(1)]);
subplot (2, 1, 2), plot (tvec, y, 'k', tvec (ti), y(ti), 'ok');
xlabel ('t'); ylabel ('y(t) = \int h(\tau)x(t-\tau) d\tau');
axis ([tau(1) tau(end) -1.0 2.0]); grid;
drawnow;
axis ([tau(1) tau(end) -1.0 2.0]); grid;
drawnow;
end

채택된 답변

Image Analyst
Image Analyst 2016년 2월 26일
편집: Image Analyst 2016년 2월 26일
You have a space between tau and (1) and (2:end), so it thinks there are two separate things, and then you end up having more than 4 elements in between the brackets. Get rid of that space. Fixed code:
axis ([tau(1) tau(end) -2.0 2.5]);
patch([tau(1:end-1); tau(1:end-1); tau(2:end); tau(2:end)],...
[zeros(1,lxh-1);xh(1:end-1);xh(2:end);zeros(1,lxh-1)],...
[.8 .8 .8], 'edgecolor', 'none');
  댓글 수: 2
Anonomous
Anonomous 2016년 2월 26일
thank you very much
Haribabu Kilari
Haribabu Kilari 2016년 9월 28일
Thank you so much

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Pole and Zero Locations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by