Home > NoiseTools > nt_find_bad_channels.m

# nt_find_bad_channels

## PURPOSE

[iBad,toGood]=nt_find_bad_channels(x,proportion,thresh1,thresh2,thresh3) - find bad channels

## SYNOPSIS

function [iBad,toGood]=nt_find_bad_channels(x,proportion,thresh1,thresh2,thresh3)

## DESCRIPTION

    [iBad,toGood]=nt_find_bad_channels(x,proportion,thresh1,thresh2,thresh3) - find bad channels

       iBad: indices of bad channels
       toGood: matrix to good channels

       x: data
       proportion: proportion of time above threshold(s) [default: 0.5]
       thresh1: threshold relative to median absolute value over all data [default: 3]
       thresh2: absolute threshold
       thresh3: applies to projection residual

     Thresholds apply to absolute value.
     'thresh3' applies to the residual of the projection of a channel on
     neighboring channels (as calculated by 'sns'), expressed as a proportion
     of median absolute value of that channel.

     NoiseTools

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_sns y=nt_sns(x,nneigbors,skip,w) - sensor noise suppression

This function is called by:

## SOURCE CODE

    0001 function [iBad,toGood]=nt_find_bad_channels(x,proportion,thresh1,thresh2,thresh3)
    0002 %[iBad,toGood]=nt_find_bad_channels(x,proportion,thresh1,thresh2,thresh3) - find bad channels
    0003 %
    0004 %   iBad: indices of bad channels
    0005 %   toGood: matrix to good channels
    0006 %
    0007 %   x: data
    0008 %   proportion: proportion of time above threshold(s) [default: 0.5]
    0009 %   thresh1: threshold relative to median absolute value over all data [default: 3]
    0010 %   thresh2: absolute threshold
    0011 %   thresh3: applies to projection residual
    0012 %
    0013 % Thresholds apply to absolute value.
    0014 % 'thresh3' applies to the residual of the projection of a channel on
    0015 % neighboring channels (as calculated by 'sns'), expressed as a proportion
    0016 % of median absolute value of that channel.
    0017 %
    0018 % NoiseTools
    0019
    0020
    0021 if nargin<2||isempty(proportion); proportion=0.5; end
    0022 if nargin<3||isempty(thresh1); thresh1=3; end
    0023 if nargin<4; thresh2=[]; end
    0024 if nargin<5; thresh3=[]; end
    0025
    0026 w=ones(size(x));
    0027
    0028 if ~isempty(thresh1)
    0029     md=median(abs(x(:)));
    0030     w(find(abs(x)>thresh1*md))=0;
    0031 end
    0032 if ~isempty(thresh2)
    0033     w(find(abs(x)>thresh2))=0;
    0034 end
    0035 if ~isempty(thresh3)
    0036     NNEIGHBORS=10;
    0037     xx=nt_sns(x,NNEIGHBORS); % remove sensor specific
    0038     md=median(abs(xx(:)));
    0039     xx=xx-x; % residual
    0040     for iChan=1:size(x,2)
    0041         w(find(abs(xx(:,iChan))>thresh3*md),iChan)=0;
    0042     end
    0043 end
    0044 iBad=find(mean(1-w)>proportion);
    0045
    0046 toGood=eye(size(x,2));
    0047 toGood(:,iBad)=[];
    0048
    0049 if nargout==0
    0050     % plot, don't return values
    0051     plot(mean(1-w), '.-');
    0052     h=line([0 size(x,2)],[proportion proportion]); set(h,'linestyle','--');
    0053     xlabel('channel'); ylabel('proportion bad');
    0054     xlim([0 size(x,2)+1])
    0055
    0056     clear iBad toGood
    0057 end
    0058
    0059
    0060
    0061