comparison m-toolbox/classes/+utils/@bin/fil.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 % FIL a wrapper for the LISO fil executable.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: FIL a wrapper for the LISO fil executable.
5 %
6 % CALL: utils.bin.fil('foo.fil')
7 % utils.bin.fil('foo.fil', 'foo2.fil', 'foo3.fil', ...)
8 % utils.bin.fil('foo.fil', 'foo2.fil', 'foo3.fil', '-c', ...)
9 %
10 % >> a = utils.bin.fil('foo.fil') % get result as AO
11 % >> f = utils.bin.fil('foo_iir.fil') % get result as MIIR
12 %
13 % >> utils.bin.fil % get fil help
14 %
15 % INPUTS:
16 % 'filename' - one or more .fil filenames
17 % 'option' - pass options to fil
18 %
19 % VERSION: $Id: fil.m,v 1.6 2011/03/24 19:57:39 ingo Exp $
20 %
21 % HISTORY: 07-10-08 M Hewitson
22 % Creation
23 %
24 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
25
26 function varargout = fil(varargin)
27
28 % Get my location
29 p = mfilename('fullpath');
30 p = p(1:end-length(mfilename));
31
32 switch computer
33 case 'MACI'
34 LISO = fullfile(p, 'liso/maci');
35 % Point to fil binary
36 FIL = ['LISO_DIR="' LISO '" ' fullfile(LISO, 'fil')];
37 case 'PCWIN'
38 LISO = fullfile(p, 'liso/win32');
39 % Point to fil binary
40 FIL = ['LISO_DIR="' LISO '" ' fullfile(LISO, 'fil.exe')];
41 otherwise
42 error('### I do not have a binary for LISO on %s', computer)
43 end
44
45 if nargin == 0
46 system(FIL);
47 return
48 end
49
50 % Loop for any options first
51 opts = '';
52 for jj=1:nargin
53 if ischar(varargin{jj})
54 if varargin{jj}(1) == '-'
55 opts = [opts ' -n ' varargin{jj}];
56 end
57 end
58 end
59
60 % Now loop over input files
61 outs = {};
62 for jj=1:nargin
63 if ischar(varargin{jj})
64 % is this a filename or an option?
65 if varargin{jj}(1) ~= '-'
66 % Check the file
67 filename = varargin{jj};
68 [path, name, ext] = fileparts(filename);
69 if ~strcmp(ext, '.fil')
70 error('### The input filename does not appear to be a fil file. [%s]', filename);
71 end
72
73 % run FIL
74 cmd = [FIL ' ' opts ' ' filename];
75 disp(['*** running: ' cmd]);
76 [status, result] = system(cmd);
77 disp(result)
78
79 if status
80 else
81 % try to load the output file
82 outfile = fullfile('.', path, [name '.out']);
83 if exist(outfile, 'file')
84 % What type of LISO output do we get here?
85
86 min = textread(filename,'%s');
87
88 % look for keywords in file
89 tfoutput = false;
90 iir = false;
91 fit = false;
92 if any(strcmp('tfoutput', min)), tfoutput = true; end
93 if any(strcmp('iir', min)), iir = true; end
94 if any(strcmp('fit', min)), fit = true; end
95
96 if iir
97 % we have an IIR filter so we load that instead of the .out
98 % file
99 filt = miir(filename);
100 if ~isempty(filt.a)
101 if nargout == 0
102 iplot(resp(filt));
103 else
104 outs = [outs {filt}];
105 end
106 end
107 elseif fit || tfoutput
108 % load data
109 fid = fopen(outfile, 'r');
110
111 % Look for the first line of data
112 comment_char = '#';
113 while ~feof(fid)
114 f = strtrim(fgetl(fid));
115 if ~isempty(f)
116 if f(1) ~= comment_char
117 break;
118 end
119 end
120 end
121
122 % Scan it to find how many columns we have in the file
123 c = regexp(f, ' +', 'split');
124 nc = numel(c);
125 scanformat = repmat('%f ', 1, nc);
126
127 % rewind file
128 fseek(fid, 0, 'bof');
129 % Read all data
130 din = textscan(fid, scanformat, 'CommentStyle', comment_char, 'CollectOutput', 1);
131 % close file
132 fclose(fid);
133 % did we get data?
134 if ~isempty(din{1})
135 din = din{1};
136 if tfoutput
137 % what kind of columns do we have?
138 dbdeg = false;
139 reim = false;
140 absdeg = false;
141 if any(strcmp('db:deg', min)), dbdeg = true; end
142 if any(strcmp('re:im', min)), reim = true; end
143 if any(strcmp('abs:deg', min)), absdeg = true; end
144 if dbdeg
145 disp('-- TF is in db:deg');
146 amp = 10.^(din(:,2)/20) .* exp(1i*din(:,3)*pi/180);
147 elseif reim
148 disp('-- TF is in re:im');
149 amp = complex(din(:,2), din(:,3));
150 elseif absdeg
151 disp('-- TF is in abs:deg');
152 amp = din(:,2) .* exp(1i*din(:,3)*pi/180);
153 else
154 error('### unknown data format. I only understand: abs:deg, db:deg, re:im, at the moment');
155 end
156 else
157 % we need to look for nout:
158 dbdeg = false;
159 reim = false;
160 absdeg = false;
161 abs = false;
162 db = false;
163 re = false;
164 im = false;
165 if any(strcmp('nout:db:deg', min)), dbdeg = true; end
166 if any(strcmp('nout:re:im', min)), reim = true; end
167 if any(strcmp('nout:abs:deg', min)), absdeg = true; end
168 if any(strcmp('db', min)), db = true; end
169 if any(strcmp('re', min)), re = true; end
170 if any(strcmp('im', min)), im = true; end
171 if any(strcmp('abs', min)), abs = true; end
172 if dbdeg
173 amp = 10.^(din(:,2)./20) .* exp(1i*din(:,3)*pi/180);
174 disp('-- Fit is in db:deg');
175 elseif reim
176 amp = complex(din(:,2), din(:,3));
177 disp('-- Fit is in re:im');
178 elseif absdeg
179 amp = din(:,2) .* exp(1i*din(:,3)*pi/180);
180 disp('-- Fit is in abs:deg');
181 elseif abs || re || im
182 amp = din(:,2);
183 elseif db
184 amp = 10.^(din(:,2)/20);
185 else
186 error('### unknown data format. I only understand: abs:deg, db:deg, re:im, at the moment');
187 end
188
189 end
190 % build output AO
191 a = ao(din(:,1), amp);
192 a.setName(name);
193 a.setXunits('Hz');
194 outs = [outs {a}];
195 end
196 else
197 warning('### unknown output type');
198 end % End switch over output type
199 end % If output file exists
200 end % If command suceeded
201 end % If is a fil file
202 end % char argument
203 end % loop over args
204
205 if nargout == 0
206 % do nothing
207 elseif nargout == numel(outs)
208 varargout{:} = outs{:};
209 elseif nargout == 1
210 allSame = true;
211 for jj=2:numel(outs)
212 if ~strcmp(class(outs{1}), class(outs{jj}))
213 allSame = false;
214 end
215 end
216 if allSame
217 varargout{1} = [outs{:}];
218 else
219 varargout{1} = outs;
220 end
221 else
222 error('### Unknown outputs');
223 end
224
225 end
226