0001 function [elec_idx, new_fmdl] = elec_rearrange( pattern, newarrange, fwd_model )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 if ischar(pattern) && strcmp(pattern,'UNIT_TEST'); do_unit_test; return; end
0021
0022 if isnumeric(pattern)
0023 p.n_elecs = pattern(1);
0024 p.n_layers= pattern(2);
0025 elseif isstruct(pattern)
0026 p = pattern;
0027 else
0028 error('Can''t interpret "pattern" input');
0029 end
0030
0031 if p.n_layers >= 3;
0032 error('elec_rearrange: can''t interpret n_layers >=2');
0033 end
0034
0035 idx = reshape(1:(p.n_elecs*p.n_layers),p.n_layers,p.n_elecs);
0036 switch lower(newarrange)
0037 case 'none';
0038 case {'odd_even','zigzag'};
0039 idx = reshape(idx,2,[])';
0040 case 'square' ;
0041 idx = reshape(idx,2,[])';
0042 idx(2:2:end,:) = fliplr(idx(2:2:end,:));
0043 otherwise;
0044 error('case %s not understood',newarrange);
0045 end
0046 elec_idx = idx(:);
0047
0048 if nargin >= 3
0049 new_fmdl = fwd_model;
0050 new_fmdl.electrode(elec_idx) = new_fmdl.electrode;
0051 end
0052
0053 function do_unit_test
0054 sqV = [1,2;4,3;5,6;8,7;9,10;12,11;13,14;16,15]';
0055 oeV = [1,2;3,4;5,6;7,8;9,10;11,12;13,14;15,16]';
0056
0057
0058 elec= reshape([1:16],8,2)';
0059 idx = elec_rearrange(size(elec'),'square');
0060 unit_test_cmp('Sq#1 [8,2]', reshape(idx,size(elec'))', sqV);
0061
0062 idx = elec_rearrange(size(elec'),'odd_even');
0063 unit_test_cmp('OE#1 [8,2]', reshape(idx,size(elec'))', oeV);
0064
0065 idx = elec_rearrange(size(elec'),'zigZaG');
0066 unit_test_cmp('OE#2 [8,2]', reshape(idx,size(elec'))', oeV);
0067
0068 idx = elec_rearrange(size(elec'),'none');
0069 unit_test_cmp('OE#2 [8,2]', idx', 1:16);
0070
0071 p.n_elecs = 8; p.n_layers = 2;
0072 idx = elec_rearrange(p,'square');
0073 idx = reshape(idx,size(elec'))';
0074 unit_test_cmp('Sq#2 [8,2]', idx, sqV);
0075
0076 elec= [1:32]';
0077 idx = elec_rearrange(size(elec),'square');
0078 idx= reshape(idx,[16,2])';
0079 unit_test_cmp('Sq [16,2]', idx(:,1:8), sqV);
0080
0081 imdl = mk_common_model('n3r2',[16,2]); fmdl1= imdl.fwd_model;
0082 subplot(221);
0083 idx = elec_rearrange([16,2],'square');
0084 fmdl1.electrode(idx) = fmdl1.electrode;
0085 show_fem_enhanced(fmdl1,[0,1]);
0086 view(10,82);
0087
0088 subplot(222);
0089 [~,fmdl2] = elec_rearrange([16,2],'square', imdl.fwd_model);
0090 show_fem_enhanced(fmdl2,[0,1]);
0091 view(10,82);
0092
0093 unit_test_cmp('fmdl#1', [fmdl1.electrode([5,9]).nodes], [156 157 219 220 128 129 191 192]);
0094 unit_test_cmp('fmdl#2', fmdl1.electrode, fmdl2.electrode)