% Matlab example of lecture convolution example % % x(t) = exp(-a t) u(t); % y(t) = r(t); % a = 4; del_t = .001; tx = 0:del_t:10; th = 0:del_t:20; Lx= length(tx); Lh= length(th); tp = [(tx(1)+th(1)):del_t:(tx(Lx)+th(Lh))]; Lp = length(tp); x = exp(-a .* tx) .* stp_fn(tx); h = rmp_fn(th); ye = ((1/a) .* rmp_fn(tp)) - (1/(a*a)) .* (1 - exp(-a .* tp)); y = del_t * conv(x,h); % Multiply by step size to approximate % rectangular rule integration. figure(3) subplot(3,1,1), plot(tx,x,'LineWidth',6), xlabel('t'), ylabel('x(t)'); title('x(t)'); subplot(3,1,2), plot(th,h,'LineWidth',6), xlabel('t'), ylabel('h(t)'); title('h(t)'); subplot(3,1,3), plot(tp,y,'r','LineWidth',16); hold on; plot(tp,ye,'b','LineWidth',12), xlabel('t'), ylabel('y(t){r} & y[t]{b}'); title('y(t) = x(t) * h(t) \{comp. red\} \{hand blue\}'); hold off; figure(4) subplot(4,1,1),plot(tp,(y-ye),'r','LineWidth',6); axis([tp(1) (th(Lh)- tx(Lx)) -.001 .04]) title('error primarily due to truncation of x(t)'); subplot(4,1,3),plot(tp,(y-ye),'r','LineWidth',6); axis([(th(Lh)-1) (th(Lh)+tx(Lx)) -10 0.1]) title('error primarily due to truncation of h(t)'); tx = 0:del_t:1; th = 0:del_t:10; Lx= length(tx); Lh= length(th); tp = [(tx(1)+th(1)):del_t:(tx(Lx)+th(Lh))]; Lp = length(tp); x = exp(-a .* tx) .* stp_fn(tx); h = rmp_fn(th); ye = ((1/a) .* rmp_fn(tp)) - (1/(a*a)) .* (1 - exp(-a .* tp)); y = del_t * conv(x,h); % Multiply by step size to approximate % rectangular rule integration. figure(3) subplot(3,1,1), hold on plot(tx,x,'g','LineWidth',2), xlabel('t'), ylabel('x(t)'); title('x(t)'); hold off subplot(3,1,2), hold on plot(th,h,'g','LineWidth',2), xlabel('t'), ylabel('h(t)'); title('h(t)'); hold off subplot(3,1,3), hold on plot(tp,y,'g','LineWidth',6); plot(tp,ye,'y','LineWidth',4), xlabel('t'), ylabel('y(t){g} & y[t]{y}'); title('y(t) = x(t) * h(t) \{comp. red\} \{hand blue\} \{comp. green\} \{hand yellow\} '); hold off figure(4) subplot(4,1,2) plot(tp,(y-ye),'g','LineWidth',3); axis([tp(1) (th(Lh)- tx(Lx)) -.04 .005]) title('error primarily due to truncation of x(t)'); subplot(4,1,4) plot(tp,(y-ye),'g','LineWidth',3); axis([(th(Lh)-1) (th(Lh)+tx(Lx)) -5 .1]) title('error primarily due to truncation of h(t)');