Skip to content

dsp_matrix_transpose source code produces incorrect result #230

@VoyageAudio

Description

@VoyageAudio

I've encountered an issue with the dsp_matrix_transpose function in the XMOS DSP Library while trying to get matrix multiplication to work. The function contains an indexing error that results in incorrect transposed matrices.

This may not have been noticed if tests on this function were only done with square matrices.

Details:

Function Affected: dsp_matrix_transpose
Location: dsp_matrix.c
Issue: Incorrect indexing when writing to the result_matrix_R array.

Problematic Code:

void dsp_matrix_transpose
(
    const int32_t* input_matrix_X,
    int32_t*       result_matrix_R,
    int32_t        row_count,
    int32_t        column_count,
    const int32_t  q_format
) {
    for (int32_t r = 0; r < row_count; ++r) {
        for (int32_t c = 0; c < column_count; ++c) {
            // Original indexing (incorrect)
            result_matrix_R[c * column_count + r] = input_matrix_X[r * column_count + c];
        }
    }
}

Explanation:

The indexing c * column_count + r used for result_matrix_R is incorrect for the transposed matrix dimensions.
This indexing assumes that both the input and output matrices have the same dimensions, which is not always the case.

Corrected Code:

void dsp_matrix_transpose
(
    const int32_t* input_matrix_X,
    int32_t*       result_matrix_R,
    int32_t        row_count,
    int32_t        column_count,
    const int32_t  q_format
) {
    for (int32_t r = 0; r < row_count; ++r) {
        for (int32_t c = 0; c < column_count; ++c) {
            // Corrected indexing
            result_matrix_R[c * row_count + r] = input_matrix_X[r * column_count + c];
        }
    }
}

The corrected indexing c * row_count + r accounts for the fact that the transposed matrix has dimensions [column_count][row_count].
This ensures that the element at position[r][c]in the input matrix is correctly placed at position [c][r] in the transposed matrix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions