root/library/matlab/itload.m @ 998

Revision 7, 10.6 kB (checked in by smidl, 17 years ago)

nefunkcni!!!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Rev Author Date
Line 
1% File:   itload.m
2% Brief:  Load an IT++ itfile content to Matlab/Octave workspace
3% Author: Tony Ottosson and Adam Piatyszek
4%
5% Usage: itload("fname.it")
6%
7% This functions loads all variables from an IT++ file format to the
8% Matlab/Octave workspace.
9%
10% -------------------------------------------------------------------------
11%
12% IT++ - C++ library of mathematical, signal processing, speech processing,
13%        and communications classes and functions
14%
15% Copyright (C) 1995-2007  (see AUTHORS file for a list of contributors)
16%
17% This program is free software; you can redistribute it and/or modify
18% it under the terms of the GNU General Public License as published by
19% the Free Software Foundation; either version 2 of the License, or
20% (at your option) any later version.
21%
22% This program is distributed in the hope that it will be useful,
23% but WITHOUT ANY WARRANTY; without even the implied warranty of
24% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25% GNU General Public License for more details.
26%
27% You should have received a copy of the GNU General Public License
28% along with this program; if not, write to the Free Software
29% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30%
31% -------------------------------------------------------------------------
32
33function itload(fname)
34
35[fid, err_msg] = fopen(fname, 'rb', 'ieee-le');
36if (fid == -1)
37  fname = [fname '.it'];
38  [fid, err_msg2] = fopen(fname, 'rb', 'ieee-le');
39  if (fid == -1)
40    error(err_msg);
41  end
42end
43
44% Check file size
45fseek(fid, 0, 'eof');
46file_size = ftell(fid);
47fseek(fid, 0, 'bof');
48
49% Read "IT++" magic string
50[d, n] = fread(fid, 5, 'char');
51if (n ~= 5 | d(1:4) ~= [73 84 43 43]')
52  error('Not an IT++ file!');
53end
54
55% Check the IT++ file version
56if (d(5) ~= 3)
57  error('Only IT++ file version 3 is supported by this function!');
58end
59
60while (1)
61  p = ftell(fid); % Save current file position
62  [d1, n] = fread(fid, 3, 'uint64'); % Read header, data, and total block sizes
63  name = fgetstr(fid); % Read current variable name
64  type = fgetstr(fid); % Read current variable type
65  fseek(fid, p+d1(1), 'bof'); % Skip header bytes
66
67  if (length(type) == 0) % A deleted entry -> skip it
68
69  % --- bin ---
70  elseif (strcmp(type, 'bin'))
71    [d, n] = fread(fid, 1, 'char');
72    assignin('caller', name, d);
73  % --- int8 (char) ---
74  elseif (strcmp(type, 'int8'))
75    [d, n] = fread(fid, 1, 'int8');
76    assignin('caller', name, d);
77  % --- int16 (short) ---
78  elseif (strcmp(type, 'int16'))
79    [d, n] = fread(fid, 1, 'int16');
80    assignin('caller', name, d);
81  % --- int32 (int) ---
82  elseif (strcmp(type, 'int32'))
83    [d, n] = fread(fid, 1, 'int32');
84    assignin('caller', name, d);
85  % --- float32 (float) ---
86  elseif (strcmp(type, 'float32'))
87    [d, n] = fread(fid, 1, 'float32');
88    assignin('caller', name, d);
89  % --- float64 (double) ---
90  elseif (strcmp(type, 'float64'))
91    [d, n] = fread(fid, 1, 'float64');
92    assignin('caller', name, d);
93  % --- cfloat32 (complex<float>) ---
94  elseif (strcmp(type, 'cfloat32'))
95    [d, n] = fread(fid, 2, 'float32');
96    d = complex(d(1), d(2));
97    assignin('caller', name, d);
98  % --- cfloat64 (complex<double>) ---
99  elseif (strcmp(type, 'cfloat64'))
100    [d, n] = fread(fid, 2, 'float64');
101    d = complex(d(1), d(2));
102    assignin('caller', name, d);
103
104  % --- bvec ---
105  elseif (strcmp(type, 'bvec'))
106    [size, n] = fread(fid, 1, 'uint64');
107    [d, n] = fread(fid, size, 'char');
108    assignin('caller', name, d);
109  % --- svec ---
110  elseif (strcmp(type, 'svec'))
111    [size, n] = fread(fid, 1, 'uint64');
112    [d, n] = fread(fid, size, 'int16');
113    assignin('caller', name, d);
114  % --- ivec ---
115  elseif (strcmp(type, 'ivec'))
116    [size, n] = fread(fid, 1, 'uint64');
117    [d, n] = fread(fid, size, 'int32');
118    assignin('caller', name, d);
119  % --- fvec ---
120  elseif (strcmp(type, 'fvec'))
121    [size, n] = fread(fid, 1, 'uint64');
122    [d, n] = fread(fid, size, 'float32');
123    assignin('caller', name, d);
124  % --- dvec ---
125  elseif (strcmp(type, 'dvec'))
126    [size, n] = fread(fid, 1, 'uint64');
127    [d, n] = fread(fid, size, 'float64');
128    assignin('caller', name, d);
129  % --- fcvec ---
130  elseif (strcmp(type, 'fcvec'))
131    [size, n] = fread(fid, 1, 'uint64');
132    [d, n] = fread(fid, size*2, 'float32');
133    d = complex(d(1:2:end), d(2:2:end));
134    assignin('caller', name, d);
135  % --- dcvec ---
136  elseif (strcmp(type, 'dcvec'))
137    [size, n] = fread(fid, 1, 'uint64');
138    [d, n] = fread(fid, size*2, 'float64');
139    d = complex(d(1:2:end), d(2:2:end));
140    assignin('caller', name, d);
141  % --- string ---
142  elseif (strcmp(type, 'string'))
143    [size, n] = fread(fid, 1, 'uint64');
144    [d, n] = fread(fid, size, 'char');
145    d = char(d);
146    assignin('caller', name, d);
147
148  % --- bmat ---
149  elseif (strcmp(type, 'bmat'))
150    [size, n] = fread(fid, 2, 'uint64');
151    [d, n] = fread(fid, size', 'char');
152    assignin('caller', name, d);
153  % --- smat ---
154  elseif (strcmp(type, 'smat'))
155    [size, n] = fread(fid, 2, 'uint64');
156    [d, n] = fread(fid, size', 'int16');
157    assignin('caller', name, d);
158  % --- imat ---
159  elseif (strcmp(type, 'imat'))
160    [size, n] = fread(fid, 2, 'uint64');
161    [d, n] = fread(fid, size', 'int32');
162    assignin('caller', name, d);
163  % --- fmat ---
164  elseif (strcmp(type, 'fmat'))
165    [size, n] = fread(fid, 2, 'uint64');
166    [d, n] = fread(fid, size', 'float32');
167    assignin('caller', name, d);
168  % --- dmat ---
169  elseif (strcmp(type, 'dmat'))
170    [size, n] = fread(fid, 2, 'uint64');
171    [d, n] = fread(fid, size', 'float64');
172    assignin('caller', name, d);
173  % --- fcmat ---
174  elseif (strcmp(type, 'fcmat'))
175    [size, n] = fread(fid, 2, 'uint64');
176    [d, n] = fread(fid, size(1)*size(2)*2, 'float32');
177    d = reshape(complex(d(1:2:end), d(2:2:end)), size(1), size(2));
178    assignin('caller', name, d);
179  % --- dcmat ---
180  elseif (strcmp(type, 'dcmat'))
181    [size, n] = fread(fid, 2, 'uint64');
182    [d, n] = fread(fid, size(1)*size(2)*2, 'float64');
183    d = reshape(complex(d(1:2:end), d(2:2:end)), size(1), size(2));
184    assignin('caller', name, d);
185
186  % --- bArray ---
187  elseif (strcmp(type, 'bArray'))
188    [size, n] = fread(fid, 1, 'uint64');
189    [d, n] = fread(fid, size, 'char');
190    assignin('caller', name, d);
191  % --- sArray ---
192  elseif (strcmp(type, 'sArray'))
193    [size, n] = fread(fid, 1, 'uint64');
194    [d, n] = fread(fid, size, 'int16');
195    assignin('caller', name, d);
196  % --- iArray ---
197  elseif (strcmp(type, 'iArray'))
198    [size, n] = fread(fid, 1, 'uint64');
199    [d, n] = fread(fid, size, 'int32');
200    assignin('caller', name, d);
201  % --- fArray ---
202  elseif (strcmp(type, 'fArray'))
203    [size, n] = fread(fid, 1, 'uint64');
204    [d, n] = fread(fid, size, 'float32');
205    assignin('caller', name, d);
206  % --- dArray ---
207  elseif (strcmp(type, 'dArray'))
208    [size, n] = fread(fid, 1, 'uint64');
209    [d, n] = fread(fid, size, 'float64');
210    assignin('caller', name, d);
211  % --- fcArray ---
212  elseif (strcmp(type, 'fcArray'))
213    [size, n] = fread(fid, 1, 'uint64');
214    [d, n] = fread(fid, size*2, 'float32');
215    d = complex(d(1:2:end), d(2:2:end));
216    assignin('caller', name, d);
217  % --- dcArray ---
218  elseif (strcmp(type, 'dcArray'))
219    [size, n] = fread(fid, 1, 'uint64');
220    [d, n] = fread(fid, size*2, 'float64');
221    d = complex(d(1:2:end), d(2:2:end));
222    assignin('caller', name, d);
223
224  % --- bvecArray ---
225  elseif (strcmp(type, 'bvecArray'))
226    [size, n] = fread(fid, 1, 'uint64');
227    clear d2;
228    for i=1:size;
229      [size2, n] = fread(fid, 1, 'uint64');
230      [d, n] = fread(fid, size2, 'char');
231      d2{i} = d;
232    end
233    assignin('caller', name, d2);
234  % --- svecArray ---
235  elseif (strcmp(type, 'svecArray'))
236    [size, n] = fread(fid, 1, 'uint64');
237    clear d2;
238    for i=1:size;
239      [size2, n] = fread(fid, 1, 'uint64');
240      [d, n] = fread(fid, size2, 'int16');
241      d2{i} = d;
242    end
243    assignin('caller', name, d2);
244  % --- ivecArray ---
245  elseif (strcmp(type, 'ivecArray'))
246    [size, n] = fread(fid, 1, 'uint64');
247    clear d2;
248    for i=1:size;
249      [size2, n] = fread(fid, 1, 'uint64');
250      [d, n] = fread(fid, size2, 'int32');
251      d2{i} = d;
252    end
253    assignin('caller', name, d2);
254  % --- vecArray ---
255  elseif (strcmp(type, 'vecArray'))
256    [size, n] = fread(fid, 1, 'uint64');
257    clear d2;
258    for i=1:size;
259      [size2, n] = fread(fid, 1, 'uint64');
260      [d, n] = fread(fid, size2, 'float64');
261      d2{i} = d;
262    end
263    assignin('caller', name, d2);
264  % --- cvecArray ---
265  elseif (strcmp(type, 'cvecArray'))
266    [size, n] = fread(fid, 1, 'uint64');
267    clear d2;
268    for i=1:size;
269      [size2, n] = fread(fid, 1, 'uint64');
270      [d, n] = fread(fid, size*2, 'float64');
271      d2{i} = complex(d(1:2:end), d(2:2:end));
272    end
273    assignin('caller', name, d2);
274  % --- stringArray ---
275  elseif (strcmp(type, 'stringArray'))
276    [size, n] = fread(fid, 1, 'uint64');
277    clear d2;
278    for i=1:size;
279      [size2, n] = fread(fid, 1, 'uint64');
280      [d, n] = fread(fid, size2, 'char');
281      d2{i} = char(d);
282    end
283    assignin('caller', name, d2);
284
285  % --- bmatArray ---
286  elseif (strcmp(type, 'bmatArray'))
287    [size, n] = fread(fid, 1, 'uint64');
288    clear d2;
289    for i=1:size;
290      [size2, n] = fread(fid, 2, 'uint64');
291      [d, n] = fread(fid, size2', 'char');
292      d2{i} = d;
293    end
294    assignin('caller', name, d2);
295  % --- smatArray ---
296  elseif (strcmp(type, 'smatArray'))
297    [size, n] = fread(fid, 1, 'uint64');
298    clear d2;
299    for i=1:size;
300      [size2, n] = fread(fid, 2, 'uint64');
301      [d, n] = fread(fid, size2', 'int16');
302      d2{i} = d;
303    end
304    assignin('caller', name, d2);
305  % --- imatArray ---
306  elseif (strcmp(type, 'imatArray'))
307    [size, n] = fread(fid, 1, 'uint64');
308    clear d2;
309    for i=1:size;
310      [size2, n] = fread(fid, 2, 'uint64');
311      [d, n] = fread(fid, size2', 'int32');
312      d2{i} = d;
313    end
314    assignin('caller', name, d2);
315  % --- matArray ---
316  elseif (strcmp(type, 'matArray'))
317    [size, n] = fread(fid, 1, 'uint64');
318    clear d2;
319    for i=1:size;
320      [size2, n] = fread(fid, 2, 'uint64');
321      [d, n] = fread(fid, size2', 'float64');
322      d2{i} = d;
323    end
324    assignin('caller', name, d2);
325  % --- cmatArray ---
326  elseif (strcmp(type, 'cmatArray'))
327    [size, n] = fread(fid, 1, 'uint64');
328    clear d2;
329    for i=1:size;
330      [size2, n] = fread(fid, 2, 'uint64');
331      [d, n] = fread(fid, size2(1)*size2(2)*2, 'float64');
332      d2{i} = reshape(complex(d(1:2:end), d(2:2:end)), size2(1), size2(2));
333    end
334    assignin('caller', name, d2);
335
336  % --- else ---
337  else
338    warning(['Not supported type: ' type]);
339  end
340
341  if (p + d1(3) >= file_size)
342    break;
343  else
344    fseek(fid, p+d1(3), 'bof');
345  end
346
347end
348
349fclose(fid);
350
351
352
353function str = fgetstr(fid)
354str = '';
355while (1)
356  [d, n] = fread(fid, 1, 'char');
357  if (d == 0)
358    break;
359  end
360  str = [str char(d)];
361end
Note: See TracBrowser for help on using the browser.