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