function[F,Xplot,h]= fftplot(x,M,deltat,pro)
% FFTPLOT calculates and plots a meaningful frequency domain
% representation of a signal.
%
% usage: FFTPLOT(x,M,deltat,proportion),
% x=input signal, M=M point FFT, deltat=sampling interval, equal to 1/fs
% where fs is the sampling frequency, proportion= ratio of fs to plot to.
%
% if you want to plot 0 to fs/10, pro is equal to 10..
%
% Example usage, given a signal x and a time vector t and relatively low
% frequencies:
%
% fft(x,length(x),t(2)-t(1),2);
%
% This assumes that the sampling interval is constant, has NOT been
% modified from the experiment. Equivalently, you could use
% length(t)/t(end). This will plot the spectrum of x from 0Hz to half
% of the sampling frequency. You can also use M>length(x), but no
% information will be gained...it may just look nicer.
X=fft(x,M); X=deltat*X; % calculate the FFT and scale.
deltaf=deltat^-1/M; % determine the sampling frequency
upperbound=round(M/pro); % how far do we plot?
kplot=0:upperbound+1; % lets plot a little further
Xplot=X(1:upperbound+2); % matlab doesn't start counting at 0.
F=deltaf*kplot; % convert to Hz
% plot the magnitude of the spectrum. if you want to plot the so called
% power spectral density, you should change to using semilogy as that is
% customary.
% also useful in analysis is to plot the phase of the signal, which can be
% accompished by plotting angle(Xplot).
h=plot(F,abs(Xplot)); xlabel('Frequency (Hz)'); grid;
axis([0 deltat^-1/pro 0 max(abs(X))+0.1*max(abs(X))]); %trim.