, , ,

Fixing a missing sensor

Use this script when one or more channels in the 275 channel CTF system are dead. It identifies the missing sensors and uses FT_CHANNELREPAIR to fix the problem, so you can compare or average this handicapped dataset with complete datasets. Use this function after preprocessing. Prepare yourself: a path to a dir with a labels and grad variable (get from a previously recorded dataset, make it yourself or try the given datapath in the code). Check the code below for proper naming.

if size(data.label,1) ~= 275
  cd('/home/electromag/jurvdwer/analysis/'); 
  load LABEL275.mat;   % read the complete set of labels, e.g. from a previous dataset that is known to be correct 
  load GRAD275.mat;    % read the gradiometer definition from a previous dataset that is known to be correct
  badchanindx = [];
  while size(data.label,1) < 275
      for ind = 1:size(data.label)
          if ~strcmp(data.label{ind},LABEL{ind})
              badchanindx = [badchanindx; ind];
              fprintf('missing sensor: %s...\n',LABEL{ind})
              data.label = [data.label(1:ind-1); LABEL{ind}; data.label(ind:end)];
              for ind2 = 1:size(data.trial,2)
                  newtrial{ind2} = [data.trial{ind2}(1:ind-1,:);zeros(1,size(data.trial{ind2},2));data.trial{ind2}(ind:end,:)];
              end
              break
          end
      end
      data.trial = newtrial;
  end
  warning('Inserting grad that does not actually belong to this dataset!')
  data.grad = grad; 
end

Subsequently you can use FT_CHANNELREPAIR for nearest neighbourhood averaging. This replaces the zeros in the broken channel with more appropriate values.

cfgmr               = []
cfgmr.badchannel    = data.label(badchanindx);
cfgmr.neighbourdist = 4;
data = ft_channelrepair(cfgmr,data);