c++ - 反卷积 C++

标签 c++ visual-studio-2010 visual-c++ math complex-numbers

我从 http://rosettacode.org/wiki/Deconvolution/1D#C 获得了一维反卷积的示例代码,而且它似乎无法正常工作。尝试构建此项目时,我首先收到此错误“_fft”:非法使用类型“void”。如果有帮助,这段代码最初是为 C 编写的,我必须进行一些更改,例如将 include 从 complex.h 更改为 complex,并使用命名空间 std。感谢您的帮助

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex>
using namespace std;

double PI;
complex<double> cplx;




void _fft(cplx buf[], cplx out[], int n, int step)
 {
if (step < n) {
    _fft(out, buf, n, step * 2);
    _fft(out + step, buf + step, n, step * 2);

    for (int i = 0; i < n; i += 2 * step) {
        cplx t = cexp(-I * PI * i / n) * out[i + step];
        buf[i / 2]     = out[i] + t;
        buf[(i + n)/2] = out[i] - t;
    }
}
}

void fft(cplx buf[], int n)
{
cplx out[n];
for (int i = 0; i < n; i++) out[i] = buf[i];
_fft(buf, out, n, 1);
}

cplx *pad_two(double g[], int len, int *ns)
{
int n = 1;
if (*ns) n = *ns;
else while (n < len) n *= 2;

cplx *buf = calloc(sizeof(cplx), n);
for (int i = 0; i < len; i++) buf[i] = g[i];
*ns = n;
return buf;
}

void deconv(double g[], int lg, double f[], int lf, double out[]) {
int ns = 0;
cplx *g2 = pad_two(g, lg, &ns);
cplx *f2 = pad_two(f, lf, &ns);

fft(g2, ns);
fft(f2, ns);

cplx h[ns];
for (int i = 0; i < ns; i++) h[i] = g2[i] / f2[i];
fft(h, ns);

for (int i = 0; i >= lf - lg; i--)
    out[-i] = h[(i + ns) % ns]/32;
free(g2);
free(f2);
}

int main()
{
PI = atan2(1,1) * 4;
double g[] = {24,75,71,-34,3,22,-45,23,245,25,52,25,-67,-96,96,31,55,36,29,-43,-7};
double f[] = { -3,-6,-1,8,-6,3,-1,-9,-9,3,-2,5,2,-2,-7,-1 };
double h[] = { -8,-9,-3,-1,-6,7 };

int lg = sizeof(g)/sizeof(double);
int lf = sizeof(f)/sizeof(double);
int lh = sizeof(h)/sizeof(double);

double h2[lh];
double f2[lf];

printf("f[] data is : ");
for (int i = 0; i < lf; i++) printf(" %g", f[i]);
printf("\n");

printf("deconv(g, h): ");
deconv(g, lg, h, lh, f2);
for (int i = 0; i < lf; i++) printf(" %g", f2[i]);
printf("\n");

printf("h[] data is : ");
for (int i = 0; i < lh; i++) printf(" %g", h[i]);
printf("\n");

printf("deconv(g, f): ");
deconv(g, lg, f, lf, h2);
for (int i = 0; i < lh; i++) printf(" %g", h2[i]);
printf("\n");
}

最佳答案

如果不运行它,cplx 将被用作一个类,而实际上它是一个变量。你能改变这一行吗:

complex<double> cplx;

像这样

typedef complex<double> cplx;

关于c++ - 反卷积 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13434859/

相关文章:

c++ - 使用命名空间与使用命名空间闭包的范围

c++ - 为什么我不能在我的程序中声明一个字符串 : "string is undeclared identifier"

c++ - 什么是 Vb.net 等同于 C++ 中的 1.0e-5?

C++ typeid 是否返回字符串类型?

c++ - std::vector 在返回之前被自动清除

c++ - 将reinterpret_cast 转换为较小的数组是否安全?有更好的选择吗?

c++ - 如何在 Visual Studio 2010 中使用 SWIG 创建 DLL

c++ - 检索 x64 masm 汇编过程的参数

c++ - 预编译头文件的主要用途

c++ - 制作按钮并处理它们