// 秒寻科技
// 交叠FFT模块
// zt
// 2023-03-04

#include "../../includes/includes.h"

// FFT交叠
// pstOverlapData:交叠数据
// pfSignalReal:返回参数，信号实部
// pfSignalImag:返回参数，信号虚部
// wLen:信号长度，一帧FFT数据
uint8 dmdl_overlap(DMDL_OVERLAP_DATA_T *pstOverlapData, flt32 *pfSignalReal, flt32 *pfSignalImag, uint16 wLen)
{
    uint16 wStartIndex = DMDL_FFT_FRAME_PAYLOAD_LEN; // 后向交叠区开始索引

    // 参数容错
    if (pstOverlapData == NULL || pfSignalReal == NULL || pfSignalImag == NULL || wLen != PUB_FFT_FRAME_LEN)
    {
        return RET_FAIL;
    }

    // 计算前向交叠(当前帧信号前向交叠区与前一帧信号后向交叠区线性相加)
    for (uint16 i = 0; i < DMDL_FFT_OVERLAP_LEN; i++)
    {
        pfSignalReal[i] += pstOverlapData->afOverlapReal[i];
        pfSignalImag[i] += pstOverlapData->afOverlapImag[i];
    }

    // 保存当前帧信号的后向交叠区
    for (uint16 i = 0; i < DMDL_FFT_OVERLAP_LEN; i++)
    {
        pstOverlapData->afOverlapReal[i] = pfSignalReal[wStartIndex + i];
        pstOverlapData->afOverlapImag[i] = pfSignalImag[wStartIndex + i];
    }

    return RET_OK;
}
