def test(monte_carlo, complex_H_with_UH, freqs, sampling_freq, filter_order): b_real_imaginary, ub_real_imaginary = LSFIR( H=monte_carlo["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, inv=True, verbose=True, UH=monte_carlo["UH"], mc_runs=10000, ) b_complex, ub_complex = LSFIR( H=complex_H_with_UH["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, inv=True, verbose=True, UH=monte_carlo["UH"], mc_runs=10000, ) assert_allclose(b_real_imaginary, b_complex, rtol=4e-2) assert_allclose(ub_real_imaginary, ub_complex, rtol=6e-1)
def test_not_implemented_LSFIR(monte_carlo, freqs, sampling_freq, filter_order): expected_error_msg_regex = ( r"LSFIR: The least-squares fitting of a digital FIR filter " r".*truncated singular-value decomposition and linear matrix propagation.*is " r"not yet implemented.*") with pytest.raises( NotImplementedError, match=expected_error_msg_regex, ): LSFIR( H=monte_carlo["H"], UH=monte_carlo["UH"], N=filter_order, tau=filter_order // 2, f=freqs, Fs=sampling_freq, inv=False, ) with pytest.raises( NotImplementedError, match=expected_error_msg_regex, ): LSFIR( H=monte_carlo["H"], UH=monte_carlo["UH"], N=filter_order, tau=filter_order // 2, f=freqs, Fs=sampling_freq, inv=False, trunc_svd_tol=0.0, )
def LSFIR_filter_fit(monte_carlo, freqs, sampling_freq): N = 12 tau = N // 2 bF, UbF = LSFIR(monte_carlo["H"], N, freqs, sampling_freq, tau, inv=True, UH=monte_carlo["UH"]) assert np.all(np.linalg.eigvals(UbF) >= 0) assert_allclose( bF, np.load( os.path.join( pathlib.Path(__file__).parent.resolve(), "reference_arrays", "test_LSFIR_bF.npz", ), )["bF"], ) assert_allclose( UbF, np.load( os.path.join( pathlib.Path(__file__).parent.resolve(), "reference_arrays", "test_LSFIR_UbF.npz", ), )["UbF"], rtol=3e-1, ) return {"bF": bF, "UbF": UbF, "N": N, "tau": tau}
def test_compare_LSFIR_with_zero_to_None_uncertainties_with_svd_for_fitting_one_over_H( monte_carlo, freqs, sampling_freq, filter_order): b_fir_svd = LSFIR( H=monte_carlo["H"], UH=np.zeros_like(monte_carlo["UH"]), N=filter_order, tau=filter_order // 2, f=freqs, Fs=sampling_freq, inv=True, )[0] b_fir_none = LSFIR( H=monte_carlo["H"], N=filter_order, tau=filter_order // 2, f=freqs, Fs=sampling_freq, inv=True, UH=None, )[0] assert_allclose(b_fir_svd, b_fir_none)
def test(monte_carlo, freqs, sampling_freq, filter_order, fit_reciprocal): b, Ub = LSFIR( H=monte_carlo["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, inv=fit_reciprocal, UH=None, ) assert _is_np_array(b) and len(b) == filter_order + 1 assert Ub is None
def test_compare_LSFIR_with_zero_to_None_uncertainties_and_mc_for_fitting_H_directly( monte_carlo, freqs, sampling_freq, filter_order): b_fir_mc = LSFIR( H=monte_carlo["H"], UH=np.zeros_like(monte_carlo["UH"]), N=filter_order, tau=filter_order // 2, f=freqs, Fs=sampling_freq, inv=False, mc_runs=2, )[0] b_fir_none = LSFIR( H=monte_carlo["H"], N=filter_order, tau=filter_order // 2, f=freqs, Fs=sampling_freq, inv=False, UH=None, )[0] assert_allclose(b_fir_mc, b_fir_none)
def test(monte_carlo, freqs, sampling_freq, filter_order, weight_vector): b, Ub = LSFIR( H=monte_carlo["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, weights=weight_vector, inv=True, UH=monte_carlo["UH"], ) assert _is_np_array(b) and len(b) == filter_order + 1 assert is_2d_square_matrix(Ub) and number_of_rows_equals_vector_dim(Ub, b)
def test(monte_carlo, freqs, sampling_freq, filter_order): b_fir_svd, Ub_fir_svd = LSFIR( H=monte_carlo["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, verbose=True, inv=True, UH=monte_carlo["UH"], ) b_fir_mc, Ub_fir_mc = LSFIR( H=monte_carlo["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, verbose=True, inv=True, UH=monte_carlo["UH"], mc_runs=10000, ) assert_allclose(b_fir_mc, b_fir_svd, rtol=9e-2) assert_allclose(Ub_fir_mc, Ub_fir_svd, atol=6e-1, rtol=6e-1)
def test_LSFIR_with_wrong_type_weights(monte_carlo, freqs, sampling_freq, filter_order): weight_list = [1] * 2 * len(freqs) with pytest.raises( TypeError, match=r"LSFIR: User-defined weighting has wrong type.*"): LSFIR( H=monte_carlo["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, weights=cast(np.ndarray, weight_list), inv=True, UH=monte_carlo["UH"], mc_runs=2, )
def test_missing_svd_uncertainties_LSFIR(monte_carlo, freqs, sampling_freq, filter_order): with pytest.raises( ValueError, match=r"LSFIR: The least-squares fitting of a digital FIR filter " r".*singular-value decomposition and linear matrix propagation.*requires that " r"uncertainties are provided via input parameter UH.*", ): LSFIR( H=monte_carlo["H"], N=filter_order, tau=filter_order // 2, f=freqs, Fs=sampling_freq, inv=True, trunc_svd_tol=0.0, )
def test_missing_mc_uncertainties_LSFIR(monte_carlo, freqs, sampling_freq, filter_order): with pytest.raises( ValueError, match=r"LSFIR: The least-squares fitting of a digital FIR filter " r".*Monte Carlo.*requires that uncertainties are provided via input " r"parameter UH.*", ): LSFIR( H=monte_carlo["H"], N=filter_order, tau=filter_order // 2, f=freqs, Fs=sampling_freq, inv=False, mc_runs=1, )
def test_LSFIR_with_wrong_len_weights(monte_carlo, freqs, sampling_freq, weight_vector, filter_order): weight_vector = weight_vector[1:] with pytest.raises( ValueError, match=r"LSFIR: User-defined weighting has wrong dimension.*", ): LSFIR( H=monte_carlo["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, weights=weight_vector, inv=True, UH=monte_carlo["UH"], mc_runs=2, )
def test_LSFIR_with_wrong_type_UH(monte_carlo, freqs, sampling_freq, filter_order): uh_list = monte_carlo["UH"].tolist() with pytest.raises( TypeError, match=r"LSFIR: if uncertainties are provided, " r"they are expected to be of type np\.ndarray.*", ): LSFIR( H=monte_carlo["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, inv=True, UH=cast(np.ndarray, uh_list), mc_runs=2, )
def test_LSFIR_with_too_short_UH(monte_carlo, freqs, sampling_freq, filter_order): too_few_rows_UH = monte_carlo["UH"][1:] with pytest.raises( ValueError, match=r"LSFIR: number of rows of uncertainties and number of " r"elements of values are expected to match\..*", ): LSFIR( H=monte_carlo["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, inv=True, UH=too_few_rows_UH, mc_runs=2, )
def test_LSFIR_with_too_short_f(monte_carlo, freqs, sampling_freq, filter_order): too_short_f = freqs[1:] with pytest.raises( ValueError, match=r"LSFIR: vector of complex frequency responses is expected to " r"contain [0-9]+ elements, corresponding to the number of frequencies.*", ): LSFIR( H=monte_carlo["H"], N=filter_order, f=too_short_f, Fs=sampling_freq, tau=filter_order // 2, inv=True, UH=monte_carlo["UH"], mc_runs=2, )
def test_LSFIR_with_complex_but_too_short_H(complex_H_with_UH, freqs, sampling_freq, filter_order): complex_h_but_too_short = complex_H_with_UH["H"][1:] with pytest.raises( ValueError, match=r"LSFIR: vector of complex frequency responses is expected to " r"contain [0-9]+ elements, corresponding to the number of frequencies.*", ): LSFIR( H=complex_h_but_too_short, N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, inv=True, UH=complex_H_with_UH["UH"], mc_runs=2, )
def test_compare_invLSFIR_to_LSFIR(monte_carlo, freqs, sampling_freq, filter_order): b_fir_inv_lsfir = invLSFIR( H=monte_carlo["H"], N=filter_order, tau=filter_order // 2, f=freqs, Fs=sampling_freq, ) b_fir = LSFIR( H=monte_carlo["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, inv=True, )[0] assert_allclose(b_fir, b_fir_inv_lsfir)
def test_LSFIR_with_nonsquare_UH(monte_carlo, freqs, sampling_freq, filter_order): too_few_columns_UH = monte_carlo["UH"][:, 1:] with pytest.raises( ValueError, match=r"LSFIR: uncertainties are expected to be " r"provided in a square matrix shape.*", ): LSFIR( H=monte_carlo["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, inv=True, UH=too_few_columns_UH, mc_runs=2, )
def test_both_propagation_methods_simultaneously_requested_uncertainties_LSFIR( monte_carlo, freqs, sampling_freq, filter_order): with pytest.raises( ValueError, match=r"LSFIR: Only one of mc_runs and trunc_svd_tol can be " r"provided but.*", ): LSFIR( H=monte_carlo["H"], UH=monte_carlo["UH"], N=filter_order, tau=filter_order // 2, f=freqs, Fs=sampling_freq, inv=False, mc_runs=2, trunc_svd_tol=0.0, )
def test_too_small_number_of_monte_carlo_runs_LSFIR(monte_carlo, freqs, sampling_freq, filter_order, inv): with pytest.raises( ValueError, match=r"LSFIR: Number of Monte Carlo runs is expected to be greater " r"than 1.*", ): LSFIR( H=monte_carlo["H"], UH=monte_carlo["UH"], N=filter_order, tau=filter_order // 2, f=freqs, Fs=sampling_freq, inv=inv, mc_runs=1, )
def test(monte_carlo, freqs, sampling_freq, filter_order): b_fir_mc, Ub_fir_mc = invLSFIR_unc( H=monte_carlo["H"], UH=monte_carlo["UH"], N=filter_order, tau=filter_order // 2, f=freqs, Fs=sampling_freq, ) b_fir, Ub_fir = LSFIR( H=monte_carlo["H"], N=filter_order, f=freqs, Fs=sampling_freq, tau=filter_order // 2, inv=True, UH=monte_carlo["UH"], ) assert_allclose(b_fir_mc, b_fir) assert_allclose(Ub_fir_mc, Ub_fir, atol=6e-1, rtol=6e-1)