signal processing using fgoalattain
consider designing a linear-phase finite impulse response (fir) filter. the problem is to design a lowpass filter with magnitude one at all frequencies between 0 and 0.1 hz and magnitude zero between 0.15 and 0.5 hz.
the frequency response h(f) for such a filter is defined by
(1) |
where a(f) is the magnitude
of the frequency response. one solution is to apply a goal attainment
method to the magnitude of the frequency response. given a function
that computes the magnitude, will
attempt to vary the magnitude coefficients a(n)
until the magnitude response matches the desired response within some
tolerance. the function that computes the magnitude response is given
in filtmin.m
. this function uses a
,
the magnitude function coefficients, and w
, the
discretization of the frequency domain of interest.
to
set up a goal attainment problem, you must specify the goal
and weights
for
the problem. for frequencies between 0 and 0.1, the goal is one. for
frequencies between 0.15 and 0.5, the goal is zero. frequencies between
0.1 and 0.15 are not specified, so no goals or weights are needed
in this range.
this information is stored in the variable goal
passed
to . the length of goal
is
the same as the length returned by the function filtmin
.
so that the goals are equally satisfied, usually weight
would
be set to abs(goal)
. however, since some of the
goals are zero, the effect of using weight=abs(goal)
will
force the objectives with weight
0 to be satisfied
as hard constraints, and the objectives with weight
1
possibly to be underattained (see goal attainment method). because all the goals are close
in magnitude, using a weight
of unity for all goals
will give them equal priority. (using abs(goal)
for
the weights is more important when the magnitude of goal
differs
more significantly.) also, setting
options = optimoptions('fgoalattain','equalitygoalcount',length(goal));
specifies that each objective should be as near as possible to its goal value (neither greater nor less than).
step 1: write a file filtmin.m
function y = filtmin(a,w)
n = length(a);
y = cos(w'*(0:n-1)*2*pi)*a ;
step 2: invoke optimization routine
% plot with initial coefficients a0 = ones(15,1); incr = 50; w = linspace(0,0.5,incr); y0 = filtmin(a0,w); clf, plot(w,y0,'-.b'); drawnow; % set up the goal attainment problem w1 = linspace(0,0.1,incr) ; w2 = linspace(0.15,0.5,incr); w0 = [w1 w2]; goal = [1.0*ones(1,length(w1)) zeros(1,length(w2))]; weight = ones(size(goal)); % call fgoalattain options = optimoptions('fgoalattain','equalitygoalcount',length(goal)); [a,fval,attainfactor,exitflag]=fgoalattain(@(x)filtmin(x,w0),... a0,goal,weight,[],[],[],[],[],[],[],options); % plot with the optimized (final) coefficients y = filtmin(a,w); hold on, plot(w,y,'r') axis([0 0.5 -3 3]) xlabel('frequency (hz)') ylabel('magnitude response (db)') legend('initial', 'final') grid on
compare the magnitude response computed with the initial coefficients and the final coefficients (magnitude response with initial and final magnitude coefficients). note that you could use the (signal processing toolbox) function in signal processing toolbox™ software to design this filter.
magnitude response with initial and final magnitude coefficients