Diff for "GrandMean" - Meg Wiki
location: Diff for "GrandMean"
Differences between revisions 1 and 7 (spanning 6 versions)
Revision 1 as of 2009-03-16 19:55:38
Size: 1759
Editor: YaaraErez
Comment:
Revision 7 as of 2009-04-03 17:05:16
Size: 1846
Editor: YaaraErez
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Simple Matlab script for averaging of Fiff-files. It assumes that all data files have identical data parameters (e.g. after interpolation). == Averaging Fiff-files ==
Simple Matlab script for averaging of Fiff-files. It assumes that all data files have identical data parameters (e.g. after interpolation). You should also have applied identical filtering, baseline correction, referencing etc. where appropriate. This script will not care about bad channels - if a channel is bad in one file, it should be treated as bad in the grand-mean as well.
Line 3: Line 4:
-------------------------------------- ----------
Line 14: Line 15:
if exist('file_out')~=1, % if not output file specified: write to current working directory
    cwd = pwd;
    file_out = fullfile( cwd, 'GrandMean.fif' );
end;
% output file name for grand-mean data
file_out = '/fullpath/GrandMean.fif';
Line 19: Line 18:
if exist('files')~=1, % you can specify a list of files here (e.g. on-line averages for all subjects in your study)
    
files = {'/fullpath/file4subj1.fif', ...
             '/fullpath/file4subj2.fif', ...
             '/fullpath/file4subj3.fif'};
end;
% List of fiff-files to be averaged
fiff_files = {'/fullpath/file4subj1.fif', ...
  '/fullpath/file4subj2.fif', ...
              '/fullpath/file4subj3.fif'};
Line 32: Line 30:
nr_files = length(fiff_files);
Line 33: Line 32:
nr_files = length(files);
Line 35: Line 33:
for f = 1:nr_files, % read all files and average all that might be relevant
    fprintf(1, '%s\n', files{f});
    data = fiff_read_evoked(files{f});
    if f==1,
for ff = 1:nr_files, % read all files and average all that might be relevant
    fprintf(1, '%s\n', fiff_files{ff});
    data = fiff_read_evoked(fiff_files{ff});
    if ff==1,

Averaging Fiff-files

Simple Matlab script for averaging of Fiff-files. It assumes that all data files have identical data parameters (e.g. after interpolation). You should also have applied identical filtering, baseline correction, referencing etc. where appropriate. This script will not care about bad channels - if a channel is bad in one file, it should be treated as bad in the grand-mean as well.


% Compute the grand-mean for a list of Fiff-files in cell array "files{}"
% The string "file_out" specifies the file name for output
% The first file of the list will be used as a template for Fiff-output
% This obviously assumes that all files have identical parameters (e.g. are
% interpolated to a standard sensor array)
% OH, March 2009

addpath /opt/mne/matlab/toolbox/;   % fiff read/write tools

% output file name for grand-mean data
file_out = '/fullpath/GrandMean.fif';

% List of fiff-files to be averaged
fiff_files = {'/fullpath/file4subj1.fif', ...
              '/fullpath/file4subj2.fif', ...
              '/fullpath/file4subj3.fif'};

fid = fopen(file_out, 'a');
if fid==-1,
    fprintf(1, 'Cannot access output file %s\n', file_out);
    return;
end;
fclose(fid);

nr_files = length(fiff_files);

epoch_gm = [0];
for ff = 1:nr_files, % read all files and average all that might be relevant
    fprintf(1, '%s\n', fiff_files{ff});
    data = fiff_read_evoked(fiff_files{ff});
    if ff==1,
        data_templ = data;   % first data set will be used as template for output
    end;
    epoch_gm = epoch_gm + data.evoked.epochs;   % average MEG data epoch
end;
epoch_gm = epoch_gm/nr_files;

data_templ.evoked.epoch = epoch_gm; % insert averaged data into template structure

fiff_write_evoked(file_out, data_templ);  % Write average

% The end of this

CbuMeg: GrandMean (last edited 2013-03-08 10:02:26 by localhost)