📄 fouriertransform.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PMC January 2008
% [f,Y] = FourierTransform (y,t)
%
% This function receives as input a sequence of samples of a waveform
% in the time domain and calculates the corresponding frequency spectrum
%
% Input:
% - y[] = vector of samples
% - t[] = vector of time values
% Output:
% - Y[] = Fourier transform of y
% - f[] = vector of frequency values
%
% NOTE: the Fourier transform vector is divided by the number of samples to
% get results applicable to continuous-time functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [f,Y] = FourierTransform(y,t)
% Calculate the Discrete Fourier Transform
Y = fftshift(fft(y))/length(y); %; divide by N to normalize
% Y = fft(y)/length(y); %; divide by N to normalize
delta_f = 1/(length(Y)*(t(2)-t(1)));
% If the vector length is odd, the DC component will be localized at
% the center of the vector
if mod(length(Y),2)
tmp = ceil(length(Y)/2); % Find the center of the array
f(tmp) = 0; % DC frequency
for nn=1:floor(length(Y)/2)
f(tmp + nn) = f(tmp + nn - 1) + delta_f;
f(tmp - nn) = -f(tmp + nn);
end
% If the vector length is even, the DC component will be localized at
% the first element of the right-hand side
else
tmp = (length(Y)/2) + 1; % Find the center of the array
f(tmp) = 0; % DC frequency
for nn=1:tmp-2
f(tmp + nn) = f(tmp + nn - 1) + delta_f;
f(tmp - nn) = -f(tmp + nn);
end
f(1) = f(2) - delta_f;
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -