0001 function status = run_eeg_csdm(rawfname,add_bad_chan,windows,reference,max_freq,Epoch,Max_microv,Min_Chan,LoPass,HiPass);
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 if nargin < 1
0031 error('filename not specified');
0032 end;
0033 if isempty(rawfname)
0034 [fid, rawfname] = get_fid('rb');
0035 if (fid < 0)
0036 error('error opening data file')
0037 fclose('all')
0038 end;
0039 fclose(fid);
0040 fid = fopen(rawfname,'rb','b');
0041 else
0042 fid = fopen(rawfname,'rb','b');
0043 if (fid < 0)
0044 error('error opening data file')
0045 fclose('all')
0046 end;
0047 end;
0048
0049 if ~(nargin ==1 | nargin ==2|nargin == 3| nargin ==4|nargin == 5|nargin == 9|nargin == 10|nargin == 8)
0050 error('inproper number of input arguments')
0051 end;
0052 if nargin < 8
0053 [Epoch,NEpoch,Max_microv,Min_Chan,Nbad_chan,bad_chan,mmask] = read_mask(rawfname);
0054 end;
0055 if nargin == 1
0056 add_bad_chan = [];
0057 windows = [1:size(mmask,1)];
0058 reference = ['average'];
0059 max_freq = 50;
0060 end;
0061 if nargin == 2
0062 windows = [1:size(mmask,1)];
0063 reference = ['average'];
0064 max_freq = 50;
0065 end;
0066 if nargin == 3
0067 reference = ['average'];
0068 max_freq = 50;
0069 end;
0070 if nargin == 4
0071 max_freq = 50;
0072 end;
0073
0074 if (isempty(windows) & nargin < 8)
0075 windows = [1:size(mmask,1)];
0076 end;
0077 if isempty(reference)
0078 reference = 'average';
0079 end;
0080 if isempty(max_freq)
0081 max_freq = 50;
0082 end
0083
0084 if nargin == 10
0085 [header_array, EventCodes,Samp_Rate, NChan, scale, NSamp, NEvenp] = rd_fragger_hdr(fid);
0086 [Epoch, NEpoch,Max_microv,Min_Chan,NBad_chan,bad_chan,mmask] = ns_artifact_edit(fid,Samp_Rate,NChan, NSamp, NEvent,scale,Epoch,Max_microv,Min_Chan,Lo_Pass,Hi_Pass);
0087 end
0088 if nargin == 9
0089 [header_array, EventCodes,Samp_Rate, NChan, scale, NSamp, NEvent] = rd_fragger_hdr(fid);
0090 [Epoch, NEpoch,Max_microv,Min_Chan,NBad_chan,bad_chan,mmask] = ns_artifact_edit(fid,Samp_Rate,NChan, NSamp, NEvent,scale,Epoch,Max_microv,Min_Chan,Lo_Pass);
0091 end;
0092 if nargin == 8
0093 [header_array, EventCodes,Samp_Rate, NChan, scale, NSamp, NEvent] = rd_fragger_hdr(fid);
0094 [Epoch, NEpoch,Max_microv,Min_Chan,NBad_chan,bad_chan,mmask] = ns_artifact_edit(fid,Samp_Rate,NChan, NSamp, NEvent,scale,Epoch,Max_microv,Min_Chan);
0095 end;
0096 fclose(fid);
0097 if isempty(windows)
0098 windows = [1:size(mmask,2)];
0099 end;
0100 for i = 1:size(windows,1)
0101 if size(windows,1) > 1
0102 if i < 10
0103 text_lab = ['.' reference(1,1:4) '.w.0'];
0104 else
0105 text_lab = ['.' reference(1,1:4) '.w.'];
0106 end;
0107 csdmfname = [rawfname(1:size(rawfname,2)-8) '.csdm.' text_lab int2str(i)];
0108 else
0109 csdmfname = [rawfname(1:size(rawfname,2)-8) '.' reference(1,1:4) '.csdm'];
0110 end;
0111 csdmfid = fopen(csdmfname,'wb');
0112 if nargin >= 8
0113 [Epoch,NEpoch_used,Nbad_chan,bad_chan,avgcsdm] = raw_eeg_csdm(rawfname,add_bad_chan,windows(i,:),reference,Epoch,NEpoch,Nbad_chan,bad_chan,mmask);
0114 else
0115 [Epoch,NEpoch_used,Nbad_chan,bad_chan,avgcsdm] = raw_eeg_csdm(rawfname,add_bad_chan,windows(i,:),reference);
0116 end;
0117 version = -1;
0118 fwrite(csdmfid,version,'integer*2');
0119 fwrite(csdmfid,Epoch,'integer*2');
0120 fwrite(csdmfid,size(windows,2)*Epoch,'integer*2')
0121 fwrite(csdmfid,NEpoch_used,'integer*2');
0122 fwrite(csdmfid,Nbad_chan,'integer*2');
0123 fwrite(csdmfid,bad_chan,'integer*2');
0124 if strcmp(reference,'average')
0125 ref_flag = 1;
0126 elseif strcmp(reference,'avgmast')
0127 ref_flag = 2;
0128 elseif strcmp(reference,'perimeter');
0129 ref_flag = 3;
0130 elseif strcmp(reference,'vertex');
0131 ref_flag = 4;
0132 elseif strcmp(reference,'laplacian');
0133 ref_flag = 5;
0134 elseif strcmp(reference(1:8),'cortical');
0135 ref_flag = 7;
0136 else
0137 ref_flag = 6;
0138 end;
0139 fwrite(csdmfid,ref_flag,'integer*2');
0140 if ref_flag == 6
0141 fwrite(csdmfid, size(reference,2),'integer*2');
0142 fwrite(csdmfid, reference,'integer*2');
0143 end;
0144 if ref_flag == 7
0145 sigma_v = str2num(reference(9:10));
0146 fwrite(csdmfid,sigma_v,'integer*2');
0147 end;
0148 max_freq = max_freq*Epoch+1;
0149 fwrite(csdmfid,max_freq,'integer*2');
0150 fwrite(csdmfid,size(avgcsdm,2),'integer*2');
0151 fwrite(csdmfid,real(avgcsdm(1:max_freq,:))','real*4');
0152 fwrite(csdmfid,imag(avgcsdm(1:max_freq,:))','real*4');
0153 fclose(csdmfid);
0154 end;
0155 status = 1;
0156 fclose('all');
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172