comparison m-toolbox/classes/@pzmodel/fromLISO.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % FROMLISO Construct a pzmodel from a LISO file
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % FUNCTION: fromLISO
5 %
6 % DESCRIPTION: Construct a pzmodel from a LISO file
7 %
8 % CALL: pzm = fromLISO(pzm, pli)
9 %
10 % PARAMETER: pzm: Empty pole/zero model
11 % pli: input plist (must contain the filename)
12 %
13 % VERSION: $Id: fromLISO.m,v 1.19 2011/08/15 12:02:46 hewitson Exp $
14 %
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
17 function pzm = fromLISO(pzm, pli)
18
19 import utils.const.*
20
21 VERSION = '$Id: fromLISO.m,v 1.19 2011/08/15 12:02:46 hewitson Exp $';
22
23 ii = pzm.getInfo(class(pzm), 'From LISO File');
24
25 % Set the method version string in the minfo object
26 ii.setMversion([VERSION '-->' ii.mversion]);
27
28 pl = applyDefaults(ii.plists, pli);
29
30 filename = find(pl, 'filename');
31
32 poles = [];
33 zeros = [];
34 gain = [];
35 plow = [];
36 pup = [];
37 zlow = [];
38 zup = [];
39 delay = 0;
40
41 % Open the file for reading
42 [fd,ms] = fopen(filename, 'r');
43 if (fd < 0)
44 error ('### can not open file: %s \n### error msg: %s',filename, ms);
45 end
46
47 utils.helper.msg(msg.OPROC2, ['reading ' filename]);
48 while ~feof(fd)
49 line = fgetl(fd);
50 % get current pointer position
51 pos = ftell(fd);
52 % get first token
53 [s,r] = strtok(line);
54 if strcmp(s, 'pole')
55 % get next token as frequency
56 [s,r] = strtok(r);
57 pf = lisoStr2Num(s);
58 % does this pole have a Q?
59 pq = NaN;
60 if ~isempty(r)
61 [s,r] = strtok(r);
62 if isnumeric(lisoStr2Num(s))
63 pq = lisoStr2Num(s);
64 end
65 end
66 % make pole
67 if isnan(pq)
68 p = pz(pf);
69 utils.helper.msg(msg.OPROC2, 'found pole: %g', pf);
70 % are there errors for this value?
71 line = fgetl(fd);
72 [s,r] = strtok(line);
73 if strcmp(s, 'param')
74 pscan = sscanf(r,'%*s %f %f');
75 pl = pz(pscan(1));
76 pu = pz(pscan(2));
77 plow = [plow pl];
78 pup = [pup pu];
79 else
80 % we did not read an error, rewind
81 fseek(fd,pos,'bof');
82 end
83 else
84 p = pz(pf, pq);
85 utils.helper.msg(msg.OPROC2, 'found pole: %g, %g', pf, pq);
86 % are there errors for this value?
87 line = fgetl(fd);
88 [s,r] = strtok(line);
89 if strcmp(s, 'param')
90 % we need to get a new line for the errors of q
91 line2 = fgetl(fd);
92 [s2,r2] = strtok(line2);
93 pf = sscanf(r,'%*s %f %f');
94 pq = sscanf(r2,'%*s %f %f');
95 pl = pz(pf(1),pq(1));
96 pu = pz(pf(2),pq(2));
97 plow = [plow pl];
98 pup = [pup pu];
99 else
100 % we did not read an error, rewind
101 fseek(fd,pos,'bof');
102 end
103 end
104 poles = [poles p];
105 elseif strcmp(s, 'zero')
106 % get next token as frequency
107 [s,r] = strtok(r);
108 zf = lisoStr2Num(s);
109 % does this zero have a Q?
110 zq = NaN;
111 if ~isempty(r)
112 [s,r] = strtok(r);
113 if isnumeric(lisoStr2Num(s))
114 zq = lisoStr2Num(s);
115 end
116 end
117 % make zero
118 if isnan(zq)
119 z = pz(zf);
120 utils.helper.msg(msg.OPROC2, 'found zero: %g', zf);
121 % are there errors for this value?
122 line = fgetl(fd);
123 [s,r] = strtok(line);
124 if strcmp(s, 'param')
125 zscan = sscanf(r,'%*s %f %f');
126 zl = pz(zscan(1));
127 zu = pz(zscan(2));
128 zlow = [zlow zl];
129 zup = [zup zu];
130 else
131 % we did not read an error, rewind
132 fseek(fd,pos,'bof');
133 end
134 else
135 z = pz(zf, zq);
136 utils.helper.msg(msg.OPROC2, 'found zero: %g, %g', zf, zq);
137 % are there errors for this value?
138 line = fgetl(fd);
139 [s,r] = strtok(line);
140 if strcmp(s, 'param')
141 % we need to get a new line for the errors of q
142 line2 = fgetl(fd);
143 [s2,r2] = strtok(line2);
144 zf = sscanf(r,'%*s %f %f');
145 zq = sscanf(r2,'%*s %f %f');
146 zl = pz(zf(1),zq(1));
147 zu = pz(zf(2),zq(2));
148 zlow = [zlow zl];
149 zup = [zup zu];
150 else
151 % we did not read an error, rewind
152 fseek(fd,pos,'bof');
153 end
154 end
155 zeros = [zeros z];
156 elseif strcmp(s, 'factor')
157 % get next token as gain
158 [s, r] = strtok(r);
159 gain = lisoStr2Num(s);
160 utils.helper.msg(msg.OPROC2, 'found factor: %g', gain);
161 % are there errors for this value?
162 line = fgetl(fd);
163 [s,r] = strtok(line);
164 if strcmp(s, 'param')
165 p = sscanf(r,'%*s %f %f');
166 gl = p(1);
167 gu = p(2);
168 else
169 % we did not read an error, rewind
170 fseek(fd,pos,'bof');
171 end
172 elseif strcmp(s, 'delay')
173 % get next token as delay
174 [s, r] = strtok(r);
175 delay = lisoStr2Num(s);
176 utils.helper.msg(msg.OPROC2, 'found delay: %g', delay);
177 % are there errors for this value?
178 line = fgetl(fd);
179 [s,r] = strtok(line);
180 if strcmp(s, 'param')
181 p = sscanf(r,'%*s %f %f');
182 dl = p(1);
183 du = p(2);
184 end
185 end
186 end
187 % Close file
188 fclose(fd);
189 % get model name
190 [path, name, ext] = fileparts(filename);
191 % set object
192
193
194 if exist('gl')
195 pl1 = copy(pl, 1);
196 pzm(1).gain = gain;
197 pzm(1).poles = poles;
198 pzm(1).zeros = zeros;
199 pzm(1).delay = delay;
200 pzm(1).name = name;
201 if c(pl1.find('name'))
202 pl1.pset('name', name);
203 end
204 % Set properties from the plist
205 pzm(1).setObjectProperties(pl1);
206 pzm(1).addHistory(ii, pl1, [], []);
207
208 pl2 = copy(pl, 1);
209 pzm(2).gain = gl;
210 pzm(2).poles = plow;
211 pzm(2).zeros = zlow;
212 pzm(2).delay = dl;
213 if isempty(pl2.find('name'))
214 pl2.pset('name', strcat(name,'_lower'));
215 end
216 % Set properties from the plist
217 pzm(2).setObjectProperties(pl2);
218 pzm(2).addHistory(ii, pl2, [], []);
219
220 pl3 = copy(pl, 1);
221 pzm(3).gain = gu;
222 pzm(3).poles = pup;
223 pzm(3).zeros = zup;
224 pzm(3).delay = du;
225 if isempty(pl3.find('name'))
226 pl3.pset('name', strcat(name,'_upper'));
227 end
228 % Set properties from the plist
229 pzm(3).setObjectProperties(pl3);
230 pzm(3).addHistory(ii, pl3, [], []);
231
232 else
233 pzm.gain = gain;
234 pzm.poles = poles;
235 pzm.zeros = zeros;
236 pzm.delay = delay;
237 if isempty(pl.find('name'))
238 pl.pset('name', name);
239 end
240 % Set properties from the plist
241 pzm.setObjectProperties(pl);
242 pzm.addHistory(ii, pl, [], []);
243 end
244
245
246
247 end
248
249 %%%%%%%%%%% A function to convert LISO number strings to doubles %%%%%%%%%%
250
251 function d = lisoStr2Num(s)
252
253 s = strrep(s, 'm', 'e-3');
254 s = strrep(s, 'u', 'e-6');
255 s = strrep(s, 'n', 'e-9');
256 s = strrep(s, 'p', 'e-12');
257 s = strrep(s, 'f', 'e-15');
258 s = strrep(s, 'k', 'e3');
259 s = strrep(s, 'M', 'e6');
260 s = strrep(s, 'G', 'e9');
261
262 d = str2double(s);
263
264 end % function d = lisoStr2Num(s)