# Path: scikits/talkbox/transforms/dct_ref.py
# def direct_dctii(x):
#     """Direct implementation (O(n^2)) of dct II.
# 
#     Notes
#     -----
# 
#     Use it as a reference only, it is not suitable for any real computation."""
#     n = x.size
#     a = np.empty((n, n), dtype = x.dtype)
#     for i in xrange(n):
#         for j in xrange(n):
#             a[i, j] = x[j] * np.cos(np.pi * (0.5 + j) * i / n)
# 
#     a[0] *= np.sqrt(1. / n)
#     a[1:] *= np.sqrt(2. / n)
# 
#     return a.sum(axis = 1)
# 
# def direct_dctii_2(x):
#     """Direct implementation (O(n^2)) of dct."""
#     # We are a bit smarter here by computing the coefficient matrix directly,
#     # but still O(N^2)
#     n = x.size
# 
#     a = np.cos(np.pi / n * np.linspace(0, n - 1, n)[:, None]
#                          * np.linspace(0.5, 0.5 + n - 1, n)[None, :])
#     a *= x
#     a[0] *= np.sqrt(1. / n)
#     a[1:] *= np.sqrt(2. / n)
# 
#     return a.sum(axis = 1)
# 
# Path: scikits/talkbox/transforms/dct.py
# def dctii(x):
#     """Compute a Discrete Cosine Transform, type II.
# 
#     The DCT type II is defined as:
# 
#         \forall u \in 0...N-1, 
#         dct(u) = a(u) sum_{i=0}^{N-1}{f(i)cos((i + 0.5)\pi u}
# 
#     Where a(0) = sqrt(1/(4N)), a(u) = sqrt(1/(2N)) for u > 0
# 
#     Parameters
#     ==========
#     x : array-like
#         input signal
# 
#     Returns
#     =======
#     y : array-like
#         DCT-II
# 
#     Note
#     ====
#     Use fft.
#     """
#     if not np.isrealobj(x):
#         raise ValueError("Complex input not supported")
#     n = x.size
#     y = np.zeros(n * 4, x.dtype)
#     y[1:2*n:2] = x
#     y[2*n+1::2] = x[-1::-1]
#     y = np.real(fft(y))[:n]
#     y[0] *= np.sqrt(.25 / n)
#     y[1:] *= np.sqrt(.5 / n)
#     return y


import numpy as np
from numpy.testing import TestCase, assert_array_almost_equal
from scikits.talkbox.transforms.dct_ref import direct_dctii, direct_dctii_2
from scikits.talkbox.transforms.dct import dctii


class TestDCTTypeII(TestCase):
    Y0 = np.array([16.58312395177700, -10.41945851392763, 0.,
        -1.12380254641222, -0., -0.37572376864581, -0., -0.16287097213722,
        0., -0.06524422831323, 0.])

    Y1 = np.array([15.81138830084189, -10.02761236237870, 0,
        -1.07406322530303, 0, -0.35136418446315, 0, -0.14207821409532, 0,
        -0.03984144487100])

    X0 = np.linspace(0, 10, 11)
    X1 = np.linspace(0, 10, 10)

    def test_simple_direct(self):
        """Test 1d input, direct implementation."""
        assert_array_almost_equal(self.Y0, direct_dctii(self.X0))
        assert_array_almost_equal(self.Y1, direct_dctii(self.X1))

    def test_simple_direct2(self):
        """Test 1d input, 2nd direct implementation."""
        assert_array_almost_equal(self.Y0, direct_dctii_2(self.X0))
        assert_array_almost_equal(self.Y1, direct_dctii_2(self.X1))

    def test_simple(self):
        """Test 1d input, fft implementation."""
        assert_array_almost_equal(self.Y0, dctii(self.X0))

# Path: scikits/talkbox/transforms/dct_ref.py
# def direct_dctii(x):
#     """Direct implementation (O(n^2)) of dct II.
# 
#     Notes
#     -----
# 
#     Use it as a reference only, it is not suitable for any real computation."""
#     n = x.size
#     a = np.empty((n, n), dtype = x.dtype)
#     for i in xrange(n):
#         for j in xrange(n):
#             a[i, j] = x[j] * np.cos(np.pi * (0.5 + j) * i / n)
# 
#     a[0] *= np.sqrt(1. / n)
#     a[1:] *= np.sqrt(2. / n)
# 
#     return a.sum(axis = 1)
# 
# def direct_dctii_2(x):
#     """Direct implementation (O(n^2)) of dct."""
#     # We are a bit smarter here by computing the coefficient matrix directly,
#     # but still O(N^2)
#     n = x.size
# 
#     a = np.cos(np.pi / n * np.linspace(0, n - 1, n)[:, None]
#                          * np.linspace(0.5, 0.5 + n - 1, n)[None, :])
#     a *= x
#     a[0] *= np.sqrt(1. / n)
#     a[1:] *= np.sqrt(2. / n)
# 
#     return a.sum(axis = 1)
# 
# Path: scikits/talkbox/transforms/dct.py
# def dctii(x):
#     """Compute a Discrete Cosine Transform, type II.
# 
#     The DCT type II is defined as:
# 
#         \forall u \in 0...N-1, 
#         dct(u) = a(u) sum_{i=0}^{N-1}{f(i)cos((i + 0.5)\pi u}
# 
#     Where a(0) = sqrt(1/(4N)), a(u) = sqrt(1/(2N)) for u > 0
# 
#     Parameters
#     ==========
#     x : array-like
#         input signal
# 
#     Returns
#     =======
#     y : array-like
#         DCT-II
# 
#     Note
#     ====
#     Use fft.
#     """
#     if not np.isrealobj(x):
#         raise ValueError("Complex input not supported")
#     n = x.size
#     y = np.zeros(n * 4, x.dtype)
#     y[1:2*n:2] = x
#     y[2*n+1::2] = x[-1::-1]
#     y = np.real(fft(y))[:n]
#     y[0] *= np.sqrt(.25 / n)
#     y[1:] *= np.sqrt(.5 / n)
#     return y


# Path: scikits/talkbox/transforms/tests/test_dct.py
import numpy as np
from numpy.testing import TestCase, assert_array_almost_equal
from scikits.talkbox.transforms.dct_ref import direct_dctii, direct_dctii_2
from scikits.talkbox.transforms.dct import dctii



class TestDCTTypeII(TestCase):
    Y0 = np.array([16.58312395177700, -10.41945851392763, 0.,
        -1.12380254641222, -0., -0.37572376864581, -0., -0.16287097213722,
        0., -0.06524422831323, 0.])

    Y1 = np.array([15.81138830084189, -10.02761236237870, 0,
        -1.07406322530303, 0, -0.35136418446315, 0, -0.14207821409532, 0,
        -0.03984144487100])

    X0 = np.linspace(0, 10, 11)
    X1 = np.linspace(0, 10, 10)

    def test_simple_direct(self):
        """Test 1d input, direct implementation."""
        assert_array_almost_equal(self.Y0, direct_dctii(self.X0))
        assert_array_almost_equal(self.Y1, direct_dctii(self.X1))

    def test_simple_direct2(self):
        """Test 1d input, 2nd direct implementation."""
        assert_array_almost_equal(self.Y0, direct_dctii_2(self.X0))
        assert_array_almost_equal(self.Y1, direct_dctii_2(self.X1))

    def test_simple(self):
        """Test 1d input, fft implementation."""
        assert_array_almost_equal(self.Y0, dctii(self.X0))

        assert_array_almost_equal(self.Y1, dctii(self.X1))