minimax optimization -凯发k8网页登录
this example shows how to solve a nonlinear filter design problem using a minimax optimization algorithm, fminimax
, in optimization toolbox™. note that to run this example you must have the signal processing toolbox™ installed.
set finite precision parameters
consider an example for the design of finite precision filters. for this, you need to specify not only the filter design parameters such as the cut-off frequency and number of coefficients, but also how many bits are available since the design is in finite precision.
nbits = 8; % how many bits have we to realize filter maxbin = 2^nbits-1; % maximum number expressable in nbits bits n = 4; % number of coefficients (order of filter plus 1) wn = 0.2; % cutoff frequency for filter rp = 1.5; % decibels of ripple in the passband w = 128; % number of frequency points to take
continuous design first
this is a continuous filter design; we use cheby1
, but we could also use ellip
, yulewalk
or remez
here:
[b1,a1] = cheby1(n-1,rp,wn); [h,w] = freqz(b1,a1,w); % frequency response h = abs(h); % magnitude response plot(w, h) title('frequency response using non-integer variables')
x = [b1,a1]; % the design variables
set bounds for filter coefficients
we now set bounds on the maximum and minimum values:
if (any(x < 0)) % if there are negative coefficients - must save room to use a sign bit % and therefore reduce maxbin maxbin = floor(maxbin/2); vlb = -maxbin * ones(1, 2*n)-1; vub = maxbin * ones(1, 2*n); else % otherwise, all positive vlb = zeros(1,2*n); vub = maxbin * ones(1, 2*n); end
scale coefficients
set the biggest value equal to maxbin and scale other filter coefficients appropriately.
[m, mix] = max(abs(x)); factor = maxbin/m; x = factor * x; % rescale other filter coefficients xorig = x; xmask = 1:2*n; % remove the biggest value and the element that controls d.c. gain % from the list of values that can be changed. xmask(mix) = []; nx = 2*n;
set optimization criteria
using optimoptions
, adjust the termination criteria to reasonably high values to promote short running times. also turn on the display of results at each iteration:
options = optimoptions('fminimax', ... 'steptolerance', 0.1, ... 'optimalitytolerance', 1e-4,... 'constrainttolerance', 1e-6, ... 'display', 'iter');
minimize the absolute maximum values
we need to minimize absolute maximum values, so we set options.minabsmax to the number of frequency points:
if length(w) == 1 options = optimoptions(options,'absolutemaxobjectivecount',w); else options = optimoptions(options,'absolutemaxobjectivecount',length(w)); end
eliminate first value for optimization
discretize and eliminate first value and perform optimization by calling fminimax:
[x, xmask] = elimone(x, xmask, h, w, n, maxbin)
x = 1×8
0.5441 1.6323 1.6323 0.5441 57.1653 -127.0000 108.0000 -33.8267
xmask = 1×6
1 2 3 4 5 8
niters = length(xmask);
disp(sprintf('performing %g stages of optimization.\n\n', niters));
performing 6 stages of optimization.
for m = 1:niters fun = @(xfree)filtobj(xfree,x,xmask,n,h,maxbin); % objective confun = @(xfree)filtcon(xfree,x,xmask,n,h,maxbin); % nonlinear constraint disp(sprintf('stage: %g \n', m)); x(xmask) = fminimax(fun,x(xmask),[],[],[],[],vlb(xmask),vub(xmask),... confun,options); [x, xmask] = elimone(x, xmask, h, w, n, maxbin); end
stage: 1
objective max line search directional iter f-count value constraint steplength derivative procedure 0 8 0 0.00329174 1 17 0.0001845 3.34e-07 1 0.0143 local minimum possible. constraints satisfied. fminimax stopped because the size of the current search direction is less than twice the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
stage: 2
objective max line search directional iter f-count value constraint steplength derivative procedure 0 7 0 0.0414182 1 15 0.01649 0.0002558 1 0.261 2 23 0.01544 6.123e-07 1 -0.0282 hessian modified local minimum possible. constraints satisfied. fminimax stopped because the size of the current search direction is less than twice the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
stage: 3
objective max line search directional iter f-count value constraint steplength derivative procedure 0 6 0 0.0716961 1 13 0.05943 2.274e-10 1 0.776 local minimum possible. constraints satisfied. fminimax stopped because the size of the current search direction is less than twice the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
stage: 4
objective max line search directional iter f-count value constraint steplength derivative procedure 0 5 0 0.129938 1 11 0.04278 -4.575e-10 1 0.183 local minimum possible. constraints satisfied. fminimax stopped because the size of the current search direction is less than twice the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
stage: 5
objective max line search directional iter f-count value constraint steplength derivative procedure 0 4 0 0.0901749 1 9 0.03867 2.097e-12 1 0.256 local minimum possible. constraints satisfied. fminimax stopped because the size of the current search direction is less than twice the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
stage: 6
objective max line search directional iter f-count value constraint steplength derivative procedure 0 3 0 0.11283 1 7 0.05033 3.192e-16 1 0.197 local minimum possible. constraints satisfied. fminimax stopped because the size of the current search direction is less than twice the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
check nearest integer values
see if nearby values produce a better filter.
xold = x; xmask = 1:2*n; xmask([n 1, mix]) = []; x = x 0.5; for i = xmask [x, xmask] = elimone(x, xmask, h, w, n, maxbin); end xmask = 1:2*n; xmask([n 1, mix]) = []; x = x - 0.5; for i = xmask [x, xmask] = elimone(x, xmask, h, w, n, maxbin); end if any(abs(x) > maxbin) x = xold; end
frequency response comparisons
we first plot the frequency response of the filter and we compare it to a filter where the coefficients are just rounded up or down:
subplot(211) bo = x(1:n); ao = x(n 1:2*n); h2 = abs(freqz(bo,ao,128)); plot(w,h,w,h2,'o') title('optimized filter versus original') xround = round(xorig)
xround = 1×8
1 2 2 1 57 -127 108 -34
b = xround(1:n); a = xround(n 1:2*n); h3 = abs(freqz(b,a,128)); subplot(212) plot(w,h,w,h3,' ') title('rounded filter versus original')
fig = gcf;
fig.nextplot = 'replace';