0001 function [Epoch, NEpoch,Max_microv,Min_Chan,NBad_chan,bad_chan,mask] = ns_artifact_edit(fid,Samp_Rate,NChan, NSamp, NEvent,scale,Epoch,Max_microv,Min_Chan,Lo_Pass,Hi_Pass);
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 if nargin < 9
0025 error('not enough parameters')
0026 end;
0027 if fid < 1
0028 error('invalid fid');
0029 end;
0030 Max_Bins = (Max_microv/scale).^2;
0031
0032 NEpoch = fix(NSamp/(Epoch*Samp_Rate));
0033
0034 mask = ones(NEpoch, NChan+1);
0035
0036 if nargin >= 10
0037
0038 [blow,alow] = butter(20,[Lo_Pass/(Samp_Rate/2)]);
0039
0040 end;
0041 if nargin == 11
0042
0043 [bband,aband] = butter(3,[Hi_Pass/(Samp_Rate/2) Lo_Pass/(Samp_Rate/2)]);
0044
0045 end;
0046
0047 for i = 1:NEpoch
0048
0049 trialdata = fread(fid,[NChan+NEvent,Samp_Rate*Epoch],'int16');
0050 trialdata = trialdata';
0051 if nargin == 10
0052
0053 for ch =1:NChan
0054 trialdata(:,ch) = filtfilt(blow,alow,trialdata(:,ch));
0055 end;
0056 end;
0057 if nargin == 11
0058 for ch =1:NChan
0059 trialdata(:,ch) = filtfilt(blow,alow,trialdata(:,ch));
0060 trialdata(:,ch) = filtfilt(bband,aband,trialdata(:,ch));
0061 end;
0062 end
0063 trialdata = trialdata.^2;
0064 max_trial = max(trialdata);
0065 bad_chan = find(max_trial > Max_Bins);
0066 mask(i,bad_chan) = zeros(1,size(bad_chan,2));
0067 if (sum(mask(i,:)) < Min_Chan)
0068 mask(i,:) = zeros(1,NChan+1);
0069 end;
0070 end;
0071
0072 grand_mask = sum(mask);
0073 max_mask = max(grand_mask);
0074
0075
0076 bad_chan = find(grand_mask < 0.75*max_mask);
0077 if bad_chan == []
0078 NBad_chan = 0;
0079 else
0080 NBad_chan = size(bad_chan,2);
0081 end
0082
0083
0084
0085
0086
0087