如何将使用numpy.fft.rfft的代码从python移植到C ++? [英] How can I port code that uses numpy.fft.rfft from python to C++?

查看:380
本文介绍了如何将使用numpy.fft.rfft的代码从python移植到C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用python编写的代码.它使用numpy计算实际输入的FFT的正数.我需要将此代码移植到C ++.

I have code written in python. It computes positive part of FFT of real input using numpy. I need to port this code to C++.

import numpy as np
interp=[131.107, 133.089, 132.199, 129.905, 132.977]
res=np.fft.rfft(interp)
print res

rfft的结果为[659.27700000 + 0.j,1.27932533-1.4548977j,-3.15032533 + 2.1158917j]

Result of rfft is [ 659.27700000+0.j, 1.27932533-1.4548977j, -3.15032533+2.1158917j]

我尝试将OpenCV用于一维DFT:

I tried to use OpenCV for 1D DFT:

std::vector<double> fft;
std::vector<double> interpolated = {131.107, 133.089, 132.199, 129.905, 132.977};
cv::dft( interpolated, fft );
for( auto it = fft.begin(); it != fft.end(); ++it ) {
    std::cout << *it << ' ';
}
std::cout << std::endl;

cv :: dft的结果为{1.42109e-14,-127.718,-94.705,6.26856,23.0231}.它与numpy.fft.rfft有很大不同.计算OpenCV的dft后,所有输入的DC值(零元素)都接近零,这看起来很奇怪.

Result of cv::dft is {1.42109e-14, -127.718, -94.705, 6.26856, 23.0231}. It is much different from numpy.fft.rfft. It looks strange that DC value (zero element) is near zero on all inputs after OpenCV's dft computed.

使用FFTW3库可以得到与OpenCV相同的结果:

Usage of FFTW3 library gave me the same results as OpenCV's results:

std::vector<double> interpolated = {131.107, 133.089, 132.199, 129.905, 132.977};
fftw_complex* out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * 3 );
fftw_plan plan = fftw_plan_dft_r2c_1d( interpolated.size( ), interpolated.data( ), out, FFTW_ESTIMATE );
fftw_execute(plan);
fftw_destroy_plan(plan);
for( size_t i = 0; i < interpolated.size( ); ++i ) {
    std::cout << " (" << out[ i ][ 0 ] << ", " << out[ i ][ 1 ] << ")";
}
fftw_free(out);

这段代码为我提供了与OpenCV相同的结果.它打印:(1.42109e-14,0)(-127.718,-94.705)(6.26856,23.0231).

This code gives me the same results as OpenCV. It prints: (1.42109e-14, 0) (-127.718, -94.705) (6.26856, 23.0231).

为什么在C ++和python中得到不同的dft结果?我在做什么错了?

Why do I get different results of dft in C++ and in python? What am I doing wrong?

谢谢!

推荐答案

目前,我正在使用gcc 4.6,它没有C ++ 11,所以我使用OpenCV 2.4尝试了此版本的代码. 8:

I'm using gcc 4.6 at the moment, which doesn't have C++11, so I tried this version of your code, using OpenCV 2.4.8:

#include <iostream>
#include "opencv2/core/core.hpp"

int main(int argc, char *argv[])
{
    double data[] = {131.107, 133.089, 132.199, 129.905, 132.977};
    std::vector<double> interpolated (data, data + sizeof(data) / sizeof(double));
    std::vector<double> fft;

    cv::dft(interpolated, fft);

    for (std::vector<double>::const_iterator it = fft.begin(); it != fft.end(); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << std::endl;
}

输出

659.277 1.27933 -1.4549 -3.15033 2.11589

同意numpy和cv2 python模块:

agrees with numpy and with the cv2 python module:

In [55]: np.set_printoptions(precision=3)

In [56]: x
Out[56]: array([ 131.107,  133.089,  132.199,  129.905,  132.977])

In [57]: np.fft.rfft(x)
Out[57]: array([ 659.277+0.j   ,    1.279-1.455j,   -3.150+2.116j])

In [58]: cv2.dft(x)
Out[58]: 
array([[ 659.277],
       [   1.279],
       [  -1.455],
       [  -3.15 ],
       [   2.116]])

我不知道为什么您的代码无法正常工作,所以我想这比答案要长得多.

I don't know why your code is not working, so I guess this is more of a long comment than an answer.

这篇关于如何将使用numpy.fft.rfft的代码从python移植到C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