MATLAB: Can anyone help me to perform an FFT from a text file, to rad/sec on x axis

fast fourierfft

Hi, I have never used Matlab before but as part of a project I need to convert data in time domain to frequency domain with the x axis in the unit of rad/sec. Can anyone help me to produce this code? The data is attached and consists of 80 samples per second. My progress on the code can be seen below. Thank you in advance.
signal = load('F072.txt');
N = length (signal);
fs = 80;
X_amp = abs(fft(signal));
bin_vals = [0 : N-1];
fax_rads_second = ((bin_vals/N)*2*pi)*80;
plot(fax_rads_second, X_amp)
xlabel('Frequency (radians/sec)')
title('Single-sided Magnitude spectrum');
axis tight

Best Answer

  • For a full discussion, see: fft (link).
    For your particular problem:
    D = load('F072.txt'); % Column Vector
    L = numel(D); % Signal Length
    Fs = 80; % Sampling Frequency (Hz)
    Fn = Fs/2; % Nyquist Frequency (Hz)
    Ts = 1/Fs; % Sampling Interval (sec)
    t = linspace(0, L, L)*Ts; % Time Vector
    plot(t, D);
    xlabel('Time (sec)')
    title('Time Domain Plot')
    S = D - mean(D); % Subtract D-C Offset
    FTS = fft(S)/L; % Fourier Transform
    Fvrs = linspace(0, 1, fix(L/2)+1)*pi; % Frequency Vector (rad/sec)
    FvHz = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector (Hz)
    Iv = 1:numel(Fvrs); % Index Vector
    plot(Fvrs, abs(FTS(Iv))*2)
    xlabel('Frequency (rad/s)')
    title('Frequency Domain Plot')
    semilogx(Fvrs, abs(FTS(Iv))*2)
    xlabel('Frequency (rad/s)')
    title('Frequency Domain Plot')
    It is necessary to subtract the d-c offset for your signal because the largest peak is about 0.2, while the d-c offset is about 4.5. The automatic axis scaling plot does would obscure the peaks if the d-c offset were included. You can recover it as the mean of the original signal.
    EDIT Corrected typographical error.