1

我在 Xilinx 的 Vitis HLS 中使用 C++ 工作。我正在编写一个简单的缓冲区来执行众所周知的重叠和添加算法(https://www.eetimes.com/fft-convolution-and-the-overlap-add-method/

我的代码创建了一个静态对象(我的缓冲区),然后使用“sample-wise”方法。

我正在使用的代码如下(标题+要合成的函数+测试台):

Overlapper_HLS.hpp:

#include "ap_axi_sdata.h"
#include "hls_stream.h"
#include <ap_fixed.h>


#define IFFT_LENGTH 16
#define INPUT_WINDOW_LENGTH 8
#define BUFFER IFFT_LENGTH - INPUT_WINDOW_LENGTH

typedef ap_fixed<16,1> ap_fixed_data_type;

typedef struct {

    ap_fixed_data_type real_part;
    ap_fixed_data_type imaginary_part;

} my_data_struct;

typedef hls::axis<my_data_struct,0,0,0> pkt_t;

typedef hls::axis<my_data_struct,0,0,0> pkt_t_out;

class Overlapper {

private:
    ap_fixed_data_type IFFT_output_real[IFFT_LENGTH];
    ap_fixed_data_type IFFT_output_imaginary[IFFT_LENGTH];

    ap_fixed_data_type output_chunk_real[INPUT_WINDOW_LENGTH];
    ap_fixed_data_type output_chunk_imaginary[INPUT_WINDOW_LENGTH];

    ap_fixed_data_type buffer_real[BUFFER];
    ap_fixed_data_type buffer_imaginary[BUFFER];

    int counter;

public:
    Overlapper() {

        for(int i = 0; i<IFFT_LENGTH; i++){
            IFFT_output_real[i]= 0;
            IFFT_output_imaginary[i] = 0;
        }

        for(int i = 0; i<INPUT_WINDOW_LENGTH; i++){
            output_chunk_real[i]= 0;
            output_chunk_imaginary[i] = 0;
        }

        for(int i = 0; i<BUFFER; i++){
            buffer_real[i]= 0;
            buffer_imaginary[i] = 0;
        }

        counter = 0;
    }

    void top_function(
        hls::stream<pkt_t> &input_signal_stream,
        hls::stream<pkt_t> &output_signal_stream) {
#pragma HLS PIPELINE
        pkt_t pkt_input_signal;
        pkt_t pkt_output_signal;
        float tmp =0;
        int i = 0;

        if(counter < IFFT_LENGTH){
            i = counter;
            input_signal_stream.read(pkt_input_signal);
            
            IFFT_output_real[i] = pkt_input_signal.data.real_part;
            IFFT_output_imaginary[i] = pkt_input_signal.data.imaginary_part;

            counter++;
            printf("printing counter: %d \n", counter);
            printf("printing i: %d \n", i);

            printf("printing input value: %f \n", pkt_input_signal.data.real_part.to_float());
            printf("printing value of IFFT_real: %f \n", IFFT_output_real[i].to_float());


        }
        else{
            //Buffer does not contain the previously stored values anymore, it is always 0.
            for(int i=0; i<BUFFER; i++){
                printf("printing content of buffer: %f \n", buffer_real[i].to_float());
            }

            for(int i=0; i<BUFFER; i++){
                IFFT_output_real[i] += buffer_real[i];
                IFFT_output_imaginary[i] += buffer_imaginary[i];
                printf("checking buffer value before sum: %f \n", buffer_real[i].to_float());
                printf("checking sum result real: %f \n", IFFT_output_real[i].to_float());
                printf("checking sum result imaginary: %f \n", IFFT_output_imaginary[i].to_float());
            }

            for(int k=INPUT_WINDOW_LENGTH; k<IFFT_LENGTH; k++){
                buffer_real[k-BUFFER] = IFFT_output_real[k];
                buffer_imaginary[k-BUFFER] = IFFT_output_imaginary[k];
                printf("Printing value of buffer: %f \n", buffer_real[k-BUFFER].to_float());

            }
            //Here it seems that the values in Buffer are stored correctly
            for(int i=0; i<BUFFER; i++){
                printf("printing content of buffer: %f \n", buffer_real[i].to_float());
            }

            for(int j=0; j<INPUT_WINDOW_LENGTH; j++){
                pkt_output_signal.data.real_part = IFFT_output_real[j];
                pkt_output_signal.data.imaginary_part = IFFT_output_imaginary[j];
                printf("printing output signal: %f \n", pkt_output_signal.data.real_part.to_float());
                output_signal_stream.write(pkt_output_signal);
            }
            counter = 0;

        }

    }

};

void overlapper(hls::stream<pkt_t> &input_signal_stream, hls::stream<pkt_t> &output_signal_stream);

Overlapper_HLS.cpp:

#include <overlapper_HLS.hpp>

void overlapper(hls::stream<pkt_t> &input_signal_stream, hls::stream<pkt_t> &output_signal_stream){
    #pragma HLS INTERFACE axis register port=input_stream
    #pragma HLS INTERFACE axis register port=output_stream
    #pragma HLS INTERFACE ap_ctrl_none port=return
    static Overlapper bb;
    bb.top_function(input_signal_stream, output_signal_stream);
}

测试台.cpp:

#include <overlapper_HLS.hpp>

int main(){

    hls::stream<pkt_t> input_stream;
    hls::stream<pkt_t> output_stream;

    pkt_t input_data;
    pkt_t temp_output;
    float int_output, input_check;
    // pkt_t input_package_check

    input_data.data.real_part = 0.1;
    input_data.data.imaginary_part = 0.1;

    for(int i=0; i<32; i++){
        input_stream.write(input_data);
    }
    for(int i=0; i<34; i++){
        overlapper(input_stream, output_stream);
    }


    for(int i=0; i<16; i++){
        output_stream.read(temp_output);
        //printf("output_value: %f", temp_output.data.real_part.to_float());
        printf("output_value: %f \n", temp_output.data.real_part.to_float());

    }


    return 0;

}

我的代码按预期工作,我唯一的问题是在top_function()我的Overlapper类内部,buffer_realbuffer_imaginary数组不存储函数的不同调用之间的值。

特别是问题出在以下代码行中:

else{
                //Here unfortunately the stored value is always 0! It seems like the values are "reset" between different calls of the function.
                for(int i=0; i<BUFFER; i++){
                    printf("printing content of buffer: %f \n", buffer_real[i]);
                }
    
                for(int i=0; i<BUFFER; i++){
                    IFFT_output_real[i] += buffer_real[i];
                    IFFT_output_imaginary[i] += buffer_imaginary[i];
                    printf("checking buffer value before sum: %f \n", buffer_real[i].to_float());
                    printf("checking sum result real: %f \n", IFFT_output_real[i].to_float());
                    printf("checking sum result imaginary: %f \n", IFFT_output_imaginary[i].to_float());
                }
    
                for(int k=INPUT_WINDOW_LENGTH; k<IFFT_LENGTH; k++){
                    //Here I am saving the values I need inside my buffer
                    buffer_real[k-BUFFER] = IFFT_output_real[k];
                    buffer_imaginary[k-BUFFER] = IFFT_output_imaginary[k];
                    printf("Printing value of buffer: %f \n", buffer_real[k-BUFFER].to_float());
    
                }
                //Once I assign the values in the previous for cycle, they seem to be stored correctly!
                for(int i=0; i<BUFFER; i++){
                    printf("printing content of buffer: %f \n", buffer_real[i]);
                }
    
                for(int j=0; j<INPUT_WINDOW_LENGTH; j++){
                    pkt_output_signal.data.real_part = IFFT_output_real[j];
                    pkt_output_signal.data.imaginary_part = IFFT_output_imaginary[j];
                    printf("printing output signal: %f \n", pkt_output_signal.data.real_part.to_float());
                    output_signal_stream.write(pkt_output_signal);
                }
                counter = 0;
    
            }

我无法弄清楚为什么我的buffer_realbuffer_imaginary似乎在不同的调用之间被重置,即使我创建的对象是静态的(实际上变量计数器在我的不同调用之间保留top_function()

编辑:我正在添加我的输出日志

#### FIRST ROUND ######
printing counter: 1 
printing i: 0 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 2 
printing i: 1 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 3 
printing i: 2 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 4 
printing i: 3 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 5 
printing i: 4 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 6 
printing i: 5 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 7 
printing i: 6 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 8 
printing i: 7 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 9 
printing i: 8 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 10 
printing i: 9 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 11 
printing i: 10 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 12 
printing i: 11 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 13 
printing i: 12 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 14 
printing i: 13 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 15 
printing i: 14 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 16 
printing i: 15 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing output signal: 0.099976 
printing output signal: 0.099976 
printing output signal: 0.099976 
printing output signal: 0.099976 
printing output signal: 0.099976 
printing output signal: 0.099976 
printing output signal: 0.099976 
printing output signal: 0.099976 

### SECOND ROUND ### 
printing counter: 1 
printing i: 0 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 2 
printing i: 1 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 3 
printing i: 2 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 4 
printing i: 3 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 5 
printing i: 4 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 6 
printing i: 5 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 7 
printing i: 6 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 8 
printing i: 7 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 9 
printing i: 8 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 10 
printing i: 9 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 11 
printing i: 10 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 12 
printing i: 11 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 13 
printing i: 12 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 14 
printing i: 13 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 15 
printing i: 14 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing counter: 16 
printing i: 15 
printing input value: 0.099976 
printing value of IFFT_real: 0.099976 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
printing first content of buffer: 0.000000 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
checking buffer value before sum: 0.000000 
checking sum result real: 0.099976 
checking sum result imaginary: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
Printing value of buffer after saving values: 0.099976 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing second content of buffer: 0.000000 
printing output signal: 0.099976 
printing output signal: 0.099976 
printing output signal: 0.099976 
printing output signal: 0.099976 
printing output signal: 0.099976 
printing output signal: 0.099976 
printing output signal: 0.099976 
printing output signal: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 
output_value: 0.099976 

您可能会注意到我的缓冲区始终为 0 值(printing second content of buffer: 0.000000),但是在我保存值的上一个周期中,它们似乎存储正确(Printing value of buffer after saving values: 0.099976)。

基本上在打印缓冲区内容的第二“轮”中,它应该0.099976代替0

4

1 回答 1

2

#define BUFFER IFFT_LENGTH - INPUT_WINDOW_LENGTH主要问题是在以下代码片段的索引中使用了 my :

 for(int k=INPUT_WINDOW_LENGTH; k<IFFT_LENGTH; k++){
                    //Here I am saving the values I need inside my buffer
                    buffer_real[k-BUFFER] = IFFT_output_real[k];
                    buffer_imaginary[k-BUFFER] = IFFT_output_imaginary[k];
                    printf("Printing value of buffer: %f \n", buffer_real[k-BUFFER].to_float());
    
                }

它弄乱了索引,因为它按原样替换,所以我的索引变为BUFFER,它仍然是 0 ,但可能会产生一些问题,因为如果按顺序添加数字会变为负数。16 - 88 - 16 + 8

更改那行代码以buffer_real[k-(BUFFER)] = IFFT_output_real[k];解决我的问题。

更好的解决方案是使用constexpr而不是定义:

#define IFFT_LENGTH 16
#define INPUT_WINDOW_LENGTH 8
constexpr int BUFFER=IFFT_LENGTH - INPUT_WINDOW_LENGTH;
于 2021-07-01T13:58:16.433 回答