% Filter design optimization.

% Plot design specification:
w = [0 0.1 0.15 0.5];
yspec = [1 1 0 0];
clf, h1 = plot(w, yspec, 'b');
set(h1, 'LineWidth', 3)

% Plot response with initial coefficients:
a0 = ones(15, 1);
incr = 50;
w = linspace(0, 0.5, incr);
y0 = filtmin(a0, w);
hold on, h2 = plot(w, y0, 'r:');
set(h2, 'LineWidth', 2)

% 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 = optimset('GoalsExactAchieve', length(goal));
[a, fval, attainfactor, exitflag] = ...
fgoalattain(...
@filtmin, a0, goal, weight, [], [], [], [], [], [], [], options, w0)

% Plot with the optimized (final) coefficients:
y = filtmin(a,w);
hold on, h3 = plot(w,y,'r');
set(h3, 'LineWidth', 2)
axis([0 0.5 -3 3])
title('Frequency Response')
xlabel('Frequency (Hz)')
ylabel('Magnitude (dB)')
legend('spec', 'initial', 'final')
grid on