0001 function [toGood,fromGood]=interpolate_bad_channels(x,iBad,coordinates,n)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 nt_greetings;
0014
0015 if nargin<3;
0016 error('!');
0017 end
0018 if nargin<4;
0019 n=3;
0020 end
0021
0022 nchans=size(x,2);
0023 toGood=eye(nchans);
0024 toGood(:,iBad)=[];
0025
0026 [closest,d]=nt_proximity(coordinates);
0027 if size(closest,1)~=nchans; error('!'); end
0028
0029 fromGood=eye(nchans);
0030 for iChan=iBad
0031 iOthers=closest(iChan,:);
0032 iOthers=setdiff(iOthers, iBad, 'stable');
0033 if numel(iOthers)<n; error('!'); end
0034 iOthers=iOthers(1:n);
0035 w=1./(d(iChan,iOthers) + eps);
0036 w=w/sum(w);
0037 fromGood(iOthers,iChan)=w;
0038 end
0039 fromGood(iBad,:)=[];
0040
0041 topo=ones(nchans,1);
0042 topo(iBad)=0;
0043 if nargout==0
0044 figure(100); clf
0045 subplot 121; nt_imagescc(fromGood);
0046 subplot 122; nt_topoplot(coordinates,topo);
0047 end
0048