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 current = current.*ones(Npat,1);
0054 for i=1:Npat
0055 cur = sp_mp(i,1:2);
0056 new_stim = sparse( cur, 1, current(i)*[-1,1], Nelec,1);
0057
0058 if (is < 1) || any(any(stim(is).stim_pattern ~= new_stim))
0059 is = is + 1;
0060 end
0061 stim(is).stimulation = 'Amp';
0062 stim(is).stim_pattern = new_stim;
0063 mes = sp_mp(i,3:4);
0064 if isfield(stim(is),'meas_pattern')
0065 stim(is).meas_pattern = [ stim(is).meas_pattern; sparse( 1, mes, gain * [-1,1], 1, Nelec)];
0066 else
0067 stim(is).meas_pattern = sparse( 1, mes, gain * [-1,1], 1, Nelec);
0068 end
0069 end
0070
0071
0072
0073
0074 function stim_flat = flatten_stim(stim, nst, nvt)
0075 stim_flat = zeros(nvt, 6);
0076 idx = 1;
0077
0078
0079
0080
0081 for i = 1:nst
0082 nmp= size(stim(i).meas_pattern, 1);
0083 [sp, jnk, spv]= find(stim(i).stim_pattern>0);
0084 [sn, jnk, snv]= find(stim(i).stim_pattern<0);
0085 [order, mp, mpv]= find(stim(i).meas_pattern>0);
0086 [~, idx2sort] = sort(order); mp = mp(idx2sort); mpv = mpv(idx2sort);
0087 [order, mn, mnv]= find(stim(i).meas_pattern<0);
0088 [~, idx2sort] = sort(order); mn = mn(idx2sort); mnv = mnv(idx2sort);
0089
0090 sp = zeros(nmp,1)+sp;
0091 sn = zeros(nmp,1)+sn;
0092 spv = zeros(nmp,1)+spv;
0093 snv = zeros(nmp,1)+snv;
0094 stim_flat(idx:idx+nmp-1,:) = ...
0095 [ sp sn ...
0096 mp mn spv mpv];
0097 idx = idx + nmp;
0098 end
0099
0100 function do_unit_test
0101 imdl = mk_common_model('a2c0',16);
0102 img = mk_image(imdl);
0103 list_in = [1,2,3,4;1,2,4,5];
0104 img.fwd_model.stimulation = stim_meas_list(list_in,16);
0105 list_out = stim_meas_list(img.fwd_model.stimulation);
0106 unit_test_cmp('pattern#1', list_in, list_out);
0107
0108
0109 vh = fwd_solve(img);
0110 list_in = [6,7,3,4;1,2,4,5];
0111 img.fwd_model.stimulation = stim_meas_list(list_in,16);
0112 list_out = stim_meas_list(img.fwd_model.stimulation);
0113 unit_test_cmp('pattern#2', list_in, list_out);
0114
0115 vh = fwd_solve(img);
0116
0117
0118 nElecs = 16;
0119 stim = mk_stim_patterns( nElecs, 1, '{ad}', '{ad}');
0120 sp_mp = stim_meas_list(stim);
0121 inj_diff = mod(sp_mp(:,2) - sp_mp(:,1), nElecs);
0122 meas_diff = mod(sp_mp(:,3) - sp_mp(:,4), nElecs);
0123 unit_test_cmp('pattern#3', true, all(inj_diff == 1) && all(meas_diff == 1));
0124
0125 stim = mk_stim_patterns( nElecs, 1, '{ad}', '{ad}', {'no_meas_current', 'no_rotate_meas'});
0126 sp_mp = stim_meas_list(stim);
0127 inj_diff = mod(sp_mp(:,2) - sp_mp(:,1), nElecs);
0128 meas_diff = mod(sp_mp(:,3) - sp_mp(:,4), nElecs);
0129 unit_test_cmp('pattern#4', true, all(inj_diff == 1) && all(meas_diff == 1));
0130
0131 stim = mk_stim_patterns( nElecs, 1, '{ad}', '{ad}', {'no_meas_current'});
0132 sp_mp = stim_meas_list(stim);
0133 inj_diff = mod(sp_mp(:,2) - sp_mp(:,1), nElecs);
0134 meas_diff = mod(sp_mp(:,3) - sp_mp(:,4), nElecs);
0135 unit_test_cmp('pattern#5', true, all(inj_diff == 1) && all(meas_diff == 1));
0136
0137 Skip = 3;
0138 stim = mk_stim_patterns( nElecs, 1, [0 1+Skip], [0 1+Skip], {'no_meas_current', 'no_rotate_meas'});
0139 sp_mp = stim_meas_list(stim);
0140 inj_diff = mod(sp_mp(:,2) - sp_mp(:,1), nElecs);
0141 meas_diff = mod(sp_mp(:,3) - sp_mp(:,4), nElecs);
0142 unit_test_cmp('pattern#6', true, all(inj_diff == Skip+1) && all(meas_diff == Skip+1));
0143
0144 nElecs = 32;
0145 Skip = 7;
0146 stim = mk_stim_patterns( nElecs, 1, [0 1+Skip], [0 1+Skip], {'meas_current', 'rotate_meas'});
0147 sp_mp = stim_meas_list(stim);
0148 inj_diff = mod(sp_mp(:,2) - sp_mp(:,1), nElecs);
0149 meas_diff = mod(sp_mp(:,3) - sp_mp(:,4), nElecs);
0150
0151
0152 unit_test_cmp('pattern#7', true, all(inj_diff == Skip+1) && all(meas_diff == Skip+1));
0153
0154
0155
0156