# Path: scikits/talkbox/spectral/basic.py
# def periodogram(x, nfft=None, fs=1):
#     """Compute the periodogram of the given signal, with the given fft size.
# 
#     Parameters
#     ----------
#     x : array-like
#         input signal
#     nfft : int
#         size of the fft to compute the periodogram. If None (default), the
#         length of the signal is used. if nfft > n, the signal is 0 padded.
#     fs : float
#         Sampling rate. By default, is 1 (normalized frequency. e.g. 0.5 is the
#         Nyquist limit).
# 
#     Returns
#     -------
#     pxx : array-like
#         The psd estimate.
#     fgrid : array-like
#         Frequency grid over which the periodogram was estimated.
# 
#     Examples
#     --------
#     Generate a signal with two sinusoids, and compute its periodogram:
# 
#     >>> fs = 1000
#     >>> x = np.sin(2 * np.pi  * 0.1 * fs * np.linspace(0, 0.5, 0.5*fs))
#     >>> x += np.sin(2 * np.pi  * 0.2 * fs * np.linspace(0, 0.5, 0.5*fs))
#     >>> px, fx = periodogram(x, 512, fs)
# 
#     Notes
#     -----
#     Only real signals supported for now.
# 
#     Returns the one-sided version of the periodogram.
# 
#     Discrepency with matlab: matlab compute the psd in unit of power / radian /
#     sample, and we compute the psd in unit of power / sample: to get the same
#     result as matlab, just multiply the result from talkbox by 2pi"""
#     # TODO: this is basic to the point of being useless:
#     #   - support Daniel smoothing
#     #   - support windowing
#     #   - trend/mean handling
#     #   - one-sided vs two-sided
#     #   - plot
#     #   - support complex input
#     x = np.atleast_1d(x)
#     n = x.size
# 
#     if x.ndim > 1:
#         raise ValueError("Only rank 1 input supported for now.")
#     if not np.isrealobj(x):
#         raise ValueError("Only real input supported for now.")
#     if not nfft:
#         nfft = n
#     if nfft < n:
#         raise ValueError("nfft < signal size not supported yet")
# 
#     pxx = np.abs(fft(x, nfft)) ** 2
#     if nfft % 2 == 0:
#         pn = nfft / 2 + 1
#     else:
#         pn = (nfft + 1 )/ 2
# 
#     fgrid = np.linspace(0, fs * 0.5, pn)
#     return pxx[:pn] / (n * fs), fgrid


from unittest import TestCase
from numpy.testing import assert_array_almost_equal
from scikits.talkbox.spectral.basic import periodogram
import numpy as np



lh = np.array([2.4, 2.4, 2.4, 2.2, 2.1, 1.5, 2.3, 2.3, 2.5, 2.0, 1.9, 1.7,
2.2, 1.8, 3.2, 3.2, 2.7, 2.2, 2.2, 1.9, 1.9, 1.8, 2.7, 3.0, 2.3, 2.0, 2.0, 2.9,
2.9, 2.7, 2.7, 2.3, 2.6, 2.4, 1.8, 1.7, 1.5, 1.4, 2.1, 3.3, 3.5, 3.5, 3.1, 2.6,
2.1, 3.4, 3.0, 2.9])

# Returned by p = spectrum(lh, plot=FALSE, detrend=FALSE, taper=0.0)
# XXX: R drops the first item (DC component) of the estimated spectrogram
lh_spec_raw = np.array([0.32650971, 0.79865114, 1.25684523, 0.66284366,
0.13803913, 1.51075717, 0.23526771, 0.66520833, 0.28393413, 0.09545270,
0.24710226, 0.18541667, 0.02353390, 0.07176963, 0.05049468, 0.11312500,

# Path: scikits/talkbox/spectral/basic.py
# def periodogram(x, nfft=None, fs=1):
#     """Compute the periodogram of the given signal, with the given fft size.
# 
#     Parameters
#     ----------
#     x : array-like
#         input signal
#     nfft : int
#         size of the fft to compute the periodogram. If None (default), the
#         length of the signal is used. if nfft > n, the signal is 0 padded.
#     fs : float
#         Sampling rate. By default, is 1 (normalized frequency. e.g. 0.5 is the
#         Nyquist limit).
# 
#     Returns
#     -------
#     pxx : array-like
#         The psd estimate.
#     fgrid : array-like
#         Frequency grid over which the periodogram was estimated.
# 
#     Examples
#     --------
#     Generate a signal with two sinusoids, and compute its periodogram:
# 
#     >>> fs = 1000
#     >>> x = np.sin(2 * np.pi  * 0.1 * fs * np.linspace(0, 0.5, 0.5*fs))
#     >>> x += np.sin(2 * np.pi  * 0.2 * fs * np.linspace(0, 0.5, 0.5*fs))
#     >>> px, fx = periodogram(x, 512, fs)
# 
#     Notes
#     -----
#     Only real signals supported for now.
# 
#     Returns the one-sided version of the periodogram.
# 
#     Discrepency with matlab: matlab compute the psd in unit of power / radian /
#     sample, and we compute the psd in unit of power / sample: to get the same
#     result as matlab, just multiply the result from talkbox by 2pi"""
#     # TODO: this is basic to the point of being useless:
#     #   - support Daniel smoothing
#     #   - support windowing
#     #   - trend/mean handling
#     #   - one-sided vs two-sided
#     #   - plot
#     #   - support complex input
#     x = np.atleast_1d(x)
#     n = x.size
# 
#     if x.ndim > 1:
#         raise ValueError("Only rank 1 input supported for now.")
#     if not np.isrealobj(x):
#         raise ValueError("Only real input supported for now.")
#     if not nfft:
#         nfft = n
#     if nfft < n:
#         raise ValueError("nfft < signal size not supported yet")
# 
#     pxx = np.abs(fft(x, nfft)) ** 2
#     if nfft % 2 == 0:
#         pn = nfft / 2 + 1
#     else:
#         pn = (nfft + 1 )/ 2
# 
#     fgrid = np.linspace(0, fs * 0.5, pn)
#     return pxx[:pn] / (n * fs), fgrid


# Path: scikits/talkbox/spectral/tests/test_basic.py
from unittest import TestCase
from numpy.testing import assert_array_almost_equal
from scikits.talkbox.spectral.basic import periodogram
import numpy as np




lh = np.array([2.4, 2.4, 2.4, 2.2, 2.1, 1.5, 2.3, 2.3, 2.5, 2.0, 1.9, 1.7,
2.2, 1.8, 3.2, 3.2, 2.7, 2.2, 2.2, 1.9, 1.9, 1.8, 2.7, 3.0, 2.3, 2.0, 2.0, 2.9,
2.9, 2.7, 2.7, 2.3, 2.6, 2.4, 1.8, 1.7, 1.5, 1.4, 2.1, 3.3, 3.5, 3.5, 3.1, 2.6,
2.1, 3.4, 3.0, 2.9])

# Returned by p = spectrum(lh, plot=FALSE, detrend=FALSE, taper=0.0)
# XXX: R drops the first item (DC component) of the estimated spectrogram
lh_spec_raw = np.array([0.32650971, 0.79865114, 1.25684523, 0.66284366,
0.13803913, 1.51075717, 0.23526771, 0.66520833, 0.28393413, 0.09545270,
0.24710226, 0.18541667, 0.02353390, 0.07176963, 0.05049468, 0.11312500,

0.02028320, 0.04174283, 0.09056918, 0.01548967, 0.02039262, 0.11912653,