0001 function [stim, meas_sel]= stim_meas_list( sp_mp , Nelec, current, gain);
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 if ischar(sp_mp) && strcmp(sp_mp,'UNIT_TEST'); do_unit_test; return; end
0029
0030 if isstruct(sp_mp)
0031 stim = sp_mp;
0032 meas_sel = [];
0033 nst = length(stim);
0034 nvt = 0;
0035 for i = 1:nst;
0036 nvt = nvt + size(stim(i).stim_pattern, 2) * size(stim(i).meas_pattern, 1);
0037 end
0038 stim = flatten_stim(stim, nst, nvt);
0039 stim = stim(:,[2 1 4 3]);
0040 return
0041 end
0042
0043 if nargin <2; Nelec = max(sp_mp(:)); end
0044 if nargin <3; current = 1; end
0045 if nargin <4; gain = 1; end
0046
0047 if any(sp_mp(:) > Nelec);
0048 error('Electrode patterns require more electrodes than Nelec');
0049 end
0050 stim = struct([]);
0051 Npat = size(sp_mp,1);
0052 is = 0;
0053 for i=1:Npat
0054 cur = sp_mp(i,1:2);
0055 new_stim = sparse( cur, 1, current*[-1,1], Nelec,1);
0056
0057 if (is < 1) || any(any(stim(is).stim_pattern ~= new_stim))
0058 is = is + 1;
0059 end
0060 stim(is).stimulation = 'Amp';
0061 stim(is).stim_pattern = new_stim;
0062 mes = sp_mp(i,3:4);
0063 if isfield(stim(is),'meas_pattern')
0064 stim(is).meas_pattern = [ stim(is).meas_pattern; sparse( 1, mes, gain * [-1,1], 1, Nelec)];
0065 else
0066 stim(is).meas_pattern = sparse( 1, mes, gain * [-1,1], 1, Nelec);
0067 end
0068 end
0069
0070
0071
0072
0073 function stim_flat = flatten_stim(stim, nst, nvt)
0074 stim_flat = zeros(nvt, 6);
0075 idx = 1;
0076
0077
0078
0079
0080 for i = 1:nst
0081 nmp= size(stim(i).meas_pattern, 1);
0082 [sp, jnk, spv]= find(stim(i).stim_pattern>0);
0083 [sn, jnk, snv]= find(stim(i).stim_pattern<0);
0084 [order, mp, mpv]= find(stim(i).meas_pattern>0);
0085 [~, idx2sort] = sort(order); mp = mp(idx2sort); mpv = mpv(idx2sort);
0086 [order, mn, mnv]= find(stim(i).meas_pattern<0);
0087 [~, idx2sort] = sort(order); mn = mn(idx2sort); mnv = mnv(idx2sort);
0088
0089 sp = zeros(nmp,1)+sp;
0090 sn = zeros(nmp,1)+sn;
0091 spv = zeros(nmp,1)+spv;
0092 snv = zeros(nmp,1)+snv;
0093 stim_flat(idx:idx+nmp-1,:) = ...
0094 [ sp sn ...
0095 mp mn spv mpv];
0096 idx = idx + nmp;
0097 end
0098
0099 function do_unit_test
0100 imdl = mk_common_model('a2c0',16);
0101 img = mk_image(imdl);
0102 list_in = [1,2,3,4;1,2,4,5];
0103 img.fwd_model.stimulation = stim_meas_list(list_in,16);
0104 list_out = stim_meas_list(img.fwd_model.stimulation);
0105 unit_test_cmp('pattern#1', list_in, list_out);
0106
0107
0108 vh = fwd_solve(img);
0109 list_in = [6,7,3,4;1,2,4,5];
0110 img.fwd_model.stimulation = stim_meas_list(list_in,16);
0111 list_out = stim_meas_list(img.fwd_model.stimulation);
0112 unit_test_cmp('pattern#2', list_in, list_out);
0113
0114 vh = fwd_solve(img);
0115
0116
0117 nElecs = 16;
0118 stim = mk_stim_patterns( nElecs, 1, '{ad}', '{ad}');
0119 sp_mp = stim_meas_list(stim);
0120 inj_diff = mod(sp_mp(:,2) - sp_mp(:,1), nElecs);
0121 meas_diff = mod(sp_mp(:,3) - sp_mp(:,4), nElecs);
0122 unit_test_cmp('pattern#3', true, all(inj_diff == 1) && all(meas_diff == 1));
0123
0124 stim = mk_stim_patterns( nElecs, 1, '{ad}', '{ad}', {'no_meas_current', 'no_rotate_meas'});
0125 sp_mp = stim_meas_list(stim);
0126 inj_diff = mod(sp_mp(:,2) - sp_mp(:,1), nElecs);
0127 meas_diff = mod(sp_mp(:,3) - sp_mp(:,4), nElecs);
0128 unit_test_cmp('pattern#4', true, all(inj_diff == 1) && all(meas_diff == 1));
0129
0130 stim = mk_stim_patterns( nElecs, 1, '{ad}', '{ad}', {'no_meas_current'});
0131 sp_mp = stim_meas_list(stim);
0132 inj_diff = mod(sp_mp(:,2) - sp_mp(:,1), nElecs);
0133 meas_diff = mod(sp_mp(:,3) - sp_mp(:,4), nElecs);
0134 unit_test_cmp('pattern#5', true, all(inj_diff == 1) && all(meas_diff == 1));
0135
0136 Skip = 3;
0137 stim = mk_stim_patterns( nElecs, 1, [0 1+Skip], [0 1+Skip], {'no_meas_current', 'no_rotate_meas'});
0138 sp_mp = stim_meas_list(stim);
0139 inj_diff = mod(sp_mp(:,2) - sp_mp(:,1), nElecs);
0140 meas_diff = mod(sp_mp(:,3) - sp_mp(:,4), nElecs);
0141 unit_test_cmp('pattern#6', true, all(inj_diff == Skip+1) && all(meas_diff == Skip+1));
0142
0143 nElecs = 32;
0144 Skip = 7;
0145 stim = mk_stim_patterns( nElecs, 1, [0 1+Skip], [0 1+Skip], {'meas_current', 'rotate_meas'});
0146 sp_mp = stim_meas_list(stim);
0147 inj_diff = mod(sp_mp(:,2) - sp_mp(:,1), nElecs);
0148 meas_diff = mod(sp_mp(:,3) - sp_mp(:,4), nElecs);
0149 unit_test_cmp('pattern#7', true, all(inj_diff == Skip+1) && all(meas_diff == Skip+1));
0150
0151
0152
0153