|
| 1 | +"""Test the filter module.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import numpy as np |
| 5 | +from unittest import mock |
| 6 | +from adc_eval import filt |
| 7 | +from adc_eval import signals |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize("dec", np.random.randint(1, 20, 4)) |
| 11 | +def test_cic_decimate_set_dec_updates_gain(dec): |
| 12 | + """Tests that changing decimation factor updates gain.""" |
| 13 | + cicfilt = filt.CICDecimate(dec=1, order=2) |
| 14 | + assert cicfilt.gain == 1 |
| 15 | + |
| 16 | + cicfilt.dec = dec |
| 17 | + assert cicfilt.gain == (dec**2) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.parametrize("order", np.random.randint(1, 20, 4)) |
| 21 | +def test_cic_decimate_set_order_updates_gain(order): |
| 22 | + """Tests that changing filter order updates gain.""" |
| 23 | + cicfilt = filt.CICDecimate(dec=2, order=1) |
| 24 | + assert cicfilt.gain == 2 |
| 25 | + |
| 26 | + cicfilt.order = order |
| 27 | + assert cicfilt.gain == (2**order) |
| 28 | + |
| 29 | + |
| 30 | +def test_cic_decimate_returns_ndarray(): |
| 31 | + """Tests the CICDecimate output data conversion.""" |
| 32 | + cicfilt = filt.CICDecimate() |
| 33 | + data = np.random.randn(100).tolist() |
| 34 | + cicfilt._xout = data |
| 35 | + |
| 36 | + assert type(cicfilt.out) == type(np.array(list())) |
| 37 | + assert cicfilt.out.all() == np.array(data).all() |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize("dec", np.random.randint(1, 20, 4)) |
| 41 | +def test_cic_decimate_function(dec): |
| 42 | + """Tests the CICDecimate decimate function.""" |
| 43 | + cicfilt = filt.CICDecimate(dec=dec) |
| 44 | + data = np.random.randn(100) |
| 45 | + cicfilt.decimate(data) |
| 46 | + |
| 47 | + exp_result = data[::dec] |
| 48 | + |
| 49 | + assert cicfilt.out.size == exp_result.size |
| 50 | + assert cicfilt.out.all() == exp_result.all() |
| 51 | + |
| 52 | + |
| 53 | +def test_cic_decimate_function_none_input(): |
| 54 | + """Tests the CICDecimate decimate function with no input arg.""" |
| 55 | + cicfilt = filt.CICDecimate(dec=1) |
| 56 | + data = np.random.randn(100) |
| 57 | + cicfilt._xfilt = data |
| 58 | + cicfilt.decimate() |
| 59 | + |
| 60 | + exp_result = data |
| 61 | + |
| 62 | + assert cicfilt.out.size == exp_result.size |
| 63 | + assert cicfilt.out.all() == exp_result.all() |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("nlen", np.random.randint(8, 2**10, 4)) |
| 67 | +def test_cic_decimate_all_ones(nlen): |
| 68 | + """Test the CICDecimate filtering with all ones.""" |
| 69 | + cicfilt = filt.CICDecimate(dec=1, order=1) |
| 70 | + data = np.ones(nlen) |
| 71 | + cicfilt.run(data) |
| 72 | + |
| 73 | + exp_data = data.copy() |
| 74 | + exp_data[0] = 0 |
| 75 | + |
| 76 | + assert cicfilt.out.all() == exp_data.all() |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize("nlen", np.random.randint(8, 2**10, 4)) |
| 80 | +def test_cic_decimate_all_zeros(nlen): |
| 81 | + """Test the CICDecimate filtering with all zeros.""" |
| 82 | + cicfilt = filt.CICDecimate(dec=1, order=1) |
| 83 | + data = np.zeros(nlen) |
| 84 | + cicfilt.run(data) |
| 85 | + |
| 86 | + exp_data = data.copy() |
| 87 | + |
| 88 | + assert cicfilt.out.all() == exp_data.all() |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize("nlen", np.random.randint(8, 2**10, 4)) |
| 92 | +def test_cic_decimate_impulse(nlen): |
| 93 | + """Test the CICDecimate filtering with impulse.""" |
| 94 | + cicfilt = filt.CICDecimate(dec=1, order=1) |
| 95 | + data = signals.impulse(nlen) |
| 96 | + cicfilt.run(data) |
| 97 | + |
| 98 | + exp_data = np.concatenate([[0], data[0:-1]]) |
| 99 | + |
| 100 | + assert cicfilt.out.all() == exp_data.all() |
| 101 | + |
| 102 | + |
| 103 | +def test_fir_lowpass_returns_ndarray(): |
| 104 | + """Tests the FIRLowPass output data conversion.""" |
| 105 | + fir = filt.FIRLowPass() |
| 106 | + data = np.random.randn(100).tolist() |
| 107 | + fir._out = data |
| 108 | + |
| 109 | + assert type(fir.out) == type(np.array(list())) |
| 110 | + assert fir.out.all() == np.array(data).all() |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.parametrize("dec", np.random.randint(1, 20, 4)) |
| 114 | +def test_fir_decimate_function(dec): |
| 115 | + """Tests the FIRLowPass decimate function.""" |
| 116 | + fir = filt.FIRLowPass(dec=dec) |
| 117 | + data = np.random.randn(100) |
| 118 | + fir.decimate(data) |
| 119 | + |
| 120 | + exp_result = data[::dec] |
| 121 | + |
| 122 | + assert fir.out.size == exp_result.size |
| 123 | + assert fir.out.all() == exp_result.all() |
| 124 | + |
| 125 | + |
| 126 | +def test_fir_decimate_function_none_input(): |
| 127 | + """Tests the FIRLowPass decimate function with no input arg.""" |
| 128 | + fir = filt.FIRLowPass(dec=1) |
| 129 | + data = np.random.randn(100) |
| 130 | + fir.yfilt = data |
| 131 | + fir.decimate() |
| 132 | + |
| 133 | + exp_result = data |
| 134 | + |
| 135 | + assert fir.out.size == exp_result.size |
| 136 | + assert fir.out.all() == exp_result.all() |
| 137 | + |
| 138 | + |
| 139 | +@mock.patch("adc_eval.filt.remez") |
| 140 | +def test_fir_lowpass_tap_generation(mock_remez, capfd): |
| 141 | + """Tests the FIRLowPass decimate function.""" |
| 142 | + fir = filt.FIRLowPass() |
| 143 | + fir.ntaps = 3 |
| 144 | + fir.bit_depth = 12 |
| 145 | + mock_remez.return_value = np.ones(3) |
| 146 | + |
| 147 | + (taps, coeffs) = fir.generate_taps(0.1) |
| 148 | + |
| 149 | + captured = capfd.readouterr() |
| 150 | + exp_coeffs = [2**12, 2**12, 2**12] |
| 151 | + |
| 152 | + assert "WARNING" in captured.out |
| 153 | + assert taps == 3 |
| 154 | + assert coeffs == exp_coeffs |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.parametrize("ntaps", np.random.randint(3, 511, 5)) |
| 158 | +def test_fir_lowpass_run(ntaps): |
| 159 | + """Tests the FIRLowPass run function.""" |
| 160 | + fir = filt.FIRLowPass() |
| 161 | + fir.ntaps = ntaps |
| 162 | + fir.bit_depth = 10 |
| 163 | + fir.coeffs = 2**10 * np.ones(ntaps) |
| 164 | + data = signals.impulse(2**12) |
| 165 | + fir.run(data) |
| 166 | + |
| 167 | + exp_sum = np.ceil((ntaps + 1) / 2) |
| 168 | + out_sum = sum(fir.out) |
| 169 | + |
| 170 | + tap_val = int(exp_sum) |
| 171 | + |
| 172 | + assert fir.out.size == data.size |
| 173 | + assert max(fir.out) == 1 |
| 174 | + assert min(fir.out) == 0 |
| 175 | + assert fir.out[0:tap_val].all() == 1 |
| 176 | + assert fir.out[tap_val + 1 :].all() == 0 |
| 177 | + assert out_sum == exp_sum |
0 commit comments