source: internals/2016/screencastmultiplayergame_control/trunk/SampleApps/Android_Sensors_Data_Extraction.m @ 16435

Last change on this file since 16435 was 16434, checked in by jesteves, 4 years ago

Added Matlab scripts that do up/down motion detection

File size: 10.6 KB
Line 
1
2
3
4%% Set up connection
5connector on josepmesteves;
6
7%% Set up data acquisition
8
9% Create link to mobile device
10clear
11m = mobiledev
12
13% Enable sensors
14m.AccelerationSensorEnabled = 1;
15m.AngularVelocitySensorEnabled = 1;
16m.OrientationSensorEnabled = 1;
17
18% Set Sampling Rate: 'Low' (0.988Hz), 'Medium' (9.9Hz), 'High' (200Hz)
19m.SampleRate = 'Medium';
20
21pause(2);
22fprintf('COMECOU!!!\n\n')
23
24% Start acquiring data
25m.Logging = 1;
26
27% Determine acquisition interval
28pause(10);
29
30% Stop acquiring data
31m.Logging = 0;
32fprintf('PAROU!!!\n\n')
33
34
35%%
36%
37%
38%
39%
40%
41%
42%
43%
44%
45%
46%
47%
48%% Retrieve logged data
49clearvars -except m
50[acc, t_acc] = accellog(m);
51[angvel, t_angvel] = angvellog(m);
52[orient, t_orient] = orientlog(m);
53
54%% Plot
55
56% Choose what to plot
57data_to_plot=1;
58
59
60if (data_to_plot == 1)
61%     close all
62%     figure
63%     plot(t_acc, acc)
64%     title('Acceleration')
65%     legend('X axis', 'Y axis', 'Z axis')
66   
67    % Filtering the signal
68    constant15=0.4; % (1-0.2)/0.2 = 4 samples delay
69    signal_for_LP_filtering = acc(:,3);
70    filtered_value(1) = signal_for_LP_filtering(2);
71    for i= 1 +1 : length(t_acc)
72        filtered_value(i) = filtered_value(i-1) + constant15 * (signal_for_LP_filtering(i) - filtered_value(i-1));
73    end
74    figure
75    hold on
76    plot(t_acc, acc(:,3), '.-b')
77    plot(t_acc, filtered_value, '.r')
78    legend('Z Acceleration', 'Z Acceleration Filtered')
79    title('Z Acceleration')
80   
81    BASE_VALUE = 9.81;
82    THRESHOLD = 5;
83    POSITIVE_THRESHOLD = BASE_VALUE + THRESHOLD;
84    NEGATIVE_THRESHOLD = BASE_VALUE - THRESHOLD;
85    plot(t_acc, ones(size(t_acc))*POSITIVE_THRESHOLD, 'k')
86    plot(t_acc, ones(size(t_acc))*NEGATIVE_THRESHOLD, 'k')
87   
88end
89
90if (data_to_plot == 2 || data_to_plot == 4)
91    figure
92    plot(t_angvel, angvel)
93    title('Angular Velocity')
94    legend('X axis (Pitch)', 'Y axis (Roll)', 'Z axis (Yaw)')
95end
96
97if (data_to_plot == 3 || data_to_plot == 4)
98    figure
99    hold on
100    plot(t_orient, orient, '.-')
101%    plot(t_orient, orient)
102    hold off
103    title('Orientation')
104    legend('Yaw', 'Pitch', 'Roll')
105end
106
107
108%% Analyze Signal (Motion Detector)
109
110% Use sample data
111%clearvars -except m
112%load('/Users/joseesteves/Documents/Projectos/Kayacast - Caixa Magica/sensor_sample_data.mat')
113
114analyze_acc_signal = 1; % Set to 1 if I want to analyze the acceleration
115% and if filtered_value was calculated
116
117detector1 = 1; % Set to 1 is using Z axis Up/Down Motion Detector
118
119
120
121
122% Detector1 detects Z axis Up/Down Motion
123% This detector assumes:
124%   - Initial resting acceleration is know (should be 1G)
125%   - Any acceleration in X and Y is neglected
126%   - Signal is filtered using an LPF, hence the absolute acceleration
127%   values (especially the peak values) do not have meaning
128
129
130if analyze_acc_signal == 1 && detector1 == 1
131   
132   
133    figure
134    hold on
135    plot(t_acc, acc(:,3), 'b')
136    plot(t_acc, filtered_value, '.r')
137    legend('Z Acceleration', 'Z Acceleration Filtered')
138    title('Z Acceleration')
139   
140    BASE_VALUE = 9.81;
141    THRESHOLD = 5;
142    POSITIVE_THRESHOLD = BASE_VALUE + THRESHOLD;
143    NEGATIVE_THRESHOLD = BASE_VALUE - THRESHOLD;
144    plot(t_acc, ones(size(t_acc))*POSITIVE_THRESHOLD, 'k')
145    plot(t_acc, ones(size(t_acc))*NEGATIVE_THRESHOLD, 'k')
146   
147   
148   
149   
150    signal_to_analyze = filtered_value;
151    signal_timestamp = t_acc;
152    INITIALIZATION_TIME = 10;
153    WINDOW_SIZE = 10; % this value specifies the time window (in samples)
154    % used by the detector
155    % NOTE: in the final implementation, the window size may have to be
156    % specified in time and then adjusted to samples! (Because sampling
157    % rate may vary at running time)
158   
159    PHASE1_MIN_DURATION = 0.1; % seconds
160    PHASE2_MIN_DURATION = 0.1; % seconds
161    TRANSITION_PHASE_MAXIMUM_TIME = 0.075; % seconds that transition is
162    % allowed to take
163
164    BASE_VALUE = 9.81;
165    THRESHOLD = 5;
166    POSITIVE_THRESHOLD = BASE_VALUE + THRESHOLD;
167    NEGATIVE_THRESHOLD = BASE_VALUE - THRESHOLD;
168    PHASE1_MIN_THRESHOLD = THRESHOLD - 2;
169    PHASE2_MAX_THRESHOLD = THRESHOLD + 2;
170   
171    phase1_detected = 0;
172    phase2_detected = 0;
173    detecting_phase1 = 0;
174    detecting_phase2 = 0;
175    phase1_starting_time = 0;
176    phase2_starting_time = 0;
177    phase1_ending_time = 0;
178    phase2_ending_time = 0;
179    phase1_minimum = 0;
180    phase2_maximum = 0;
181   
182    number_of_actions_detected = 0; % number of consecutive phase1 and
183    % phase2 successfully detected
184   
185    for t_sample = 1:length(signal_timestamp)
186        if t_sample < INITIALIZATION_TIME
187            % Not enough information and filter may have distorted the
188            % signal. Hence, do nothing
189            continue;
190        end
191       
192        if ~phase1_detected
193            % Detect phase1. 2 Scenarios:
194            %   - Signal hasn't dropped below NEGATIVE_THRESHOLD
195            %   - Signal has dropped below NEGATIVE_THRESHOLD but we didn't
196            %   confirm that phase1 occurred yet.
197           
198            if signal_to_analyze(t_sample) < NEGATIVE_THRESHOLD && ...
199                    ~detecting_phase1
200                detecting_phase1 = 1;
201                phase1_starting_time = signal_timestamp(t_sample);
202            end
203           
204            if detecting_phase1
205                % continua negative?
206                %   - se sim, fazer update do minimo
207                %   - se nao, determinar se  fase1 ocorreu
208               
209                if signal_to_analyze(t_sample) < NEGATIVE_THRESHOLD
210                    phase1_minimum = min(phase1_minimum, signal_to_analyze(t_sample));
211                    % Note: don't think it is necessary, but here I can
212                    % easily detected if the signal is monotonic before
213                    % and after the biggest negative peak
214                    continue;
215                else
216                    % Determine if phase1 occurred:
217                    %   - time > PHASE1_MIN_DURATION
218                    %   - peak < PHASE1_MIN_THRESHOLD
219                   
220                    phase1_detected = 0;
221                    detecting_phase1 = 0;
222
223                    phase1_ending_time = signal_timestamp(t_sample);
224                    if phase1_ending_time - phase1_starting_time > PHASE1_MIN_DURATION
225                        if phase1_minimum < PHASE1_MIN_THRESHOLD
226                            phase1_detected = 1;
227                            fprintf(['Detected Phase 1 starting at ', num2str(phase1_starting_time), ...
228                                's and ending at ', num2str(phase1_ending_time), 's\n\n'])
229                            hold on
230                            plot(phase1_starting_time, NEGATIVE_THRESHOLD, 'ok', 'linewidth',2)
231                            plot(phase1_ending_time, NEGATIVE_THRESHOLD, 'ok', 'linewidth',2)
232                            plot([phase1_starting_time phase1_ending_time], ...
233                                [NEGATIVE_THRESHOLD NEGATIVE_THRESHOLD], 'k', 'linewidth',5)
234                            continue;
235                        end
236                    end
237                end
238            end
239           
240           
241           
242        else %if phase1_detected
243           
244            % If transition maximum time has expired and we still did not
245            % reach a possible phase2
246            if signal_timestamp(t_sample) - phase1_ending_time > ...
247                    TRANSITION_PHASE_MAXIMUM_TIME && ~detecting_phase2
248                phase1_detected = 0;
249                continue;
250            % else:
251            %   - we are still inside the transition window or
252            %   - we are already analyzing a possible phase2
253            end
254           
255            % Value is above POSITIVE_THRESHOLD for the first time
256            if signal_to_analyze(t_sample) > POSITIVE_THRESHOLD && ...
257                    ~detecting_phase2
258                phase2_starting_time = signal_timestamp(t_sample);
259                detecting_phase2 = 1;
260            end
261           
262            if detecting_phase2
263                %   - Value is above POSITIVE_THRESHOLD => update maximum
264                %   - Value is below POSITIVE_THRESHOLD => determine is
265                %   phase2 has occurred
266               
267                if signal_to_analyze(t_sample) > POSITIVE_THRESHOLD
268                    phase2_maximum = max(phase2_maximum, signal_to_analyze(t_sample));
269                else
270                    % Possible phase2 ended. Determine if phase2 occurred:
271                    %   - time > PHASE2_MIN_DURATION
272                    %   - peak > PHASE2_MAX_THRESHOLD
273                   
274                    phase1_detected = 0;
275                    phase2_detected = 0;
276                    detecting_phase2 = 0;
277
278                    phase2_ending_time = signal_timestamp(t_sample);
279                    if phase2_ending_time - phase2_starting_time > PHASE2_MIN_DURATION
280                        if phase2_maximum > PHASE2_MAX_THRESHOLD
281                            % Detected phase2 after a phase1 occurred.
282                            % Hence, an action happened. Do the stuff I
283                            % want here.
284                            phase2_detected = 1; % this flag really doesn't matter...
285                            number_of_actions_detected = number_of_actions_detected + 1;
286                            fprintf(['Detected Phase 2 starting at ', num2str(phase2_starting_time), ...
287                                's and ending at ', num2str(phase2_ending_time), 's\n\n'])
288                            hold on
289                            plot(phase2_starting_time, POSITIVE_THRESHOLD, 'og', 'linewidth',2)
290                            plot(phase2_ending_time, POSITIVE_THRESHOLD, 'og', 'linewidth',2)
291                            plot([phase2_starting_time phase2_ending_time], ...
292                                [POSITIVE_THRESHOLD POSITIVE_THRESHOLD], 'g', 'linewidth',5)
293                           
294%                             calculate_action_score = @(phase1_min, ...
295%                                 phase1_duration, phase2_max, phase2_duration) ...
296%                                 (phase1_min * phase1_duration + phase2_max * phase2_duration);
297%                                 % TODO finish score function (http://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html#f4-70159)
298%                                 % Take out the threshold from the minimum and maximum values
299                           
300                            continue;
301                        end
302                    end
303                end
304            end
305        end
306    end
307end
308
309
310
311
312
313% Coisas a melhorar:
314%   - Contar o inicio da primeira fase so se o telemovel estiver minimamente
315%   horizontal;
316%   - Tirar a componente da gravidade (facil fazer em Android), porque pode
317%   estar presente em qualquer eixo. Retirar o valor base nao funciona
318%   quando a pessoa inclina o telefone durante o movimento.
319
320
321
322
323%% Clean up
324
325clear m
326connector off
Note: See TracBrowser for help on using the repository browser.