简体   繁体   中英

After processing numpy arrays with c++ Eigen library the output is wrong

I need to pass numpy array to c++ function, process it and return the another numpy array.

Right now I've figured out how to pass and return the array, but some step of processing the results just not right.

double * process(double *input, int length) {
    Map<Matrix<double, 1, Dynamic>> map(input, length);
    MatrixXd frame(map);
    ...
    RowVectorXd b(7);
    b << 6.46597871e-01,   0.00000000e+00,  -1.93979361e+00,
             1.66081634e-16,   1.93979361e+00,   0.00000000e+00,
            -6.46597871e-01;
    RowVectorXd a(7);
    a <<  1.        , -0.76984955, -1.87432239,  0.90522394,  1.47938621,
               -0.32233495, -0.41804178;

    RowVectorXd temp1 = frame.row(0);
    RowVectorXd temp2 = frame.row(1);
    RowVectorXd first = filter(temp1, a, b);
    ...

}

RowVectorXd filter(RowVectorXd &input, RowVectorXd &alpha, RowVectorXd &beta) {
    float a0 = alpha(0);
    int order = beta.cols();
    RowVectorXd a = alpha.segment(1, order - 1).reverse() / a0;
    RowVectorXd b = beta.reverse() / a0;
    int size = input.size();
    RowVectorXd output(size + (order - 2) * 2);

    RowVectorXd joined = addZeros(input, order - 1);
    size = joined.cols() - (order - 2);

    for (int k = order; k < size ; k++) {
        RowVectorXd x = joined.segment(k - order, order);
        RowVectorXd y = output.segment(k - order, order - 1);
        double temp = b.dot(x) - a.dot(y);
        cout << " " << temp;
        output(k - 1) = b.dot(x) - a.dot(y);
    }
    return output.segment(order - 1, input.cols());
}

The filtering function is already tested. And the process function is too. So if I'm calling them from c++ everything is alright, but when I'm calling them from python it's not.

Step by step I've checked everything and figured out that the filtering function returning wrong results, when processing function called from python.

So for example if first four items of output vector should be -2.37, -3.184, 5.00 etc the real output is 8.68251e+175 2.49091e+175 1.13384e+176 7.03121e+175 Some ridiculously high numbers.

I've tried to remove all optimizations, that in my opinion make this error happen (like -ffast-math O3) but the output of filtering is still wrong.

So that's debug compiling options

-std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -fPIC

And that's release compiling options

-std=c++0x -O3 -Wall -c -fmessage-length=0 -march=core2 -ffast-math -fPIC

I suppose that the problem could be with memory aligning or something like that when the dot products is calculating, but that's just a thought...

Can someone point to me what is the problem here?

好的,问题出在未初始化的向量输出上,从c ++调用时包含零,从python调用时包含随机的东西。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM