Skip to content

Fix: Correct the order for complex values real, imag -> imag, real (AEGHB-1452)#252

Open
James-Rhodes wants to merge 1 commit intoespressif:masterfrom
James-Rhodes:bugfix/complex-value-ordering
Open

Fix: Correct the order for complex values real, imag -> imag, real (AEGHB-1452)#252
James-Rhodes wants to merge 1 commit intoespressif:masterfrom
James-Rhodes:bugfix/complex-value-ordering

Conversation

@James-Rhodes
Copy link

This is inline with the documentation online at:
https://docs.espressif.com/projects/esp-idf/en/stable/esp32c5/api-guides/wifi.html#wi-fi-channel-state-information

Description

This PR fixes the accidental swap of the real and imaginary components for the ESP Crab module. According to the documentation at https://docs.espressif.com/projects/esp-idf/en/stable/esp32c5/api-guides/wifi.html#wi-fi-channel-state-information the imaginary component should come first, followed by the real component. The previous code implementation did the opposite, getting the real and then the imaginary. This PR simply swaps the order to match the documentation and avoid further confusion.

This change was discussed along with several other issues within #246

I believe this bug may also exist within the console_test application specifically here:

def csi_data_handle(self, data):
    # Rotate data
    g_csi_amplitude_array[:-1] = g_csi_amplitude_array[1:]
    g_rssi_array[:-1] = g_rssi_array[1:]
    g_radio_header_pd.iloc[1:] = g_radio_header_pd.iloc[:-1]

    csi_raw_data = data['data']
    data_len = int(data['len']) if 'len' in data else len(csi_raw_data)

    if data_len == 104:
        for i in range(CSI_DATA_COLUMNS):
            idx = csi_vaid_subcarrier_index[i]

            if idx * 4 + 3 >= len(csi_raw_data):
                return
            real_high = csi_raw_data[idx * 4 + 1]
            imag_high = csi_raw_data[idx * 4 + 3]
            real_low = csi_raw_data[idx * 4] & 0xFF
            imag_low = csi_raw_data[idx * 4 + 2] & 0xFF

            real = (((int(real_high) << 12) >> 4) | real_low)
            imag = (((int(imag_high) << 12) >> 4) | imag_low)
            if real > 32767:
                real = real - 65536
            if imag > 32767:
                imag = imag - 65536

            g_csi_amplitude_array[-1][i] = np.abs(complex(real, imag))
    else:
        for i in range(CSI_DATA_COLUMNS):
            if csi_vaid_subcarrier_index[i]*2+1 >= len(csi_raw_data):
                return
            g_csi_amplitude_array[-1][i] = np.abs(complex(csi_raw_data[csi_vaid_subcarrier_index[i]*2], csi_raw_data[csi_vaid_subcarrier_index[i]*2+1]))

    g_rssi_array[-1] = data['rssi']
    g_radio_header_pd.loc[0] = data[1:len(CSI_DATA_COLUMNS_NAMES)-1]
    # print(g_radio_header_pd.loc[0])

    return

However, I do not use this, so could not test it to double check. If I am right and this is a bug. Let me know and I will fix it.

Related

Testing

I have run the application locally on my own board and verified it still works. This change will not change the results as it is only shifting the phase by 90 degrees. However, it makes it consistent with the documentation and avoids future confusion.


Checklist

Before submitting a Pull Request, please ensure the following:

  • 🚨 This PR does not introduce breaking changes.
  • All CI checks (GH Actions) pass.
  • Documentation is updated as needed.
  • Tests are updated or added as necessary.
  • Code is well-commented, especially in complex areas.
  • Git history is clean — commits are squashed to the minimum necessary.

@github-actions github-actions bot changed the title Fix: Correct the order for complex values real, imag -> imag, real Fix: Correct the order for complex values real, imag -> imag, real (AEGHB-1452) Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant