function [f,p] = analyze1(y,Fs)
% Matlab function analyze1(y,Fs) 
% plots the waveform and power spectrum of a vector y, with sampling
% frequency Fs. Truncates the series to be a perfect power of 2.  
% 
% Call [f,p] = analyze1(y,Fs)
%
% Inputs:   y = a vector series with constant sampling frequency
%           Fs = the sampling frequency for the above (samples/s)
% Ouputs:   The power spectrum (p vs. f) 
%           f = a vector of the frequencies in the power spectrum (Hz)
%           p = a vector of the corresponding powers for the elements in f
%
% Based on Function developed by
% Mark R. Petersen, U. of Colorado Boulder Applied Math Dept, Feb 2004
% Modified by S. Scott Moor                             Jan. 2015

% variables 
% y = a time series to be analyzed,
% Fs = the sampling rate for the above time series (samples/s)
% N = sample size for FFT, a power of 2 < the length of the sample
% c = result of FFT(Fast Fouier Transform)
% p = power from each frequency
% f = frequencies matching p

% Calculate the power spectrum based on the fast Fouier Transform
N = 2^(nextpow2(length(y))-1); % find a power of 2 for fft
c = fft(y(1:N))/N;             % take fft
p = 2*abs( c(2:N/2));          % compute power at each frequency
f = (1:N/2-1)*Fs/N;            % frequency corresponding to p

% Plot power spectrum on a semilog plot.
plot(f,p)   
xlabel('Frequency (Hz)')
ylabel('Power')
title('Power Spectrum')
