diff --git a/src/machinevisiontoolbox/base/findpeaks.py b/src/machinevisiontoolbox/base/findpeaks.py index fc8a2cb3..e6cf5d5d 100644 --- a/src/machinevisiontoolbox/base/findpeaks.py +++ b/src/machinevisiontoolbox/base/findpeaks.py @@ -84,7 +84,9 @@ def findpeaks( n = round(scale / dx) * 2 + 1 kernel = np.ones((n,)) (k,) = np.nonzero(y == sp.signal.order_filter(y, kernel, n - 1)) - k = np.array([kk for kk in k if kk >= scale and kk <= len(y) - scale]) + k = np.array( + [kk for kk in k if kk >= scale and kk < len(y) - scale], dtype=np.intp + ) else: # take the zero crossings dy = np.diff(y) diff --git a/tests/base/test_findpeaks.py b/tests/base/test_findpeaks.py index 25e1103d..533869c1 100644 --- a/tests/base/test_findpeaks.py +++ b/tests/base/test_findpeaks.py @@ -90,6 +90,14 @@ def test_no_peaks(self): self.assertIsInstance(ymax, np.ndarray) self.assertEqual(len(x), len(ymax)) + def test_endpoints_are_not_peaks(self): + """Test that the last scale samples cannot be peaks.""" + y = np.array([0, 0, 1]) + x, ymax = findpeaks(y, scale=1) + + self.assertEqual(len(x), 0) + self.assertEqual(len(ymax), 0) + def test_all_equal(self): """Test signal with all equal values""" y = np.array([1, 1, 1, 1, 1])