myfft.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. function [X,f,P,Nt]=myfft(x,dtORfs,plotSpec)
  2. % [X,f,P,Nt]=myfft(x,dtORfs,plotSpec)
  3. %
  4. % Inputs
  5. % x : time domain sigmal
  6. % dtORfs : sampling period or frequency. Currently assumes that dtORfs <=1
  7. % is a sampling period, whereas dtORfs >1 is a sampling frequency.
  8. % plotSpec: =1 => show plot; not specified, or =0 => don't plot
  9. %
  10. % Outputs
  11. % X : fft(x)
  12. % f : frequency range (Hz)
  13. % P : power spectrum of x
  14. % Nt: time stamp for x (based on your input dtORfs).
  15. %
  16. % Example:
  17. % dt=0.01; t=0:dt:5;
  18. % x=7*sin(2*pi*10*t) + 19*sin(2*pi*23.5*t);
  19. % [X,f,P,Nt]=myfft(x,dtORfs,1);
  20. %
  21. % Note in this example that the multiplier of t inside the sin function (i.e,. 2*pi*10)
  22. % is frequency in radians/sec (omega). Recall that f = 2*pi*omega. f is in Hz.
  23. % So in the example above, the two frequencies are 10Hz or 2*pi*10 rad/s;
  24. % and 23.5Hz or 2*pi*23.5 rad/s
  25. %
  26. % Shreesh P. Mysore
  27. % shreesh@stanford.edu
  28. % 2.10.07
  29. %
  30. xhat=[];
  31. if nargin<3, plotSpec=0; end
  32. %-- dt or fs?
  33. if dtORfs > 1, Fs = dtORfs; dt = 1/Fs; else dt = dtORfs; Fs =1/dt; end
  34. % min sampling frequency is 1Hz just as a way to identify what is being inputted.
  35. %-- fft
  36. N=length(x); Nt = dt*(1:N);
  37. Nfft = 2^nextpow2(N); %N=2^round(log2(N));
  38. %X=fft(x,Nfft)/N;Pxx=2*abs(X);P=Pxx(1:Nfft/2);
  39. X=fft(x,N)/N;Pxx=2*abs(X);
  40. %<==>
  41. %X = fft(x);Pxx = 2*sqrt(X.*conj(X))/N; %Pxx = X.*conj(X)/N;
  42. %-- power spectrum %magnitude of complex Fourier coefficients)
  43. P=Pxx(1:floor(N/2)+1,:);
  44. %P=0.5*Pxx; % all points in Pxx
  45. % %-- phase of coefficients
  46. phaseAngle = unwrap (angle (X(1:floor(N/2)+1))); %in radians
  47. phaseAngle = phaseAngle*180/pi; %in degrees
  48. %-- frequency range
  49. f = Fs/2*linspace(0,1,Nfft/2);
  50. f = Fs/2*linspace(0,1,floor(N/2)+1);
  51. f = ((0: 1/(floor(N/2)+1): 1-1/(floor(N/2)+1))*Fs/2).';
  52. %[length(f) N]
  53. % pause
  54. %f = ((0: 1/N: 1-1/N)*Fs).'; %all points in Pxx
  55. %-- plotting
  56. if plotSpec==1,
  57. figure
  58. % ind = find(f>0 & f~=10); P(ind) = zeros(size(ind));
  59. % X(ind) = zeros(size(ind));
  60. % X = [X(1:Nfft/2) fliplr(X(1:Nfft/2))];
  61. %xhat = ifft(N*X);
  62. subplot(3,1,1), plot(Nt,x);xh=xlabel('time (seconds)');
  63. set(gca,'fontsize',12)
  64. yh=ylabel('x'); set(xh,'fontsize',15);set(yh,'fontsize',15)
  65. axis tight;
  66. subplot(3,1,2), plot(f,P,'.-'),
  67. set(gca,'fontsize',12)
  68. xh=xlabel('frequency (Hz)'); yh=ylabel('| fft(x) |'); %th = title('Frequency spectrum of X');
  69. set(xh,'fontsize',15);set(yh,'fontsize',15); %set(th,'fontsize',20);
  70. %subplot(3,1,3), plot(Nt,xhat(1:N));title('x'), xlabel('time (seconds)')
  71. set(gcf,'color','w')
  72. % subplot(3,1,3),
  73. % plot(f,phaseAngle,'-'),
  74. % set(gca,'fontsize',12); set(gca,'ylim',[-180 180])
  75. % xh=xlabel('frequency (Hz)'); yh=ylabel('phase (deg)');
  76. % set(xh,'fontsize',15);set(yh,'fontsize',15);
  77. % %subplot(3,1,3), plot(Nt,xhat(1:N));title('x'), xlabel('time (seconds)')
  78. % set(gcf,'color','w')
  79. end