matlab 求傅里叶变换(fft 模板)

柔光的暖阳◎ 2023-06-12 09:11 160阅读 0赞

总结:求傅里叶变换,通常必须用函数fft(), 但是求出来的是双边谱,需要转换为单边谱。这个时候有一标准化的动作:

  1. y=fft(s);
  2. p2=abs(y/L);
  3. p1=p2(1:L/2+1);
  4. p1(2:end-1)=2*p1(2:end-1);

完整代码如下:

  1. %% Noisy Signal
  2. % Use Fourier transforms to find the frequency components of a signal buried
  3. % in noise.
  4. % Specify the parameters of a signal with a sampling frequency of 1 kHz and
  5. % a signal duration of 1 second.
  6. % Copyright 2015 The MathWorks, Inc.
  7. Fs = 1000; % Sampling frequency
  8. T = 1/Fs; % Sampling period
  9. L = 1000; % Length of signal
  10. t = (0:L-1)*T; % Time vector
  11. %%
  12. % Form a signal containing a 50 Hz sinusoid of amplitude 0.7 and a 120 Hz
  13. % sinusoid of amplitude 1.
  14. S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
  15. %%
  16. % Corrupt the signal with zero-mean white noise with a variance of 4.
  17. X = S + 2*randn(size(t));
  18. %%
  19. % Plot the noisy signal in the time domain. It is difficult to identify
  20. % the frequency components by looking at the signal |X(t)|.
  21. figure(1);
  22. subplot(311)
  23. plot(1000*t(1:50),X(1:50));
  24. title('Signal Corrupted with Zero-Mean Random Noise');
  25. xlabel('t (milliseconds)');
  26. ylabel('X(t)')
  27. %%
  28. % Compute the Fourier transform of the signal.
  29. Y = fft(X);
  30. %%
  31. % Compute the two-sided spectrum |P2|. Then compute the single-sided
  32. % spectrum |P1| based on |P2| and the even-valued signal length |L|.
  33. P2 = abs(Y/L);
  34. P1 = P2(1:L/2+1);
  35. P1(2:end-1) = 2*P1(2:end-1);
  36. %%
  37. % Define the frequency domain |f| and plot the single-sided amplitude
  38. % spectrum |P1|. The amplitudes are not exactly at 0.7 and 1, as expected, because of the added
  39. % noise. On average, longer signals produce better frequency approximations.
  40. f = Fs*(0:(L/2))/L;
  41. subplot(312)
  42. plot(f,P1);
  43. title('Single-Sided Amplitude Spectrum of X(t)');
  44. xlabel('f (Hz)');
  45. ylabel('|P1(f)|');
  46. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  47. Y = fft(S);
  48. P2 = abs(Y/L);
  49. P1 = P2(1:L/2+1);
  50. P1(2:end-1) = 2*P1(2:end-1);
  51. subplot(313);
  52. plot(f,P1);
  53. title('Single-Sided Amplitude Spectrum of S(t)')
  54. xlabel('f(Hz)');
  55. ylabel('|P1(f)|');
  56. grid on;

运行结果:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjUyODA4OQ_size_16_color_FFFFFF_t_70

后来发现太麻烦了,还是有一个自己写得fft函数比较顺手:

  1. function [f, spectrum ] = gan_fft(s,Fs,L)
  2. %GAN_FFT 此处显示有关此函数的摘要
  3. % 此处显示详细说明
  4. y=fft(s);
  5. p2=abs(y/L);
  6. p1=p2(1:L/2+1);
  7. p1(2:end-1)=2*p1(2:end-1);
  8. f = Fs*(0:(L/2))/L;
  9. spectrum=p1;
  10. end

发表评论

表情:
评论列表 (有 0 条评论,160人围观)

还没有评论,来说两句吧...

相关阅读

    相关 快速变换 FFT 学习小记

    今天终于考完《信号与系统分析》。据学长说这门课一个寝室能有一个通过的。。。 我唯一感兴趣的一部分:“离散傅里叶变换与快速傅里叶变换”老师表示不讲不考。。。。 趁着还没把课本