【问题标题】:FFTW plan segmentation faultFFTW 计划分段错误
【发布时间】:2017-01-27 11:18:24
【问题描述】:

我正在使用 FFTW3 对多列数据执行 fft(即多通道音频,我希望转换每个通道)。这在 OSX 上运行良好,但将代码移植到 linux 会给我一个 seg 错误。

const int fftwFlags = FFTW_PRESERVE_INPUT|FFTW_PATIENT;

struct fft {
    fftw_complex **complexSig;
    double **realSig;
    fftw_plan forwardR2C;
    int fftLen;
    int numChan;
}

void create FFT(struct fft *fft) {

int bufLen = 1024;
int numChan = 4;
fft->fftLen = bufLen;
fft->numChan = numChan;

fft->realSig = fftw_malloc(sizeof(double *) * numChan);
for(int i = 0; i < numChan; i++) {
    fft->realSig[i] = fftw_malloc(sizeof(double) * bufLen);
}

fft->complexSig = fftw_malloc(sizeof(fftw_complex *) * numChan);
for(int i = 0; i < numChan; i++) {
    fft->complexSig[i] = fftw_malloc(sizeof(fftw_complex) * bufLen);
}

fft->forwardR2C = fftw_plan_many_dft_r2c(1, &fft->fftLen, fft->numChan, *fft->realSig, &fft->fftLen, 1, fft->fftLen, *fft->complexSig, &fft->fftLen, 1, fft->fftLen, fftwFlags);

}

valgrind 显示 fftw 规划器正在尝试访问超出此数组的末尾(8 个字节,一个样本),从而导致分段错误。当将分配给 realSig 的内存量增加到 bufLen * 2 时,不会出现此错误。

我确信这是我告诉 FFTW 读取我的数据的错误,但我无法发现它!

【问题讨论】:

  • 这么多未知变量.... fftLen 和 bufLen 和 numCha 和 and and 是什么
  • 刚刚编辑了问题:) 变量是根据输入设备自动设置的,所以我忘了澄清!

标签: c segmentation-fault fft fftw


【解决方案1】:

您似乎假设连续的 malloc 调用将是连续的,当然它们不太可能是连续的(您可能只是在 OS X 上“走运”了)。您可以通过大量分配来轻松解决此问题,例如

void createFFT(struct fft *fft)
{
    const int bufLen = 1024;
    const int numChan = 4;

    fft->fftLen = bufLen;
    fft->numChan = numChan;

    fft->realSig = fftw_malloc(sizeof(double *) * numChan);
                                     // array of numChan pointers
    fft->realSig[0] = fftw_malloc(sizeof(double) * numChan  * bufLen);
                                     // one large contiguous block of size `numChan * bufLen`
    for(int i = 1; i < numChan; i++) // init pointers
    {
        fft->realSig[i] = fft->realSig[i - 1] + bufLen;
    }

    // ...

}

注意:完成后,您只需:

fftw_free(fft->realSig[0]);

【讨论】:

  • 很好的答案,我确实假设连续的 malloc 调用会产生连续的内存。回顾 fftw doc here 我还告诉 fftw 计划者我的“矩阵”是连续的,跨度/跨度为 1。
猜你喜欢
  • 2019-06-26
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多