Home > data-import > run_moving_event_detection.m

run_moving_event_detection

PURPOSE ^

Imports the sensor data. Resample the data with frequency and

SYNOPSIS ^

function [ cell_pos_moving_event, cell_ori_moving_event] = run_moving_event_detection( sid, frequency, sensors, sensor_data )

DESCRIPTION ^

 Imports the sensor data. Resample the data with frequency and
 calculate the speed. Based on the speed, this function detects the moving
 event and saves all the event datas into corresponding files. This
 function supposes the sensor ID and sensor name as follows:
   %   sendor ID     sensor name         description
    %   1           head_child          sensor in child's head
    %   2           head_parent         sensor in parent's head
    %   3           left-hand_child     sensor in child's left hand
    %   4           right-hand_child    sensor in child's right hand
    %   5           left-hand_parent    sensor in parent's left hand
    %   6           right-hand_parent   sensor in parent's right hand
 If the sensor number or sensor name is differnet, you must update
 function get_sensor_name in the end of this file.

INPUT:
  SID              subject ID or list of subject ID
  FREQUENCY        sample frequency. default is 60 Hz.

OUTPUT:
  CELL_POS_MOVING_EVENT    a cell of moving event based on position speed 
                           for all sensors
  CELL_ORI_MOVING_EVENT    a cell of moving event based on orientation
                           speed for all sensors.

SIDE EFFECT:
  Data of position moving event and orientation moving event will be
  saved in the directory 'drived' of specified subject ID(s).

EXAMPLE 1:
   run_moving_event_detection(1401);
 %   save position moving event and orientation moving event of subject
 %   1401.

EXAMPLE 2:
   subject_list = list_subjects(14);
   run_moving_event_detection( subject_list );
 %   save position moving event and orientation moving event of subjects  
 %   of experiment 14.

EXAMPLE 3:
   run_moving_event_detection(1401, 50hz);
 %   save position moving event and orientation moving event of subject
 %   1401, the psotion data is resampled in 50HZ.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [ cell_pos_moving_event, cell_ori_moving_event] = run_moving_event_detection( sid, frequency, sensors, sensor_data )
