stim_meas_list

PURPOSE ^

STIM_MEAS_LIST: mk stimulation pattern from list of electrodes

SYNOPSIS ^

function [stim, meas_sel]= stim_meas_list( sp_mp , Nelec, current, gain);

DESCRIPTION ^

STIM_MEAS_LIST: mk stimulation pattern from list of electrodes
 [stim, meas_sel]= stim_meas_list( sp_mp , Nelec, amplitude, gain);
 [sp_mp]         = stim_meas_list( stim);

 stim =    EIDORS stimulation structure
     use: fwd_model.stimulation = stim;
 meas_sel =EIDORS meas_select (select all values specified)
                to form part of a fwd_model object

 sp_mp = matrix [stim+, stim-, meas+, meas-] x N_patterns

 Nelec  = Num of electrodes         DEFAULT = max sp_mp
 current= drive current levels,     DEFAULT = .010 Amp
 gain   = gain on voltage channels  DEFAULT = 1

 example: stim_meas_list([1,2,3,4; 1,2,4,5])
    create stim pattern in to elecs 1,2 with differential
    measurements on electrodes 3,4 and 4,5;
    then convert the stim struct back to a list of stim/meas pairs
  s=stim_meas_list([1,2,3,4; 1,2,4,5]);
  stim_meas_list(s)
  ans=[1,2,3,4; 1,2,4,5]

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [stim, meas_sel]= stim_meas_list( sp_mp , Nelec, current, gain);
0002 %STIM_MEAS_LIST: mk stimulation pattern from list of electrodes
0003 % [stim, meas_sel]= stim_meas_list( sp_mp , Nelec, amplitude, gain);
0004 % [sp_mp]         = stim_meas_list( stim);
0005 %
0006 % stim =    EIDORS stimulation structure
0007 %     use: fwd_model.stimulation = stim;
0008 % meas_sel =EIDORS meas_select (select all values specified)
0009 %                to form part of a fwd_model object
0010 %
0011 % sp_mp = matrix [stim+, stim-, meas+, meas-] x N_patterns
0012 %
0013 % Nelec  = Num of electrodes         DEFAULT = max sp_mp
0014 % current= drive current levels,     DEFAULT = .010 Amp
0015 % gain   = gain on voltage channels  DEFAULT = 1
0016 %
0017 % example: stim_meas_list([1,2,3,4; 1,2,4,5])
0018 %    create stim pattern in to elecs 1,2 with differential
0019 %    measurements on electrodes 3,4 and 4,5;
0020 %    then convert the stim struct back to a list of stim/meas pairs
0021 %  s=stim_meas_list([1,2,3,4; 1,2,4,5]);
0022 %  stim_meas_list(s)
0023 %  ans=[1,2,3,4; 1,2,4,5]
0024 
0025 % (C) 2010,2015 Andy Adler, Alistair Boyle. License: GPL version 2 or version 3
0026 % $Id: stim_meas_list.m 5150 2015-08-18 14:36:32Z fab-b $
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    % create a new stim if it isn't the same as the last one
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') % append pattern if required
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 % take in a stim/meas struct
0071 % return a matrix of stim/meas pairs, per row with drive current 'i' and measurement gain 'g' calculated
0072 % [ +s -s +m -m i g ]
0073 function stim_flat = flatten_stim(stim, nst, nvt)
0074   stim_flat = zeros(nvt, 6);
0075   idx = 1;
0076   % TODO calculate 'gain' when it matched (m+ == - m-)
0077   % TODO calculate 'gain' when it is unmatched (m+ ~= m-)
0078   % TODO calculate 'current' when it matched (s+ == - s-)
0079   % TODO calculate 'current' when it is unmatched (s+ ~= s-)
0080   for i = 1:nst
0081       nmp= size(stim(i).meas_pattern, 1); % number of measurement patterns for this stim pair
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       % expand s+/s- to match the size of m+/m-
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 ... % stim pairs
0095             mp mn spv mpv];  % meas pairs
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

Generated on Wed 21-Jun-2017 09:29:07 by m2html © 2005