MATLAB: Obtaining result in Frequency Domain using FFT in Matlab

fft frequency domainMATLAB

Attached here are the two vectors: Time and Displacement
Vector displ.mat is the displacement vector obtained after running a dynamic response analysis under a load.
I want to obtain the fundamental frequency which can be easily picked up by plotting the two vectors using `plot(Time,displ)`. In this plot, one can easily see a sine wave with a frequency of roughly 0.5 Hz. So, I expect to see this peak of 0.5 Hz using `fft` function of Matlab.
I am using the following code and but not getting the expected peak at 0.5 Hz:
load Time.mat
load displ.mat
t = Time;
Fs = 149;
x = displ;
x = detrend(x,0);
xdft = fft(x);
freq = 0:Fs/length(x):Fs/2;
xdft = xdft(1:length(x)/2+1);
plot(freq,abs(xdft));
[~,I] = max(abs(xdft));
fprintf('Maximum occurs at %d Hz.\n',freq(I));
plot(Time,displ)

Best Answer

Here you go:
T = load('Rehan Rehan Time.mat');
D = load('Rehan Rehan displ.mat');
t = T.Time;
d = D.displ;
L = length(t); % Signal Length (samples)
Ts = mean(diff(t)); % Sampling Interval (seconde)
Fs = 1/Ts; % Sampling Frequency (Hz)
Fn = Fs/2; % Nyquist Frequency (Hz)
dm = d-mean(d);
ft_d = fft(dm)/L; % Normalised Fourier Transform
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector
[pks,idx] = findpeaks(abs(ft_d(Iv))*2);
lbl = sprintf('Frequency = %.3f\nAmplitude = %.3f', Fv(idx), pks);
figure(1)
plot(Fv, abs(ft_d(Iv))*2)
grid
xlabel('Frequency (Hz)')
ylabel('Amplitude (m)')
text(Fv(idx)+0.1, pks, lbl, 'HorizontalAlignment','left', 'VerticalAlignment','top')