Home > bioelectromagnetism > elec_distance_bad.m

elec_distance_bad

PURPOSE ^

elec_distance_bad - identify electrodes that were poorly digitised

SYNOPSIS ^

function elec_distance_bad(filename)

DESCRIPTION ^

 elec_distance_bad - identify electrodes that were poorly digitised

 This utility is designed to identify electrodes that were
 poorly digitised by the Polhemus digitiser.  It calculates
 interelectrode distances and identifies electrodes with
 excessive distance (Mean +/- [2 * StDev]).

 The script creates output to text data files of nearest
 neighbour distances (*.nn) and outliers in both nearest
 neighbour distances and consecutive distances (*.bad).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function elec_distance_bad(filename)
0002 
0003 % elec_distance_bad - identify electrodes that were poorly digitised
0004 %
0005 % This utility is designed to identify electrodes that were
0006 % poorly digitised by the Polhemus digitiser.  It calculates
0007 % interelectrode distances and identifies electrodes with
0008 % excessive distance (Mean +/- [2 * StDev]).
0009 %
0010 % The script creates output to text data files of nearest
0011 % neighbour distances (*.nn) and outliers in both nearest
0012 % neighbour distances and consecutive distances (*.bad).
0013 %
0014 
0015 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:33 $
0016 
0017 % Licence:  GNU GPL, no implied or express warranties
0018 % History:  05/2000, Darren.Weber_at_radiology.ucsf.edu
0019 %
0020 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0021 
0022     if ~exist('filename','var')
0023         filename = 'eeg_example_data/elec_124_cart.txt';
0024     end
0025 
0026 % Read electrode data, using utility 'elec_load'
0027 
0028     [elec,type,X,Y,Z] = elec_load(filename);
0029     
0030     % Find index values for electrodes, excluding PAN and centroid/ref
0031     electrodes = ones(size(type)) * 69;
0032     Index = find(type == electrodes);
0033     
0034     Ei = elec(Index);  Xi = X(Index);  Yi = Y(Index);  Zi = Z(Index);
0035     
0036 % plot electrode position grid
0037     elec_meshplot(Xi,Yi,Zi);
0038     
0039     view(2);            % top view
0040 %   view(-90, 0);        % left side view
0041 %   view( 90, 0);        % right side view
0042 %   view(-180,0);        % front view
0043 %   view(   0,0);        % back view
0044 
0045 % return;
0046 
0047 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0048 % Calculate inter-electrode distances
0049 
0050   [dist_e, dist_d] = elec_distance(Ei,Xi,Yi,Zi);
0051 
0052 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0053 % Identify distance between consecutive and nearest neighbour electrodes
0054 
0055   [C_e, C_d] = elec_distance_consec(dist_e, dist_d);
0056   [NN_e, NN_d] = elec_distance_nn(dist_e, dist_d);
0057 
0058   %clear dist_e, dist_d;
0059 
0060   NN_d = NN_d(:,2:end);  % remove column 1, all zero distances
0061   NN_e = NN_e(:,2:end);
0062 
0063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0064 % Check for abnormal small or large distances
0065 
0066   fprintf('\n%s\n', 'Calculating outliers of electrode distances.');
0067 
0068   NN_bad_e = cell(0);
0069   NN_bad_d = [];
0070 
0071   NN_Mean = mean(NN_d(:,1));
0072   NN_StDev = std(NN_d(:,1));
0073   NN_low  = NN_Mean - (2 * NN_StDev);
0074   NN_high = NN_Mean + (2 * NN_StDev);
0075 
0076   for e = 1:length(NN_d(:,1))
0077 
0078     if(NN_d(e,1) > 0)
0079 
0080         if (NN_d(e,1) < NN_low)
0081 
0082             NN_bad_e(end + 1) = NN_e(e,1);
0083             NN_bad_d(end + 1) = NN_d(e,1);
0084 
0085         elseif (NN_d(e,1) > NN_high)
0086 
0087             NN_bad_e(end + 1) = NN_e(e,1);
0088             NN_bad_d(end + 1) = NN_d(e,1);
0089         end
0090     end
0091   end
0092   clear e;
0093 
0094 
0095   C_bad_e = cell(0);
0096   C_bad_d = [];
0097 
0098   C_Mean  = mean(C_d);
0099   C_StDev = std(C_d);
0100   C_low   = C_Mean - (2 * C_StDev);
0101   C_high  = C_Mean + (2 * C_StDev);
0102 
0103   for e = 1:length(C_d)
0104 
0105     if(C_d(e) > 0)
0106 
0107         if (C_d(e) < C_low)
0108 
0109             C_bad_e(end + 1) = C_e(e);
0110             C_bad_d(end + 1) = C_d(e);
0111 
0112         elseif (C_d(e) > C_high)
0113 
0114             C_bad_e(end + 1) = C_e(e);
0115             C_bad_d(end + 1) = C_d(e);
0116         end
0117     end
0118   end
0119   clear e;
0120 
0121 
0122 
0123 
0124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0125 % highlight bad electrodes on plot
0126 
0127   %%%%%%%%%%%%%%%%%%%%%%%%%%%%
0128   % Nearest neighbour outliers
0129 
0130 
0131   if ~isempty(NN_bad_d)
0132 
0133       Xb = [];  Yb = [];  Zb = [];
0134 
0135       for n = 1:length(NN_bad_e)
0136         e = str2num(char(NN_bad_e(n)));
0137         index = strmatch(num2str(e(1)), char(elec), 'exact');
0138         Xb(n) = X(index);
0139         Yb(n) = Y(index);
0140         Zb(n) = Z(index);
0141       end
0142       clear index e n;
0143 
0144 
0145       plot3(Xb,Yb,Zb,'gx',Xb,Yb,Zb,'gd');
0146   end
0147 
0148   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0149   % Consecutive electrode distance outliers
0150 
0151   if ~isempty(C_bad_d)
0152 
0153        Xb = [];  Yb = [];  Zb = [];
0154 
0155        for n = 1:length(C_bad_e)
0156          e = str2num(char(C_bad_e(n)));
0157          index = strmatch(num2str(e(1)), char(elec), 'exact');
0158          Xb(n) = X(index);
0159          Yb(n) = Y(index);
0160          Zb(n) = Z(index);
0161        end
0162        clear index e n;
0163 
0164        plot3(Xb,Yb,Zb,'bx',Xb,Yb,Zb,'bd');
0165   end
0166 
0167 
0168 
0169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0170 % Output 10 nearest neighbour electrode data
0171 
0172   [pathstr,name,ext,versn] = fileparts(filename);
0173   nnfile = fullfile(pathstr,strcat(name,'.nn'));
0174 
0175   FID = fopen(nnfile,'w');
0176 
0177   fprintf(  1,'%20s%10.4f\n','mean',NN_Mean,'stdev',NN_StDev,'low',NN_low,'high',NN_high);
0178   fprintf(FID,'%20s%10.4f\n','mean',NN_Mean,'stdev',NN_StDev,'low',NN_low,'high',NN_high);
0179 
0180   labels = str2num(char(NN_e(:,1)));
0181   nn = [labels NN_d(:,1)];
0182 
0183   for n = 2:10
0184       labels = str2num(char(NN_e(:,n)));
0185       nn(:,(end+1):(end+2)) = [labels(:,2) NN_d(:,n)];
0186   end
0187 
0188 %  fprintf(  1,'%10d: %10d%10.4f%, 10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f\n',nn');
0189 %  fprintf(FID,'%10d: %10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f, %10d%10.4f\n',nn');
0190 
0191 
0192 
0193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0194 % Output bad electrode labels and distance values
0195 
0196   [pathstr,name,ext,versn] = fileparts(filename);
0197   bfile = fullfile(pathstr,strcat(name,'.bad'));
0198 
0199   FID = fopen(bfile,'w');
0200 
0201   if(NN_bad_d)
0202 
0203       NN_bad_en = str2num(char(NN_bad_e));
0204       NN_bad = [NN_bad_en(:,1) NN_bad_d']';
0205       fprintf('\n%s\n\n', 'Nearest neighbour summary stats:');
0206       fprintf('%-10s%10.4f\n','NN_Mean',NN_Mean,'NN_StDev',NN_StDev,'NN_low',NN_low,'NN_high',NN_high);
0207       fprintf('\n%s\n\n', 'Nearest neighbour outliers (green):');
0208       fprintf('%10d%10.4f\n', NN_bad);
0209 
0210       fprintf(FID,'\n%s\n\n', 'Nearest neighbour summary stats:');
0211       fprintf(FID,'%-10s%10.4f\n','NN_Mean',NN_Mean,'NN_StDev',NN_StDev,'NN_low',NN_low,'NN_high',NN_high);
0212 
0213       fprintf(FID,'\n%s\n\n', 'Nearest neighbour outliers:');
0214       fprintf(FID,'%10d%10.4f\n', NN_bad);
0215 
0216 
0217   end
0218 
0219   if(C_bad_d)
0220 
0221       C_bad_en = str2num(char(C_bad_e));
0222       C_bad = [C_bad_en(:,1) C_bad_d'];
0223       fprintf('\n%s\n\n', 'Consecutive electrode summary stats:');
0224       fprintf('%-10s%10.4f\n','C_Mean',C_Mean,'C_StDev',C_StDev,'C_low',C_low,'C_high',C_high);
0225       fprintf('\n%s\n\n', 'Consecutive electrode outliers (blue):');
0226       fprintf('%10.0f%10.4f\n', C_bad);
0227 
0228       fprintf(FID,'\n%s\n\n', 'Consecutive electrode summary stats:');
0229       fprintf(FID,'%-10s%10.4f\n','C_Mean',C_Mean,'C_StDev',C_StDev,'C_low',C_low,'C_high',C_high);
0230 
0231       fprintf(FID,'\n%s\n\n', 'Consecutive electrode outliers:');
0232       fprintf(FID,'%10d%10.4f\n', C_bad);
0233 
0234   end

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003