0001 function [ref_trialdata, bad_chan] = grab_ns_data(seconds,reference,Max_microv,rawfname);
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 if nargin < 1
0018 error('seconds not specified');
0019 end;
0020 if (nargin < 2);
0021 reference = 'average';
0022 end;
0023 if reference == [];
0024 reference = 'average';
0025 end;
0026 if (nargin < 3);
0027 Max_microv = 100;
0028 end;
0029 if Max_microv == [];
0030 Max_microv = 100;
0031 end;
0032 if nargin < 4
0033 [fid, fname, pathname] = get_fid('rb','*.*');
0034 rawfname = [pathname fname];
0035 else
0036 fid = fopen(rawfname,'rb','ieee-be');
0037 end;
0038
0039 [header_array, EventCodes,Samp_Rate, NChan, scale, NSamp, NEvent] = rd_fragger_hdr(fid);
0040
0041 max_seconds = fix(NSamp/Samp_Rate);
0042 if max(seconds) > max_seconds
0043 fclose('all');
0044 error('seconds greater than file length');
0045 end;
0046 trialdata = zeros(NChan+NEvent,Samp_Rate*size(seconds,2));
0047 skip_bytes = Samp_Rate*(seconds(1)-1)*(NChan+NEvent)*2;
0048
0049 fseek(fid,skip_bytes,'cof');
0050 Max_Bins = (Max_microv/scale).^2;
0051 mask = ones(1,NChan+1);
0052 trialdata = fread(fid,[NChan+NEvent,Samp_Rate*size(seconds,2)],'int16');
0053 trialdata2 = zeros(NChan+1,Samp_Rate*size(seconds,2));
0054 trialdata2(1:NChan,:) = trialdata(1:NChan,:).*scale;
0055 trialdata2 = trialdata2';
0056 trialdata(1:NChan,:) = trialdata(1:NChan,:).^2;
0057 max_trial = max(trialdata(1:NChan,:)');
0058 bad_chan = find(max_trial > Max_Bins);
0059 mask(bad_chan) = zeros(1,size(bad_chan,2));
0060 if ~(strcmp(reference,'laplacian')|strcmp(reference(1:2),'co'))
0061 ref_trialdata = new_reference(trialdata2(:,1:NChan),mask,reference);
0062 elseif strcmp(reference,'laplacian')
0063 [xelec,yelec,zelec] = electrodes(NChan + 1);
0064 good_chan = find(mask);
0065
0066 ref_trialdata = laplacian_trial(trialdata2,good_chan,xelec,yelec,zelec);
0067 elseif strcmp(reference(1:8),'cortical')
0068 [xelec,yelec,zelec] = electrodes(NChan+1);
0069 sources = xyz2tp(xelec,yelec,zelec);
0070 A = transfer_matrix(500,0.2,80,8,8.2,8.7,9.2,7.8,sources,sources);
0071 good_chan = find(mask);
0072 good_trial = zeros(size(trialdata2,1),size(good_chan,2));
0073 trialdata3 = average_reference(trialdata2(:,1:NChan),mask);
0074 good_trial = trialdata3(:,good_chan);
0075 good_A = zeros(size(good_chan,2),size(good_chan,2));
0076 good_A = A(good_chan,good_chan);
0077 ref_trialdata = zeros(size(trialdata3,1),size(trialdata3,2));
0078 sigma_m = 1;
0079 sigma_v = str2num(reference(9:10));
0080 [m,deviations] = bayes_dipole_trial(good_A,good_trial',sigma_v,sigma_m);
0081 ref_trialdata(:,good_chan) = m';
0082 end;
0083 ref_trialdata = zeromean(ref_trialdata);
0084
0085