% function: compute_lp_coeffs_cc
%
% arguments:
%
%  pred_coeffs: (output) predictor coefficients
%  lp_order: (input) order of the lp model
%  autocor_coeffs: (input) autocorrelation coefficients
%  gain: (output) gain of the lp model
%  
% return value: a logical_1 indicating status
%
% this function computes the coefficients for the lp model using the
% levinson-durbin recursion
%
function [pred_coeff, gain]=calculate_lp_coeffs(data, order);

   data=data';

   auto_coeff = autocorrelation(data,order);

   % initialize the each of the coefficients
   %
   pred_coeff = zeros(1, order+1);
   temp_pred_coeff = zeros(1, order+1);
   refl_coeff = zeros(1, order+1);
  
   % initialize the error term
   %
   lp_error = auto_coeff(1);

   % this is the core of the levinson-durbin recursion
   %
   for i = 2:order+1

      % initialize the reflection coefficient
      %
      refl_coeff(i) = 0.0;

      % update all of the reflection coefficients
      %
      for j = 2:i
         refl_coeff(i) = refl_coeff(i) - temp_pred_coeff(j)*auto_coeff(i-j+1);
      end

      % add the autocorrelation coefficient to the reflection coeff
      % and divide by the previous error term
      %
      
      refl_coeff(i) = refl_coeff(i) + auto_coeff(i);
      refl_coeff(i) = refl_coeff(i) / lp_error;

      % compute the new set of predictor coefficients
      %
      pred_coeff(i) = refl_coeff(i);

      % update the predictor coefficients
      %
      for j = 2:i
        pred_coeff(j) = pred_coeff(j) - (refl_coeff(i)*temp_pred_coeff(i-j+1));
      end

      % compute the new error term
      %
      lp_error = lp_error * (1.0 - refl_coeff(i)^2);

      % update the temporary predictor coeffs
      %
      for j = 1:i
         temp_pred_coeff(j) = pred_coeff(j);
      end
    end

    % set the gain value
    %
    gain = lp_error;
  
