fft.c 1.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/*-----------------------------------------------------------------------------------------
 * Copyright (C) 2016  For the list of authors, see file AUTHORS.
 *
 * This file is part of IFOS.
 *
 * IFOS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
 *
 * IFOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IFOS. See file COPYING and/or <http://www.gnu.org/licenses/gpl-2.0.html>.
 -----------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------
 *   1D-FFT
 *  ----------------------------------------------------------------------*/

#include "fd.h"
#include <fftw3.h>

void  fft(float *array, float *arrayim, int npad, int dir) {

	/* declaration of local variables */
	int i,j;
		
	/* declaration of variables for FFTW3*/
	fftw_complex *in,*out;
	fftw_plan p;
		
	in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * npad);
	out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * npad);
	
	for (j=0;j<npad;j++){
		in[j][0]=array[j+1];
		in[j][1]=arrayim[j+1];
	}
	if (dir==1) { p = fftw_plan_dft_1d(npad, in, out, FFTW_FORWARD, FFTW_ESTIMATE); }
	else { p = fftw_plan_dft_1d(npad, in, out, FFTW_BACKWARD, FFTW_ESTIMATE); }

	fftw_execute(p);
		
	 /* write output into data matrix */
        for(j=0;j<npad;j++){
		array[j+1] = out[j][0];
		arrayim[j+1] = out[j][1];

	}
	
	fftw_free(in);	
	fftw_free(out);	
	fftw_destroy_plan(p);
	
}