function [f1,f2] = extract_formants(A)
%function [f1,f2] = extract_formants(A)
%
% EXTRACT_FORMANTS -- Accepts a vector of AR coefficients (A) and
%                     produces estimates of the first two formant
%                     locations
%
% Input Parameters:
%  A    -- a vector of AR coefficients
%
% Output Parameters:
% f1    -- estimate of the first formant frequency
% f2    -- estimate of the second formant frequency
%

[H,f] = freqz(1,A,1024,8192);

H = 20*log10(abs(H));

dif = sign(diff(H));
inds = find(dif(2:end) < dif(1:end-1)) + 1;

if length(inds) > 1
    plot(f,H);
    hold on;
    stem(f(inds(1:2)),H(inds(1:2)));
    hold off;

    f1 = f(inds(1));
    f2 = f(inds(2));
else
    plot(f,H);
    f1 = f(inds(1));
    f2 = f1;
end