def test_morlet_1d(): """ Tests for Morlet wavelets: - Make sure that it has exact zero mean - Make sure that it has a fast decay in time - Check that the maximal frequency is relatively close to xi, up to 1% accuracy """ size_signal = [2**13] Q_range = np.arange(1, 20, dtype=int) for N in size_signal: for Q in Q_range: xi_max = compute_xi_max(Q) xi_range = xi_max / np.power(2, np.arange(7)) for xi in xi_range: sigma = compute_sigma_psi(xi, Q) # get the morlet for these parameters psi_f = morlet_1d(N, xi, sigma, normalize='l2') # make sure that it has zero mean assert np.isclose(psi_f[0], 0.) # make sure that it has a fast decay in time psi = np.fft.ifft(psi_f) psi_abs = np.abs(psi) assert np.min(psi_abs) / np.max(psi_abs) < 1e-4 # Check that the maximal frequency is relatively close to xi, # up to 1 percent k_max = np.argmax(np.abs(psi_f)) xi_emp = float(k_max) / float(N) assert np.abs(xi_emp - xi) / xi < 1e-2
def test_get_max_dyadic_subsampling(): """ Tests on the subsampling formula for wavelets, to check that the retained value does not create aliasing (the wavelet should have decreased by a relative value of 1e-2 at the border of the aliasing.) """ N = 2**12 Q_range = np.arange(1, 20, dtype=int) J = 7 for Q in Q_range: xi_max = compute_xi_max(Q) xi_range = xi_max * np.power(0.5, np.arange(J * Q) / float(Q)) for xi in xi_range: sigma = compute_sigma_psi(xi, Q) j = get_max_dyadic_subsampling(xi, sigma) if j > 0: # if there is subsampling # compute the corresponding Morlet psi_f = morlet_1d(N, xi, sigma) # find the integer k such that k = N // 2**(j + 1) assert np.abs(psi_f[k]) / np.max(np.abs(psi_f)) < 1e-2 else: # pass this case: we have detected that there cannot # be any subsampling, and we assume that the filters are not # aliased already pass
def test_gauss_1d(): """ Tests for Gabor low-pass - Make sure that it has a fast decay in time - Make sure that it is symmetric, up to 1e-7 absolute precision """ N = 2**13 J = 7 P_range = [1, 5] sigma0 = 0.1 tol = 1e-7 for j in range(1, J + 1): for P in P_range: sigma_low = sigma0 / math.pow(2, j) g_f = gauss_1d(N, sigma_low, P_max=P) # check the symmetry of g_f assert np.max(np.abs(g_f[1:N // 2] - g_f[N // 2 + 1:][::-1])) < tol # make sure that it has a fast decay in time phi = np.fft.ifft(g_f) assert np.min(phi) > - tol assert np.min(np.abs(phi)) / np.max(np.abs(phi)) < 1e-4 Q = 1 xi = compute_xi_max(Q) sigma = compute_sigma_psi(xi, Q) with pytest.raises(ValueError) as ve: gauss_1d(N, xi, sigma, P_max=5.1) assert "should be an int" in ve.value.args[0] with pytest.raises(ValueError) as ve: gauss_1d(N, xi, sigma, P_max=-5) assert "should be non-negative" in ve.value.args[0]
def test_compute_xi_max(): """ Tests that 0.25 <= xi_max(Q) <= 0.5, whatever Q """ Q_range = np.arange(1, 21, dtype=int) for Q in Q_range: xi_max = compute_xi_max(Q) assert xi_max <= 0.5 assert xi_max >= 0.25
def test_morlet_1d(): """ Tests for Morlet wavelets: - Make sure that it has exact zero mean - Make sure that it has a fast decay in time - Check that the maximal frequency is relatively close to xi, up to 1% accuracy """ size_signal = [2**13] Q_range = np.arange(1, 20, dtype=int) P_range = [1, 5] for N in size_signal: for Q in Q_range: xi_max = compute_xi_max(Q) xi_range = xi_max / np.power(2, np.arange(7)) for xi in xi_range: for P in P_range: sigma = compute_sigma_psi(xi, Q) # get the morlet for these parameters psi_f = morlet_1d(N, xi, sigma, normalize='l2', P_max=P) # make sure that it has zero mean assert np.isclose(psi_f[0], 0.) # make sure that it has a fast decay in time psi = np.fft.ifft(psi_f) psi_abs = np.abs(psi) assert np.min(psi_abs) / np.max(psi_abs) < 1e-3 # Check that the maximal frequency is relatively close to xi, # up to 1 percent k_max = np.argmax(np.abs(psi_f)) xi_emp = float(k_max) / float(N) assert np.abs(xi_emp - xi) / xi < 1e-2 Q = 1 xi = compute_xi_max(Q) sigma = compute_sigma_psi(xi, Q) with pytest.raises(ValueError) as ve: morlet_1d(size_signal[0], xi, sigma, P_max=5.1) assert "should be an int" in ve.value.args[0] with pytest.raises(ValueError) as ve: morlet_1d(size_signal[0], xi, sigma, P_max=-5) assert "should be non-negative" in ve.value.args[0]