Advertisement
Guest User

convolution.m

a guest
Jan 6th, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.39 KB | None | 0 0
  1. % compare different ways of convolution
  2. % exp.decay (x) gauss
  3. clear all;
  4. h=0.05; %discretization step
  5. x=-10:h:10;
  6. y=zeros(1,length(x)); % non-convoluted signal
  7. lambda=1.2;
  8. for i=1:length(x)
  9.     if x(i)>=0
  10.         y(i)=lambda*exp(-x(i)*lambda); %int y dx = 1
  11.     end
  12. end
  13. %============================================
  14. % decay analytically convoluted with gauss,
  15. % integral taken from Ryzhik, Gradshtein, p. 321 #3.322.2
  16. sigma=0.25;
  17. sigma2=sigma^2;
  18. beta=sigma2/2;
  19. gamma=lambda-x./sigma2;
  20. analyt=lambda./sqrt(2*pi*sigma2).*sqrt(pi*beta).*exp(-x.^2./2./sigma2).*...
  21.        exp(beta.*gamma.^2).*(1-erf(gamma.*sqrt(beta)));
  22.  
  23. plot(x,y,'r-',x,analyt,'b.');
  24. hold on;
  25.  
  26. %============================================  
  27. % by-point convolution with internal matlab routine 'conv'
  28. g=exp(-x.^2/(2*sigma2))./sqrt(2*pi*sigma2);
  29. y1=conv(y,g);
  30. x1=-20:h:20;
  31. plot(x1,y1./length(x1)*40,'g+'); % normalization is L/Npoints
  32. % to make this closer to analytical curve, decrease h
  33. %============================================
  34. % with fourier transform
  35. f_y=fft(y);
  36. f_g=fft(g);
  37. f_conv=zeros(1,length(f_y));
  38. f_conv=f_y.*f_g;
  39. conv=abs(ifft(f_conv));
  40. % NOTE: here first point is placed at x=h
  41. % (not at x=0!)
  42. ind=202; % x(202)==h
  43. xnew=[x(ind:end),x(1:ind-1)];
  44. plot(xnew,conv.*h,'r.');%,x(ind),conv(1)*h,'mx');
  45. hold off;
  46. legend('non-conv decay','analyt. conv. decay','by-point convolution','FFT conv decay');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement