function A = rc2lpc(K,varargin)
% RC2LPC Convert Reflection Cofficients to Linear Prediction Coefficients.
% A = RC2LPC(K), where K is length P vector of reflection
% coefficients, returns the vector of prediction coefficients of order P
% linear predictor.
%
% A = RC2LPC(K,'matrix'), where K is length P vector of reflection
% coefficients, returns the (P+1)-by-(P+1) size matrix. This LPC matrix
% consists of LPC vectors of order 0 through P found by using the Levinson
% recursion.
%
% Nabin Sharma
% April 02, 2009
error(nargchk(1,2,nargin,'struct'));
p = length(K); % Order of linear prediction
len = length(varargin);
switch len
case 0
a1 = K(1); % a1(1) = k1
for i = 2:p
a = a1;
for j = 1:(i-1)
a1(j) = a(j) + K(i)*a(i-j);
end
a1(i) = K(i);
end
A = [1, a1];
case 1
if strcmp(varargin,'matrix')
A = zeros(p+1,p+1); % initialize the prediction matrix
A(:,1)=1; % set all a(0) to 1
A(2,2) = K(1); % set a1(1) = k1
for i = 2:p
for j=1:(i-1)
A(i+1,j+1)=A(i,j+1)+K(i)*A(i,i-j+1);
end
A(i+1,i+1)=K(i);
end
else
error('Check the input arguments.');
end
end