Home > vbmeg > external > mne > mne_make_combined_event_file.m

mne_make_combined_event_file

PURPOSE ^

SYNOPSIS ^

function mne_make_combined_event_file(rawname,eventname,include,all,threshold)

DESCRIPTION ^

   mne_make_combined_event_file(rawname,eventname,include,all,threshold)

   rawname     Name of the raw data file to scan
   eventname   Name of the text format event file to output
   include     Stimulus channel names to combine

               This defaults to STI 001...STI 006

   all         If true, include all trigger line transitions in the file
               instead of the leading edges only
   threshold   Threshold for detection of transition between inactive and active states

   Create both a fif and eve format event file combining STI 001...STI 006
   This function facilitates processing of Neuromag 122 data which do not
   contain a composite trigger channel

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function mne_make_combined_event_file(rawname,eventname,include,all,threshold)
0002 %
0003 %   mne_make_combined_event_file(rawname,eventname,include,all,threshold)
0004 %
0005 %   rawname     Name of the raw data file to scan
0006 %   eventname   Name of the text format event file to output
0007 %   include     Stimulus channel names to combine
0008 %
0009 %               This defaults to STI 001...STI 006
0010 %
0011 %   all         If true, include all trigger line transitions in the file
0012 %               instead of the leading edges only
0013 %   threshold   Threshold for detection of transition between inactive and active states
0014 %
0015 %   Create both a fif and eve format event file combining STI 001...STI 006
0016 %   This function facilitates processing of Neuromag 122 data which do not
0017 %   contain a composite trigger channel
0018 %
0019 %
0020 
0021 %
0022 %   Author : Matti Hamalainen, MGH Martinos Center
0023 %   License : BSD 3-clause
0024 %
0025 %   Revision 1.4  2009/03/12 11:04:22  msh
0026 %   Included a threshold parameter
0027 %
0028 %   Revision 1.3  2009/03/09 19:24:16  msh
0029 %   Fixed errors in making the event files and allowed the text event file
0030 %   not to be specified
0031 %
0032 %   Revision 1.2  2009/01/04 14:45:35  msh
0033 %   The me string was wrong.
0034 %
0035 %   Revision 1.1  2008/10/31 13:07:04  msh
0036 %   Added mne_make_combined_event_file function
0037 %
0038 %
0039 
0040 %
0041 me='MNE:mne_make_combined_event_file';
0042 %
0043 %   Setup for reading the raw data
0044 %
0045 try
0046     raw = fiff_setup_read_raw(rawname);
0047 catch
0048     error(me,'%s',mne_omit_first_line(lasterr));
0049 end
0050 %
0051 %   Set up pick list
0052 %
0053 if nargin < 3 || isempty(include)
0054     include{1} = 'STI 001';
0055     include{2} = 'STI 002';
0056     include{3} = 'STI 003';
0057     include{4} = 'STI 004';
0058     include{5} = 'STI 005';
0059     include{6} = 'STI 006';
0060 end
0061 if nargin < 4
0062     all = false;
0063 end
0064 if nargin < 5
0065     threshold = 0.1;
0066 end
0067 want_meg   = false;
0068 want_eeg   = false;
0069 want_stim  = false;
0070 %
0071 picks = fiff_pick_types(raw.info,want_meg,want_eeg,want_stim,include,raw.info.bads);
0072 
0073 from = raw.first_samp;
0074 to   = raw.last_samp;
0075 %
0076 %   Read a data segment
0077 %   times output argument is optional
0078 %
0079 try
0080     [ data, times ] = fiff_read_raw_segment(raw,from,to,picks);
0081 catch
0082     fclose(raw.fid);
0083     error(me,'%s',mne_omit_first_line(lasterr));
0084 end
0085 fprintf(1,'Read %d samples.\n',size(data,2));
0086 %
0087 %   Remember to close the file descriptor
0088 %
0089 fclose(raw.fid);
0090 %
0091 %   Make the combined channel
0092 %
0093 samples=[from:to];
0094 comb=zeros(1,size(data,2));
0095 for j = 1:size(data,1)
0096     for k = 1:size(data,2)
0097         if data(j,k) > threshold
0098             data(j,k) = 1;
0099             comb(k) = comb(k) + 2^(j-1);
0100         end
0101     end
0102 end
0103 %
0104 %   Write the text file
0105 %
0106 if exist('eventname','var') && ~isempty(eventname)
0107     fd = fopen(eventname,'w');
0108     if fd < 0
0109         error(me,'Cannot open file %s',eventname);
0110     end
0111 else
0112     fd = -1;
0113 end
0114 
0115 p = 0;
0116 q = 0;
0117 for k = 2:size(data,2)
0118     if comb(k) ~= comb(k-1)
0119         if fd >= 0 && (all || comb(k-1) == 0)
0120             fprintf(fd,'%6d %-10.4f %3d %3d\n',samples(k)-samples(1),times(k)-times(1),comb(k-1),comb(k));
0121             q = q + 1;
0122         end
0123         p = p + 1;
0124         events(p,1:3) = [ samples(k) comb(k-1) comb(k) ];
0125     end
0126 end
0127 if fd >= 0
0128     fprintf(1,'Wrote text event file %s (%d events)\n',eventname,q);
0129     fclose(fd);
0130 end
0131 %
0132 %     Write the binary file
0133 %
0134 eventname_fif = strrep(rawname,'.fif','-eve.fif');
0135 try
0136     mne_write_events(eventname_fif,events);
0137 catch
0138     error(me,'%s',mne_omit_first_line(lasterr));
0139 end
0140 fprintf(1,'Wrote binary event file %s (%d events)\n',eventname_fif,size(events,1));
0141 
0142 return;
0143 
0144 end

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