0001 function status = make_eeg_coherence(csdmfname,powerfname,obs_labels,add_bad);
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 [fid, csdmfname] = get_fid('rb');
0032 if fid < 0
0033 error('invalid filename')
0034 end;
0035 fclose(fid);
0036 end;
0037
0038 if nargin < 2
0039 [pfid,powerfname] = put_fid('rb');
0040 obs_labels = csdmfname;
0041 fclose(pfid);
0042 end;
0043
0044 if nargin < 3
0045 obs_labels = csdmfname;
0046 end;
0047 if isempty(powerfname)
0048 [pfid,powerfname] = put_fid('rb');
0049 fclose(pfid);
0050 end;
0051 version = -2;
0052 pfid = fopen(powerfname,'wb');
0053 fwrite(pfid,version,'int16');
0054 fwrite(pfid,size(csdmfname,1),'int16');
0055
0056 fwrite(pfid,size(obs_labels,2),'int16');
0057 for i = 1:size(obs_labels,1)
0058 fwrite(pfid,obs_labels(i,:),'char');
0059 end;
0060 nfiles = size(csdmfname,1);
0061
0062
0063 PEpoch = zeros(1,nfiles);
0064 PWindow_Length = zeros(1,nfiles);
0065 PNEpoch = zeros(1,nfiles);
0066 PNbad_chan = zeros(1,nfiles);
0067 Pbad_chan = zeros(nfiles,129);
0068 Pref_flag = zeros(1,nfiles);
0069 PNchan = zeros(1,nfiles);
0070 PNfreq = zeros(1,nfiles);
0071 Preference = zeros(nfiles,129);
0072
0073 for i = 1:size(csdmfname,1)
0074 fid = fopen(csdmfname(i,:),'rb');
0075 frewind(fid);
0076 version = fread(fid,1,'int16');
0077 if version ~= -1
0078 error(['file: ' csdmfname(i,:) 'is not a csdm file']);
0079 fclose(fid)
0080 end;
0081 [PEpoch(i),PWindow_Length(i),PNEpoch(i),PNbad_chan(i),bad_chan,Pref_flag(i),reference,PNchan(i),PNfreq(i)] = rd_csdm_hdr(fid);
0082
0083 if Pref_flag(i) ~= 6
0084 Preference(i,1:size(reference,2)) = reference;
0085 else
0086 Preference(i,1:size(reference,2)) = int2str(reference);
0087 end;
0088 if strcmp(reference,'laplacian')
0089 bad_chan = [bad_chan 22 26 33 39 45 57 64 70 75 83 90 96 101 115 121 1 8 14];
0090 end
0091 if nargin == 4
0092 bad_chan = [bad_chan add_bad(i,:)];
0093 end;
0094 Pbad_chan(i,1:size(bad_chan,2)) = bad_chan;
0095 PNBad_chan(i) = size(bad_chan,2);
0096 fclose(fid);
0097 end;
0098
0099 fwrite(pfid,PEpoch,'int16');
0100 fwrite(pfid,PWindow_Length,'int16')
0101 fwrite(pfid,PNEpoch,'int16');
0102 fwrite(pfid,PNbad_chan,'int16');
0103 fwrite(pfid,Pbad_chan,'int16');
0104 fwrite(pfid,Pref_flag,'int16');
0105 fwrite(pfid,Preference,'char');
0106 PNchan = (PNchan.*(PNchan+ones(1,nfiles)))/2;
0107 fwrite(pfid,PNchan,'int16');
0108 fwrite(pfid,PNfreq,'int16');
0109
0110 for i = 1:size(csdmfname,1)
0111 fid = open_file_w_byte_order(csdmfname(i,:),-1);
0112 version = fread(fid,1,'int16');
0113 if version ~= -1
0114 error(['file: ' csdmfname(i,:) 'is not a csdm file']);
0115 fclose(fid)
0116 end;
0117 [Epoch,Window_Length,NEpoch,Nbad_chan,bad_chan,reference,ravgcsdm,iavgcsdm] = rd_csdm(fid);
0118 avg_coherence = eeg_coherence(ravgcsdm,iavgcsdm,bad_chan);
0119 fwrite(pfid,avg_coherence','float');
0120 fclose(fid);
0121 end;
0122
0123 fclose(pfid)
0124 status = 1;
0125
0126
0127
0128
0129