function [c,a]=cepcoeff(sg,P,Q)

%%  c = cepcoeff(sg,P,Q)
%%  
%%  c -- LPC-derived cepstral coefficients
%%  sg-- signal segment
%%  P -- order of LPC model
%%  Q -- number of coefficients
%%

%% LPC analysis
a = lpc(sg,P);

%% LPC-derived cepstral coefficients
c=zeros(Q,1);
c(1)=log(std(sg)^2);
c(2)=-a(2);

for n=3:Q
  temp_sum=0;
  if (n<=P+1)
     for m=1:(n-1)-1
      temp_sum=temp_sum+(1-m/(n-1))*a(m+1)*c(n-m);
     end
     c(n) = -a(n)-temp_sum;
  else
     for m=1:P
      temp_sum=temp_sum+(1-m/(n-1))*a(m+1)*c(n-m);
     end
     c(n) = -temp_sum;
  end
end

%% Cepstral Weighting
%%w=1+(Q/2).*sin((pi/Q).*(1:Q));
%%c=c.*w';
a=a';
return