I wrote this code for taking FFT of any time history vector in which T is the time step vector and S is the related displacement or any given quantity you hope to plot their spectrum.
ps:STfile is my model displacement time history for all degrees of freedom which I saved it as a .mat file
clc
clear
load STfile
% is your starting vector of time
data = S(:,12); % vector of data you want to resample
N = length(T);
for ii=1:(N-1)
DT(ii) = T(ii+1)-T(ii);
end
DTS = mean(DT);
data_TS = timeseries(data,T); % define the data as a timeseries
new_T = T(1):DTS:T(end); % new vector of time with fixed dt=1/fs
data_res = resample(data_TS,new_T); % data resampled at constant fs
plot(data_res)
y=getdatasamples(data_res,1:N);
Fs=1./DTS;
NFFT = 2^nextpow2(N);
Y = fft(y,NFFT)/N; % the division by N is to scale the amplitude
freq = Fs/2*linspace(0,1,NFFT/2+1); % vector of frequencies for the plot
figure()
plot(freq,2*abs(Y(1:NFFT/2+1)),'r') % multiplicated by 2 to recover the energy related
% to the negative frequencies since in this way only half spectrum is plotted
xlabel('Frequency(rad/sec)')
xlim([0 0.05])
example result for this code(time history of heave velocity)
spectrum.periodogram
? What ispsd()
? I don't think those are standard MATLAB functions/classes? – TomHpsd = dspdata.psd(s,[-2000:2000])
but I am getting errors. What do I need to do? – Merin