function [theta,L,err,coef] = ORFsysidC(u,y,alpha)

% ORFSYSIDC = 
%    SISO System identification using orthogonal rational 
%    functions phi_k, with user defined poles and
%    with respect to a weight defined by the input.
%    The identification is based on a continuous inner product.
%
% use: [theta,L,err,coef] = ORFsysidC(u,y,alpha)
%  output:
%   theta = coefficients in expansion: G_hat = sum_k theta_k phi_k
%   L     = reflection coefficients of the ORF
%   err   = relative error of the approximation
%   coef  = Fourier coefficients of the weight
%  input:
%   u     = input signal  (column vector !)
%   y     = output signal (column vector !)
%   alpha = the poles of the ORF 
%
% See also: ORFsysidD1, ORFsysidD2, interactiveORFsysid, ORFcalcul

% Patrick Van gucht and Adhemar Bultheel, september, 11, 2000.

%initializations
if (size(u,1)<size(u,2)), % row vector --> column vector
   u=transpose(u);
end
if (size(y,1)<size(y,2)), % row vector --> column vector
   y=transpose(y);
end

N = min(length(u),length(y)); % Length of the signal
n = length(alpha);
u = u(1:N);
y = y(1:N);

hulp = conv(conj(u),flipud(u));
coef = hulp(N:2*N-1); % Fourier coefficients of the weight
omega0 = coef(1);

x(:,1)=u/sqrt(omega0);     
xstar(:,1)=u/sqrt(omega0); 
L = zeros(n,1);

alpha = [0 alpha];
k = 2;
gamma_num = [coef(2:N); 0];
gamma_den = coef;
beta = zeros(N-1,1);

for k=1:n,
   z = alpha(k+1).^[0:N-1];
   L(k) = -conj((z*gamma_num)/(z*gamma_den)); % reflectioncoefficient
   hulp = sqrt((1-abs(alpha(k+1))^2)/(1-abs(alpha(k))^2)/(1-abs(L(k))^2));
   v1 = filter([-alpha(k)' 1],[1 -alpha(k+1)],x(:,k));
   v2 = filter([1 -alpha(k)],[1 -alpha(k+1)],xstar(:,k));
   x(:,k+1) = hulp*(v1 + L(k)'*v2);
   xstar(:,k+1) = hulp*(L(k)*v1+v2);
   if (k<n),
      beta(N-1) = L(k)'*gamma_den(N) + gamma_num(N);
      for l=N-2:-1:1,
         beta(l) = L(k)'*gamma_den(l+1) + gamma_num(l+1) + alpha(k+1)*beta(l+1);
      end
      gamma_den = gamma_den + L(k)*gamma_num;
      gamma_num(1) = beta(1);
      gamma_num(2:N-1) = beta(2:N-1)-alpha(k+1)'*beta(1:N-2);
      gamma_num(N) = -alpha(k+1)'*beta(N-1);
   end
end

% IDENTIFICATION
theta = x\y;
yhat = x*theta;
err = norm(y-yhat)/norm(y);  


