Important: For those who do not have Microsoft Office 2007, copy the entire file below except Part 1, download the mat file below ('Accel_12_04_2009.mat'). Before run the Matlab code, do the following: On the Menu bar of the command window, Select "File" -> "Import Data..." and select the file 'Accel_12_04_2009.mat' Then "Run" the Matlab code.
Matlab Code for Accelerometer:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Part 1
delete all, clear all, close all, clc;
[filename,pathname]=uigetfile('*.xlsx','Load the Excel file to read');
num = xlsread(filename);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Part 2
bufsize = 401 %Entire buffer size = 401;
peaksize = 1;
guardsize = 20;
threshold = 50; %Threshold for detecting peak, define 40
winsize = (bufsize - (2*guardsize+peaksize))/2; %2 intervals for taking average
period = 100; %Distance between peaks for detecting "true possitive peaks"
x = num(:,1);%Accel siganl 1,4
button = num(:,2);%Button 2,5
lenx = length(x);
peakdetect = zeros(lenx,1);
cfar = zeros(lenx,1);
truepositive = zeros(lenx,1);
reallen = lenx -(2*(guardsize+winsize));
leftbound2 = winsize+2*guardsize+peaksize;
peakpos = winsize+guardsize;
peakbuffersize = 10;
count = 0;
figure, plot(x,'b');
pos = 0;
hold on;
for i = 1:reallen-1;
aveleft(i) = mean(x(i:i+winsize-1));
averight(i) = mean(x((i+leftbound2):(i+bufsize-1)));
ave = (aveleft(i) + averight(i))/2;
cfar(i+peakpos) = ave;
peak = x(i+peakpos);
%Method to detetc peak. If peak is detected, set the y value equals to
%700 at the location of peak
if peak > (ave+threshold)
peakdetect(i+peakpos) = 700;
pos = i+peakpos;
%Method to detect the false detection for peak
%Assume that the distance between "true positive" peaks is in
%the range of "period" defined above.
if (pos - savepos) < period;
count = count + 1;
truepositive(i+peakpos) = 10*count+300;
else
count = 0;
end
else
peakdetect(i+peakpos) = 0;
end
savepos = pos;
end
plot(peakdetect, 'r');
plot(cfar, 'm');
plot(button, 'k');
plot(truepositive,'gx');
legend('Accel signal', 'Peak detect', 'Cfar', 'Button', 'True positive peaks')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%