# Path: scikits/talkbox/tools/correlations.py
# def nextpow2(n):
#     """Return the next power of 2 such as 2^p >= n.
# 
#     Notes
#     -----
#     Infinite and nan are left untouched, negative values are not allowed."""
#     if np.any(n < 0):
#         raise ValueError("n should be > 0")
# 
#     if np.isscalar(n):
#         f, p = np.frexp(n)
#         if f == 0.5:
#             return p-1
#         elif np.isfinite(f):
#             return p
#         else:
#             return f
#     else:
#         f, p = np.frexp(n)
#         res = f
#         bet = np.isfinite(f)
#         exa = (f == 0.5)
#         res[bet] = p[bet]
#         res[exa] = p[exa] - 1
#         return res
# 
# def acorr(x, axis=-1, onesided=False, scale='none'):
#     """Compute autocorrelation of x along given axis.
# 
#     Parameters
#     ----------
#     x : array-like
#         signal to correlate.
#     axis : int
#         axis along which autocorrelation is computed.
#     onesided: bool, optional
#         if True, only returns the right side of the autocorrelation.
#     scale: {'none', 'coeff'}
#         scaling mode. If 'coeff', the correlation is normalized such as the
#         0-lag is equal to 1.
# 
#     Notes
#     -----
#     Use fft for computation: is more efficient than direct computation for
#     relatively large n.
#     """
#     if not np.isrealobj(x):
#         raise ValueError("Complex input not supported yet")
#     if not scale in ['none', 'coeff']:
#         raise ValueError("scale mode %s not understood" % scale)
# 
#     maxlag = x.shape[axis]
#     nfft = 2 ** nextpow2(2 * maxlag - 1)
# 
#     if axis != -1:
#         x = np.swapaxes(x, -1, axis)
#     a = _acorr_last_axis(x, nfft, maxlag, onesided, scale)
#     if axis != -1:
#         a = np.swapaxes(a, -1, axis)
#     return a


import numpy as np
from numpy.testing import TestCase, assert_array_equal, \
                          assert_array_almost_equal, dec
from scikits.talkbox.tools.correlations import nextpow2, acorr
from scikits.talkbox.tools.cacorr import acorr as cacorr
        0.19762845849802, 0.28656126482213, 0.38735177865613, 0.49802371541502,
        0.61660079051383, 0.74110671936759, 0.86956521739130, 1.00000000000000,
        0.86956521739130, 0.74110671936759, 0.61660079051383, 0.49802371541502,
        0.38735177865613, 0.28656126482213, 0.19762845849802, 0.12252964426877,
        0.06324110671937, 0.02173913043478,])

    Xm = np.linspace(1, 22, 22).reshape(2, 11)
    Ym = np.array([[11.,   32.,   62.,  100.,  145.,  196.,  252.,  312.,  375.,
                    440.,  506.,  440.,  375.,  312.,  252.,  196.,  145.,  100.,
                    62.,   32.,   11.],
                   [264.,   538.,   821.,  1112.,  1410.,  1714.,  2023.,  2336.,
                    2652.,  2970.,  3289.,  2970.,  2652.,  2336.,  2023.,  1714.,
                    1410.,  1112.,   821.,   538.,   264.]])

    def test_simple(self):
        """Test autocorrelation for a rank 1 array."""
        a = self.acorr(self.X)
        assert_array_almost_equal(a, self.Y)

    def test_axis0(self):
        """Test autocorrelation along default axis."""
        a = self.acorr(self.Xm)
        assert_array_almost_equal(a, self.Ym)

    def test_axis1(self):
        """Test autocorrelation along axis 0."""
        a = self.acorr(self.Xm.T, axis=0)
        assert_array_almost_equal(a, self.Ym.T)

    def test_normalization(self):

# Path: scikits/talkbox/tools/correlations.py
# def nextpow2(n):
#     """Return the next power of 2 such as 2^p >= n.
# 
#     Notes
#     -----
#     Infinite and nan are left untouched, negative values are not allowed."""
#     if np.any(n < 0):
#         raise ValueError("n should be > 0")
# 
#     if np.isscalar(n):
#         f, p = np.frexp(n)
#         if f == 0.5:
#             return p-1
#         elif np.isfinite(f):
#             return p
#         else:
#             return f
#     else:
#         f, p = np.frexp(n)
#         res = f
#         bet = np.isfinite(f)
#         exa = (f == 0.5)
#         res[bet] = p[bet]
#         res[exa] = p[exa] - 1
#         return res
# 
# def acorr(x, axis=-1, onesided=False, scale='none'):
#     """Compute autocorrelation of x along given axis.
# 
#     Parameters
#     ----------
#     x : array-like
#         signal to correlate.
#     axis : int
#         axis along which autocorrelation is computed.
#     onesided: bool, optional
#         if True, only returns the right side of the autocorrelation.
#     scale: {'none', 'coeff'}
#         scaling mode. If 'coeff', the correlation is normalized such as the
#         0-lag is equal to 1.
# 
#     Notes
#     -----
#     Use fft for computation: is more efficient than direct computation for
#     relatively large n.
#     """
#     if not np.isrealobj(x):
#         raise ValueError("Complex input not supported yet")
#     if not scale in ['none', 'coeff']:
#         raise ValueError("scale mode %s not understood" % scale)
# 
#     maxlag = x.shape[axis]
#     nfft = 2 ** nextpow2(2 * maxlag - 1)
# 
#     if axis != -1:
#         x = np.swapaxes(x, -1, axis)
#     a = _acorr_last_axis(x, nfft, maxlag, onesided, scale)
#     if axis != -1:
#         a = np.swapaxes(a, -1, axis)
#     return a


# Path: scikits/talkbox/tools/tests/test_correlations.py
import numpy as np
from numpy.testing import TestCase, assert_array_equal, \
                          assert_array_almost_equal, dec
from scikits.talkbox.tools.correlations import nextpow2, acorr
from scikits.talkbox.tools.cacorr import acorr as cacorr

        0.19762845849802, 0.28656126482213, 0.38735177865613, 0.49802371541502,
        0.61660079051383, 0.74110671936759, 0.86956521739130, 1.00000000000000,
        0.86956521739130, 0.74110671936759, 0.61660079051383, 0.49802371541502,
        0.38735177865613, 0.28656126482213, 0.19762845849802, 0.12252964426877,
        0.06324110671937, 0.02173913043478,])

    Xm = np.linspace(1, 22, 22).reshape(2, 11)
    Ym = np.array([[11.,   32.,   62.,  100.,  145.,  196.,  252.,  312.,  375.,
                    440.,  506.,  440.,  375.,  312.,  252.,  196.,  145.,  100.,
                    62.,   32.,   11.],
                   [264.,   538.,   821.,  1112.,  1410.,  1714.,  2023.,  2336.,
                    2652.,  2970.,  3289.,  2970.,  2652.,  2336.,  2023.,  1714.,
                    1410.,  1112.,   821.,   538.,   264.]])

    def test_simple(self):
        """Test autocorrelation for a rank 1 array."""
        a = self.acorr(self.X)
        assert_array_almost_equal(a, self.Y)

    def test_axis0(self):
        """Test autocorrelation along default axis."""
        a = self.acorr(self.Xm)
        assert_array_almost_equal(a, self.Ym)

    def test_axis1(self):
        """Test autocorrelation along axis 0."""
        a = self.acorr(self.Xm.T, axis=0)
        assert_array_almost_equal(a, self.Ym.T)

    def test_normalization(self):

        a = self.acorr(self.X, scale='coeff')