======================= RFI Blanking Test ======================= Purpose ======= This test checks the RFI detection & blanking functionality by confirming that the correct number of frames in the raw station beam output are blanked when a step function input is applied at the ADC with RFI blanking enabled. **Test:** * Applies a step function (constant amplitude of 63 units) to ADC inputs using the in-built ADC test pattern generator * Verifies that blanked samples appear as **-128 + -128j** in complex representation * Verifies that the raw station beam output contains the expected number of blanked samples (2272-2274 frames) * Validates that the RFI counters (``rfi_counts``) match the expected number of RFI-affected ADC frames (2251-2253 frames) **Test Output:** The plot below shows the station beam output in channel 1 polarisation 0 produced as a result of the step function applied in the test. It is seen to contain a continuous fixed pulse of blanked samples as expected. .. image:: images/rfi_blanking_test2_pol0_ch1.png **NOTE:** This test currently supports only 1 TPM and 1 antenna. Methodology =========== 1. Connect to station and configure initial parameters. 2. Configure and enable RFI blanking. 3. Configure the Network addresses & DAQ. 4. Configure ADC in-built pattern generator to output 0s and clear broadband RFI counters. 5. Capture raw station beam data while the step is applied using the ADC in-built pattern generator. 6. Evaluate: Step ADC input should result in a fixed number of blanked station beam samples within expected range. 7. Validate: RFI counter value should match expected number of RFI-affected ADC frames. Understanding the Expected RFI Behaviour ======================================== Before the step signal is applied, both the long-term and short-term averages are zero. When the step occurs, the short-term average rises above the long-term average, causing the RFI flag to be asserted for that frame. The flag stays high until the long-term average approaches the short-term average after a certain number of frames. This number is derived mathematically in the next section to be approximately 2252.85 frames (when RFI factor = 1). Because deassertion happens only at frame boundaries, this results in **2253 RFI-flagged ADC frames** when the step aligns exactly with a frame boundary. In practice, several timing factors introduce small variations: * Short-term average updates every 2 frames. * Long-term average updates every 1 frame. * The step may occur anywhere within a 216-sample frame. * Convergence (2252.85 frames) rarely aligns exactly with a frame boundary Because of these factors, the expected number of flagged ADC frames falls within **2251-2253**. The RFI counter exposed by the TPM API (`rfi_counts`) is expected to fall in this range. This range is further validated by feeding a step function as input into the `Python model of the RFI detection module in firmware `_. Each time-domain ADC frame affects 22 frames in the frequency-domain (see `RFI Blanking `_). Therefore, 2253 ADC frames correspond to 2253 + 21 = 2274 station beam samples being blanked. Allowing for the variation described above, the expected range of station beam blanked is **2272-2274**. Step-Response Analysis ====================== The RFI detection algorithm implemented in firmware is explained in detail at `RFI Detection Module Documentation `_. In order to determine the number of RFI frames that will be blanked in test case # 2, we need to do a step response analysis for long-term and short-term averages. When a step function of constant amplitude *A* is applied as the ADC input, the the corresponding instantaneous power is: .. math:: P[n] = 4A^{2} Long-Term Accumulator --------------------- The firmware module calculates the running long-term sum at every time sample: .. math:: S_{\text{acc}}[n] = S_{\text{acc}}[n-1] + P[n] - \frac{S_{\text{acc}}[n-1]}{D}, where .. math:: D = \text{g_long_avg} \times \text{num_samples_in_frame} \times \text{oversampling_factor} = 1024 \times 216 \times \frac{32}{27} = 262144 The long-term cumulative sum equation matches the first-order IIR filter form: .. math:: y[n] = a_1\,y[n-1] + b_0\,x[n]. where .. math:: a_1 = 1 - \frac{1}{D} and .. math:: b_0 = 1 Step Response From the IIR Form ------------------------------- According to the first-order IIR derivation from `this source at UC Santa Barbara `_, the step response is .. math:: y[n] = b_0 \times \frac{1 - {a_1}^{\,n+1}}{1 - a_1} Applying this to the accumulator and substituting the values of :math:`a_1` and :math:`b_0` gives .. math:: S_{\text{acc}}[n] = 4A^{2}D \left(1 - a_1^{\,n+1}\right) Long-Term Average ----------------- The long-term average is computed in the RFI detection module as .. math:: S_{\text{long}}[n] = \frac{S_{\text{acc}}[n]}{1024/2} = \frac{S_{\text{acc}}[n]}{512} = 512 \,(4A^{2}) \left(1 - a_1^{\,n+1}\right) Short-Term Average ------------------ The short-term average integrates over ``2 * 216 = 432`` samples, which yields a constant value after the step: .. math:: S_{\text{short}} = 432\,(4A^{2}) Solving for Convergence ----------------------- RFI is asserted when the short-term average power exceeds the long-term average power times the RFI factor. The number of samples it takes for RFI to be deasserted after the step is determined by solving for the convergence: .. math:: S_{\text{short}} = S_{\text{long}}[n] \times {\text{RFI factor}} Substitution produces .. math:: 432\,(4A^{2}) = 512\,(4A^{2})\,(1 - a_1^{\,n+1}) \times {\text{RFI factor}} leading to .. math:: a_1^{\,n+1} = 1 - \frac{432\,(4A^{2})}{512\,(4A^{2}) \times {\text{RFI factor}}} Cancelling :math:`4A^{2}` and taking log on both sides: .. math:: (n+1)\,\log(a_1) = \log\!\left( 1 - \frac{432}{512 \times \text{RFI factor}} \right) Solving for `n`: .. math:: n = \frac{\log\!\left( 1 - \frac{432}{512 \times \text{RFI factor}} \right)}{\log(a_1)} - 1 Substituting :math:`a_1` and evaluating: .. math:: n \approx 486615.45 \text{ time-multiplexed samples} Since each ADC frame contains 216 time-multiplexed samples, the corresponding 'RFI counts' in terms of frames is given by: .. math:: \text{RFI counts} = \frac{486615.45}{216} \approx 2252.85 \text{ frames} Thus, the long-term average rises up and meets the short-term average at approximately **2253 frames** after the step and the RFI flag gets deasserted after this. The plot below shows how the long-term and short-term average change in response to a step input applied at frame 4097. The convergence occurs at frame 6349. .. image:: images/powers_plot.png