我正在尝试学习如何使用快速傅立叶变换,并从名为four1的C语言数值配方中复制了快速傅立叶变换算法。我写了一个小的函数测试,傅立叶变换一个简单的函数。但是在执行时,程序会返回一个分段错误。我找不到哪里有问题,你能帮我吗?
#include <stdio.h>
#include <math.h>
#define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr
void four1(float data[], unsigned long nn, int isign)
{
unsigned long n,mmax,m,j,istep,i;
double wtemp,wr,wpr,wpi,wi,theta;
float tempr,tempi;
n=nn << 1;
j=1;
for (i=1;i<n;i+=2) {
if (j > i) {
SWAP(data[j],data[i]);
SWAP(data[j+1],data[i+1]);
}
m=n >> 1;
while (m >= 2 && j > m) {
j -= m;
m >>= 1;
}
j += m;
}
mmax=2;
while (n > mmax) {
istep=mmax << 1;
theta=isign*(6.28318530717959/mmax);
wtemp=sin(0.5*theta);
wpr = -2.0*wtemp*wtemp;
wpi=sin(theta);
wr=1.0;
wi=0.0;
for (m=1;m<mmax;m+=2) {
for (i=m;i<=n;i+=istep) {
j=i+mmax;
tempr=wr*data[j]-wi*data[j+1];
tempi=wr*data[j+1]+wi*data[j];
data[j]=data[i]-tempr;
data[j+1]=data[i+1]-tempi;
data[i] += tempr;
data[i+1] += tempi;
}
wr=(wtemp=wr)*wpr-wi*wpi+wr;
wi=wi*wpr+wtemp*wpi+wi;
}
mmax=istep;
}
}
void fourier_transform_test (FILE* output_file)
{
/*
This function serves as a test to see whether my implementation of the
fft is working or not.
*/
int n = 30; // number of samples
float x[n]; // array that holds all values for x
// misc
int i = 0;
printf("Running fourier transform tests...\n");
fprintf(output_file, "# x t\n");
// fill the array x with values to be transformed
for (i = 0; i <= (n - 1); i++)
x[i] = cos((2 * 3.1415 * i) / 10);
// according to the Numerical Recipies, I have to decrement the pointer to data
// by one to compensate for the zero-offset
four1(x-1, 64, 1);
// loop through the transformed array x and print results to a file
for (i = 0; i <= (n - 1); i++)
fprintf(output_file, "%i\t%f\n", i, x[i]);
fclose(output_file);
}
int main (int argc, char *argv[])
{
// open data_file to write results
FILE* file;
if (argc == 1)
file = fopen("results.dat", "w");
else
file = fopen(argv[1], "w");
fourier_transform_test(file);
return 0;
} 我使用的是最新的Debian,gcc 4(非常确定)
对于那些想亲眼看看这本书的人:http://www.nrbook.com/a/bookcpdf/c12-2.pdf (它是合法的)
发布于 2011-02-18 08:56:21
您的数组中只有30个值,但是您告诉four1您的数组有64个浮点数。
four1(x-1, n, 1);发布于 2011-02-18 08:58:21
您的浮点数输入数组的长度为30,但是您将64作为nn值传递。然后将n设置为两次nn,即128:
n = nn << 1;(顺便说一句,鉴于nn是unsigned long,这行代码只是编写n = nn * 2的一种模糊方式)。
然后,该函数访问最大为data[128]的值,该值等同于x[127],并且远远超出数组的界限。
发布于 2011-02-18 09:25:33
这是你崩溃的堆栈记录:
Breakpoint 1, main (argc=1, argv=0x7fffffffe3c8) at main.c:86
86 if (argc == 1)
(gdb) n
88 file = fopen("results.dat", "w");
(gdb) n
92 fourier_transform_test(file);
(gdb) print file
$1 = (FILE *) 0x603010
(gdb) n
Running fourier transform tests...
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7313974 in fclose () from /lib/libc.so.6
(gdb) bt
#0 0x00007ffff7313974 in fclose () from /lib/libc.so.6
#1 0x0000000000400e55 in fourier_transform_test (output_file=0xffffe1e0ffffe1e0) at main.c:78你的output_file指针和堆栈帧都因为这行错误地认为x[]的大小超过30个浮点数而被清理:
four1(x-1, 64, 1);这里是通过达到数组限制来覆盖output_file指针的地方:
Breakpoint 1, main (argc=1, argv=0x7fffffffe3c8) at main.c:90
90 if (argc == 1)
(gdb) n
92 file = fopen("results.dat", "w");
(gdb) n
96 fourier_transform_test(file);
(gdb) s
fourier_transform_test (output_file=0x603010) at main.c:52
52 {
(gdb) watch output_file
Hardware watchpoint 2: output_file
(gdb) c
Continuing.
Running fourier transform tests...
Hardware watchpoint 2: output_file
Old value = (FILE *) 0x603010
New value = (FILE *) 0x0
four1 (data=0x7fffffffe1cc, nn=64, isign=1) at main.c:16
16 SWAP(data[j+1],data[i+1]);
(gdb) bt
#0 four1 (data=0x7fffffffe1cc, nn=64, isign=1) at main.c:16
#1 0x0000000000400dfe in fourier_transform_test (output_file=0x0) at main.c:72
#2 0x0000000000400edb in main (argc=1, argv=0x3f4f07073e9df6ca) at main.c:96
(gdb) l
11 n=nn << 1;
12 j=1;
13 for (i=1;i<n;i+=2) {
14 if (j > i) {
15 SWAP(data[j],data[i]);
16 SWAP(data[j+1],data[i+1]); <---i=39;j=101
17 }
18 m=n >> 1;
19 while (m >= 2 && j > m) {
20 j -= m;
(gdb) info locals
m = 64
j = 101
istep = 6
wpr = 6.953355807477696e-310
wpi = 0
n = 128
wtemp = 4.9406564584124654e-324
wr = 6.9533490701916841e-310
wi = 6.953355807463467e-310
mmax = 4603839450133099783
i = 39
theta = 6755399441055756
tempr = 0
tempi = -nan(0x7fe260)
(gdb) 这里有一本关于如何实现傅里叶变换的好书-- http://www.dspguide.com/
https://stackoverflow.com/questions/5036550
复制相似问题