%FFT analysis for ME 345 Lab 5
%Based on MATLAB FFT help file

clear all
close all
%Load the file 'data.txt' and read the time and acceleration
data=load('data.txt');
t=data(:,1);
y=data(:,2);

Fs=20;%Sampling frequency
L=length(y);
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
Y(1)=Y(1)/2;
f = Fs/2*linspace(0,1,NFFT/2+1);

% Plot single-sided amplitude spectrum.
plot(f,2*abs(Y(1:NFFT/2+1))) 
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')

%Plot waveform
figure
plot(t,y);
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');