MATLAB: I have an excel data of time vs voltage plot of a signal and have to plot a fft for the same. the excitation frequency is 50khz, but the fft peak is arriving at 35khz. how do i fix this

fft

the matlab code is mentioned below
if true
% code
end
[D,T] = xlsread('50KHZ.xlsx');
F = D(:,2); % Data Channel
Ts = 8e-4; % Sampling Interval (s)
Fs = 1/Ts; % Sampling Frequency (Hz)
Fn = Fs/2; % excitation Frequency
LF = size(F,1); % Length of Data Vector
T = linspace(0,1,LF)*Ts; % Create Time Vector
figure(1) % Plot Data
plot(T, F)
grid
xlabel('Time')
ylabel('Voltage')
FF = fft(F)/LF; % Fourier Series of Data, Freq Vector
Fv = linspace(0,1,fix(LF/2)+1)*Fn;
Iv = 1:length(Fv); % Index Vector
figure(2) % Plot FFT
plot(Fv, abs(FF(Iv)))
grid
xlabel('Frequency (Hz)')
ylabel('Amplitude')

Best Answer

I recognise that code (thank you for quoting it). The only problem may be that your sampling interval, ‘Ts’ is incorrect.
Assuming that ‘D(:,1)’ is your time vector (I don’t know if it is), see if changing the ‘Ts’ assignment to:
Ts = mean(diff(D(:,1))); % Sampling Interval (s)
produces the correct plot.
If ‘D(:,1)’ is your time vector, also change the ‘T’ assignment to:
T = D(:,1);
NOTE This is obviously UNTESTED CODE. I don’t see any typographical errors.