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