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
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,
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
filtmin.m. This function uses
the magnitude function coefficients, and
discretization of the frequency domain of interest.
set up a goal attainment problem, you must specify the
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
fgoalattain. The length of
the same as the length returned by the function
So that the goals are equally satisfied, usually
be set to
abs(goal). However, since some of the
goals are zero, the effect of using
force the objectives with
weight 0 to be satisfied
as hard constraints, and the objectives with
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
the weights is more important when the magnitude of
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).
function y = filtmin(a,w) n = length(a); y = cos(w'*(0:n-1)*2*pi)*a ;
% 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
firpm (Signal Processing Toolbox) function
in Signal Processing Toolbox™ software to design this filter.
Magnitude Response with Initial and Final Magnitude Coefficients