matlab 求傅里叶变换(fft 模板)
总结:求傅里叶变换,通常必须用函数fft(), 但是求出来的是双边谱,需要转换为单边谱。这个时候有一标准化的动作:
y=fft(s);
p2=abs(y/L);
p1=p2(1:L/2+1);
p1(2:end-1)=2*p1(2:end-1);
完整代码如下:
%% Noisy Signal
% Use Fourier transforms to find the frequency components of a signal buried
% in noise.
% Specify the parameters of a signal with a sampling frequency of 1 kHz and
% a signal duration of 1 second.
% Copyright 2015 The MathWorks, Inc.
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
%%
% Form a signal containing a 50 Hz sinusoid of amplitude 0.7 and a 120 Hz
% sinusoid of amplitude 1.
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
%%
% Corrupt the signal with zero-mean white noise with a variance of 4.
X = S + 2*randn(size(t));
%%
% Plot the noisy signal in the time domain. It is difficult to identify
% the frequency components by looking at the signal |X(t)|.
figure(1);
subplot(311)
plot(1000*t(1:50),X(1:50));
title('Signal Corrupted with Zero-Mean Random Noise');
xlabel('t (milliseconds)');
ylabel('X(t)')
%%
% Compute the Fourier transform of the signal.
Y = fft(X);
%%
% Compute the two-sided spectrum |P2|. Then compute the single-sided
% spectrum |P1| based on |P2| and the even-valued signal length |L|.
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
%%
% Define the frequency domain |f| and plot the single-sided amplitude
% spectrum |P1|. The amplitudes are not exactly at 0.7 and 1, as expected, because of the added
% noise. On average, longer signals produce better frequency approximations.
f = Fs*(0:(L/2))/L;
subplot(312)
plot(f,P1);
title('Single-Sided Amplitude Spectrum of X(t)');
xlabel('f (Hz)');
ylabel('|P1(f)|');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Y = fft(S);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
subplot(313);
plot(f,P1);
title('Single-Sided Amplitude Spectrum of S(t)')
xlabel('f(Hz)');
ylabel('|P1(f)|');
grid on;
运行结果:
后来发现太麻烦了,还是有一个自己写得fft函数比较顺手:
function [f, spectrum ] = gan_fft(s,Fs,L)
%GAN_FFT 此处显示有关此函数的摘要
% 此处显示详细说明
y=fft(s);
p2=abs(y/L);
p1=p2(1:L/2+1);
p1(2:end-1)=2*p1(2:end-1);
f = Fs*(0:(L/2))/L;
spectrum=p1;
end
还没有评论,来说两句吧...