Convolution Demo
Contents
Download MATLAB files: conv.zip
Show f(x) and h(x)
x = linspace(-4,4,161); % plot f(x) y = f(x); subplot(2,1,1); plot(x,y,'k','LineWidth',2); ylabel('f(x)'); axis([-4 4 0 4]); % plot h(x) subplot(2,1,2); y = h(x); plot(x,y,'k','LineWidth',2); ylabel('h(x)'); axis([-4 4 0 4]);
Show convolution
Reference: g.m (analytic result)
subplot; x = linspace(-8,8,321); dx = x(2)-x(1); g1 = g(x); g2 = convn(f(x),h(x),'same')*dx; plot(x,g1,x,g2,'LineWidth',2); axis([-8 8 0 4]);
Another example
x = linspace(-8,8,321); dx = x(2)-x(1); f1 = rect(x); f2 = rect(x,4); g3 = convn(f1,f2,'same')*dx; subplot(3,1,1); plot(x,f1,'b','LineWidth',2); ylabel('f1(x)'); axis([-8 8 0 2]); subplot(3,1,2); plot(x,f2,'g','LineWidth',2); ylabel('f2(x)'); axis([-8 8 0 2]); subplot(3,1,3); plot(x,g3,'k','LineWidth',2); ylabel('f1(x)*f2(x)'); axis([-8 8 0 2]);
polynomial
x^2 - x + 3 2x + 1 ------------ 2 x^3 -2 x^2 + 6 x x^2 - x + 3 ---------------------- 2 x^3 - x^2 + 5 x + 3
subplot; p1 = [1 -1 3]; % x^2 - x + 3 x = linspace(-8,8,321); y = polyval(p1,x); plot(x,y); p2 = [2 1]; % 2x+1 p3 = conv(p1,p2); disp('results of polynomial convolution'); disp(p3)
results of polynomial convolution 2 -1 5 3