Home > vbmeg > external > mne > fiff_write_dig_file.m

fiff_write_dig_file

PURPOSE ^

SYNOPSIS ^

function fiff_write_dig_file(filename,lpa,nas,rpa,hpi,eeg,eegref,extra)

DESCRIPTION ^

 function fiff_write_dig_file(filename,lpa,nas,rpa,hpi,eeg,eegref,extra)

 Create a fif file containing the Polhemus data

 filename      Output file name

 The following need to be specified in the Neuromag MEG
 coordinate system in meters

 lpa           Left auricular point
 nas           Nasion
 rpa           Right auricular point
 hpi           HPI coil locations               (optional)
 eeg           EEG electrode locations          (optional)
 eegref        EEG reference electrode location (optional)
 extra         Additional head surface points   (optional)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function fiff_write_dig_file(filename,lpa,nas,rpa,hpi,eeg,eegref,extra)
0002 %
0003 % function fiff_write_dig_file(filename,lpa,nas,rpa,hpi,eeg,eegref,extra)
0004 %
0005 % Create a fif file containing the Polhemus data
0006 %
0007 % filename      Output file name
0008 %
0009 % The following need to be specified in the Neuromag MEG
0010 % coordinate system in meters
0011 %
0012 % lpa           Left auricular point
0013 % nas           Nasion
0014 % rpa           Right auricular point
0015 % hpi           HPI coil locations               (optional)
0016 % eeg           EEG electrode locations          (optional)
0017 % eegref        EEG reference electrode location (optional)
0018 % extra         Additional head surface points   (optional)
0019 %
0020 
0021 %
0022 %   Author : Matti Hamalainen, MGH Martinos Center
0023 %   License : BSD 3-clause
0024 %
0025 %   Revision 1.1  2008/06/04 16:23:24  msh
0026 %   Added fiff_write_dig_file.m
0027 %
0028 %
0029 
0030 
0031 %
0032 me   = 'MNE:mne_create_dig_file';
0033 %
0034 global FIFF;
0035 if isempty(FIFF)
0036    FIFF = fiff_define_constants();
0037 end
0038 %
0039 %   Start writing...
0040 %
0041 fid  = fiff_start_file(filename);
0042 %
0043 % Make the transform to bring LPA and RPA to the x axis and nasion
0044 % to the y axis
0045 %
0046 d1 = nas - lpa;
0047 d2 = rpa - lpa;
0048 
0049 alpha = d1*d2'/(d2*d2');
0050 r0 = (1-alpha)*lpa + alpha*rpa;     % Origin is where nasion projects on the LPA - RPA line
0051 ex = d2/sqrt(d2*d2');               % x axis through the auricular points, positive from left to right
0052 ey = (nas-r0);                      % y axis points from the origin to the nasion
0053 ey = ey/sqrt(ey*ey');               % Normalize
0054 ez = cross(ex,ey);                  % Use cross product to get a right-handed coordinate system
0055 
0056 T = inv([ ex 0 ; ey 0 ; ez 0 ; r0 1 ]);
0057 % The above gives the coordinate transform from the Neuromag coordinates to the
0058 % Polhemus data coordinates; inverse is needed to go the other way
0059 T = T(:,1:3);   % We just need the first three columns because the result is not an augmented vector
0060 
0061 %
0062 % Ready to start
0063 %
0064 fiff_start_block(fid,FIFF.FIFFB_ISOTRAK);
0065 
0066 fiff_write_int(fid,FIFF.FIFF_MNE_COORD_FRAME,FIFF.FIFFV_COORD_HEAD);
0067 
0068 d.kind  = FIFF.FIFFV_POINT_CARDINAL;
0069 d.ident = FIFF.FIFFV_POINT_LPA;
0070 d.r     = [lpa 1]*T;
0071 fiff_write_dig_point(fid,d);
0072 
0073 d.kind  = FIFF.FIFFV_POINT_CARDINAL;
0074 d.ident = FIFF.FIFFV_POINT_NASION;
0075 d.r     = [nas 1]*T;
0076 fiff_write_dig_point(fid,d);
0077 
0078 d.kind  = FIFF.FIFFV_POINT_CARDINAL;
0079 d.ident = FIFF.FIFFV_POINT_RPA;
0080 d.r     = [rpa 1]*T;
0081 fiff_write_dig_point(fid,d);
0082 
0083 
0084 fprintf(1,'Wrote the fiducial locations\n');
0085 
0086 if exist('hpi','var')
0087    for k = 1:size(hpi,1)
0088       d.kind  = FIFF.FIFFV_POINT_HPI;
0089       d.ident = k;
0090       d.r     = [hpi(k,:) 1]*T;
0091       fiff_write_dig_point(fid,d);
0092    end
0093    if (size(hpi,1) > 0)
0094       fprintf(1,'Wrote %d HPI coil locations\n',size(hpi,1));
0095    end
0096 end
0097 
0098 if exist('eeg','var')
0099    if exist('eegref','var') && ~isempty(eegref)
0100       d.kind  = FIFF.FIFFV_POINT_EEG;
0101       d.ident = 0;
0102       d.r     = [eegref 1]*T;
0103       fiff_write_dig_point(fid,d);
0104       fprintf(1,'Wrote EEG reference electrode location\n');
0105    end
0106    for k = 1:size(eeg,1)
0107       d.kind  = FIFF.FIFFV_POINT_EEG;
0108       d.ident = k;
0109       d.r     = [eeg(k,:) 1]*T;
0110       fiff_write_dig_point(fid,d);
0111    end
0112    if (size(eeg,1) > 0)
0113       fprintf(1,'Wrote %d EEG electrode locations\n',size(eeg,1));
0114    end
0115 end
0116 
0117 if exist('extra','var')
0118    for k = 1:size(extra,1)
0119       d.kind  = FIFF.FIFFV_POINT_EXTRA;
0120       d.ident = k;
0121       d.r     = [extra(k,:) 1]*T;
0122       fiff_write_dig_point(fid,d);
0123    end
0124    if (size(extra,1) > 0)
0125       fprintf(1,'Wrote %d extra points\n',size(extra,1));
0126    end
0127 end
0128 
0129 fiff_end_block(fid,FIFF.FIFFB_ISOTRAK);
0130 
0131 fiff_end_file(fid);
0132

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