def test_findrfc(): t = linspace(0, 7 * pi, 250) x = sin(t) + 0.1 * sin(50 * t) ind = findextrema(x) assert_array_almost_equal( ind, np.array( [1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 18, 19, 21, 23, 25, 26, 28, 29, 31, 33, 35, 36, 38, 39, 41, 43, 45, 46, 48, 50, 51, 53, 55, 56, 58, 60, 61, 63, 65, 67, 68, 70, 71, 73, 75, 77, 78, 80, 81, 83, 85, 87, 88, 90, 92, 93, 95, 97, 99, 100, 102, 103, 105, 107, 109, 110, 112, 113, 115, 117, 119, 120, 122, 124, 125, 127, 129, 131, 132, 134, 135, 137, 139, 141, 142, 144, 145, 147, 149, 151, 152, 154, 156, 157, 159, 161, 162, 164, 166, 167, 169, 171, 173, 174, 176, 177, 179, 181, 183, 184, 186, 187, 189, 191, 193, 194, 196, 198, 199, 201, 203, 205, 206, 208, 209, 211, 213, 215, 216, 218, 219, 221, 223, 225, 226, 228, 230, 231, 233, 235, 237, 238, 240, 241, 243, 245, 247, 248])) _ti, tp = t[ind], x[ind] for method in ['clib', 2, 1, 0]: ind1 = findrfc(tp, 0.3, method=method) if method in [1, 0]: ind1 = ind1[:-1] assert_array_almost_equal( ind1, np.array([0, 9, 32, 53, 74, 95, 116, 137])) assert_array_almost_equal( tp[ind1], np.array( [-0.00743352, 1.08753972, -1.07206545, 1.09550837, -1.07940458, 1.07849396, -1.0995006, 1.08094452]))
def test_findrfc(): t = linspace(0, 7 * pi, 250) x = sin(t) + 0.1 * sin(50 * t) ind = findextrema(x) assert_array_almost_equal( ind, np.array([ 1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 18, 19, 21, 23, 25, 26, 28, 29, 31, 33, 35, 36, 38, 39, 41, 43, 45, 46, 48, 50, 51, 53, 55, 56, 58, 60, 61, 63, 65, 67, 68, 70, 71, 73, 75, 77, 78, 80, 81, 83, 85, 87, 88, 90, 92, 93, 95, 97, 99, 100, 102, 103, 105, 107, 109, 110, 112, 113, 115, 117, 119, 120, 122, 124, 125, 127, 129, 131, 132, 134, 135, 137, 139, 141, 142, 144, 145, 147, 149, 151, 152, 154, 156, 157, 159, 161, 162, 164, 166, 167, 169, 171, 173, 174, 176, 177, 179, 181, 183, 184, 186, 187, 189, 191, 193, 194, 196, 198, 199, 201, 203, 205, 206, 208, 209, 211, 213, 215, 216, 218, 219, 221, 223, 225, 226, 228, 230, 231, 233, 235, 237, 238, 240, 241, 243, 245, 247, 248 ])) _ti, tp = t[ind], x[ind] ind1 = findrfc(tp, 0.3) assert_array_almost_equal(ind1, np.array([0, 9, 32, 53, 74, 95, 116, 137])) assert_array_almost_equal( tp[ind1], np.array([ -0.00743352, 1.08753972, -1.07206545, 1.09550837, -1.07940458, 1.07849396, -1.0995006, 1.08094452 ]))
def test_findrfc(): t = linspace(0, 7 * pi, 250) x = sin(t) + 0.1 * sin(50 * t) ind = findextrema(x) assert_allclose(ind, [1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 18, 19, 21, 23, 25, 26, 28, 29, 31, 33, 35, 36, 38, 39, 41, 43, 45, 46, 48, 50, 51, 53, 55, 56, 58, 60, 61, 63, 65, 67, 68, 70, 71, 73, 75, 77, 78, 80, 81, 83, 85, 87, 88, 90, 92, 93, 95, 97, 99, 100, 102, 103, 105, 107, 109, 110, 112, 113, 115, 117, 119, 120, 122, 124, 125, 127, 129, 131, 132, 134, 135, 137, 139, 141, 142, 144, 145, 147, 149, 151, 152, 154, 156, 157, 159, 161, 162, 164, 166, 167, 169, 171, 173, 174, 176, 177, 179, 181, 183, 184, 186, 187, 189, 191, 193, 194, 196, 198, 199, 201, 203, 205, 206, 208, 209, 211, 213, 215, 216, 218, 219, 221, 223, 225, 226, 228, 230, 231, 233, 235, 237, 238, 240, 241, 243, 245, 247, 248]) _ti, tp = t[ind], x[ind] for method in ['clib', 2, 1, 0]: ind1 = findrfc(tp, 0.3, method=method) if method in [1, 0]: ind1 = ind1[:-1] assert_allclose(ind1, [0, 9, 32, 53, 74, 95, 116, 137]) # print(tp[ind1].tolist()) truth = [-0.007433524853697526, 1.0875397175924215, -1.0720654490829054, 1.0955083650755328, -1.0794045843842426, 1.0784939627613357, -1.0995005995649583, 1.0809445217915996] assert_allclose(tp[ind1], truth)
def test_rfcfilter(): # 1. Filtered signal y is the turning points of x. x = sea() y = rfcfilter(x[:, 1], h=0.0, method=1) assert_array_almost_equal( y[0:5], np.array([-1.2004945, 0.83950546, -0.09049454, -0.02049454, -0.09049454])) # 2. This removes all rainflow cycles with range less than 0.5. y1 = rfcfilter(x[:, 1], h=0.5, method=0) assert_array_almost_equal( y1[0:5], np.array([-1.2004945, 0.83950546, -0.43049454, 0.34950546, -0.51049454])) # return t = linspace(0, 7 * pi, 250) x = sin(t) + 0.1 * sin(50 * t) ind = findextrema(x) assert_array_almost_equal( ind, np.array( [1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 18, 19, 21, 23, 25, 26, 28, 29, 31, 33, 35, 36, 38, 39, 41, 43, 45, 46, 48, 50, 51, 53, 55, 56, 58, 60, 61, 63, 65, 67, 68, 70, 71, 73, 75, 77, 78, 80, 81, 83, 85, 87, 88, 90, 92, 93, 95, 97, 99, 100, 102, 103, 105, 107, 109, 110, 112, 113, 115, 117, 119, 120, 122, 124, 125, 127, 129, 131, 132, 134, 135, 137, 139, 141, 142, 144, 145, 147, 149, 151, 152, 154, 156, 157, 159, 161, 162, 164, 166, 167, 169, 171, 173, 174, 176, 177, 179, 181, 183, 184, 186, 187, 189, 191, 193, 194, 196, 198, 199, 201, 203, 205, 206, 208, 209, 211, 213, 215, 216, 218, 219, 221, 223, 225, 226, 228, 230, 231, 233, 235, 237, 238, 240, 241, 243, 245, 247, 248])) _ti, tp = t[ind], x[ind] tp03 = rfcfilter(tp, 0.3) assert_array_almost_equal( tp03, np.array( [-0.00743352, 1.08753972, -1.07206545, 1.09550837, -1.07940458, 1.07849396, -1.0995006, 1.08094452, 0.11983423])) tp3 = findrfc_astm(tp) assert_array_almost_equal((77, 3), tp3.shape) # print(tp3[-5:]) assert_array_almost_equal(tp3[-5:], [[0.01552179, 0.42313414, 1.], [1.09750448, -0.00199612, 0.5], [1.09022256, -0.00927804, 0.5], [0.48055514, 0.60038938, 0.5], [0.03200274, 0.15183698, 0.5]]) assert_array_almost_equal(tp3[:5], [[0.03578165, 0.28906389, 1.], [0.03602834, 0.56726584, 1.], [0.03816623, 0.76461446, 1.], [0.0638364, 0.92381302, 1.], [0.07759006, 0.99628738, 1.]])
def test_rfcfilter(sea1): # 1. Filtered signal y is the turning points of x. x = sea1 y = rfcfilter(x[:, 1], h=0.0, method=1) assert_allclose( y[0:5], [-1.2004945, 0.83950546, -0.09049454, -0.02049454, -0.09049454]) # 2. This removes all rainflow cycles with range less than 0.5. y1 = rfcfilter(x[:, 1], h=0.5, method=0) assert_allclose( y1[0:5], [-1.2004945, 0.83950546, -0.43049454, 0.34950546, -0.51049454]) # return t = np.linspace(0, 7 * pi, 250) x = sin(t) + 0.1 * sin(50 * t) ind = findextrema(x) assert_allclose(ind, [ 1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 18, 19, 21, 23, 25, 26, 28, 29, 31, 33, 35, 36, 38, 39, 41, 43, 45, 46, 48, 50, 51, 53, 55, 56, 58, 60, 61, 63, 65, 67, 68, 70, 71, 73, 75, 77, 78, 80, 81, 83, 85, 87, 88, 90, 92, 93, 95, 97, 99, 100, 102, 103, 105, 107, 109, 110, 112, 113, 115, 117, 119, 120, 122, 124, 125, 127, 129, 131, 132, 134, 135, 137, 139, 141, 142, 144, 145, 147, 149, 151, 152, 154, 156, 157, 159, 161, 162, 164, 166, 167, 169, 171, 173, 174, 176, 177, 179, 181, 183, 184, 186, 187, 189, 191, 193, 194, 196, 198, 199, 201, 203, 205, 206, 208, 209, 211, 213, 215, 216, 218, 219, 221, 223, 225, 226, 228, 230, 231, 233, 235, 237, 238, 240, 241, 243, 245, 247, 248 ]) _ti, tp = t[ind], x[ind] tp03 = rfcfilter(tp, 0.3) # print(tp03.tolist()) truth = [ -0.007433524853697526, 1.0875397175924215, -1.0720654490829054, 1.0955083650755328, -1.0794045843842426, 1.0784939627613357, -1.0995005995649583, 1.0809445217915996, 0.11983423290349654 ] assert_allclose(tp03, truth) tp3 = findrfc_astm(tp) assert_allclose((77, 3), tp3.shape) # print(tp3[-5:].tolist()) assert_allclose(tp3[-5:], [[0.01552179103405038, 0.4231341427960734, 1.0], [1.0975044823202456, -0.001996117244712714, 0.5], [1.090222560678279, -0.00927803888667933, 0.5], [0.48055514444405156, 0.600389377347548, 0.5], [0.032002742614076624, 0.15183697551757316, 0.5]]) # print(tp3[:5].tolist()) assert_allclose(tp3[:5], [[0.035781645324019146, 0.28906389183961456, 1.0], [0.03602834384593512, 0.5672658361052029, 1.0], [0.038166226239640555, 0.7646144604852383, 1.0], [0.06383640016547976, 0.9238130173264235, 1.0], [0.07759005562881188, 0.9962873791766909, 1.0]])
def test_rfcfilter(): # 1. Filtered signal y is the turning points of x. x = sea() y = rfcfilter(x[:, 1], h=0.0, method=1) assert_array_almost_equal( y[0:5], np.array( [-1.2004945, 0.83950546, -0.09049454, -0.02049454, -0.09049454])) # 2. This removes all rainflow cycles with range less than 0.5. y1 = rfcfilter(x[:, 1], h=0.5, method=0) assert_array_almost_equal( y1[0:5], np.array( [-1.2004945, 0.83950546, -0.43049454, 0.34950546, -0.51049454])) # return t = linspace(0, 7 * pi, 250) x = sin(t) + 0.1 * sin(50 * t) ind = findextrema(x) assert_array_almost_equal( ind, np.array([ 1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 18, 19, 21, 23, 25, 26, 28, 29, 31, 33, 35, 36, 38, 39, 41, 43, 45, 46, 48, 50, 51, 53, 55, 56, 58, 60, 61, 63, 65, 67, 68, 70, 71, 73, 75, 77, 78, 80, 81, 83, 85, 87, 88, 90, 92, 93, 95, 97, 99, 100, 102, 103, 105, 107, 109, 110, 112, 113, 115, 117, 119, 120, 122, 124, 125, 127, 129, 131, 132, 134, 135, 137, 139, 141, 142, 144, 145, 147, 149, 151, 152, 154, 156, 157, 159, 161, 162, 164, 166, 167, 169, 171, 173, 174, 176, 177, 179, 181, 183, 184, 186, 187, 189, 191, 193, 194, 196, 198, 199, 201, 203, 205, 206, 208, 209, 211, 213, 215, 216, 218, 219, 221, 223, 225, 226, 228, 230, 231, 233, 235, 237, 238, 240, 241, 243, 245, 247, 248 ])) _ti, tp = t[ind], x[ind] tp03 = rfcfilter(tp, 0.3) assert_array_almost_equal( tp03, np.array([ -0.00743352, 1.08753972, -1.07206545, 1.09550837, -1.07940458, 1.07849396, -1.0995006, 1.08094452, 0.11983423 ])) tp3 = findrfc_astm(tp) assert_array_almost_equal((77, 3), tp3.shape) # print(tp3[-5:]) assert_array_almost_equal( tp3[-5:], [[0.01552179, 0.42313414, 1.], [1.09750448, -0.00199612, 0.5], [1.09022256, -0.00927804, 0.5], [0.48055514, 0.60038938, 0.5], [0.03200274, 0.15183698, 0.5]]) assert_array_almost_equal( tp3[:5], [[0.03578165, 0.28906389, 1.], [0.03602834, 0.56726584, 1.], [0.03816623, 0.76461446, 1.], [0.0638364, 0.92381302, 1.], [0.07759006, 0.99628738, 1.]])
def _remove_index_to_data_too_close_to_each_other(ix_e, is_too_small, di_e, ti_e, tmin): is_too_close = np.hstack((is_too_small[0], is_too_small[:-1] | is_too_small[1:], is_too_small[-1])) # Find opening (no) and closing (nc) index for data beeing to close: iy = findextrema(np.hstack([0, 0, is_too_small, 0])) no = iy[:2] - 1 nc = iy[1::2] for start, stop in zip(no, nc): iz = slice(start, stop) i_ok = _find_ok_peaks(di_e[iz], ti_e[iz], tmin) if len(i_ok): is_too_close[start + i_ok] = 0 # Remove data which is too close to other data. if is_too_close.any(): i_ok, = where(1 - is_too_close) ix_e = ix_e[i_ok] return ix_e
def test_rfcfilter(): # 1. Filtered signal y is the turning points of x. x = sea() y = rfcfilter(x[:, 1], h=0, method=1) assert_array_almost_equal( y[0:5], np.array( [-1.2004945, 0.83950546, -0.09049454, -0.02049454, -0.09049454])) # 2. This removes all rainflow cycles with range less than 0.5. y1 = rfcfilter(x[:, 1], h=0.5) assert_array_almost_equal( y1[0:5], np.array( [-1.2004945, 0.83950546, -0.43049454, 0.34950546, -0.51049454])) t = linspace(0, 7 * pi, 250) x = sin(t) + 0.1 * sin(50 * t) ind = findextrema(x) assert_array_almost_equal( ind, np.array([ 1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 18, 19, 21, 23, 25, 26, 28, 29, 31, 33, 35, 36, 38, 39, 41, 43, 45, 46, 48, 50, 51, 53, 55, 56, 58, 60, 61, 63, 65, 67, 68, 70, 71, 73, 75, 77, 78, 80, 81, 83, 85, 87, 88, 90, 92, 93, 95, 97, 99, 100, 102, 103, 105, 107, 109, 110, 112, 113, 115, 117, 119, 120, 122, 124, 125, 127, 129, 131, 132, 134, 135, 137, 139, 141, 142, 144, 145, 147, 149, 151, 152, 154, 156, 157, 159, 161, 162, 164, 166, 167, 169, 171, 173, 174, 176, 177, 179, 181, 183, 184, 186, 187, 189, 191, 193, 194, 196, 198, 199, 201, 203, 205, 206, 208, 209, 211, 213, 215, 216, 218, 219, 221, 223, 225, 226, 228, 230, 231, 233, 235, 237, 238, 240, 241, 243, 245, 247, 248 ])) _ti, tp = t[ind], x[ind] tp03 = rfcfilter(tp, 0.3) assert_array_almost_equal( tp03, np.array([ -0.00743352, 1.08753972, -1.07206545, 1.09550837, -1.07940458, 1.07849396, -1.0995006, 1.08094452, 0.11983423 ]))
def test_rfcfilter(): # 1. Filtered signal y is the turning points of x. x = sea() y = rfcfilter(x[:, 1], h=0, method=1) assert_array_almost_equal( y[0:5], np.array([-1.2004945, 0.83950546, -0.09049454, -0.02049454, -0.09049454])) # 2. This removes all rainflow cycles with range less than 0.5. y1 = rfcfilter(x[:, 1], h=0.5) assert_array_almost_equal( y1[0:5], np.array([-1.2004945, 0.83950546, -0.43049454, 0.34950546, -0.51049454])) t = linspace(0, 7 * pi, 250) x = sin(t) + 0.1 * sin(50 * t) ind = findextrema(x) assert_array_almost_equal( ind, np.array( [1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 18, 19, 21, 23, 25, 26, 28, 29, 31, 33, 35, 36, 38, 39, 41, 43, 45, 46, 48, 50, 51, 53, 55, 56, 58, 60, 61, 63, 65, 67, 68, 70, 71, 73, 75, 77, 78, 80, 81, 83, 85, 87, 88, 90, 92, 93, 95, 97, 99, 100, 102, 103, 105, 107, 109, 110, 112, 113, 115, 117, 119, 120, 122, 124, 125, 127, 129, 131, 132, 134, 135, 137, 139, 141, 142, 144, 145, 147, 149, 151, 152, 154, 156, 157, 159, 161, 162, 164, 166, 167, 169, 171, 173, 174, 176, 177, 179, 181, 183, 184, 186, 187, 189, 191, 193, 194, 196, 198, 199, 201, 203, 205, 206, 208, 209, 211, 213, 215, 216, 218, 219, 221, 223, 225, 226, 228, 230, 231, 233, 235, 237, 238, 240, 241, 243, 245, 247, 248])) _ti, tp = t[ind], x[ind] tp03 = rfcfilter(tp, 0.3) assert_array_almost_equal( tp03, np.array( [-0.00743352, 1.08753972, -1.07206545, 1.09550837, -1.07940458, 1.07849396, -1.0995006, 1.08094452, 0.11983423]))
def test_findextrema(): t = linspace(0, 7 * pi, 250) x = sin(t) ind = findextrema(x) assert_array_almost_equal(ind, np.array([18, 53, 89, 125, 160, 196, 231]))
def test_findextrema(): t = linspace(0, 7 * pi, 250) x = sin(t) ind = findextrema(x) assert_allclose(ind, [18, 53, 89, 125, 160, 196, 231])
def findpot(data, t=None, thresh=None, tmin=1): ''' Retrun indices to Peaks over threshold values Parameters ---------- data, t : array-like data-values and sampling-times, respectively. thresh : real scalar minimum threshold for levels in data. tmin : real scalar minimum distance to another peak [same unit as t] (default 1) Returns ------- Ie : ndarray indices to extreme values, i.e., all data > tresh which are at least tmin distance apart. Example ------- >>> import pylab >>> import wafo.data >>> from wafo.misc import findtc >>> x = wafo.data.sea() >>> t, data = x.T >>> itc, iv = findtc(data,0,'dw') >>> ytc, ttc = data[itc], t[itc] >>> ymin = 2*data.std() >>> tmin = 10 # sec >>> I = findpot(data, t, ymin, tmin) >>> yp, tp = data[I], t[I] >>> Ie = findpot(yp, tp, ymin,tmin) >>> ye, te = yp[Ie], tp[Ie] >>> h = pylab.plot(t,data,ttc,ytc,'ro', ... t,zeros(len(t)),':', ... te, ye,'k.',tp,yp,'+') See also -------- fitgenpar, decluster, extremalidx ''' Data = arr(data) if t is None: ti = np.arange(len(Data)) else: ti = arr(t) Ie, = where(Data > thresh) Ye = Data[Ie] Te = ti[Ie] if len(Ye) <= 1: return Ie dT = np.diff(Te) notSorted = np.any(dT < 0) if notSorted: I = np.argsort(Te) Te = Te[I] Ie = Ie[I] Ye = Ye[I] dT = np.diff(Te) isTooSmall = (dT <= tmin) if np.any(isTooSmall): isTooClose = np.hstack( (isTooSmall[0], isTooSmall[:-1] | isTooSmall[1:], isTooSmall[-1])) # Find opening (NO) and closing (NC) index for data beeing to close: iy = findextrema(np.hstack([0, 0, isTooSmall, 0])) NO = iy[::2] - 1 NC = iy[1::2] for no, nc in zip(NO, NC): iz = slice(no, nc) iOK = _find_ok_peaks(Ye[iz], Te[iz], tmin) if len(iOK): isTooClose[no + iOK] = 0 # Remove data which is too close to other data. if isTooClose.any(): # len(tooClose)>0: iOK, = where(1 - isTooClose) Ie = Ie[iOK] return Ie
def findpot(data, t=None, thresh=None, tmin=1): ''' Retrun indices to Peaks over threshold values Parameters ---------- data, t : array-like data-values and sampling-times, respectively. thresh : real scalar minimum threshold for levels in data. tmin : real scalar minimum distance to another peak [same unit as t] (default 1) Returns ------- Ie : ndarray indices to extreme values, i.e., all data > tresh which are at least tmin distance apart. Example ------- >>> import pylab >>> import wafo.data >>> from wafo.misc import findtc >>> x = wafo.data.sea() >>> t, data = x.T >>> itc, iv = findtc(data,0,'dw') >>> ytc, ttc = data[itc], t[itc] >>> ymin = 2*data.std() >>> tmin = 10 # sec >>> I = findpot(data, t, ymin, tmin) >>> yp, tp = data[I], t[I] >>> Ie = findpot(yp, tp, ymin,tmin) >>> ye, te = yp[Ie], tp[Ie] >>> h = pylab.plot(t,data,ttc,ytc,'ro',t,zeros(len(t)),':',te, ye,'k.',tp,yp,'+') See also -------- fitgenpar, decluster, extremalidx ''' Data = arr(data) if t is None: ti = np.arange(len(Data)) else: ti = arr(t) Ie, = where(Data > thresh); Ye = Data[Ie] Te = ti[Ie] if len(Ye) <= 1: return Ie dT = np.diff(Te) notSorted = np.any(dT < 0); if notSorted: I = np.argsort(Te) Te = Te[I] Ie = Ie[I] Ye = Ye[I] dT = np.diff(Te) isTooSmall = (dT <= tmin) if np.any(isTooSmall): isTooClose = np.hstack((isTooSmall[0], isTooSmall[:-1] | isTooSmall[1:], isTooSmall[-1])) #Find opening (NO) and closing (NC) index for data beeing to close: iy = findextrema(np.hstack([0, 0, isTooSmall, 0])) NO = iy[::2] - 1 NC = iy[1::2] for no, nc in zip(NO, NC): iz = slice(no, nc) iOK = _find_ok_peaks(Ye[iz], Te[iz], tmin) if len(iOK): isTooClose[no + iOK] = 0 # Remove data which is too close to other data. if isTooClose.any(): #len(tooClose)>0: iOK, = where(1 - isTooClose) Ie = Ie[iOK] return Ie