function x=realCepstrum(x,N)
%%REALCEPSTRUM Compute the real cepstrum. The real cepstrum is the inverse
%              Fourier transform of the real logarithm of the magnitude of
%              the Fourier transform of a real sequence. The cepstrum is
%              often used in signal identification. The real cepstrum plays
%              a role in the design of certain filters.
%
%INPUTS: x An xDimX1 or 1XxDim real sequence.
%        N The length of the discrete Fourier transforms to use in the
%          computation of the cepstrum. If this parameter is omitted or an
%          empty matrix is passed, then the default of N=length(x) is used.
%
%OUTPUTS: x The real cepstrum of the input signal x. This will be length-N.
%
%The computation of the real cepstrum is described in [1]. However, the
%implementation here is more straighforward.
%
%REFERENCES:
%[1] T. F. Quatieri and J. M. Tribolet, "Computation of the real cepstrum
%    and minimum-phase reconstruction," in Programs for Digital Signal
%    Processing, Digital Signal Processing Committee, IEEE Acoustics,
%    Speech, and Signal Processing Society, Ed. New York: IEEE Press, 1979,
%    ch. 7.2.
%
%January 2016 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

if(nargin<2||isempty(N))
    N=length(x);
end

if(~isreal(x))
   error('The input must be real')
end

%The ifft should be real; this just deals with any possible finite
%precision issues.
x=real(ifft(log(abs(fft(x,N))),N));

end

%LICENSE:
%
%The source code is in the public domain and not licensed or under
%copyright. The information and software may be used freely by the public.
%As required by 17 U.S.C. 403, third parties producing copyrighted works
%consisting predominantly of the material produced by U.S. government
%agencies must provide notice with such work(s) identifying the U.S.
%Government material incorporated and stating that such material is not
%subject to copyright protection.
%
%Derived works shall not identify themselves in a manner that implies an
%endorsement by or an affiliation with the Naval Research Laboratory.
%
%RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE
%SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY THE NAVAL
%RESEARCH LABORATORY FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS
%OF RECIPIENT IN THE USE OF THE SOFTWARE.
