gpt4 book ai didi

c - 真实数据序列上的 FFTW

转载 作者:行者123 更新时间:2023-11-30 15:32:36 25 4
gpt4 key购买 nike

我正在读取一个原始声音文件,并尝试对其运行 fft,目的是在最后获取 PSD,但我在开始时收到了一个错误,我可以'不太明白,希望在这里得到一些帮助,代码是:

#include <stdio.h>
#include <fftw3.h>

int main(){
char* fileName = "sound.raw";
FILE* inp = NULL;
double* data = NULL;
int index = 0;
fftw_plan plan;
fftw_complex* out;
double r,i;
int N = 8192;

//Allocating the memory for the input data

data = (double*) fftw_malloc(sizeof(double)*N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*N);
plan = fftw_plan_dft_r2c_1d(N,data,out,FFTW_FORWARD);
// opening the file for reading
inp = fopen(fileName,"rb");
if(inp== NULL){
printf(" couldn't open the file \n ");
return -1;
}
while(!feof(inp)){
printf( " index %d",index); // just get where the program crashs
if(index < N){
fread(&data[index],sizeof(short),1,inp);
index = index +1;
}
else{
index = 0;
fftw_execute(plan);
printf("New Plan \n");
printf(" Real \t imag \t Magn \t \n");
for(index = 0 ; index<N; index++){
r=out[index][0];
i =out[index][1];
printf("%lf \t %lf \t %lf \t \n",r,i,index);
}
index = 0 ;
}
}
return 0 ;
}

index = 8106时程序崩溃,并且我确信该文件包含更多数据。我得到的错误是:

Segmentation fault (core dumped )

我知道该错误与尝试访问不允许的内存的指针有关,我的问题是如何解决这个问题!

更新

我再次检查了程序,错误完全符合:

fftw_execute(plan) ; 

希望对大家有更多的帮助!

提前致谢!

最佳答案

找到了,错误出在计划函数的参数中:

plan = fftw_plan_dft_r2c_1d(N,data,out,FFTW_FORWARD); //

应该改为

plan = fftw_plan_dft_r2c_1d(N,data,out,FFTW_MEASURE);

plan = fftw_plan_dft_r2c_1d(N,data,out,FFTW_ESTIMATE);

因为转换的方向隐含在函数名称中!

无论如何,谢谢!

关于c - 真实数据序列上的 FFTW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24139974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com