Home > vbmeg > external > mne > mne_read_cov.m

mne_read_cov

PURPOSE ^

SYNOPSIS ^

function [cov] = mne_read_cov(fid,node,cov_kind)

DESCRIPTION ^

 [cov] = mne_read_cov(fid,node,kind)

 Reads a covariance matrix from a fiff file

 fid       - an open file descriptor
 node      - look for the matrix in here
 cov_kind  - what kind of a covariance matrix do we want?

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [cov] = mne_read_cov(fid,node,cov_kind)
0002 %
0003 % [cov] = mne_read_cov(fid,node,kind)
0004 %
0005 % Reads a covariance matrix from a fiff file
0006 %
0007 % fid       - an open file descriptor
0008 % node      - look for the matrix in here
0009 % cov_kind  - what kind of a covariance matrix do we want?
0010 %
0011 
0012 %
0013 %
0014 %   Author : Matti Hamalainen, MGH Martinos Center
0015 %   License : BSD 3-clause
0016 %
0017 %   Revision 1.6  2008/09/26 20:49:26  msh
0018 %   Enabled reading of a sparse covariance matrix
0019 %
0020 %   Revision 1.5  2007/01/29 21:21:22  msh
0021 %   Added reading of the additional source prior covariances.
0022 %
0023 %   Revision 1.4  2006/05/03 18:53:06  msh
0024 %   Approaching Matlab 6.5 backward compatibility
0025 %
0026 %   Revision 1.3  2006/04/29 12:44:10  msh
0027 %   Added covariance matrix writing routines.
0028 %
0029 %   Revision 1.2  2006/04/23 15:29:40  msh
0030 %   Added MGH to the copyright
0031 %
0032 %   Revision 1.1  2006/04/20 21:50:04  msh
0033 %   Added mne_read_cov.m
0034 %
0035 %
0036 
0037 me='MNE:mne_read_cov';
0038 
0039 global FIFF;
0040 if isempty(FIFF)
0041     FIFF = fiff_define_constants();
0042 end
0043 %
0044 %   Find all covariance matrices
0045 %
0046 covs = fiff_dir_tree_find(node,FIFF.FIFFB_MNE_COV);
0047 if isempty(covs)
0048     error(me,'No covariance matrices found');
0049 end
0050 %
0051 %   Is any of the covariance matrices a noise covariance
0052 %
0053 for p = 1:length(covs)
0054     tag = find_tag(covs(p),FIFF.FIFF_MNE_COV_KIND);
0055     if ~isempty(tag) && tag.data == cov_kind
0056         this = covs(p);
0057         %
0058         %   Find all the necessary data
0059         %
0060         tag = find_tag(this,FIFF.FIFF_MNE_COV_DIM);
0061         if isempty(tag)
0062             error(me,'Covariance matrix dimension not found');
0063         end
0064         dim = tag.data;
0065         tag = find_tag(this,FIFF.FIFF_MNE_COV_NFREE);
0066         if isempty(tag)
0067             nfree = -1;
0068         else
0069             nfree = tag.data;
0070         end
0071         tag = find_tag(this,FIFF.FIFF_MNE_ROW_NAMES);
0072         if isempty(tag)
0073             names = [];
0074         else
0075             names = fiff_split_name_list(tag.data);
0076             if size(names,2) ~= dim
0077                 error(me,'Number of names does not match covariance matrix dimension');
0078             end
0079         end
0080         tag = find_tag(this,FIFF.FIFF_MNE_COV);
0081         if isempty(tag)
0082             tag = find_tag(this,FIFF.FIFF_MNE_COV_DIAG);
0083             if isempty(tag)
0084                 error(me,'No covariance matrix data found');
0085             else
0086                 %
0087                 %   Diagonal is stored
0088                 %
0089                 data = tag.data;
0090                 diagmat = true;
0091                 fprintf('\t%d x %d diagonal covariance (kind = %d) found.\n',dim,dim,cov_kind);
0092             end
0093         else
0094             if ~issparse(tag.data)
0095                 %
0096                 %   Lower diagonal is stored
0097                 %
0098                 vals = tag.data;
0099                 data = zeros(dim,dim);
0100                 % XXX : should remove for loops
0101                 q = 1;
0102                 for j = 1:dim
0103                     for k = 1:j
0104                         data(j,k) = vals(q);
0105                         q = q + 1;
0106                     end
0107                 end
0108                 for j = 1:dim
0109                     for k = j+1:dim
0110                         data(j,k) = data(k,j);
0111                     end
0112                 end
0113                 diagmat = false;
0114                 fprintf('\t%d x %d full covariance (kind = %d) found.\n',dim,dim,cov_kind);
0115             else
0116                 diagmat = false;
0117                 data = tag.data;
0118                 fprintf('\t%d x %d sparse covariance (kind = %d) found.\n',dim,dim,cov_kind);
0119             end
0120         end
0121         %
0122         %   Read the possibly precomputed decomposition
0123         %
0124         tag1 = find_tag(this,FIFF.FIFF_MNE_COV_EIGENVALUES);
0125         tag2 = find_tag(this,FIFF.FIFF_MNE_COV_EIGENVECTORS);
0126         if ~isempty(tag1) && ~isempty(tag2)
0127             eig    = tag1.data;
0128             eigvec = tag2.data;
0129         else
0130             eig    = [];
0131             eigvec = [];
0132         end
0133         %
0134         %   Read the projection operator
0135         %
0136         projs = fiff_read_proj(fid,this);
0137         %
0138         %   Read the bad channel list
0139         %
0140         bads = fiff_read_bad_channels(fid,this);
0141         %
0142         %   Put it together
0143         %
0144         cov.kind   = cov_kind;
0145         cov.diag   = diagmat;
0146         cov.dim    = dim;
0147         cov.names  = names;
0148         cov.data   = data;
0149         cov.projs  = projs;
0150         cov.bads   = bads;
0151         cov.nfree  = nfree;
0152         cov.eig    = eig;
0153         cov.eigvec = eigvec;
0154         %
0155         return;
0156     end
0157 end
0158 
0159 error(me,'Did not find the desired covariance matrix');
0160 
0161 return;
0162 
0163     function [tag] = find_tag(node,findkind)
0164         
0165         for pp = 1:node.nent
0166             kind = node.dir(pp).kind;
0167             pos  = node.dir(pp).pos;
0168             if kind == findkind
0169                 tag = fiff_read_tag(fid,pos);
0170                 return;
0171             end
0172         end
0173         tag = [];
0174         return
0175     end
0176 
0177 end

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