Detect Movement

The process will identify when a subject has moved during a recording. This runs only on continuous, raw recordings for CTF MEG where continuous head localization is recorded (and the HLC channels are collected.) This process is currently being tested. If you find a bug or have other comments related to its performance, please provide comments here or on the Forum.

The end goal if this detection is to find when the subject has moved beyond an acceptable limit and cut the longer recordings in to shorter recording and assign a new head position to each. There are two steps:

1. Detect the movement. This will go through the recording and mark short transient movements (<5 seconds) as 'BAD' and mark the beginning of longer movements where the file can be cut. It generates a file called headpostions.txt which contains the recommended locations where the file should be cut and the new head position that should be written to the new file.

   1 function split_ctf_recording(origDS, newDSpath, sampRate, totalTrials)
   2 % splits CTF .ds recordings into chunks according to the head
   3 % movement.  Run the Brainstorm process 'process_detect_movement.m' first to get the
   4 % headpositions.txt file
   5 % 
   6 % Inputs: 	origDS		- file to split
   7 %		newDSpath	- directory where the new file will be written
   8 %		sampRate	- sampling rate of the data set
   9 %		totalTrial	- total trials in the file
  10 %
  11 % Elizabeth Bock 2013-2015 
  12 
  13 %% Info
  14 info = load(fullfile(origDS, 'headpositions.txt'));
  15 [p,n] = fileparts(origDS);
  16 
  17 posFile = dir([origDS '/*.pos']);
  18 posFile = fullfile(origDS, posFile.name);
  19 
  20 % find files that are > 5 secs
  21 len = diff(info(:,1));
  22 ind = find(len > sampRate*5);
  23 disp(['Files ' num2str(ind') ' are greater than 5 seconds']);
  24 
  25 %% split first chunk
  26 if info(1,1) == 1
  27     disp('first chunk has a new head position also')
  28     % go directly to the 'N-1 chunks' to get the new head position
  29 else
  30     st = 1;
  31     en = round(info(1,1)/sampRate) - 1;
  32     newDS = [newDSpath '/' n '_s1.ds'];
  33     command = ['ctf newDs -includeTrialRange ' num2str(st) ' ' num2str(en) ' ' origDS ' ' newDS];
  34     system(command)
  35     try
  36         copyfile(posFile,newDS)
  37     catch
  38         disp (['cannot copy pos file to ' newDS])
  39     end
  40 end
  41 
  42 
  43 %% next N-1 chunks
  44 for ii = 1:size(info,1) - 1
  45     % split next chunk
  46     st = max(1,round(info(ii,1)/sampRate));
  47     en = round(info(ii+1,1)/sampRate) - 1;
  48     newDS = [newDSpath '/' n '_s' num2str(ii+1) '.ds'];
  49     command = ['ctf newDs -includeTrialRange ' num2str(st) ' ' num2str(en) ' ' origDS ' ' newDS];
  50     system(command) 
  51 
  52     % update head position (need mm for input to this function)
  53     na = info(ii,3:5).*1000;
  54     le = info(ii,6:8).*1000;
  55     re = info(ii,9:11).*1000;
  56     command = ['ctf changeHeadPos -na ' num2str(na) ' -le ' num2str(le) ' -re ' num2str(re) ' ' newDS];
  57     system(command)
  58 
  59     try
  60         copyfile(posFile,newDS)
  61     catch
  62         disp (['cannot copy pos file to ' newDS])
  63     end
  64 end
  65 
  66 %% last chunk
  67 ind = size(info,1);
  68 st = round(info(ind,1)/sampRate);
  69 en = totalTrials;
  70 newDS = [newDSpath '/' n '_s' num2str(ind+1) '.ds'];
  71 command = ['ctf newDs -includeTrialRange ' num2str(st) ' ' num2str(en) ' ' origDS ' ' newDS];
  72 system(command) 
  73 
  74 % update head position
  75 na = info(ind,3:5).*1000;
  76 le = info(ind,6:8).*1000;
  77 re = info(ind,9:11).*1000;
  78 command = ['ctf changeHeadPos -na ' num2str(na) ' -le ' num2str(le) ' -re ' num2str(re) ' ' newDS];
  79 system(command)
  80 
  81 try
  82     copyfile(posFile,newDS)
  83 catch
  84     disp (['cannot copy pos file to ' newDS])
  85 end
1. Cut the recording into shorter recordings using the CTF software. Use the headpositions.txt file to know where to make the new .ds files and what the new head position should be. This is done outside the Brainstorm software, using the CTF commandline program newDs. You can see an example here: split_ctf_recording.m

To run the movement detection, put the raw link in the Process1 box -> Run -> Events -> Detect movement [Experimental]

Tutorials/MovementDetect (last edited 2015-05-08 12:18:33 by ?ElizabethBock)