Home > bml > signal > bml_rereference.m

bml_rereference

PURPOSE ^

BML_REREFERENCE applies re-referencing scheme to raw

SYNOPSIS ^

function [ref,U] = bml_rereference(cfg,raw)

DESCRIPTION ^

 BML_REREFERENCE applies re-referencing scheme to raw

 Use as
   [ref,U] = bml_rereference(cfg,raw)

 cfg.label - Nx1 cellstr with labels. Defaults to raw.label
 cfg.group - Nx1 integer array identifying groups of electrodes
   contacts of same group are re-referenced together
   values <=0 and nans are not re-referenced. 
   If not specified, groups are defined by string before underscore '_'
   (i.e. ecog_*, micro_*, macro_*, audio_*, envaudio_*, etc)
 cfg.method - string, method to be implemented
   'CAR' or 'common', common average referencing (default)
   'LAR' or 'local', local average referencing
   'VAR' or 'variable', variable average referencing

 raw - ft_datatype_raw to be re-referenced
 
 Returns
 ref - ft_datatype_raw with re-referenced data
 U - double, unmixing matrix. ref = U * raw

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [ref,U] = bml_rereference(cfg,raw)
0002 
0003 % BML_REREFERENCE applies re-referencing scheme to raw
0004 %
0005 % Use as
0006 %   [ref,U] = bml_rereference(cfg,raw)
0007 %
0008 % cfg.label - Nx1 cellstr with labels. Defaults to raw.label
0009 % cfg.group - Nx1 integer array identifying groups of electrodes
0010 %   contacts of same group are re-referenced together
0011 %   values <=0 and nans are not re-referenced.
0012 %   If not specified, groups are defined by string before underscore '_'
0013 %   (i.e. ecog_*, micro_*, macro_*, audio_*, envaudio_*, etc)
0014 % cfg.method - string, method to be implemented
0015 %   'CAR' or 'common', common average referencing (default)
0016 %   'LAR' or 'local', local average referencing
0017 %   'VAR' or 'variable', variable average referencing
0018 %
0019 % raw - ft_datatype_raw to be re-referenced
0020 %
0021 % Returns
0022 % ref - ft_datatype_raw with re-referenced data
0023 % U - double, unmixing matrix. ref = U * raw
0024 
0025 assert(isstruct(raw),"invalid raw");
0026 assert(all(ismember({'trial','time','label'},fieldnames(raw))),"invalid raw");
0027 
0028 label     = bml_getopt(cfg,'label',raw.label);
0029 group     = bml_getopt(cfg,'group');
0030 method    = bml_getopt_single(cfg,'method','CAR');
0031 
0032 %inferring groups from labels
0033 if isempty(group)
0034   sl=split(label,'_');
0035   sl=sl(:,1);
0036   usl=unique(sl);
0037   group = bml_map(sl,usl,1:length(usl));
0038 end
0039 
0040 %assigning orphan raw labels to null group
0041 in=ismember(raw.label,label);
0042 if ~all(in)
0043   label = [label;raw.label(~in)];
0044   group = [group; zeros(sum(~in),1)];
0045 end
0046 
0047 %re-ordering group to match raw.label
0048 group = bml_map(raw.label,label,group);
0049 label = raw.label;
0050 
0051 %creating null group
0052 group(ismissing(group))=0;
0053 group(group<=0)=0;
0054 g0 = sum(group<=0);
0055 
0056 %keeping original order
0057 [g,g_idx]=sort(group);
0058 ug = unique(g);
0059 [N,~] = histc(g,ug);
0060 
0061 %creating blocks
0062 if ismember(method,{'CAR','common'})
0063   U_blocks=cellfun(@bml_comavgref_matrix,num2cell(N),'UniformOutput',false);
0064 elseif ismember(method,{'LAR','local'})
0065   U_blocks=cellfun(@bml_locavgref_matrix,num2cell(N),'UniformOutput',false);
0066 elseif ismember(method,{'VAR','variable'})
0067   warning('Using experimental method VAR, susceptible to high amplitude artifacts')
0068   
0069   %calculating variance-covariance matrix
0070   cfg=[];
0071   cfg.covariance = 'yes';
0072   cfg.vartrllength = 2;
0073   tl_raw=ft_timelockanalysis(cfg,raw);
0074   
0075   %grouping matrix into blocks
0076   COVs = cell(length(ug),1);
0077   for i=1:length(ug)
0078     COVs{i,1}=tl_raw.cov(g_idx(g==ug(i)),g_idx(g==ug(i)));
0079   end
0080   
0081   %creating blocks
0082   U_blocks=cellfun(@bml_varavgref_matrix,COVs,'UniformOutput',false);
0083   
0084 else
0085   error('unknown method')
0086 end
0087 
0088 %replacing block by identity for null group
0089 if g0 > 0
0090   U_blocks{1} = eye(N(1));
0091 end
0092 
0093 %joining blocks
0094 U = blkdiag(U_blocks{:});
0095 U(g_idx,g_idx) = U;
0096 %image(U,'CDataMapping','scaled')
0097   
0098 %applying unmixing matrix to data
0099 ref = bml_apply(@(x) U*x, raw);
0100   
0101

Generated on Tue 25-Sep-2018 10:08:19 by m2html © 2005