TorchDSP / torchsig

TorchSig is an open-source signal processing machine learning toolkit based on the PyTorch data handling pipeline.
MIT License
174 stars 40 forks source link

iq_imbalance: dc offset is not calculated properly #229

Closed nalexandros closed 1 week ago

nalexandros commented 9 months ago

Describe the bug IQ Imbalance considers that the hardware has non-idealities and that the in-phase and quadrature are not perfectly aligned. As a result, the IQ data will have slightly different amplitude, phase and dc-offset.

Problem description: By setting to zero the amplitude_imbalance_db and the phase_imbalance we can isolate the effect of the dc_offset_db. The expectation is that the constellation diagram will be moved to a different location, while keeping the scaling, rotation and shape. However, this is not the case as is shown in iq_imbalance.png. Instead the data are scaled in a similar manner as with amplitude imbalance.

Solution: The function iq_imbalance_fixed modifies a couple of lines. The resulting plot (iq_imbalance.png) shows that the IQ data have been shifted as expected.

To Reproduce to_torchsig_debug_iq_imbalance.zip

Use the Python code and the *.npy file to reproduce the bug and get the bugfix.

Expected behavior The IQ data must be shifted, not scaled, as seen in the picture.

def iq_imbalance_fixed(
    iq: np.ndarray,
    amplitude_imbalance_db: float,
    phase_imbalance: float,
    dc_offset_db: float,
) -> np.ndarray:
    amplitude_imbalance_linear = 10 ** (amplitude_imbalance_db / 10.0)
    iq_transformed = (amplitude_imbalance_linear * iq.real + 1j * amplitude_imbalance_linear * iq.imag)

    iq_transformed = (np.exp(-1j * phase_imbalance / 2.0) * iq_transformed.real +
                      np.exp(1j * (np.pi / 2.0 + phase_imbalance / 2.0)) * iq_transformed.imag)

    dc_offset_linear = 10 ** (dc_offset_db / 10.0)
    dc_offset_linear_complex = dc_offset_linear + 1j * dc_offset_linear
    iq_transformed += dc_offset_linear_complex
    return iq_transformed
ereoh commented 2 weeks ago

Hello! Just letting you know that we are actively working on this fix for the v0.6.1 release in a few weeks. Thanks for bringing this issue up to our attention.

MattCarrickPL commented 1 week ago

@nalexandros We finalized the code for the IQ imbalance fix that will be released as part of 0.6.1, so I'm closing this out for now. If there are additional problems in the future we are more than happy to take another look at it.