clc;
fprintf('Beginning to run %s.m ...\n', mfilename);
close all;
clear;
workspace;
format long g;
format compact;
hFig1 = figure;
sa = load('acceleration.mat')
accel = sa.acceleration1;
st = load('time.mat')
t = st.time1;
plot(t, accel, 'b.-', 'MarkerSize', 9);
grid on;
hold on;
fontSize = 20;
xlabel('Time', 'FontSize', fontSize);
ylabel('Acceleration', 'FontSize', fontSize);
title('Original Signal', 'FontSize', fontSize);
hFig1.WindowState = 'maximized';
yline(0, 'LineWidth', 2);
outlierIndexes = isoutlier(accel);
plot(t(outlierIndexes), accel(outlierIndexes), 'ro', 'MarkerSize', 15);
tGood = t(~outlierIndexes);
accelGood = accel(~outlierIndexes);
windowWidth = 51;
smoothedy = sgolayfilt(accelGood, 2, windowWidth);
hold on;
plot(tGood, smoothedy, 'r-', 'LineWidth', 2);
legend('Original Signal', 'X axis', 'Outliers', 'Smoothed Signal');
smoothedy = interp1(tGood, smoothedy, t);
signal = accel - smoothedy;
hFig2 = figure;
plot(t, signal, 'b.-', 'MarkerSize', 9);
grid on;
hold on;
title('Corrected Signal', 'FontSize', fontSize);
xlabel('Time', 'FontSize', fontSize);
ylabel('Acceleration', 'FontSize', fontSize);
hFig2.Units = 'normalized';
hFig2.Position = [.2, .2, .5, .5];
yline(0, 'LineWidth', 2);