Home > vbmeg > external > mne > mne_ex_average_epochs.m

mne_ex_average_epochs

PURPOSE ^

SYNOPSIS ^

function [data] = mne_ex_average_epochs(dataname,origname,outname)

DESCRIPTION ^

   An example of averaging over epochs

   function mne_ex_average_epochs(dataname,origname,outname)

   dataname  - Name of a epoch data. The description file will
               be <dataname>_desc.mat and the epoch file <dataname>.epochs
   origname  - Name of the file from which the epochs were extracted.
   outname   - Name of the output file (optional)

   Returns an evoked data structure identical to the ones 
   returned from fiff_read_evoked

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [data] = mne_ex_average_epochs(dataname,origname,outname)
0002 %
0003 %   An example of averaging over epochs
0004 %
0005 %   function mne_ex_average_epochs(dataname,origname,outname)
0006 %
0007 %   dataname  - Name of a epoch data. The description file will
0008 %               be <dataname>_desc.mat and the epoch file <dataname>.epochs
0009 %   origname  - Name of the file from which the epochs were extracted.
0010 %   outname   - Name of the output file (optional)
0011 %
0012 %   Returns an evoked data structure identical to the ones
0013 %   returned from fiff_read_evoked
0014 %
0015 %
0016 
0017 %
0018 %   Author : Matti Hamalainen, MGH Martinos Center
0019 %   License : BSD 3-clause
0020 %
0021 %   Revision 1.2  2007/11/13 10:55:32  msh
0022 %   Specify the second argument to all calls to the exist function
0023 %
0024 %   Revision 1.1  2006/09/27 20:15:11  msh
0025 %   Added a new example to demonstrate averaging.
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 %   Set up file names
0041 %
0042 descname  = sprintf('%s_desc.mat',dataname);
0043 epochname = sprintf('%s.epochs',dataname);
0044 %
0045 %   Check that all the necessary files exist
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 %   Load the epoch info
0058 %
0059 load(descname)
0060 fprintf(1,'Epoch info loaded\n');
0061 %
0062 %   Read the measurement info from the original data file
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 %   Adjust the measurement info with the information from epoch description
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 %   Load the first epoch
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 %   Load all subsequent ones and average
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     %   You should design an artefact rejection routine yourself
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 %   Just put together...
0114 %
0115 evoked.aspect_kind = int32(FIFF.FIFFV_ASPECT_AVERAGE);
0116 evoked.nave        = int32(nave);
0117 %
0118 %   This makes the reasonable assumption that all epochs are
0119 %   not only of the same length but also have the same time scale
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 %   Do we want the output?
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

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005