TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
matread.m
Go to the documentation of this file.
1 function [HDR,data,t]=matread(HDR,arg2,idxlist)
2 % MATRREAD Loads (parts of) data stored in Matlab-format
3 %
4 % [HDR,data,timeindex]=matread(HDR,block_number, [startidx, endidx])
5 % This is the recommended use for Matlab-files generated from ADICHT data
6 % Before using MATREAD, HDR=MATOPEN(filename, 'ADI', ...) must be applied.
7 %
8 % [HDR,data,timeindex]=matread(HDR,Variable_Name, [startidx, endidx])
9 % can be used for other Matlab4 files.
10 % Variable name is a string which identifies a Matlab Variable.
11 % Before using MATREAD, HDR=MATOPEN(filename, 'r', ...) must be applied.
12 %
13 % see also: EEGREAD, FREAD, EEGOPEN, EEGCLOSE
14 
15 % $Revision: 1.1 $
16 % $Id: matread.m 2205 2009-10-27 12:18:15Z schloegl $
17 % Copyright (c) 1997-2003 by Alois Schloegl
18 % a.schloegl@ieee.org
19 
20 % This library is free software; you can redistribute it and/or
21 % modify it under the terms of the GNU Library General Public
22 % License as published by the Free Software Foundation; either
23 % Version 2 of the License, or (at your option) any later version.
24 %
25 % This library is distributed in the hope that it will be useful,
26 % but WITHOUT ANY WARRANTY; without even the implied warranty of
27 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 % Library General Public License for more details.
29 %
30 % You should have received a copy of the GNU Library General Public
31 % License along with this library; if not, write to the
32 % Free Software Foundation, Inc., 59 Temple Place - Suite 330,
33 % Boston, MA 02111-1307, USA.
34 
35 CHt=[];
36 if ~ischar(arg2)
37  if strcmp(HDR.TYPE,'ADI'); %HDR.ADI.Mode,
38  BlockNo=abs(arg2);
39  if isempty(BlockNo), BlockNo=1:length(HDR.ADI.DB); end;
40  if (isinf(BlockNo) | (length(BlockNo)~=1))
41  if nargout<3,
42  fprintf(2,'Warning MATREAD: missing output argument, time information cannot be returned\n');
43  end;
44  CHd=HDR.ADI.DB(BlockNo);
45  CHt=HDR.ADI.TB(BlockNo);
46  elseif ((BlockNo<1) | BlockNo>length(HDR.ADI.DB)),
47  fprintf(2,'Warning MATREAD: tried to read block %i, but only %i blocks in file %s\n',BlockNo,length(HDR.ADI.DB),HDR.FileName);
48  CHd=[];CHt=[];
49  else
50  CHd=HDR.ADI.DB(BlockNo);
51  CHt=HDR.ADI.TB(BlockNo);
52  end;
53  else
54  CHd=arg2;
55  fprintf(2,'Warning MATREAD: Variable identified by number\n');
56  end;
57 else %if ischar(arg2)
58  CHd=find(strcmp(arg2,{HDR.Var.Name}));
59  if isempty(CHd)
60  %HDR.ErrNo=-1;
61  fprintf(2,'Warning MATREAD: Variable %s not found\n',arg2);
62  data=[];
63  return;
64  end;
65 end;
66 if length(CHd)>1
67  fprintf(2,'Warning MATREAD: Only one block/variable can be read\n');
68  CHd=CHd(1);
69 end;
70 
71 if nargin<4, end;
72 
73 if nargin<3,
74  idxlist=[1,HDR.Var(CHd).Size(2)];
75 else
76  if ~all(isfinite(idxlist)),
77  idxlist=[1,HDR.Var(CHd).Size(2)];
78  end;
79 end
80 
81 % if length(idxlist)<2,idxlist=idxlist*[1 1]; end;
82 
83 if (idxlist(1)<1) %| (idxlist(length(idxlist))>HDR.Var(CHd).Size(2))),
84  fprintf(2,'Warning MATREAD #%i: Invalid File Position %f-%f\n',CHd,[idxlist(1)-1,idxlist(length(idxlist))]);
85  idxlist(1)=1;
86  %return;
87 end;
88 if idxlist(length(idxlist))>HDR.Var(CHd).Size(2),
89  fprintf(2,'Warning MATREAD #%i: endidx exceeds block length %f-%f\n',CHd,[HDR.Var(CHd).Size(2),idxlist(length(idxlist))]);
90  idxlist=[idxlist(1),HDR.Var(CHd).Size(2)];
91  %return;
92 end;
93 if 0; %((idxlist(1)<1) | (idxlist(length(idxlist))>HDR.Var(CHd).Size(2))),
94  fprintf(2,'ERROR MATREAD #%i: Invalid File Position %f-%f\n',CHd,[idxlist(1)-1,idxlist(length(idxlist))]);
95  return;
96 end;
97 Pos = (idxlist(1)-1) * HDR.Var(CHd).Size(1) * HDR.Var(CHd).SizeOfType;
98 Len = min(idxlist(length(idxlist)), HDR.Var(CHd).Size(2)) - idxlist(1) + 1;
99 
100 fseek(HDR.FILE.FID, round(HDR.Var(CHd).Pos + Pos), -1); % round must be explicite, otherwise fseek does incorrect rounding
101 [msg,errno] = ferror(HDR.FILE.FID);
102 if errno, fprintf(2,'ERROR MATREAD: FSEEK does not work Pos=%f\n%s',HDR.Var(CHd).Pos+Pos,msg); return; end;
103 
104 count = 0;
105 
106 if ~any(CHd==HDR.ADI.DB), % for non-data blocks
107  data=repmat(nan,HDR.Var(CHd).Size(1),Len);
108  while (count<Len),
109  cc = min(2^12,Len-count);
110  % [CHd,idxlist(1),idxlist(length(idxlist)),Len,HDR.Var(CHd).Size(2)]
111  [dta,c] = readnextblock4(HDR.FILE.FID,HDR.Var(CHd),cc);
112  data(:,count+(1:size(dta,2))) = dta;
113  count = count + cc;
114  end;
115 else
116  BLOCKSIZE=64*3*25*4; % 2^12; % You can change this for optimizing on your platform
117 
118  %HDR=mat_setfilter(HDR,BlockNo);% set filters for this block
119  for k=1:max(HDR.SIE.InChanSelect),
120  if k <= HDR.NS,
121  if HDR.SIE.FILT==1;
122  [tmp,HDR.Filter.Z(:,k)] = filter(HDR.Filter.B,HDR.Filter.A,zeros(length(HDR.Filter.B),1));
123  HDR.FilterOVG.Z = HDR.Filter.Z;
124  end;
125  end;
126  end;
127 
128  if isfield(HDR,'iFs') & (HDR.iFs>0) & (HDR.iFs<inf),
129  Fs=HDR.SampleRate(find(CHd==HDR.ADI.DB))/HDR.iFs;
130  data = repmat(nan,ceil(Len*size(HDR.SIE.T,2)/size(HDR.SIE.T,1)/Fs),length(HDR.SIE.ChanSelect));
131  else
132  Fs=1;
133  data = repmat(nan,ceil(Len*size(HDR.SIE.T,2)/size(HDR.SIE.T,1)),length(HDR.SIE.ChanSelect));
134  end;
135  count=0; count2=0;
136  while (count2<Len),
137  cc = min(BLOCKSIZE,Len-count2);
138  % [Len, count2, count, Fs, (Len-count2)*Fs, cc]
139  % [CHd,idxlist(1),idxlist(length(idxlist)),Len,HDR.Var(CHd).Size(2)]
140  [dta,c] = readnextblock4(HDR.FILE.FID,HDR.Var(CHd),cc);
141  % ferror(HDR.FILE.FID)
142  % [Len,cc,count,BLOCKSIZE,HDR.FILE.Pos,ftell(HDR.FILE.FID)],
143  [nc,cc] = size(dta);
144 
145  % if any postprocessing, resample to internal Sampling rate iFs=1000Hz)
146  if Fs~=1,
147  dta=rs(dta',Fs,1)'; %**************************************************************************************1
148  end;
149 
150  if nc~=HDR.NS, % reorganizing of channels
151  dta=sparse(find(HDR.ADI.index{arg2}),1:sum(HDR.ADI.index{arg2}>0),1)*dta;
152  dta(find(~HDR.ADI.index{arg2}),:)=nan;
153  end;
154 
155  for k = sort(HDR.SIE.InChanSelect), %1:size(data,2);
156  %k=HDR.SIE.chanselect(K),
157  if k<=HDR.NS,
158  if HDR.SIE.FILT,
159  [dta(k,:),HDR.Filter.Z(:,k)]=filter(HDR.Filter.B,HDR.Filter.A,dta(k,:),HDR.Filter.Z(:,k));
160  end;
161  end;
162  end;
163 
164  if HDR.SIE.RS,
165  dta = rs(dta(HDR.SIE.ChanSelect,:)',HDR.SIE.T); %RS% ***********************************************2
166  else
167  dta = dta(HDR.SIE.ChanSelect,:)';
168  end;
169  data(count+(1:size(dta,1)),:) = dta;
170  count = count + size(dta,1);
171  count2 = count2 + cc;
172  end;
173 
174  %%%%% Overflow Detection %%%%%
175  if HDR.SIE.TH,
176  for k=1:find(~isnan(sum(HDR.SIE.THRESHOLD,2))),
177  ch = phrchan(HDR.FILE.Name);
178  tmp = (data(:,k)<HDR.SIE.THRESHOLD(1,k)) | (data(:,ch)>HDR.SIE.THRESHOLD(2,k));
179  data(tmp,k) = NaN;
180  end;
181  end;
182 
183  % load time vector
184  if nargout>2,
185  if ~isempty(CHt),
186  fseek(HDR.FILE.FID, round(HDR.Var(CHt).Pos + (idxlist(1)-1)*HDR.Var(CHt).SizeOfType),-1);
187  [t,c]=readnextblock4(HDR.FILE.FID,HDR.Var(CHt),idxlist(length(idxlist))-min(idxlist)+1);
188  if length(t)>1,
189  tmp=diff(t);
190  if any(abs(tmp-tmp(length(tmp)))>1000*eps)
191  fprintf(2,'Warning %s: Sampling is not equally spaced in %s [%i,%i]\n',mfilename,HDR.FileName,idxlist(1),idxlist(length(idxlist)));
192  end;
193  end;
194 
195  %%%%% Delay of Filtering
196  if HDR.SIE.FILT,
197  t = t' + HDR.Filter.Delay;
198  else
199  t = t';
200  end;
201 
202  % if any postprocessing, resample to internal Sampling rate iFs=1000Hz)
203  if isfield(HDR,'iFs') & (HDR.iFs>0) & (HDR.iFs<inf),
204  Fs=HDR.SampleRate(find(CHd==HDR.ADI.DB))/HDR.iFs;
205  if Fs~=1,
206  t=rs(t,Fs,1); %*********************************************************************3
207  end;
208  end;
209 
210  %%%%% Resampling of the time
211  if HDR.SIE.RS,
212  t=rs(t,HDR.SIE.T); %RS% %******************************************************************4
213  end;
214  else
215  fprintf(2,'Warning %s: timeindex is not available \n',mfilename);
216  end;
217 
218  end;
219 end;
220 
221 
222 function [data,c]=readnextblock4(fid,VarInfo,Len),
223  dt=VarInfo.Type(3);
224  if dt==0, type = 'float64';
225  elseif dt==6, type = 'uint8';
226  elseif dt==4, type = 'uint16';
227  elseif dt==3, type = 'int16';
228  elseif dt==2, type = 'int32';
229  elseif dt==1, type = 'float32';
230  else
231  fprintf(2,'Error %s: unknown data type\n',mfilename);
232  return;
233  end;
234 
235  [data,c]=fread(fid,[VarInfo.Size(1),Len],type);
236 
237  if VarInfo.Type(5);
238  %HDR.ErrNo=-1;
239  fprintf(2,'Warning %s: imaginary data not test\n',mfilename);
240 
241  [di,c]=fread(fid,[VarInfo.Size(1),Len],type);
242  data=data+i*di;
243  end;
244 
245  if VarInfo.Type(4)==1,data=char(data); end;
246 
247 
matread
function matread(in HDR, in arg2, in idxlist)
readnextblock4
function readnextblock4(in fid, in VarInfo, in Len)
rs
function rs(in y1, in T, in f2)