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)')
ylabel('Amplitude');
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
    figure(1)
    plot(t, D);
    grid
    xlabel('Time (sec)')
    ylabel('Amplitude')
    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
    figure(2)
    plot(Fvrs, abs(FTS(Iv))*2)
    grid
    xlabel('Frequency (rad/s)')
    ylabel('Amplitude')
    title('Frequency Domain Plot')
    figure(3)
    semilogx(Fvrs, abs(FTS(Iv))*2)
    grid
    xlabel('Frequency (rad/s)')
    ylabel('Amplitude')
    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.