0001 function [ compdata ] = fiff_read_ctf_comp(fid,node,chs)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 global FIFF;
0044 if isempty(FIFF)
0045 FIFF = fiff_define_constants();
0046 end
0047
0048 me='MNE:fiff_read_ctf_comp';
0049
0050 if nargin ~= 3
0051 error(me,'Incorrect number of arguments');
0052 end
0053
0054 compdata = struct('ctfkind',{},'kind',{},'save_calibrated',{},'rowcals',{},'colcals',{},'data',{});
0055 comps = fiff_dir_tree_find(node,FIFF.FIFFB_MNE_CTF_COMP_DATA);
0056
0057 for k = 1:length(comps)
0058 node = comps(k);
0059
0060
0061
0062 mat = fiff_read_named_matrix(fid,node,FIFF.FIFF_MNE_CTF_COMP_DATA);
0063 for p = 1:node.nent
0064 kind = node.dir(p).kind;
0065 pos = node.dir(p).pos;
0066 if kind == FIFF.FIFF_MNE_CTF_COMP_KIND
0067 tag = fiff_read_tag(fid,pos);
0068 break;
0069 end
0070 end
0071 if ~exist('tag','var')
0072 error(me,'Compensation type not found');
0073 end
0074
0075
0076
0077 one.ctfkind = tag.data;
0078 clear('tag');
0079 one.kind = int32(-1);
0080 if one.ctfkind == hex2dec('47314252')
0081 one.kind = int32(1);
0082 elseif one.ctfkind == hex2dec('47324252')
0083 one.kind = int32(2);
0084 elseif one.ctfkind == hex2dec('47334252')
0085 one.kind = int32(3);
0086 else
0087 one.kind = one.ctfkind;
0088 end
0089 for p = 1:node.nent
0090 kind = node.dir(p).kind;
0091 pos = node.dir(p).pos;
0092 if kind == FIFF.FIFF_MNE_CTF_COMP_CALIBRATED
0093 tag = fiff_read_tag(fid,pos);
0094 break;
0095 end
0096 end
0097 if ~exist('tag','var')
0098 calibrated = false;
0099 else
0100 calibrated = tag.data;
0101 end
0102 one.save_calibrated = calibrated;
0103 one.rowcals = ones(1,size(mat.data,1));
0104 one.colcals = ones(1,size(mat.data,2));
0105 if ~calibrated
0106
0107
0108
0109
0110
0111
0112 for p = 1:length(chs)
0113 ch_names{p} = chs(p).ch_name;
0114 end
0115 for col = 1:size(mat.data,2)
0116 p = strmatch(mat.col_names{col},ch_names,'exact');
0117 if isempty(p)
0118 error(me,'Channel %s is not available in data',mat.col_names{col});
0119 elseif length(p) > 1
0120 error(me,'Ambiguous channel %s',mat.col_names{col});
0121 end
0122 col_cals(col) = 1.0/(chs(p).range*chs(p).cal);
0123 end
0124
0125
0126
0127 for row = 1:size(mat.data,1)
0128 p = strmatch(mat.row_names{row},ch_names,'exact');
0129 if isempty(p)
0130 error(me,'Channel %s is not available in data',mat.row_names{row});
0131 elseif length(p) > 1
0132 error(me,'Ambiguous channel %s',mat.row_names{row});
0133 end
0134 row_cals(row) = chs(p).range*chs(p).cal;
0135 end
0136 mat.data = diag(row_cals)*mat.data*diag(col_cals);
0137 one.rowcals = row_cals;
0138 one.colcals = col_cals;
0139 end
0140 one.data = mat;
0141 compdata(k) = one;
0142 clear('row_cals');
0143 clear('col_cals');
0144 end
0145
0146 if length(compdata) > 0
0147 fprintf(1,'\tRead %d compensation matrices\n',length(compdata));
0148 end
0149
0150 return;
0151
0152 end
0153