Search code examples
cvalgrindfftw

Cannot retrieve original image using FFT with FFTW


I'm using FFTW in my C code and I have some issue. First, I can transform an original image to two images (mag+phase) and get back the original image with the inverse transform. However, If I want to get a mag file centered in frequency it does not work anymore: the final image has some issues.

Here some pieces of my code. Can someone help me to find the error in my code?

EDIT: I've fixed the code to follow @francis recommandation, but my issues is always here.


enum {
    TYPE_CENTERED,
    TYPE_REGULAR
};

static void fft_to_spectra(fits* fit, fftw_complex *frequency_repr, double *as,
        double *ps, int nbdata) {
    unsigned int i;

    for (i = 0; i < nbdata; i++) {
        double r = creal(frequency_repr[i]);
        double im = cimag(frequency_repr[i]);
        as[i] = hypot(r, im);
        ps[i] = atan2(im, r);
    }
}

static void fft_to_freq(fits* fit, fftw_complex *frequency_repr, double *as, double *ps, int nbdata) {
    unsigned int i;

    for (i = 0; i < nbdata; i++) {
        frequency_repr[i] = as[i] * (cos(ps[i]) + I * sin(ps[i]));
    }
}

void change_symmetry(unsigned int width, unsigned int height, unsigned int i, unsigned int j, unsigned int *x,
        unsigned int *y) {

    if (i < width / 2 && j < height / 2) {
        *x = i + width / 2;
        *y = j + height / 2;
    }
    if (i >= width / 2 && j < height / 2) {
        *x = i - width / 2;
        *y = j + height / 2;
    }
    if (i < width / 2 && j >= height / 2) {
        *x = i + width / 2;
        *y = j - height / 2;
    }
    if (i >= width / 2 && j >= height / 2) {
        *x = i - width / 2;
        *y = j - height / 2;
    }
}

static void centered(WORD *buf, unsigned int width,
        unsigned int height) {
    unsigned int i, j;

    WORD *temp = malloc(width * height * sizeof(WORD));
    for (j = 0; j < height; j++) {
        for (i = 0; i < width; i++) {
            unsigned int x = i;
            unsigned int y = j;
            change_symmetry(width, height, i, j, &x, &y);

            temp[j * width + i] = buf[y * width + x];
        }
    }

    memcpy(buf, temp, sizeof(WORD) * width * height);
    free(temp);
}

static void normalisation_spectra(unsigned int w, unsigned int h, double *modulus, double *phase,
        WORD *abuf, WORD *pbuf) {
    unsigned int i;

    for (i = 0; i < h * w; i++) {
        pbuf[i] = round_to_WORD(((phase[i] + M_PI) * USHRT_MAX_DOUBLE / (2 * M_PI)));
        abuf[i] = round_to_WORD((modulus[i] / w / h));
    }
}

static void save_dft_information_in_gfit(fits *fit) {
    strcpy(gfit.dft.ord, fit->dft.type);
    strcpy(gfit.dft.ord, fit->dft.ord);
}

