function [coef, gain] = lpcoef(x,p)
%LPCOEF	Generates the linear prediction coefficients.
%   	[coef,gain] = lpcoef(x,p) generates the linear prediction
%   	coefficients of x using a pth order model.  
%   	The function returns two items, a vector containing the 
%   	coefficients and a scalar containing the gain.
%       The coef output is a vector of size p+1 containing
%       the coefficients 
%                           ^
%                      [1  -a_k]       
%
%   3/4/93    J. Greenberg


if ((size(x,1) > 1) & (size(x,2) > 1))
   error ('Signal must be a vector')
end
if (length(p) > 1)
   error ('Model order must be a scalar')
end

x = x(:);
xlen = length(x);

rlong = xcorr(x,x);
r = rlong(xlen+1:xlen+p);
R = toeplitz(rlong(xlen:xlen+p-1));
coeftemp = R\r;
gain = sqrt(rlong(xlen) - coeftemp' * r);
coef = [1; -coeftemp];



