corrected amplitude of FFT_time2freq.m and added a DFT function

pull/1/head
Sebastian Held 2010-04-21 17:58:02 +02:00
parent 7e2bdc67e2
commit f356ff105c
2 changed files with 24 additions and 7 deletions

16
matlab/DFT_time2freq.m Normal file
View File

@ -0,0 +1,16 @@
function f_val = DFT_time2freq( t, val, freq )
% val = FFT_time2freq( t, val, freq )
%
% computes the DFT at the given frequencies
if numel(t) ~= numel(val)
error 'numel(t) ~= numel(val)'
end
f_val = zeros(1,numel(freq));
for f_idx=1:numel(freq)
f_val(f_idx) = sum( val .* exp( -1i * 2*pi*freq(f_idx) * t ) );
end
f_val = f_val / numel(t);
f_val = f_val * 2; % single-sided spectrum

View File

@ -1,9 +1,10 @@
function [f,val] = FFT_time2freq( t, val ) function [f,val] = FFT_time2freq( t, val )
dt=t(2)-t(1); dt=t(2)-t(1); % timestep
val = [val zeros(1,5000)]; L=numel(val); % signal length
L=numel(val); NFFT = 2^nextpow2(L); % next power of 2 (makes fft fast)
f = (0:L-1)/L/dt; %very fine freq resolution... NFFT = NFFT+100000;
f = f(1:floor(L/2)); val = fft( val, NFFT)/L;
val = 2*fft(val)/L; f = 1/(2*dt) * linspace(0,1,NFFT/2+1);
val = val(1:floor(L/2));
val = 2*val; % single-sided spectrum