static void FFTD(fits *fit, fits *x, fits *y, int type_order, int layer) {
    WORD *xbuf = x->pdata[layer];
    WORD *ybuf = y->pdata[layer];
    WORD *gbuf = fit->pdata[layer];
    unsigned int i;
    unsigned int width = fit->rx, height = fit->ry;
    int nbdata = width * height;

    fftw_complex *spatial_repr = fftw_malloc(sizeof(fftw_complex) * nbdata);
    if (!spatial_repr) {
        return;
    }
    fftw_complex *frequency_repr = fftw_malloc(sizeof(fftw_complex) * nbdata);
    if (!frequency_repr) {
        fftw_free(spatial_repr);
        return;
    }

    /* copying image selection into the fftw data */
#ifdef _OPENMP
#pragma omp parallel for num_threads(com.max_thread) private(i) schedule(static) if(nbdata > 15000)
#endif
    for (i = 0; i < nbdata; i++) {
        spatial_repr[i] = (double) gbuf[i];
    }

    /* we run the Fourier Transform */
    fftw_plan p = fftw_plan_dft_2d(height, width, spatial_repr, frequency_repr,
            FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_execute(p);

    /* we compute modulus and phase */
    double *modulus = malloc(nbdata * sizeof(double));
    double *phase = malloc(nbdata * sizeof(double));

    fft_to_spectra(fit, frequency_repr, modulus, phase, nbdata);

    //We normalize the modulus and the phase
    normalisation_spectra(width, height, modulus, phase, xbuf, ybuf);
    if (type_order == TYPE_CENTERED) {
        strcpy(x->dft.ord, "CENTERED");
        centered(xbuf, width, height);
        centered(ybuf, width, height);
    }

    free(modulus);
    free(phase);
    fftw_destroy_plan(p);
    fftw_free(spatial_repr);
    fftw_free(frequency_repr);
}

static void FFTI(fits *fit, fits *xfit, fits *yfit, int type_order, int layer) {
    WORD *xbuf = xfit->pdata[layer];
    WORD *ybuf = yfit->pdata[layer];
    WORD *gbuf = fit->pdata[layer];
    unsigned int i;
    unsigned int width = xfit->rx;
    unsigned int height = xfit->ry;
    int nbdata = width * height;

    double *modulus = calloc(1, nbdata * sizeof(double));
    double *phase = calloc(1, nbdata * sizeof(double));

    if (type_order == TYPE_CENTERED) {
        centered(xbuf, width, height);
        centered(ybuf, width, height);
    }

    for (i = 0; i < height * width; i++) {
        modulus[i] = (double) xbuf[i] * (width * height);
        phase[i] = (double) ybuf[i] * (2 * M_PI / USHRT_MAX_DOUBLE);
        phase[i] -= M_PI;
    }

    fftw_complex* spatial_repr = fftw_malloc(sizeof(fftw_complex) * nbdata);
    if (!spatial_repr) {
        return;
    }

    fftw_complex* frequency_repr = fftw_malloc(sizeof(fftw_complex) * nbdata);
    if (!frequency_repr) {
        fftw_free(spatial_repr);
        return;
    }

    fft_to_freq(fit, frequency_repr, modulus, phase, nbdata);

    fftw_plan p = fftw_plan_dft_2d(height, width, frequency_repr, spatial_repr,
            FFTW_BACKWARD, FFTW_ESTIMATE);
    fftw_execute(p);

    for (i = 0; i < nbdata; i++) {
        double pxl = creal(spatial_repr[i]) / nbdata;
        gbuf[i] = round_to_WORD(pxl);
    }

    free(modulus);
    free(phase);
    fftw_destroy_plan(p);
    fftw_free(spatial_repr);
    fftw_free(frequency_repr);
}

Here my images, the original one and the FFTD(centered)->FFTI result enter image description here enter image description here


Solution

  • The plan is created using the flag FFTW_MEASURE. Hence, several DFT are computed and the input array is likely overwritten. Here is the start of the description of planner flags in the documentation of FFTW:

    • FFTW_ESTIMATE specifies that, instead of actual measurements of different algorithms, a simple heuristic is used to pick a (probably sub-optimal) plan quickly. With this flag, the input/output arrays are not overwritten during planning.

    • FFTW_MEASURE tells FFTW to find an optimized plan by actually computing several FFTs and measuring their execution time. Depending on your machine, this can take some time (often a few seconds). FFTW_MEASURE is the default planning option.

    Either switch to FFTW_ESTIMATE or create the plan before populating the input array:

    /* we run the Fourier Transform */
    fftw_plan p = fftw_plan_dft_2d(width, height, spatial_repr, frequency_repr,
            FFTW_FORWARD, FFTW_MEASURE);
    
    /* copying image selection into the fftw data */
    #ifdef _OPENMP
    #pragma omp parallel for num_threads(com.max_thread) private(i) schedule(static) if(nbdata > 15000)
    #endif
    for (i = 0; i < nbdata; i++) {
        spatial_repr[i] = (double) gbuf[i];
    }
    

    If you intend to a single image, using FFTW_ESTIMATE is the way to go. On the contrary, if you consider treating multiple images, creating the plan once using FFTW_MEASURE and storing it is a good option. Then you may use New-array Execute Functions each time a FFT is to be performed:

    fftw_execute_dft(p, spatial_repr, frequency_repr);
    

    You can test the return value of malloc() or fftw_malloc() to check if the allocations went right. If not, it returns NULL. fftw_malloc() is implemented as function *X(kernel_malloc)(size_t n) in fftw-3.3.6-pl2/kernel/kalloc.c . It calls functions like memalign() or _aligned_malloc() among others. Both these two return NULL just like malloc() in case of failure. Finally, I did not spotted a critical issue regarding memory allocation of deallocation in the piece of code you provided.

    The argument double nbdata in fft_to_spectra() should likely be an integer. Valgrind might have considered it as strange...

    EDIT : the change_symmetry() is to be modified for odd sizes. Something like:

    void change_symmetry_forward(unsigned int width, unsigned int height, unsigned int i, unsigned int j, unsigned int *x,
        unsigned int *y) {
         *x = i + width / 2;
         if (*x>=width){
               *x=*x-width;
         }
         *y = j + height / 2;
         if(*y>=height){
              *y =*y-height;
         }
    }
    

    and

    void change_symmetry_backward(unsigned int width, unsigned int height, unsigned int i, unsigned int j, unsigned int *x,
        unsigned int *y) {
         *x = i +width- width / 2;
         if (*x>=width){
               *x=*x-width;
         }
         *y = j +height- height / 2;
         if(*y>=height){
              *y =*y-height;
         }
    }