Advertisement
Guest User

Untitled

a guest
Aug 17th, 2011
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. function [] = fseriesdemo(f,b,NN)
  2. % FSERIESDEMO Plots a function f and it's Fourier series approximation.
  3. % FSERIESDEMO(f,b,N) plots the function f and it's N term Fourier series
  4. % approximation from zero to b. FSERIESDEMO uses the FFT to approximate
  5. % the Trigonometric Fourier Series coefficients. FSERIESDEMO only works
  6. % for the function from 0 to b. Input N must be an integer >=2.
  7. % Example:
  8. %
  9. % f = @(x) sin(x)./x+(cos(x)).^2-exp(x/8)+x.^(x/20);
  10. % fseriesdemo(f,20,100) % Approx. f on (0,20) with 100 terms.
  11. %
  12. %
  13. % Author: Matt Fig
  14. % Contact: popkenai@yahoo.com
  15.  
  16. % First some argument checking.
  17. if nargin<3
  18. error ('Not enough arguments. See help.')
  19. end
  20.  
  21. if ~isreal(NN) || ~isreal(b) || b<=0 || NN < 2 || floor(NN)~=NN
  22. error ('2nd and 3rd args must be positive real scalars. See help.')
  23. end
  24.  
  25. n = 2*NN;
  26. T = b-eps+(b-eps)/(n-2); % Get the appropriate endpoint for fft.
  27. x = linspace(eps,T,n+1); % Use this x in fft.
  28. x(end) = []; % But not the endpoint.
  29. fun = f(x); % Get discrete points.
  30. FUNC = fft(fun); % Call fft
  31. FUNC = [conj(FUNC(NN+1)) FUNC(NN+2:end) FUNC(1:NN+1)]/n; % Shift/scale.
  32. A0 = FUNC(NN+1); % First cosine term.
  33. AN = 2*real(FUNC(NN+2:end)); % General cosine term.
  34. BN = -2*imag(FUNC(NN+2:end)); % General sine term.
  35.  
  36.  
  37. %My Version
  38. function [a0 ak bk]=fcoeffs_fft(profile,angles,Ncoeffs)
  39. %FCOEFFS_FFT Fourier coefficients (trigonometric) via Fast Fourier Transform (FFT)
  40. % [a0,ak,bk]=fcoeffs_fft(profile,angles,Ncoeffs) determines the Fourier coefficients of
  41. % irregulary spaced angles and profile.
  42. % Ncoeffs is the number of Fourier coefficients to return
  43. % Oversamples input via a (periodic) cubic spline interpolation
  44. % Returns DC component a0, cosine terms ak and sine terms bk
  45.  
  46.  
  47. %Oversampling the data points so the total number of points is Ntheta
  48. Ntheta = 1024;
  49. theta = linspace(-pi,pi,Ntheta);
  50. delta_theta = 2*pi/Ntheta;
  51.  
  52. %This defines a temporary vector that "wraps around" pi to -pi
  53. theta_over = -pi:delta_theta:(pi + 5*delta_theta);
  54.  
  55. %Creating the function to which the fft can be applied
  56. pp = spline(angles,profile);
  57. zeta_temp= ppval(pp,theta_over);
  58. %Overwrite here
  59. zeta = zeros(size(theta));
  60. zeta(1:Ntheta) = zeta_temp(1:Ntheta);
  61. zeta(1:6)=zeta_temp(Ntheta:Ntheta +5);
  62.  
  63. zetahat=fft(zeta);
  64.  
  65. a0 = zetahat(1)/Ntheta; %DC component
  66.  
  67. for ik = 1:Ncoeffs
  68. ij = ik +1; %index for fft
  69.  
  70. ak(ik)=2*real(zetahat(ij))/(Ntheta); %cosine terms
  71. bk(ik)=-2*imag(zetahat(ij))/(-1.*Ntheta); %sine terms
  72. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement