Download this file

92 lines (79 with data), 2.7 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
function [output,state,EE,F,SMA] = ACC_Activity(X,Fs)
%% = Estimate Activity from ACC and adaptively adjust bandpass Filter = %%
%% ============================== Method ============================== %%
% Uses the Signal Magnitude Area (SMA) Or Energy Expenditure to estimate
% the Current activity level of the subject, tunes the filter parameters
% based on that and filter X, Y and Z channels. It first removes the
% gravitational components with a high pass filter.
%% ============================== Inputs ============================== %%
% X : Matrix 3*n where each row, represents a single Channel of the
% acceleration
% Fs : Sampling freqeuncy
%% =========================== Outputs ========================= %%
% output = matrix of [3*n] , where n is the length of each X, Y and Z
% channel.
% state = is an integer. 0 means no or very low activity, 1 means median
% activtiy and 2 means high activity (e.g. Jogging)
% EE : Energy Expenditure over a minute
% F : the paramteres used for filtering.
%% ========================= Input Handling =========================== %%
if nargin < 2
error('Please Enter Both X and Fs. Fs is the sampling rate!');
end
[m,n] = size(X);
if m ~= 3
error('Matrix X should have 3 rows, each row is one Acceleration channel');
end
%% ==== Eliminate static gravitation by high pass filtering =====%%
output = zeros(m,n);
F=[1 12];
Wn=F*2/Fs;
N = 3;
[a,b] = butter(N,Wn);
if max(m,n) >= 60*Fs
Y = zeros(m,60*Fs);
else
Y = zeros(m,length(X(1,:)));
end
for i = 1 : m
if max(m,n) >= 60*Fs
Y(i,:) = filtfilt( a,b, X(i,1:60*Fs));
less = 0;
else
Y(i,:) = filtfilt( a,b, X(i,:));
less = 1;
end
end
%% ========== Compute SMA && Compute Energy Expenditure ============== %%
% ------------------------------- SMA --------------------------------- %
SMA = (sum(abs(Y(:,1))) + sum(abs(Y(:,2))) + sum(abs(Y(:,3))))...
/n;
%% ============================== EE ================================ %%
delta_A = sqrt(diff(Y(1,:)).^2 + diff(Y(2,:)).^2 + diff(Y(3,:)).^2);
EE = sum(delta_A);
if less
T = length(X(1,:))/Fs;
EE = EE/T;
EE = EE*60;
end
%% Determine States over a minute
if EE >= 15
if EE >= 45
state = 2; %Jogging or high activity
F = [0.3 0.7];
else
state = 1; %mild activity ex. walking
F = [0.2 0.6];
end
else
state = 0; %Static
F = [0.2 0.4];
end
%% =========== adaptively filter the signal based on its state ==== %%
Wn=F*2/Fs;
N = 3;
[a,b] = butter(N,Wn);
for i = 1 : m
output(i,:) = filtfilt(a,b,X(i,:));
end
end