function [ ] = nickFunction01( dataCell, trimPoints, movingAverageSize )

% step 0: input cellArray of IMU data with all rows using the Matlab inport tool
% step 1: convert to matrix of floats
% step 2: detrend the data to remove DC offset
% step 2: obtain welch spectrum, plot both time and freq domains
% step 3: apply low pass filter, plot both time and freq domains
% step 4: integrate the filtered data to obtain velocity estimate
close all

% Input data format
% 1. sample #
% 2. accel x 
% 3. accel y
% 4. accel z
% 5. ...

%   Detailed explanation goes here

%% Step 1: convert cellArray to floatArray

% number of points to remove trim from start of data set
%trimPoints = 150;

% conver from cell array to matrix
dataImport = cell2mat(dataCell(trimPoints:end, 1:3));
numSamples = linspace(0,size(dataImport,1),size(dataImport,1))';

% [sample#, accel x, accel y, accel z....]
dataFloat = [numSamples, dataImport];

% convert from m/sec^2 into mm/sec^2
x = dataFloat(:,3).*-1000;

figure(1)
plot(x)
title('Raw Data')
xlabel('Samples');
ylabel('Acceleration Magnitude (mm/s^2)');

%% Step 2: detrend the data
x_detrend = detrend(x);

figure(2)
plot(x_detrend)
title('After Detrending');
xlabel('Samples');
ylabel('Acceleration Magnitude (mm/s^2)');



%% Step 2: obtain welch spectrum

% sampling freq of signal
fs = 100; %[Hz]

% length of DFT
nfft = 256;

% time values form input signal
time = 1/fs.*numSamples; % [sec]

% length of window 
window = hamming(nfft);

% number of overlapped samples
noverlap = length(window)/2;

% Raw data
[pxx,f] = pwelch(x, window, noverlap, nfft, fs);

% Detrend data
[pxx_detrend,f_detrend] = pwelch(x_detrend, window, noverlap, nfft, fs);

figure(3)
plot(f, 10*log10(pxx), 'b-')
title('Welch Method - Raw Data');
xlabel('Frequency (Hz)')
ylabel('Magnitude (dB)')
grid

figure(4)
plot(f_detrend, 10*log10(pxx_detrend),'b-')
title('Welch Method - Detrend Data');
xlabel('Frequency (Hz)')
ylabel('Magnitude (dB)')
grid

%% Step 03: Filter data

% moving average filter
a = 1;
b = ones(1,movingAverageSize)./movingAverageSize;
[H,F] = freqz(b,a,1024,fs);

figure(10)
plot(F,10*log10(H),'-');
xlabel('Frequency (Hz)')
ylabel('Magnitude (dB)')

y_filter = filter(b,a,x_detrend);

figure(5)
hold on
plot(x_detrend, '-k');
plot(y_filter, '--b');
hold off
xlabel('Samples');
ylabel('Acceleration Magnitude (mm/s^2)');
grid on
legend('Original Data','Smoothed Data','Location','SouthEast')
title('Plot of Original and Smoothed Data')




%% Step 04: Numerical integration

%reference: http://www.mathworks.com/help/matlab/ref/cumtrapz.html

Z = cumtrapz(x_detrend).*(1/fs);
Y = cumtrapz(y_filter).*(1/fs);

figure(6)
hold on 
plot(time, Z, 'k-');
plot(time, Y, 'b--');
hold off
title('Numerical Integration - Trapezoid Rule');
xlabel('Time (sec)')
ylabel('Velocity (mm/s)')


end