diff --git a/tangostationcontrol/tangostationcontrol/common/calibration.py b/tangostationcontrol/tangostationcontrol/common/calibration.py index 37d44304dd02387eec954f221def980b3971bd41..3c9370b4f9b1a2871445c14b6436ef009c19fb29 100644 --- a/tangostationcontrol/tangostationcontrol/common/calibration.py +++ b/tangostationcontrol/tangostationcontrol/common/calibration.py @@ -238,8 +238,8 @@ def delay_compensation(delays_seconds: numpy.ndarray, clock: int): # chain, while input_* are the amount of (negative) delay to apply # to compensate. - # compute the coarse correction, in samples - signal_delays_samples = numpy.round(delays_seconds * clock).astype(numpy.uint32) + # compute the correction, in samples + signal_delays_samples = delays_seconds * clock # correct for the coarse delay by delaying the other signals to line up # we cannot configure a negative number of samples, so we must delay @@ -247,13 +247,11 @@ def delay_compensation(delays_seconds: numpy.ndarray, clock: int): # # This introduces a constant shift in timing for all samples, # as we shift all of them to obtain a non-negative delay. - input_delays_samples = max(signal_delays_samples) - signal_delays_samples - - # compute the remainder, in seconds - signal_delays_subsample_seconds = delays_seconds - signal_delays_samples / clock - input_delays_subsample_seconds = -signal_delays_subsample_seconds + input_delays_samples = numpy.round( + max(signal_delays_samples) - signal_delays_samples + ) - return input_delays_samples, input_delays_subsample_seconds + return input_delays_samples def calibrate_input_samples_delay( @@ -273,7 +271,7 @@ def calibrate_input_samples_delay( # compute the required compensation clock = sdpfirmware.clock_RW - input_samples_delay, _ = delay_compensation(signal_delay_seconds, clock) + input_samples_delay = delay_compensation(signal_delay_seconds, clock) # read-modify-write on [fpga][(input, polarisation)] fpga_signal_input_samples_delay = sdp.FPGA_signal_input_samples_delay_RW @@ -341,8 +339,6 @@ def loss_compensation(losses_dB: numpy.ndarray): signal_attenuation_integer_dB = numpy.round(losses_dB).astype(numpy.uint32) # correct for the coarse loss by dampening the signals to line up. - input_attenuation_integer_dB = numpy.round( - numpy.max(losses_dB) - losses_dB - ) + input_attenuation_integer_dB = numpy.round(numpy.max(losses_dB) - losses_dB) return input_attenuation_integer_dB diff --git a/tangostationcontrol/test/common/test_calibration.py b/tangostationcontrol/test/common/test_calibration.py index d96cf07a3b8dea1bf1fbc207fbe483cc683ebc17..36f789a49ec5c26a2b3596b40cd298fe44b358a1 100644 --- a/tangostationcontrol/test/common/test_calibration.py +++ b/tangostationcontrol/test/common/test_calibration.py @@ -239,24 +239,13 @@ class TestDelayCompensation(base.TestCase): # compute delay compensation return delay_compensation(delays_seconds, clock) - def test_whole_sample_shifts_no_remainder(self): - """Test whether delay compensation indeed has no remainder if we shift whole samples.""" - - # delay to compensate for, in samples - delay_samples = [1, 2, 3, 4] - - _, remainder_seconds = self._compute_delay_compensation(delay_samples) - - # verify that there is no remainder - self.assertTrue(numpy.all(remainder_seconds == 0.0), msg=f"{remainder_seconds}") - def test_sample_shifts_line_up(self): """Test whether signals line up after the computed delay compensation.""" # delay to compensate for, in samples delay_samples = [1, 2, 3, 4] - sample_shift, _ = self._compute_delay_compensation(delay_samples) + sample_shift = self._compute_delay_compensation(delay_samples) # sample_shift and delay_samples together should line everything up effective_signal_delay = delay_samples + sample_shift @@ -272,19 +261,23 @@ class TestDelayCompensation(base.TestCase): """Test correctness of the delay compensation remainders.""" # delays in samples we want to compensate for. they all round to the same sample - delay_samples = [0.75, 1.0, 1.25] + delay_samples = [0.76, 1.0, 1.25] - sample_shift, remainder_seconds = self._compute_delay_compensation( - delay_samples - ) + sample_shift = self._compute_delay_compensation(delay_samples) # should not result in any sample shifts self.assertEqual(0, sample_shift[0]) self.assertEqual(0, sample_shift[1]) self.assertEqual(0, sample_shift[2]) - # remainder should correspond with differences. - # NB: these are the remainders to apply to line up the signals. - self.assertAlmostEqual(+0.25, remainder_seconds[0] / 5e-9) - self.assertAlmostEqual(0.00, remainder_seconds[1] / 5e-9) - self.assertAlmostEqual(-0.25, remainder_seconds[2] / 5e-9) + def test_delay_round_nearest(self): + """Test correctness of the delay compensation rounding.""" + + # delays in samples we want to compensate for. they all round to the same sample + delay_samples = [0.0, 1.6] + + sample_shift = self._compute_delay_compensation(delay_samples) + + # should not result in any sample shifts + self.assertEqual(2, sample_shift[0]) + self.assertEqual(0, sample_shift[1])