1 | % File: itsave.m |
---|
2 | % Brief: Saves Matlab/Octave workspace variables to an IT++ itfile |
---|
3 | % Author: Tony Ottosson and Adam Piatyszek |
---|
4 | % |
---|
5 | % Usage: itsave("fname.it", var1, [var2], ...) |
---|
6 | % |
---|
7 | % This function saves a set of Matlab/Octave workspace variables to an IT++ |
---|
8 | % file format. Currently, only vectors and 2-D matrices can be saved. The |
---|
9 | % type of data saved is detected automatically and can be one of the |
---|
10 | % following types: bvec, bmat, ivec, imat, vec, mat, cvec, cmat. |
---|
11 | % |
---|
12 | % ------------------------------------------------------------------------- |
---|
13 | % |
---|
14 | % IT++ - C++ library of mathematical, signal processing, speech processing, |
---|
15 | % and communications classes and functions |
---|
16 | % |
---|
17 | % Copyright (C) 1995-2007 (see AUTHORS file for a list of contributors) |
---|
18 | % |
---|
19 | % This program is free software; you can redistribute it and/or modify |
---|
20 | % it under the terms of the GNU General Public License as published by |
---|
21 | % the Free Software Foundation; either version 2 of the License, or |
---|
22 | % (at your option) any later version. |
---|
23 | % |
---|
24 | % This program is distributed in the hope that it will be useful, |
---|
25 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
26 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
27 | % GNU General Public License for more details. |
---|
28 | % |
---|
29 | % You should have received a copy of the GNU General Public License |
---|
30 | % along with this program; if not, write to the Free Software |
---|
31 | % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
32 | % |
---|
33 | % ------------------------------------------------------------------------- |
---|
34 | |
---|
35 | |
---|
36 | function itsave(fname, varargin) |
---|
37 | |
---|
38 | if (nargin > 1) |
---|
39 | vars = varargin; |
---|
40 | else |
---|
41 | error('Usage: itsave("fname.it", var1, [var2], ...)'); |
---|
42 | end |
---|
43 | |
---|
44 | nargs = size(vars,2); |
---|
45 | |
---|
46 | % The current file-version of it_file |
---|
47 | file_version = 3; |
---|
48 | |
---|
49 | [fid, err_msg] = fopen(fname, 'wb', 'ieee-le'); |
---|
50 | if (fid == -1) |
---|
51 | error(err_msg); |
---|
52 | end |
---|
53 | |
---|
54 | % Write a file header consisting of "IT++" and a char containing the file |
---|
55 | % version number |
---|
56 | fprintf(fid, 'IT++%c', file_version); |
---|
57 | |
---|
58 | for ai=1:nargs |
---|
59 | if (exist('OCTAVE_VERSION')) % check for octave |
---|
60 | vname = deblank(argn(ai+1,:)); % octave way of getting parameter name |
---|
61 | is_octave=1; %used by function itsizeof to identify octave |
---|
62 | else |
---|
63 | vname = inputname(ai+1); % matlab way of getting parameter name |
---|
64 | is_octave=0; %used by function itsizeof to identify matlab |
---|
65 | end |
---|
66 | v = vars{ai}; |
---|
67 | |
---|
68 | is_scalar = all(size(v) == 1); % true if scalar (for future use) |
---|
69 | is_vector = 0;%(sum(size(v) > 1) <= 1); % true if a vector (or a scalar) |
---|
70 | is_intbin = 0;%min(min(v == round(v))); % true if integer or binary |
---|
71 | |
---|
72 | if ( isreal(v) && is_intbin ) % binary or integer type |
---|
73 | if (max(max(v)) == 1 && min(min(v)) == 0) % binary type |
---|
74 | % Calculate sizes |
---|
75 | if (is_vector) |
---|
76 | hdr_bytes = 3 * itsizeof(uint64(0),is_octave) + size(vname,2)+1 ... |
---|
77 | + itsizeof('bvec',is_octave)+1 +1; |
---|
78 | data_bytes = itsizeof(uint64(0),is_octave) + itsizeof(char(0),is_octave) * prod(size(v)); |
---|
79 | else % a matrix |
---|
80 | hdr_bytes = 3 * itsizeof(uint64(0),is_octave) + size(vname,2)+1 ... |
---|
81 | + itsizeof('bmat',is_octave)+1 +1; |
---|
82 | data_bytes = 2 * itsizeof(uint64(0),is_octave) + itsizeof(char(0),is_octave) * prod(size(v)); |
---|
83 | end |
---|
84 | block_bytes = hdr_bytes + data_bytes; |
---|
85 | |
---|
86 | % Write header sizes |
---|
87 | fwrite(fid, [hdr_bytes data_bytes block_bytes], 'uint64'); |
---|
88 | % Write variable name as string |
---|
89 | fprintf(fid, '%s%c', vname); fwrite(fid, 0, 'char'); |
---|
90 | |
---|
91 | % Write data type string, empty description string and data size |
---|
92 | if (is_vector) |
---|
93 | fprintf(fid, 'bvec'); fwrite(fid, 0, 'char'); fwrite(fid, 0, 'char'); |
---|
94 | fwrite(fid, size(v(:),1), 'uint64'); |
---|
95 | else % a matrix |
---|
96 | fprintf(fid, 'bmat'); fwrite(fid, 0, 'char'); fwrite(fid, 0, 'char'); |
---|
97 | fwrite(fid, size(v), 'uint64'); |
---|
98 | end |
---|
99 | % Write data |
---|
100 | fwrite(fid, v, 'char'); |
---|
101 | |
---|
102 | else % integer type |
---|
103 | % Calculate sizes |
---|
104 | if (is_vector) |
---|
105 | hdr_bytes = 3 * itsizeof(uint64(0),is_octave) + size(vname,2)+1 ... |
---|
106 | + itsizeof('ivec',is_octave)+1 +1; |
---|
107 | data_bytes = itsizeof(uint64(0),is_octave) + itsizeof(int32(0),is_octave) * prod(size(v)); |
---|
108 | else % a matrix |
---|
109 | hdr_bytes = 3 * itsizeof(uint64(0),is_octave) + size(vname,2)+1 ... |
---|
110 | + itsizeof('imat',is_octave)+1 +1; |
---|
111 | data_bytes = 2 * itsizeof(uint64(0),is_octave) + itsizeof(int32(0),is_octave) * prod(size(v)); |
---|
112 | end |
---|
113 | block_bytes = hdr_bytes + data_bytes; |
---|
114 | |
---|
115 | % Write header sizes |
---|
116 | fwrite(fid, [hdr_bytes data_bytes block_bytes], 'uint64'); |
---|
117 | % Write variable name as string |
---|
118 | fprintf(fid, '%s%c', vname); fwrite(fid, 0, 'char'); |
---|
119 | |
---|
120 | % Write data type string, empty description string and data size |
---|
121 | if (is_vector) |
---|
122 | fprintf(fid, 'ivec'); fwrite(fid, 0, 'char'); fwrite(fid, 0, 'char'); |
---|
123 | fwrite(fid, size(v(:),1), 'uint64'); |
---|
124 | else % a matrix |
---|
125 | fprintf(fid, 'imat'); fwrite(fid, 0, 'char'); fwrite(fid, 0, 'char'); |
---|
126 | fwrite(fid, size(v), 'uint64'); |
---|
127 | end |
---|
128 | % Write data |
---|
129 | fwrite(fid, v, 'int32'); |
---|
130 | end % binary or integer |
---|
131 | |
---|
132 | elseif (isa(v, 'double')) % double precision floating point type |
---|
133 | if (isreal(v)) % Check if real values |
---|
134 | % Calculate sizes |
---|
135 | if (is_vector) |
---|
136 | hdr_bytes = 3 * itsizeof(uint64(0),is_octave) + size(vname,2)+1 ... |
---|
137 | + itsizeof('dvec',is_octave)+1 + 1; |
---|
138 | data_bytes = itsizeof(uint64(0),is_octave) + itsizeof(double(0),is_octave) * prod(size(v)); |
---|
139 | else % a matrix |
---|
140 | hdr_bytes = 3 * itsizeof(uint64(0),is_octave) + size(vname,2)+1 ... |
---|
141 | + itsizeof('dmat',is_octave)+1 + 1; |
---|
142 | data_bytes = 2 * itsizeof(uint64(0),is_octave) + itsizeof(double(0),is_octave) ... |
---|
143 | * prod(size(v)); |
---|
144 | end |
---|
145 | block_bytes = hdr_bytes + data_bytes; |
---|
146 | |
---|
147 | % Write a header |
---|
148 | fwrite(fid, [hdr_bytes data_bytes block_bytes], 'uint64'); |
---|
149 | % Writes variable name as string |
---|
150 | fprintf(fid, '%s%c', vname); fwrite(fid, 0, 'char'); |
---|
151 | |
---|
152 | if (is_vector) |
---|
153 | fprintf(fid, 'dvec'); fwrite(fid, 0, 'char'); fwrite(fid, 0, 'char'); |
---|
154 | fwrite(fid, size(v(:),1), 'uint64'); |
---|
155 | else % a matrix |
---|
156 | fprintf(fid, 'dmat'); fwrite(fid, 0, 'char'); fwrite(fid, 0, 'char'); |
---|
157 | fwrite(fid, size(v), 'uint64'); |
---|
158 | end |
---|
159 | fwrite(fid, v, 'float64'); |
---|
160 | |
---|
161 | else % complex values |
---|
162 | % Calculate sizes |
---|
163 | if (is_vector) |
---|
164 | hdr_bytes = 3 * itsizeof(uint64(0),is_octave) + size(vname,2)+1 ... |
---|
165 | + itsizeof('dcvec',is_octave)+1 + 1; |
---|
166 | data_bytes = itsizeof(uint64(0),is_octave) + 2 * itsizeof(double(0),is_octave) ... |
---|
167 | * prod(size(v)); |
---|
168 | else % a matrix |
---|
169 | hdr_bytes = 3 * itsizeof(uint64(0),is_octave) + size(vname,2)+1 ... |
---|
170 | + itsizeof('dcmat',is_octave)+1 + 1; |
---|
171 | data_bytes = 2 * itsizeof(uint64(0),is_octave) + 2 * itsizeof(double(0),is_octave) ... |
---|
172 | * prod(size(v)); |
---|
173 | end |
---|
174 | block_bytes = hdr_bytes + data_bytes; |
---|
175 | |
---|
176 | % Writes header sizes |
---|
177 | fwrite(fid, [hdr_bytes data_bytes block_bytes], 'uint64'); |
---|
178 | % Write variable name as string |
---|
179 | fprintf(fid, '%s', vname); fwrite(fid, 0, 'char'); |
---|
180 | |
---|
181 | if (is_vector) |
---|
182 | % Write data type string, empty description string and data size |
---|
183 | fprintf(fid, 'dcvec'); fwrite(fid, 0, 'char'); fwrite(fid, 0, 'char'); |
---|
184 | fwrite(fid, size(v(:),1), 'uint64'); |
---|
185 | % Write data |
---|
186 | for i=1:size(v(:),1) |
---|
187 | fwrite(fid, real(v(i)), 'float64'); |
---|
188 | fwrite(fid, imag(v(i)), 'float64'); |
---|
189 | end |
---|
190 | else % a matrix |
---|
191 | % Write data type string, empty description string and data size |
---|
192 | fprintf(fid, 'dcmat'); fwrite(fid, 0, 'char'); fwrite(fid, 0, 'char'); |
---|
193 | fwrite(fid, size(v), 'uint64'); |
---|
194 | % Write data |
---|
195 | for j=1:size(v,2) |
---|
196 | for i=1:size(v,1) |
---|
197 | fwrite(fid, real(v(i,j)), 'float64'); |
---|
198 | fwrite(fid, imag(v(i,j)), 'float64'); |
---|
199 | end |
---|
200 | end |
---|
201 | end |
---|
202 | end % real or complex |
---|
203 | else |
---|
204 | warning(['Variable ''' vname ''' is neither a vector nor matrix. Not saved.']); |
---|
205 | end |
---|
206 | |
---|
207 | end |
---|
208 | |
---|
209 | fclose(fid); |
---|
210 | |
---|
211 | |
---|
212 | % sizeof function (like C-style sizeof) |
---|
213 | % returns no. bytes used by a variable |
---|
214 | function nbytes=itsizeof(in,is_octave) |
---|
215 | if (~is_octave) |
---|
216 | tmp=whos('in'); |
---|
217 | nbytes=tmp.bytes; |
---|
218 | |
---|
219 | % matlab uses 2 bytes by default for char |
---|
220 | % overwrite using 1 byte for file format |
---|
221 | if (ischar(in)) |
---|
222 | nbytes=size(in,2); |
---|
223 | end; |
---|
224 | else |
---|
225 | nbytes=sizeof(in); |
---|
226 | end; |
---|