|
| 1 | +from mhkit.utils import upcrossing, peaks, troughs, heights, periods, custom |
| 2 | +import unittest |
| 3 | +from numpy.testing import assert_allclose |
| 4 | +import numpy as np |
| 5 | +from scipy.optimize import fsolve |
| 6 | + |
| 7 | + |
| 8 | +class TestUpcrossing(unittest.TestCase): |
| 9 | + @classmethod |
| 10 | + def setUpClass(self): |
| 11 | + self.t = np.linspace(0, 4, 1000) |
| 12 | + |
| 13 | + self.signal = self._example_waveform(self, self.t) |
| 14 | + |
| 15 | + # Approximiate points for the zero crossing, |
| 16 | + # used as starting points in numerical |
| 17 | + # solution. |
| 18 | + self.zero_cross_approx = [0, 2.1, 3, 3.8] |
| 19 | + |
| 20 | + def _example_waveform(self, t): |
| 21 | + # Create simple wave form to analyse. |
| 22 | + # This has been created to perform |
| 23 | + # a simple independent calcuation that |
| 24 | + # the mhkit functions can be tested against. |
| 25 | + |
| 26 | + A = np.array([0.5, 0.6, 0.3]) |
| 27 | + T = np.array([3, 2, 1]) |
| 28 | + w = 2 * np.pi / T |
| 29 | + |
| 30 | + signal = np.zeros(t.size) |
| 31 | + for i in range(A.size): |
| 32 | + signal += A[i] * np.sin(w[i] * t) |
| 33 | + |
| 34 | + return signal |
| 35 | + |
| 36 | + def _example_analysis(self, t, signal): |
| 37 | + # NB: This only works due to the construction |
| 38 | + # of our test signal. It is not suitable as |
| 39 | + # a general approach. |
| 40 | + grad = np.diff(signal) |
| 41 | + |
| 42 | + # +1 to get the index at turning point |
| 43 | + turning_points = np.flatnonzero(grad[1:] * grad[:-1] < 0) + 1 |
| 44 | + |
| 45 | + crest_inds = turning_points[signal[turning_points] > 0] |
| 46 | + trough_inds = turning_points[signal[turning_points] < 0] |
| 47 | + |
| 48 | + crests = signal[crest_inds] |
| 49 | + troughs = signal[trough_inds] |
| 50 | + |
| 51 | + heights = crests - troughs |
| 52 | + |
| 53 | + zero_cross = fsolve(self._example_waveform, self.zero_cross_approx) |
| 54 | + periods = np.diff(zero_cross) |
| 55 | + |
| 56 | + return crests, troughs, heights, periods |
| 57 | + |
| 58 | + def test_peaks(self): |
| 59 | + want, _, _, _ = self._example_analysis(self.t, self.signal) |
| 60 | + |
| 61 | + got = peaks(self.t, self.signal) |
| 62 | + |
| 63 | + assert_allclose(got, want) |
| 64 | + |
| 65 | + def test_troughs(self): |
| 66 | + _, want, _, _ = self._example_analysis(self.t, self.signal) |
| 67 | + |
| 68 | + got = troughs(self.t, self.signal) |
| 69 | + |
| 70 | + assert_allclose(got, want) |
| 71 | + |
| 72 | + def test_heights(self): |
| 73 | + _, _, want, _ = self._example_analysis(self.t, self.signal) |
| 74 | + |
| 75 | + got = heights(self.t, self.signal) |
| 76 | + |
| 77 | + assert_allclose(got, want) |
| 78 | + |
| 79 | + def test_periods(self): |
| 80 | + _, _, _, want = self._example_analysis(self.t, self.signal) |
| 81 | + |
| 82 | + got = periods(self.t, self.signal) |
| 83 | + |
| 84 | + assert_allclose(got, want, rtol=1e-3, atol=1e-3) |
| 85 | + |
| 86 | + def test_custom(self): |
| 87 | + want, _, _, _ = self._example_analysis(self.t, self.signal) |
| 88 | + |
| 89 | + # create a similar function to finding the peaks |
| 90 | + def f(ind1, ind2): return np.max(self.signal[ind1:ind2]) |
| 91 | + |
| 92 | + got = custom(self.t, self.signal, f) |
| 93 | + |
| 94 | + assert_allclose(got, want) |
| 95 | + |
| 96 | + def test_peaks_with_inds(self): |
| 97 | + want, _, _, _ = self._example_analysis(self.t, self.signal) |
| 98 | + |
| 99 | + inds = upcrossing(self.t, self.signal) |
| 100 | + |
| 101 | + got = peaks(self.t, self.signal, inds) |
| 102 | + |
| 103 | + assert_allclose(got, want) |
| 104 | + |
| 105 | + def test_trough_with_inds(self): |
| 106 | + _, want, _, _ = self._example_analysis(self.t, self.signal) |
| 107 | + |
| 108 | + inds = upcrossing(self.t, self.signal) |
| 109 | + |
| 110 | + got = troughs(self.t, self.signal, inds) |
| 111 | + |
| 112 | + assert_allclose(got, want) |
| 113 | + |
| 114 | + def test_heights_with_inds(self): |
| 115 | + _, _, want, _ = self._example_analysis(self.t, self.signal) |
| 116 | + |
| 117 | + inds = upcrossing(self.t, self.signal) |
| 118 | + |
| 119 | + got = heights(self.t, self.signal, inds) |
| 120 | + |
| 121 | + assert_allclose(got, want) |
| 122 | + |
| 123 | + def test_periods_with_inds(self): |
| 124 | + _, _, _, want = self._example_analysis(self.t, self.signal) |
| 125 | + |
| 126 | + inds = upcrossing(self.t, self.signal) |
| 127 | + |
| 128 | + got = periods(self.t, self.signal, inds) |
| 129 | + |
| 130 | + assert_allclose(got, want, rtol=1e-3, atol=1e-3) |
| 131 | + |
| 132 | + def test_custom_with_inds(self): |
| 133 | + want, _, _, _ = self._example_analysis(self.t, self.signal) |
| 134 | + |
| 135 | + inds = upcrossing(self.t, self.signal) |
| 136 | + |
| 137 | + # create a similar function to finding the peaks |
| 138 | + def f(ind1, ind2): return np.max(self.signal[ind1:ind2]) |
| 139 | + |
| 140 | + got = custom(self.t, self.signal, f, inds) |
| 141 | + |
| 142 | + assert_allclose(got, want) |
0 commit comments