# Path: scikits/talkbox/misc/peak_picking.py
# def find_peaks(x, neighbours):
#     peaks = []
#     nx = x.size
# 
#     assert 2 * neighbours + 1 <= nx
# 
#     if nx == 1:
#         peaks.append(0)
#         return peaks
#     elif nx == 2:
#         if x[0] > x[1]:
#             peaks.append(0)
#         else:
#             peaks.append(1)
#             return peaks
# 
#     # Handle points which have less than neighs samples on their left
#     for i in range(neighbours):
#         cur = x[i]
#         m = x[i+1]
#         # look at the left of the current position
#         for j in range(i):
#             if m < x[j]:
#                 m = x[j]
#         # look at the right of the current position
#         for j in range(i+1, i+neighbours):
#             if m < x[j]:
#                 m = x[j]
# 
#         if cur > m:
#             peaks.append(i)
#             #assert(pkcnt <= (nx / neighbours + 1))
# 
#     # Handle points which have at least neighs samples on both their left
#     # and right
#     for i in range(neighbours, nx - neighbours):
#         cur = x[i]
#         m = x[i+1];
#         # look at the left
#         for j in range(i - neighbours, i):
#             if m < x[j]:
#                 m = x[j]
#         # look at the right
#         for j in range(i+1, i+neighbours):
#             if m < x[j]:
#                 m = x[j]
# 
#         if cur > m:
#             peaks.append(i)
#             #assert(pkcnt <= (nx / neighbours + 1))
# 
#     # Handle points which have less than neighs samples on their right
#     for i in range(nx - neighbours, nx):
#         cur = x[i]
#         m = x[i-1]
#         # look at the left
#         for j in range(i - neighbours, i):
#             if m < x[j]:
#                 m = x[j]
# 
#         # look at the right
#         for j in range(i+1, nx):
#             if m < x[j]:
#                 m = x[j]
# 
#         if cur > m:
#             peaks.append(i)
#             #assert(pkcnt <= (nx / neighbours + 1))
# 
#     return peaks


import numpy as np
from numpy.testing import TestCase, assert_array_equal, \
                          assert_array_almost_equal, dec
from scikits.talkbox.misc.peak_picking import find_peaks


class TestFindPeaks(TestCase):
    def test_simple(self):
        x = np.sin(np.linspace(0, 6 * np.pi, 256))
        p = find_peaks(x, 10)
        assert_array_equal(p, [21, 106, 191, 255])

if __name__ == "__main__":

# Path: scikits/talkbox/misc/peak_picking.py
# def find_peaks(x, neighbours):
#     peaks = []
#     nx = x.size
# 
#     assert 2 * neighbours + 1 <= nx
# 
#     if nx == 1:
#         peaks.append(0)
#         return peaks
#     elif nx == 2:
#         if x[0] > x[1]:
#             peaks.append(0)
#         else:
#             peaks.append(1)
#             return peaks
# 
#     # Handle points which have less than neighs samples on their left
#     for i in range(neighbours):
#         cur = x[i]
#         m = x[i+1]
#         # look at the left of the current position
#         for j in range(i):
#             if m < x[j]:
#                 m = x[j]
#         # look at the right of the current position
#         for j in range(i+1, i+neighbours):
#             if m < x[j]:
#                 m = x[j]
# 
#         if cur > m:
#             peaks.append(i)
#             #assert(pkcnt <= (nx / neighbours + 1))
# 
#     # Handle points which have at least neighs samples on both their left
#     # and right
#     for i in range(neighbours, nx - neighbours):
#         cur = x[i]
#         m = x[i+1];
#         # look at the left
#         for j in range(i - neighbours, i):
#             if m < x[j]:
#                 m = x[j]
#         # look at the right
#         for j in range(i+1, i+neighbours):
#             if m < x[j]:
#                 m = x[j]
# 
#         if cur > m:
#             peaks.append(i)
#             #assert(pkcnt <= (nx / neighbours + 1))
# 
#     # Handle points which have less than neighs samples on their right
#     for i in range(nx - neighbours, nx):
#         cur = x[i]
#         m = x[i-1]
#         # look at the left
#         for j in range(i - neighbours, i):
#             if m < x[j]:
#                 m = x[j]
# 
#         # look at the right
#         for j in range(i+1, nx):
#             if m < x[j]:
#                 m = x[j]
# 
#         if cur > m:
#             peaks.append(i)
#             #assert(pkcnt <= (nx / neighbours + 1))
# 
#     return peaks


# Path: scikits/talkbox/misc/tests/test_find_peaks.py
import numpy as np
from numpy.testing import TestCase, assert_array_equal, \
                          assert_array_almost_equal, dec
from scikits.talkbox.misc.peak_picking import find_peaks



class TestFindPeaks(TestCase):
    def test_simple(self):
        x = np.sin(np.linspace(0, 6 * np.pi, 256))
        p = find_peaks(x, 10)
        assert_array_equal(p, [21, 106, 191, 255])

if __name__ == "__main__":

    run_module_suite()