-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccidentDetection_Realtime.m
133 lines (117 loc) · 3.27 KB
/
AccidentDetection_Realtime.m
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
clc;
clear;
% addpath(genpath('X:\XX\XX'));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set parameters %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% filter window size
n=3;
% gravitational acceleration
g=9.8;
% detector length (~3s)
D=800;
% detector tail length (fall detection)
T=80;
% detection threshold
th=120;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Acceleration data include 'accel_x, accel_y,%
% accel_z' on x, y and z axis %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% read the data file
f=tsvread('XXX.tsv');
% calibration data
t0=f(:,2);
t1=t0-t0(1);
t2=datetime(t1./1000,'ConvertFrom','posixtime','Format','mm:ss.SSS');
t=seconds(timeofday(t2));
accel_x=f(:,3)./g;
accel_y=f(:,4)./g;
accel_z=f(:,5)./g;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Extract the features of the signal %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% signal filter
ax=medfilt1(accel_x,n);
ay=medfilt1(accel_y,n);
az=medfilt1(accel_z,n);
% SVM - signal vector magnitude
svm=sqrt(ax.^2+ay.^2+az.^2);
% Power
pow0=abs(svm).^2;
pow=[t pow0];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Accident detection %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y=[t accel_y];
detected=[];
% slide window across the signal
v=length(y)-D;
k=1;
while v~=0
v=v-1;
% find the anomalous targets (Fall detection)
if y(D,2)>-0.1 & y(D,2)<0.5
% calculate the signal power
for i=1:length(pow)
if pow(i,1)==y(D,1)
% save the maximum power value in the set of samples (T)
pmax=max(pow(i-T:i,2));
end
end
% accident detection
if pmax>th
detected(k,:)=y(D,:);
power_value(k,:)=pmax;
k=k+1;
% anomaly detector (Window)
y=circshift(y,-(1:length(y)-D));
else
y=circshift(y,-(1:length(y)-D));
end
else
y=circshift(y,-(1:length(y)-D));
end
end
% output detection result
if isempty(detected)==0
for i=1:length(pow)
for j=1:length(power_value)
if pow(i,2)==power_value(j,1)
% save the time series of the accident
at(j,1)=pow(i,1);
end
end
end
% find duplicate values
accid_t=unique(at);
% print the warning text
format='WARNING: Accident detected at around %6.3f s.\n';
fprintf(format,accid_t);
% output result graph
figure;
subplot(2,1,1)
plot(t,accel_y);
hold on
plot(detected(:,1),detected(:,2),'r+');
title('Accident detection on Y-axis');
xlabel('Time(s)');
ylabel('Acceleration(g)');
legend('Y-axis acceleration signal','Accident target');
subplot(2,1,2)
plot(t,pow0);
hold on
plot(at,power_value,'ro');
title('Power of Accident detected');
xlabel('Time(s)');
ylabel('Power');
legend('Acceleration pow','Accident target');
else
% output power graph
figure;
plot(t,pow0);
title('Aceleration Power');
xlabel('Time(s)');
ylabel('Power');
fprintf('NO accident detected.\n');
end