-
Notifications
You must be signed in to change notification settings - Fork 2
/
fir_expand.m
408 lines (372 loc) · 9.2 KB
/
fir_expand.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
% function [h, fn, an, dn] = fir_expand(n, f, a, d, a_min, dbg)
%
% Inputs:
% n: number of taps
% f: frequency bands
% a: amplitude at band edges
% d: ripple in bands
% a_min: minimum amplitude to allow, default if empty is min(0,min(a-d));
% dbg: flag to turn on debugging statements/plots
%
% Outputs
% h: new filter
% fn: new frequency bands
% an: new amplitudes
% dn: new ripple
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Spectral-Spatial RF Pulse Design for MRI and MRSI MATLAB Package
%
% Authors: Adam B. Kerr and Peder E. Z. Larson
%
% (c)2007-2011 Board of Trustees, Leland Stanford Junior University and
% The Regents of the University of California.
% All Rights Reserved.
%
% Please see the Copyright_Information and README files included with this
% package. All works derived from this package must be properly cited.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% $Header: /home/adam/cvsroot/src/ss/fir_expand.m,v 1.5 2012/02/01 00:41:22 peder Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [hn,fn,an,dn,status] = fir_expand(n,f,a,d,a_min,dbg)
% Check input parameters
%
if nargin < 4,
error(['Usage: function [h,fn,an,dn,status] = fir_expand(n,f,a,' ...
'd,[dbg])']);
end;
if nargin < 5,
a_min = [];
end;
if nargin < 6,
dbg = 0;
end;
if (find(a(1:2:end)-a(2:2:end)))
error('Only works for flat bands now');
end;
% Confirm baseline design works
%
fn = f;
an = a;
dn = d;
[hn,status] = fir_pm(n,f,a,d,a_min,dbg);
if strcmp(status,'Failed')
% warning('Baseline design infeasible.');
return;
end;
% Now get number of transition regions, and type of filter
%
nband = length(f)/2;
if min(f) < 0,
real_filter = 0;
else
real_filter = 1;
end;
if bitget(n,1) == 1,
odd_filter = 1;
else
odd_filter = 0;
end;
% Get frequency grid
%
oversamp = 7;
if real_filter
m = oversamp * n;
fg = [0:m-1]/(m-1);
else
m = 2 * oversamp * n;
fg = [-m/2:m/2-1]/(m/2);
end;
% Keep expanding each filter band up to point filter fails
% Choose lower-amplitude band to expand if possible, else expand
% bands equally
%
fbest = f;
if real_filter,
ntran = nband+1;
else
ntran = nband;
end;
if dbg >= 2,
ftry_fig = figure;
end
for tran = 1:ntran,
if dbg >= 1,
fprintf(1, 'Optimizing transition %d of %d\n', tran, ntran);
end;
% Get indices of left/right bands
%
if real_filter
if (tran == 1)
lband = 0; % Real-filter, no band left of band 1
rband = 1;
elseif (tran == nband+1)
lband = nband;
rband = 0;
else
lband = tran-1;
rband = tran;
end;
else
lband = mod(tran-2,nband)+1;
rband = mod(tran-1,nband)+1;
end;
% Get starting fgrid indices corresponding to
% band edges.
%
if lband ~= 0,
lidx = find((fg <= f(lband*2)), 1, 'last');
end;
if rband ~= 0,
ridx = find((fg >= f(rband*2-1)), 1, 'first');
end;
% Get max number of grid points the band edges
% can move, as well as the step size for each
% band
%
if rband == 0,
nlmax = m - lidx;
nrmax = 0;
elseif lband == 0,
nlmax = 0;
nrmax = ridx - 1;
else
nlmax = mod(ridx-lidx, m);
nrmax = nlmax;
end;
ntotal = max(nlmax, nrmax);
% Find order of band edges to expand
% -- preference is to expand stopbands first, then passbands
% in order to minimize energy in filter
% Only add bands that are stopbands
%
band_order = [];
if ((lband ~= 0) && (rband~=0))
if a(lband*2) == min(a)
band_order = [0];
end;
if (a(rband*2) == min(a))
band_order = [band_order 1];
end;
elseif (lband ~= 0)
if a(lband*2) == min(a)
band_order = [band_order 0];
end;
elseif (rband ~= 0)
if (a(rband*2) == min(a))
band_order = [band_order 1];
end;
end;
if 0
band_order = [0 1]; % 0 = left, 1 = right
if ((lband ~= 0) && (rband ~= 0) && ...
(a(lband*2) > a(rband*2-1))), % swap order
band_order = [1 0];
end;
end;
% Bisection search on how many points to add to band
% edges. nlbot and nrbot are known working solutions.
%
nlbot = 0; nltop = nlmax;
nrbot = 0; nrtop = nrmax;
for working_band = band_order,
if working_band == 0, % lband
if dbg >= 2,
fprintf(1, 'Expanding left edge...\n');
end;
% Update nltop for any right edge expansion
%
if lband ~= 0
nltop = min(nltop, ntotal-nrbot);
end
while ((nltop - nlbot) > 0)
if dbg >= 2,
fprintf(1,'nlbot: %d nltop: %d -- nrbot: %d nrtop: %d -- ntotal: %d\n', ...
nlbot, nltop, nrbot, nrtop, ntotal);
end;
% Advance left band and calculate feasibility
%
nltry = ceil((nltop + nlbot)/2);
lidxtry = mod(lidx + nltry -1, m) + 1;
ftry_cont = fbest;
ftry_cont(lband*2) = fg(lidxtry);
% Create new freq spec from ftry_cont that removes
% any possible wraps (only occurs for complex filters)
%
ftry = ftry_cont;
atry = a;
dtry = d;
if ftry(1) > ftry(2),
if (ftry(1) == 1),
ftry(1) = -1;
else
ftry = [-1 ftry(2:end) ftry(1) 1];
atry = [a a(1) a(2)];
dtry = [d d(1)];
end;
elseif ftry(end-1) > ftry(end),
if (ftry(end) == -1)
ftry(end) = 1;
else
ftry = [-1 ftry(end) ftry(1:end-1) 1];
atry = [a(end) a(end) a];
dtry = [d(end) d];
end;
end;
if dbg>=2,
clf;
hold on;
nb = length(ftry)/2;
for bnd = 1:nb,
plot([ftry(bnd*2-1) ftry(bnd*2-1) ftry(bnd*2) ftry(bnd*2)], ...
log10(1+[0 (atry(bnd*2-1)+dtry(bnd)) (atry(bnd*2)+dtry(bnd)) 0]), 'r');
end;
for bnd = 1:nband,
plot([f(bnd*2-1) f(bnd*2-1) f(bnd*2) f(bnd*2)], ...
log10(1+[0 (a(bnd*2-1)+d(bnd)) (a(bnd*2)+d(bnd)) 0]), 'b');
end;
drawnow;
fprintf(1,'<Pausing>');
pause;
fprintf(1,'\r \r');
end;
if any(diff(ftry) <= 0)
status = 'Failed';
else
[h,status] = fir_pm(n,ftry,atry,dtry,a_min,dbg);
end;
if strcmp(status, 'Solved')
if dbg >= 3,
fprintf(1,'nltry: %d Solved\n',nltry);
end;
% Update nlbot
%
nlbot = nltry;
% Update current best results
%
fbest = ftry_cont;
% Update output
%
hn = h;
fn = ftry;
an = atry;
dn = dtry;
else
if dbg >= 3,
fprintf(1,'nltry: %d Failed\n',nltry);
end;
% Update nltop
%
if nltop == nlbot+1
nltop = nlbot;
else
nltop = nltry;
end;
end;
end; % while
else % working band is rband
if dbg >= 2,
fprintf(1, 'Expanding right edge...\n');
end;
% Update nrtop for any left edge expansion
%
if rband ~= 0
nrtop = min(nrtop, ntotal-nlbot);
end
while ((nrtop - nrbot) > 0)
if dbg >= 2,
fprintf(1,'nlbot: %d nltop: %d -- nrbot: %d nrtop: %d -- ntotal: %d\n', ...
nlbot, nltop, nrbot, nrtop, ntotal);
end;
% Advance right band and calculate feasibility
%
nrtry = ceil((nrtop + nrbot)/2);
ridxtry = mod(ridx - nrtry -1, m) + 1;
ftry_cont = fbest;
ftry_cont(rband*2-1) = fg(ridxtry);
% Create new freq spec from ftry_cont that removes
% any possible wraps (only occurs for complex filters)
%
ftry = ftry_cont;
atry = a;
dtry = d;
if ftry(1) > ftry(2),
if (ftry(1) == 1)
ftry(1) = -1;
else
ftry = [-1 ftry(2:end) ftry(1) 1];
atry = [a a(1) a(2)];
dtry = [d d(1)];
end;
elseif ftry(end-1) > ftry(end),
if (ftry(end) == -1)
ftry(end) = 1;
else
ftry = [-1 ftry(end) ftry(1:end-1) 1];
atry = [a(end) a(end) a];
dtry = [d(end) d];
end;
end;
if dbg>=2,
clf;
hold on;
nb = length(ftry)/2;
for bnd = 1:nb,
plot([ftry(bnd*2-1) ftry(bnd*2-1) ftry(bnd*2) ftry(bnd*2)], ...
log10(1+[0 (atry(bnd*2-1)+dtry(bnd)) (atry(bnd*2)+dtry(bnd)) 0]), 'r');
end;
for bnd = 1:nband,
plot([f(bnd*2-1) f(bnd*2-1) f(bnd*2) f(bnd*2)], ...
log10(1+[0 (a(bnd*2-1)+d(bnd)) (a(bnd*2)+d(bnd)) 0]), 'b');
end;
drawnow;
fprintf(1,'<Pausing>');
pause;
fprintf(1,'\r \r');
end;
if any(diff(ftry) <= 0)
status = 'Failed';
else
[h,status] = fir_pm(n,ftry,atry,dtry,a_min,dbg);
end;
if strcmp(status, 'Solved')
if dbg >= 3,
fprintf(1,'nrtry: %d Solved\n',nrtry);
end;
% Update nrbot
%
nrbot = nrtry;
% Update current best results
%
fbest = ftry_cont;
% Update output
%
hn = h;
fn = ftry;
an = atry;
dn = dtry;
else
if dbg >= 3,
fprintf(1,'nrtry: %d Failed\n',nrtry);
end;
% Update nrtop
%
if nrtop == nrbot+1
nrtop = nrbot;
else
nrtop = nrtry;
end;
end;
end; % while
end; % if working band
end; % for working band
if dbg >= 2,
fprintf(1,'nlbot: %d nltop: %d -- nrbot: %d nrtop: %d -- ntotal: %d\n', ...
nlbot, nltop, nrbot, nrtop, ntotal);
end;
end % for tran
status = 'Solved';