0001 function fiff_write_evoked(name,data)
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
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062 me='MNE:fiff_write_evoked';
0063 if nargin ~= 2
0064 error(me,'File name and data required as an arguments');
0065 end
0066
0067 global FIFF;
0068 if isempty(FIFF)
0069 FIFF = fiff_define_constants();
0070 end
0071
0072
0073
0074 fid = fiff_start_file(name);
0075 fiff_start_block(fid,FIFF.FIFFB_MEAS);
0076 fiff_write_id(fid,FIFF.FIFF_BLOCK_ID);
0077 if ~isempty(data.info.meas_id)
0078 fiff_write_id(fid,FIFF.FIFF_PARENT_BLOCK_ID,data.info.meas_id);
0079 end
0080
0081
0082
0083 fiff_start_block(fid,FIFF.FIFFB_MEAS_INFO);
0084
0085
0086
0087 blocks = [ FIFF.FIFFB_SUBJECT FIFF.FIFFB_HPI_MEAS FIFF.FIFFB_HPI_RESULT FIFF.FIFFB_ISOTRAK FIFF.FIFFB_PROCESSING_HISTORY ];
0088 have_hpi_result = false;
0089 have_isotrak = false;
0090 if length(blocks) > 0 && isfield(data.info,'filename') && ~isempty(data.info.filename)
0091 [ fid2, tree ] = fiff_open(data.info.filename);
0092 for k = 1:length(blocks)
0093 nodes = fiff_dir_tree_find(tree,blocks(k));
0094 fiff_copy_tree(fid2,tree.id,nodes,fid);
0095 if blocks(k) == FIFF.FIFFB_HPI_RESULT && length(nodes) > 0
0096 have_hpi_result = true;
0097 end
0098 if blocks(k) == FIFF.FIFFB_ISOTRAK && length(nodes) > 0
0099 have_isotrak = true;
0100 end
0101 end
0102 fclose(fid2);
0103 end
0104
0105
0106
0107 fiff_write_float(fid,FIFF.FIFF_SFREQ,data.info.sfreq);
0108 fiff_write_float(fid,FIFF.FIFF_HIGHPASS,data.info.highpass);
0109 fiff_write_float(fid,FIFF.FIFF_LOWPASS,data.info.lowpass);
0110 fiff_write_int(fid,FIFF.FIFF_NCHAN,data.info.nchan);
0111 if [ ~isempty(data.info.meas_date) ]
0112 fiff_write_int(fid,FIFF.FIFF_MEAS_DATE,data.info.meas_date);
0113 end
0114
0115
0116
0117 if ~have_hpi_result
0118 if ~isempty(data.info.dev_head_t)
0119 fiff_write_coord_trans(fid,data.info.dev_head_t);
0120 end
0121 if ~isempty(data.info.ctf_head_t)
0122 fiff_write_coord_trans(fid,data.info.ctf_head_t);
0123 end
0124 end
0125
0126
0127
0128 for k = 1:data.info.nchan
0129
0130
0131
0132 data.info.chs(k).scanno = k;
0133 fiff_write_ch_info(fid,data.info.chs(k));
0134 end
0135
0136
0137
0138 if ~isempty(data.info.dig) && ~have_isotrak
0139 fiff_start_block(fid,FIFF.FIFFB_ISOTRAK);
0140 for k = 1:length(data.info.dig)
0141 fiff_write_dig_point(fid,data.info.dig(k))
0142 end
0143 fiff_end_block(fid,FIFF.FIFFB_ISOTRAK);
0144 end
0145
0146
0147
0148 fiff_write_proj(fid,data.info.projs);
0149
0150
0151
0152 fiff_write_ctf_comp(fid,data.info.comps);
0153
0154
0155
0156 if length(data.info.bads) > 0
0157 fiff_start_block(fid,FIFF.FIFFB_MNE_BAD_CHANNELS);
0158 fiff_write_name_list(fid,FIFF.FIFF_MNE_CH_NAME_LIST,data.info.bads);
0159 fiff_end_block(fid,FIFF.FIFFB_MNE_BAD_CHANNELS);
0160 end
0161
0162
0163 fiff_end_block(fid,FIFF.FIFFB_MEAS_INFO);
0164
0165
0166
0167 fiff_start_block(fid,FIFF.FIFFB_PROCESSED_DATA);
0168 for set = 1:length(data.evoked)
0169 fiff_start_block(fid,FIFF.FIFFB_EVOKED);
0170
0171
0172
0173 if size(data.evoked(set).comment,2) > 0
0174 fiff_write_string(fid,FIFF.FIFF_COMMENT,data.evoked(set).comment);
0175 end
0176
0177
0178
0179 fiff_write_int(fid,FIFF.FIFF_FIRST_SAMPLE,data.evoked(set).first);
0180 fiff_write_int(fid,FIFF.FIFF_LAST_SAMPLE,data.evoked(set).last);
0181
0182
0183
0184 fiff_start_block(fid,FIFF.FIFFB_ASPECT);
0185
0186 fiff_write_int(fid,FIFF.FIFF_ASPECT_KIND, ...
0187 data.evoked(set).aspect_kind);
0188 fiff_write_int(fid,FIFF.FIFF_NAVE,data.evoked(set).nave);
0189 decal = zeros(data.info.nchan,data.info.nchan);
0190 for k = 1:data.info.nchan
0191 decal(k,k) = 1.0/(data.info.chs(k).cal);
0192 end
0193 fiff_write_float_matrix(fid,FIFF.FIFF_EPOCH,decal*data.evoked(set).epochs);
0194
0195 fiff_end_block(fid,FIFF.FIFFB_ASPECT);
0196
0197 fiff_end_block(fid,FIFF.FIFFB_EVOKED);
0198 end
0199 fiff_end_block(fid,FIFF.FIFFB_PROCESSED_DATA);
0200
0201 fiff_end_block(fid,FIFF.FIFFB_MEAS);
0202
0203 fiff_end_file(fid);
0204 return;