Search code examples
c++g++fftwwaveletdwt

convfft() call in fftw causes crash. Is it about improper library linking?


In my study, i use 1-D DWT as a part of the algorithm and i write the application in 32-bit ubuntu linux with C++. As a novice linux user, i'm having some problems about compiling and linking my codes.

To implement discrete wavelet transform in C++;

  • I downloaded the wavelib package in code-google page and extracted the wavelib folder to the same folder with my main.cpp file. I used source code of wavelib which requires fftw library.

  • I downloaded latest fftw library from http://www.fftw.org/ and installed it by given instructions.

I compiled the code with following command in terminal.

g++ -g -o main main.cpp wavelib/src/static/wavelet2s.cpp -I. -Iwavelib/src/static -I/usr/local/include -L/usr/local/lib -lfftw3

In main() function, a random one dimensional vector array is defined and the denoise1d() function is called. In denoise1d() function, wavelib functions, dwt() and idwt(), are called.

The execution crashed with an error as follows;

Error in `/home/user/Desktop/linux/main': free(): invalid next size (normal): 0x0819cce0

Program received signal SIGABRT, Aborted. 0xb7fdd424 in __kernel_vsyscall ()

Then I used codeblocks for step-by-step debugging of the code.

I found out that the execution crashes in dwt1() function where the convfft() function from fftw library is called. dwt1() function is called by dwt() for one-level transformation.

dwt1() function is defined at line 1700 of wavelib/src/static/wavelet2s.cpp

convfft() function is called at line 1719 of wavelet2s.cpp and defined in fftw shared library

Why does convfft() function call cause memory error? Is it possible that i could not link fftw library properly?

Thanks in advance.

Related codes are attached below. (requires wavelib and fftw as addressed above)

#include "wavelet2s.h"
#include "fftw3.h"
#include <vector>
#include <iostream>
using namespace std;

#define BufSize 64

void denoise1d(vector<double> &s, string nm);

int main (int argc, char **argv)
{
    string wfname = "db2";
    vector<double> signal(BufSize+1);
    for(int i=0; i<BufSize+1;i++)
        signal[i] = 1.0d / (double)rand();

    denoise1d(signal,wfname);
    return 0;
}

void denoise1d(vector<double> &s, string nm)
{
    vector<double> iC, iFlag, iL;

    // perform 4-Level DWT
    dwt(s, 4, nm, iC, iFlag, iL);

    // doing some denoising operations on wavelet coeffs

 //Perform 4-Level IDWT
    idwt(iC, iFlag, nm, s,iL);
}

Solution

  • I finally found out my bug. Prior to calling denoise1d function, a 65-sized vector is defined. However, dwt is assumed to accept input sizes of power-of-2 (In this case 64). I fixed the bug and the code run without errors.

    int main (int argc, char **argv)
    {
        string wfname = "db2";
        vector<double> signal(BufSize);
        for(int i=0; i<BufSize;i++)
            signal[i] = 1.0d / (double)rand();
    
        denoise1d(signal,wfname);
        return 0;
    }