0002 % Imports the sensor data. Resample the data with frequency and
0003 % calculate the speed. Based on the speed, this function detects the moving
0004 % event and saves all the event datas into corresponding files. This
0005 % function supposes the sensor ID and sensor name as follows:
0006 %   %   sendor ID     sensor name         description
0007 %    %   1           head_child          sensor in child's head
0008 %    %   2           head_parent         sensor in parent's head
0009 %    %   3           left-hand_child     sensor in child's left hand
0010 %    %   4           right-hand_child    sensor in child's right hand
0011 %    %   5           left-hand_parent    sensor in parent's left hand
0012 %    %   6           right-hand_parent   sensor in parent's right hand
0013 % If the sensor number or sensor name is differnet, you must update
0014 % function get_sensor_name in the end of this file.
0015 %
0016 %INPUT:
0017 %  SID              subject ID or list of subject ID
0018 %  FREQUENCY        sample frequency. default is 60 Hz.
0019 %
0020 %OUTPUT:
0021 %  CELL_POS_MOVING_EVENT    a cell of moving event based on position speed
0022 %                           for all sensors
0023 %  CELL_ORI_MOVING_EVENT    a cell of moving event based on orientation
0024 %                           speed for all sensors.
0025 %
0026 %SIDE EFFECT:
0027 %  Data of position moving event and orientation moving event will be
0028 %  saved in the directory 'drived' of specified subject ID(s).
0029 %
0030 %EXAMPLE 1:
0031 %   run_moving_event_detection(1401);
0032 % %   save position moving event and orientation moving event of subject
0033 % %   1401.
0034 %
0035 %EXAMPLE 2:
0036 %   subject_list = list_subjects(14);
0037 %   run_moving_event_detection( subject_list );
0038 % %   save position moving event and orientation moving event of subjects
0039 % %   of experiment 14.
0040 %
0041 %EXAMPLE 3:
0042 %   run_moving_event_detection(1401, 50hz);
0043 % %   save position moving event and orientation moving event of subject
0044 % %   1401, the psotion data is resampled in 50HZ.
0045 %
0046 %
0047 
0048     if nargin < 1 || isempty(sid)
0049        error('you must provide parameter sid as subject ID or list of subject ID'); 
0050     end
0051     
0052     if nargin < 2 || isempty(frequency)
0053         frequency = 60;
0054     end
0055     
0056     if numel(sid) > 1
0057         %a list of subject ID
0058         for i = 1 : numel(sid)
0059             run_moving_event_detection(sid(i), frequency);
0060         end
0061         return
0062     end
0063     
0064     %read and resample the sensor data of all sensors.
0065     if ~exist('sensor_data', 'var') || isempty(sensor_data)
0066         try
0067             [s_pos, s_rot] = read_pos_sensor(sid, [ ], frequency);
0068         catch ME
0069             disp(ME);
0070             return;
0071         end
0072     else
0073         s_pos = sensor_data{1};
0074         s_rot = sensor_data{2};
0075     end
0076     
0077     max_sensor_id = numel( s_pos );
0078     sensor_list = 1:max_sensor_id;
0079     valid_sensor_list = [];
0080 
0081     %--select sensors with data and pair for hand-------------------------
0082     for sensor_id = sensor_list
0083         if ~isempty(s_pos{sensor_id})
0084             valid_sensor_list = [valid_sensor_list sensor_id];
0085         end
0086     end
0087     sensor_list = valid_sensor_list;
0088     
0089     if exist('sensors', 'var') && ~isempty(sensors)
0090         sensor_list = sensor_list(ismember(sensor_list, sensors));
0091     end
0092     
0093     %to ensure both hands are there - commented out not sure why this is
0094     %here - sbf
0095 %     if (~ismember(3, sensor_list)) || (~ismember(4, sensor_list))
0096 %         sensor_list = setdiff(sensor_list, [3 4]);
0097 %     end
0098 %     if (~ismember(5, sensor_list)) || (~ismember(6, sensor_list))
0099 %         sensor_list = setdiff(sensor_list, [5 6]);
0100 %     end
0101     %-------------------------------------------------------------------
0102     
0103     %parameters for detect moving event of head
0104     params_rot.thresh_lo = 9;    % degree/sec
0105     params_rot.thresh_hi = 40;    % degree/sec
0106     params_rot.fixation_creep = 6;  %degree/sec
0107     params_rot.min_fixation = 0.5;       % sec - min. fixation after aggregation
0108     params_rot.fixation_filter = 0.1;    % sec - min. before aggregation
0109     params_rot.moving_filter = 0.1;      % sec
0110     
0111     factor = 25.4; %inches to mm
0112     params_pos.thresh_lo = 1*factor; % mm/sec
0113     params_pos.thresh_hi = 5*factor; % mm/sec
0114     params_pos.fixation_creep = 1*factor; % mm/sec
0115     params_pos.min_fixation = 0.5;
0116     params_pos.fixation_filter = 0.1;
0117     params_pos.moving_filter = 0.1;      % sec
0118     
0119     
0120     pos_movements = cell(numel( s_pos ), 1);
0121     rot_movements = cell( numel( s_rot ), 1);
0122     for sensor_index = 1 : numel(sensor_list) 
0123         sensor_id = sensor_list(sensor_index);
0124         if sensor_id <= 2 
0125             %head sensor, use the default parameters
0126         else
0127             %hand sensor, we should find a suitable parameters
0128             params_rot.thresh_lo = 50.0;
0129             params_rot.thresh_hi = 120.0;
0130             params_rot.fixation_creep = 15;
0131             
0132             params_pos.thresh_lo = 2*factor; % mm/sec
0133             params_pos.thresh_hi = 5*factor; % mm/sec
0134             params_pos.fixation_creep = 1*factor;
0135             %break; %now we produce moving event for head only.
0136         end
0137         
0138 %         pos_mvmt = detect_movement(s_pos{sensor_id}, params_pos);
0139 %         pos_movements{sensor_id} = cevent2event(cevent_category_equals(pos_mvmt, [2 3]));
0140 %
0141 %         rot_mvmt = detect_movement(s_rot{sensor_id}, params_orient);
0142 %         rot_movements{sensor_id} = cevent2event(cevent_category_equals(rot_mvmt, [2 3]));
0143         
0144         pos_movements{sensor_id} = detect_moving_event(s_pos{sensor_id}, params_pos);
0145         rot_movements{sensor_id} = detect_moving_event(s_rot{sensor_id}, params_rot);
0146     end
0147     
0148     %to save moving event to file
0149     save_path ='';% 'ywz/'; %save into a temp directory for test only, so we can compare variables
0150     for sensor_index = 1 : numel(sensor_list) 
0151         sensor_id = sensor_list(sensor_index);
0152         sensor_name = get_sensor_name( sensor_id ); %head_child, head_parent, ...
0153         if isempty(sensor_name)
0154             disp(['senor ' num2str(sensor_id) ' may be wrong']);
0155             continue;
0156         end
0157         variable_name_suffix = regexprep(sensor_name, '_', '_moving_');
0158         
0159         %record position moving event
0160         variable_name = [save_path 'event_motion_pos_' variable_name_suffix];
0161         record_variable(sid, variable_name, pos_movements{sensor_id} );
0162         
0163         %record orientation moving event
0164         variable_name = [save_path 'event_motion_rot_' variable_name_suffix];
0165         record_variable(sid, variable_name, rot_movements{sensor_id} );
0166     end
0167     disp('Done with movement detection');
0168     cell_pos_moving_event = pos_movements;
0169     cell_ori_moving_event = rot_movements;
0170 end
0171 
0172 
0173 %%
0174 function [ result ] = get_sensor_name( sensor_id )
0175     sensor_names = { 'head_child', 'head_parent', ...
0176                      'left-hand_child', 'right-hand_child', ...
0177                      'left-hand_parent', 'right-hand_parent' };
0178     if sensor_id <1 || sensor_id > numel(sensor_names)
0179         result =[];
0180     else
0181         result = sensor_names{ sensor_id };
0182     end
0183 end
0184

Generated on Tue 23-May-2017 20:00:55 by m2html © 2005