如何获得信号的傅立叶变换

时间:2014-12-29 10:23:09

标签: matlab signal-processing fft wavelet

在下面的代码中,我试图得到fourier transform的静止信号(x3)。但是在运行时,我得到的情节绝对是错误的,并且没有显示信号x3的任何频率。

请指导我并帮助我正确地获取fourier transform

代码

 %% Time specifications:
  Fs = 8000;                       % samples per second
  dt = 1/Fs;                       % seconds per sample
 StopTime = 1;                    % seconds
 t = (0:dt:StopTime-dt);         % seconds

 x1 = (10)*cos(2*pi*3*(t));
 x2 = x1 + (10)*cos(2*pi*5*(t));
 x3 = x2 + (10)*cos(2*pi*10*(t));


 %% here i try to Plot fourier transform of the signal x3:
 NFFT = 2^nextpow2(StopTime); % Next power of 2 from length of y
 Y = fft(y,NFFT)/StopTime;
 f = Fs/2*linspace(0,1,NFFT/2+1);
 figure;
 plot(f,2*abs(Y(1:NFFT/2+1)));

 %% Plot the signal versus time:
 figure;
 hold on;
 plot(t,x1,'r');
 plot(t,x2,'g');
 plot(t,x3,'b');

Update_1 enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您无法看到预期的结果,因为NFFT的值为1表示当您将NFFT/2+1写为Y的索引时,它不会是整数值所以MATLAB警告你。您可以像这样计算NFFT

NFFT = 2^nextpow2(length(t))

而不是写

NFFT = 2^nextpow2(StopTime)

好吧,试试这个:

Fs       = 8000;                 % samples per second
dt       = 1/Fs;                 % seconds per sample
StopTime = 1;                    % seconds
t        = (0 : dt : StopTime-dt);   % seconds

x1 = 10 * cos(2*pi*3*t);
x2 = x1 + 10 * cos(2*pi*5*t);
x3 = x2 + 10 * cos(2*pi*10*t);


%% here i try to Plot fourier transform of the signal x3:
NFFT = 2 ^ nextpow2(length(t));     % Next power of 2 from length of y
Y    = fft(x3, NFFT) / StopTime;
f    = Fs / 2 * linspace(0, 1, NFFT/2 + 1);
figure;
plot(f, 2 * abs( Y( 1:length(f) ) ) ); % // Also try this: plot(f(f <= 200), 2 * abs( Y( 1:length(f(f <= 200)) ) ) )

%% Plot the signal versus time:
figure;
hold on;
plot(t, x1, 'r');
plot(t, x2, 'g');
plot(t, x3, 'b');

<强>图:

enter image description here enter image description here

修改

1 - 其实你没有&#39;必须使用nextpow()功能。如果您使用它,fft()功能可以更快地运行。因为时间效率,fft()就像每次递归地将信号除以2一样。然后计算每个零件的离散傅里叶变换并收集它们。这意味着当信号向量长度为​​2的幂时,FFT是最有效的。

2 - 将fft结果除以StopTime部分没有&#39;对我也没有任何意义。将fft结果除以NFFT在理论上可能更方便。