Removing drift from noisy accelerometer data

99 views (last 30 days)
Jack Upton
Jack Upton on 13 Apr 2020
Commented: Emily Keys on 8 Apr 2023
Hi all,
I am using the sensors within my phone to generate a CSV file of accelerations in 3-axis (x,y,z). I have now imported the data to matlab using the CSVread funtion and have began processing the data.
I have applied a filter to reduce some of the noise from the signal however upon integration the signal still drifts. The code I am using is shown below, for simplicitys sake I am only showing data from one axis. Any help is appreciated
clear; close; clc;
D=csvread('test20m3.csv');
t=D(:,1); %Define time
XAccRaw=D(:,5); %Define X acceleration
XAcc=XAccRaw*9.81; %Convert to m/s^2
d=designfilt('lowpassfir','filterorder',10,'CutOffFrequency',10,'SampleRate',100); %Lowpass FIR filter
AX=filtfilt(d,XAcc); %Apply filter to data
VX=cumtrapz(t,AX); %Integrate acceleration to get velocity
SX=cumtrapz(t,VX); %Integrate velocity to get displacement
figure(1);
plot(t,SX);
xlabel(Time (s));
ylabel(Displacement (m))

Answers (2)

Image Analyst
Image Analyst on 14 Apr 2020
I think this works great. It's well commented, practically self documenting but if you have any questions, ask.
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Load data and plot it.
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'; % Maximize the figure window.
% Draw a line at y=0
yline(0, 'LineWidth', 2);
% A moving trend is influenced by the huge outliers, so get rid of those first.
% Find outliers
outlierIndexes = isoutlier(accel);
plot(t(outlierIndexes), accel(outlierIndexes), 'ro', 'MarkerSize', 15);
% Extract the good data.
tGood = t(~outlierIndexes);
accelGood = accel(~outlierIndexes);
% plot(t(~outlierIndexes), accel(~outlierIndexes), 'mo', 'MarkerSize', 10); % Plot circles around the good data.
% Do a Savitzky-Golay filter (moving quadratic).
windowWidth = 51; % Smaller for tighter following of original data, bigger for smoother curve.
smoothedy = sgolayfilt(accelGood, 2, windowWidth);
hold on;
plot(tGood, smoothedy, 'r-', 'LineWidth', 2);
legend('Original Signal', 'X axis', 'Outliers', 'Smoothed Signal');
% Now it looks pretty reasonable since we didn't include the outliers.
% But smoothedy has fewer points so if we're to subtract it from the original
% we have to fill in the missing points.
smoothedy = interp1(tGood, smoothedy, t);
% Now subtract the smoothed signal to get the variation
signal = accel - smoothedy;
% Plot it.
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]; % Size the figure window.
% Draw a line at y=0
yline(0, 'LineWidth', 2);
  10 Comments
Image Analyst
Image Analyst on 7 Apr 2023
Same reply (not sure why you ignored it). Start a new discussion thread and attach your data and code, especially the code to turn acceleration into displacement.

Sign in to comment.


Ameer Hamza
Ameer Hamza on 13 Apr 2020
If by drift, you mean the signal continually move away from the actual value then you can try to use detrend() function: https://www.mathworks.com/help/matlab/ref/detrend.html
  12 Comments
Jack Upton
Jack Upton on 18 Apr 2020
I thought that would be the case as thats what I have read elsewhere,Ive tried to implement it previously using GPS as well as my accelerometer results but I have had no luck
Ameer Hamza
Ameer Hamza on 18 Apr 2020
Yes, it can be a bit complicated because you need to estimate the model of your system. You can try to follow some example on Kalman filter in MATLAB: https://www.mathworks.com/discovery/kalman-filter.html

Sign in to comment.

Categories

Find more on Measurements and Spatial Audio in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!