Exemplo n.º 1
0
def test_WaveletFilter_hard_10_known():
    """WaveletFilter.filter 'hard' reproduces analytic result (high T)"""
    from electrolib.filters.dwtfile import WaveletFilter
    w = WaveletFilter('db1', 1, 'hard', 10)
    x =     np.array([0, 2, 4, 6, 8, 10, 12, 14])
    xf_th = np.array([1, 1, 5, 5, 9,  9, 13, 13])
    xf = w.filter(x)
    assert np.allclose(xf, xf_th)
Exemplo n.º 2
0
def test_WaveletFilter_hard_0_identity():
    """WaveletFilter.filter with 'hard', '0' should reproduce signal"""
    from electrolib.filters.dwtfile import WaveletFilter
    for nlevels in range(5):
        w = WaveletFilter('db2', nlevels, 'hard', 0)
        x = np.sin(np.arange(0,10,0.1))
        xf = w.filter(x)
        assert np.allclose(x, xf)
Exemplo n.º 3
0
def test_dwt_idwt_inverse():
    """dwt/idwt should invert each other (to machine precision)"""
    from electrolib.filters.dwtfile import WaveletFilter as W
    x = np.sin(np.arange(0,10,0.1))
    w = W.wavelet('db2')
    zi = W.zero_state(w)
    cA, cD, zf = W.dwt(x, w, zi)
    x_rec = W.idwt(cA, cD, w)
    assert np.allclose(x, x_rec)
Exemplo n.º 4
0
def test_WaveletFilter_low_stop():
    """WaveletFilter.filter 'soft' reproduces analytic result (low T)"""
    from electrolib.filters.dwtfile import WaveletFilter
    w = WaveletFilter('db1', 1, 'soft', np.sqrt(2), low_stop=True)
    xl  = np.array([  0,  0,  1,  1,  2,  2,  3,  3])
    xh  = np.array([ -1,  1, -1,  1, -2,  2, -2,  2])
    xhf = np.array([  0,  0,  0,  0, -1,  1, -1,  1])
    x     = xl + xh
    xf = w.filter(x)
    assert np.allclose(xf, xhf)
Exemplo n.º 5
0
def test_WaveletFilter_hard_2_known():
    """WaveletFilter.filter 'hard' reproduces analytic result (low T)"""
    from electrolib.filters.dwtfile import WaveletFilter
    w = WaveletFilter('db1', 1, 'hard', 2)
    xl  = np.array([  0,  0,  1,  1,  2,  2,  3,  3])
    xh  = np.array([ -1,  1, -1,  1, -2,  2, -2,  2])
    xhf = np.array([  0,  0,  0,  0, -2,  2, -2,  2])
    x     = xl + xh
    xf_th = xl + xhf
    xf = w.filter(x)
    assert np.allclose(xf, xf_th)
Exemplo n.º 6
0
def test_dwt_blocks():
    """dwt block filtering should be nearly equal to dwt1 in one go"""
    # Basic setup
    from electrolib.filters.dwtfile import WaveletFilter as W
    x = np.sin(np.arange(0,10,0.1))
    w = W.wavelet('db2')
    zi = W.zero_state(w)
    overlap = len(zi[0])
    # Prepare for loop
    nstep = 10
    cA = np.zeros(len(x)/2)
    cD = np.zeros_like(cA)
    state = zi
    for n in range(0, len(x), nstep):
        cAn, cDn, state = W.dwt(x[n:n+nstep], w, state)
        cA[n/2:(n+nstep)/2] = cAn[:-overlap]
        cD[n/2:(n+nstep)/2] = cDn[:-overlap]
    # Do in one go
    cA1, cD1, zf = W.dwt(x, w, zi)
    cA1 = cA1[:-overlap]
    cD1 = cD1[:-overlap]
    assert np.allclose(cA1, cA)
    assert np.allclose(cD1, cD)