0001 function [data] = mne_ex_average_epochs(dataname,origname,outname)
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 me='MNE:mne_ex_average_epochs';
0030
0031 global FIFF;
0032 if isempty(FIFF)
0033 FIFF = fiff_define_constants();
0034 end
0035
0036 if nargin ~= 2 && nargin ~= 3
0037 error(me,'Incorrect number of arguments');
0038 end
0039
0040
0041
0042 descname = sprintf('%s_desc.mat',dataname);
0043 epochname = sprintf('%s.epochs',dataname);
0044
0045
0046
0047 if exist(descname,'file') ~= 2
0048 error(me,'The description file %s does not exist',descname);
0049 end
0050 if exist(epochname,'file') ~= 2
0051 error(me,'The epoch data file %s does not exist',epochname);
0052 end
0053 if exist(origname,'file') ~= 2
0054 error(me,'The original data file %s does not exist',origname);
0055 end
0056
0057
0058
0059 load(descname)
0060 fprintf(1,'Epoch info loaded\n');
0061
0062
0063
0064 try
0065 info = fiff_read_meas_info(origname);
0066 catch
0067 error(me,'%s',mne_omit_first_line(lasterr));
0068 end
0069 fprintf(1,'Measurement info loaded\n');
0070
0071
0072
0073 for k = 1:MNE_epoch_info.nchan
0074 sel{k} = deblank(MNE_epoch_info.ch_names(k,:));
0075 end
0076 info = fiff_pick_info(info,fiff_pick_channels(info.ch_names,sel));
0077
0078 info.sfreq = MNE_epoch_info.sfreq;
0079 info.lowpass = MNE_epoch_info.lowpass;
0080 info.highpass = MNE_epoch_info.highpass;
0081
0082 fprintf(1,'Averaging');
0083
0084
0085
0086 try
0087 [one,fid] = mne_read_epoch(MNE_epoch_info,1,-1);
0088 catch
0089 error(me,'Failed to read the first epoch');
0090 end
0091 ave = one;
0092 nave = 1;
0093 fprintf(1,'.');
0094
0095
0096
0097 for k = 2:MNE_epoch_info.nepoch
0098 try
0099 [one,fid] = mne_read_epoch(MNE_epoch_info,k,fid);
0100 catch
0101 error(me,'Failed to read the %dth epoch',k);
0102 end
0103
0104
0105
0106 ave = ave + one;
0107 nave = nave + 1;
0108 fprintf(1,'.');
0109 end
0110 ave = ave/nave;
0111 fprintf(1,'[done]\n');
0112
0113
0114
0115 evoked.aspect_kind = int32(FIFF.FIFFV_ASPECT_AVERAGE);
0116 evoked.nave = int32(nave);
0117
0118
0119
0120
0121 evoked.first = int32(MNE_epoch_info.epochs(1,4));
0122 evoked.last = int32(evoked.first + MNE_epoch_info.epochs(1,5) - 1);
0123 evoked.comment = 'Put your own comment here';
0124 evoked.times = double(evoked.first:1:evoked.last)/info.sfreq;
0125 evoked.epochs = ave;
0126
0127 data.info = info;
0128 data.evoked = evoked;
0129
0130
0131
0132 if nargin == 3
0133 try
0134 fiff_write_evoked(outname,data);
0135 fprintf(1,'Wrote %s\n',outname);
0136 catch
0137 error(me,'Failed to write the data');
0138 end
0139 end
0140
0141
0142
0143