2012-11-17 91 views
3

我从http://rosettacode.org/wiki/Deconvolution/1D#C得到了一维解卷积的代码示例,它似乎并不正确。当试图构建这个项目时,我首先得到这个错误'_fft':非法使用'void'类型。如果有帮助的话,这段代码最初是用于C的,我不得不做一些改变,比如将include从complex.h改为复杂的,并且使用namespace std。感谢您的帮助解卷积C++

#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"); 
} 

回答

2

没有运行这个,cplx被用作一个类,当它实际上是一个变量。你可以改变这一行:

complex<double> cplx; 

这样

typedef complex<double> cplx; 
+0

虽然会有更多的错误,我怀疑http://codepad.org/T5xqEcwU – Caribou

+0

我发现同样的事情。看起来这是一个额外的破坏。我以某种方式怀疑这种编译在任何环境中。 – marko

+0

就像我说的,我发现它在线,并且无法验证它是否曾在任何环境下编译过,但我改为按照建议的typedef,但是然后cexp函数不起作用。有什么想法吗?感谢您的帮助 –