fft.c 6.11 KB
Newer Older
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1
/*
2
3
4
5
6
7
8
9
10
*				fft.c
*
* Single precision FFT functions.
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
*	This file part of:	SExtractor
*
*	Copyright:		(C) 2007-2010 IAP/CNRS/UPMC
Emmanuel Bertin's avatar
Emmanuel Bertin committed
11
*
12
*	Author:			Emmanuel Bertin (IAP)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
13
*
14
*	License:		GNU General Public License
Emmanuel Bertin's avatar
Emmanuel Bertin committed
15
*
16
17
18
19
20
21
22
23
24
25
*	SExtractor 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, either version 3 of the License, or
*	(at your option) any later version.
*	SExtractor 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 SExtractor. If not, see <http://www.gnu.org/licenses/>.
Emmanuel Bertin's avatar
Emmanuel Bertin committed
26
*
27
*	Last modified:		11/10/2010
Emmanuel Bertin's avatar
Emmanuel Bertin committed
28
*
29
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
30
31
32
33
34
35
36
37
38

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

39
#include FFTW_H
Emmanuel Bertin's avatar
Emmanuel Bertin committed
40
41
42
43
44
45
46
47
48

#include "define.h"
#include "globals.h"
#include "fft.h"
#include "prefs.h"
#ifdef USE_THREADS
#include "threads.h"
#endif

Emmanuel Bertin's avatar
Emmanuel Bertin committed
49
 fftwf_plan	fplan, bplan;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
50
51
52
53
 int    firsttimeflag;
#ifdef USE_THREADS
 pthread_mutex_t	fftmutex;
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
54
 fftwf_complex 	*fdata1;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
55
56
57
58
59
60
61
62
63
#define SWAP(a,b)       tempr=(a);(a)=(b);(b)=tempr

/****** fft_init ************************************************************
PROTO	void fft_init(void)
PURPOSE	Initialize the FFT routines
INPUT	-.
OUTPUT	-.
NOTES	Global preferences are used for multhreading.
AUTHOR	E. Bertin (IAP)
64
VERSION	26/06/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
65
 ***/
66
void    fft_init(int nthreads)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
67
68
69
70
71
 {
  if (!firsttimeflag)
    {
#ifdef USE_THREADS
    QPTHREAD_MUTEX_INIT(&fftmutex, NULL);
72
#ifdef HAVE_FFTW_MP
73
74
75
76
    if (nthreads > 1)
      {
      if (!fftw_init_threads())
        error(EXIT_FAILURE, "*Error*: thread initialization failed in ", "FFTW");
77
      fftwf_plan_with_nthreads(prefs.nthreads);
78
      }
79
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#endif
    firsttimeflag = 1;
    }

  return;
  }


/****** fft_end ************************************************************
PROTO	void fft_init(void)
PURPOSE	Clear up stuff set by FFT routines
INPUT	-.
OUTPUT	-.
NOTES	-.
AUTHOR	E. Bertin (IAP)
95
VERSION	26/06/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
96
97
98
99
100
101
102
103
 ***/
void    fft_end(void)
 {

  if (firsttimeflag)
    {
    firsttimeflag = 0;
#ifdef USE_THREADS
104
#ifdef HAVE_FFTW_MP
105
    fftwf_cleanup_threads();
106
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
107
108
    QPTHREAD_MUTEX_DESTROY(&fftmutex);
#endif
109
    fftwf_cleanup();
Emmanuel Bertin's avatar
Emmanuel Bertin committed
110
111
112
113
114
115
    }

  return;
  }


Emmanuel Bertin's avatar
Emmanuel Bertin committed
116
117
118
119
120
121
122
/****** fft_reset ***********************************************************
PROTO	void fft_reset(void)
PURPOSE	Reset the FFT plans
INPUT	-.
OUTPUT	-.
NOTES	-.
AUTHOR	E. Bertin (IAP)
123
VERSION	08/10/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
 ***/
void    fft_reset(void)
 {
  if (fplan)
    {
    QFFTWFREE(fdata1);
    fftwf_destroy_plan(fplan);
    }
  if (bplan)
    fftwf_destroy_plan(bplan);
  fplan = bplan = NULL;

  return;
  }


Emmanuel Bertin's avatar
Emmanuel Bertin committed
140
/****** fft_conv ************************************************************
141
PROTO	void fft_conv(float *data1, float *fdata2, int *size)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
142
143
144
145
146
147
PURPOSE	Optimized 2-dimensional FFT convolution using the FFTW library.
INPUT	ptr to the first image,
	ptr to the Fourier transform of the second image,
	image size vector.
OUTPUT	-.
NOTES	For data1 and fdata2, memory must be allocated for
148
	size[0]* ... * 2*(size[naxis-1]/2+1) floats (padding required).
Emmanuel Bertin's avatar
Emmanuel Bertin committed
149
AUTHOR	E. Bertin (IAP)
150
VERSION	01/12/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
151
 ***/
152
void    fft_conv(float *data1, float *fdata2, int *size)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
153
  {
154
   float		*fdata1p,*fdata2p,
155
156
			real,imag, fac;
   int			i, npix,npix2;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
157
158
159
160
161
162
163
164
165

/* Convert axis indexing to that of FFTW */
  npix = size[0]*size[1];
  npix2 = (((size[0]/2) + 1)*2) * size[1];

/* Forward FFT "in place" for data1 */
#ifdef USE_THREADS
  QPTHREAD_MUTEX_LOCK(&fftmutex);
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
166
167
168
169
  if (!fplan)
    {
    QFFTWMALLOC(fdata1, fftwf_complex, npix2);
    fplan = fftwf_plan_dft_r2c_2d(size[1], size[0], data1,
170
        (fftwf_complex *)fdata1, FFTW_ESTIMATE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
171
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
172
173
174
#ifdef USE_THREADS
  QPTHREAD_MUTEX_UNLOCK(&fftmutex);
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
175
176
177
  fftwf_execute_dft_r2c(fplan, data1, fdata1);

//  fftwf_execute(plan);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
178
179
180
181

#ifdef USE_THREADS
  QPTHREAD_MUTEX_LOCK(&fftmutex);
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
182
//  fftwf_destroy_plan(plan);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
183
184
185
186
187
188
#ifdef USE_THREADS
  QPTHREAD_MUTEX_UNLOCK(&fftmutex);
#endif

/* Actual convolution (Fourier product) */
  fac = 1.0/npix;  
189
  fdata1p = (float *)fdata1;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
190
191
192
193
194
195
196
197
198
199
200
201
202
  fdata2p = fdata2;
  for (i=npix2/2; i--; fdata2p+=2)
    {
    real = *fdata1p **fdata2p - *(fdata1p+1)**(fdata2p+1);
    imag = *(fdata1p+1)**fdata2p + *fdata1p**(fdata2p+1);
    *(fdata1p++) = fac*real;
    *(fdata1p++) = fac*imag;
    }

/* Reverse FFT */
#ifdef USE_THREADS
  QPTHREAD_MUTEX_LOCK(&fftmutex);
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
203
204
  if (!bplan)
    bplan = fftwf_plan_dft_c2r_2d(size[1], size[0], (fftwf_complex *)fdata1, 
205
        data1, FFTW_ESTIMATE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
206
207
208
#ifdef USE_THREADS
  QPTHREAD_MUTEX_UNLOCK(&fftmutex);
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
209
  fftwf_execute_dft_c2r(bplan, fdata1, data1);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
210

Emmanuel Bertin's avatar
Emmanuel Bertin committed
211
//  fftwf_execute(plan);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
212
213
214
215

#ifdef USE_THREADS
  QPTHREAD_MUTEX_LOCK(&fftmutex);
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
216
//  fftwf_destroy_plan(plan);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
217
218
219
220
221
222
223
224
225
226
/* Free the fdata1 scratch array */
#ifdef USE_THREADS
  QPTHREAD_MUTEX_UNLOCK(&fftmutex);
#endif

  return;
  }


/****** fft_rtf ************************************************************
227
PROTO	float *fft_rtf(float *data, int *size)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
228
229
230
231
232
233
PURPOSE	Optimized 2-dimensional FFT "in place" using the FFTW library.
INPUT	ptr to the image,
	ptr to image size vector.
OUTPUT	Pointer to the compressed, memory-allocated Fourier transform.
NOTES	Input data may end up corrupted.
AUTHOR	E. Bertin (IAP)
234
VERSION	08/10/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
235
 ***/
236
float	*fft_rtf(float *data, int *size)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
237
  {
238
239
240
   fftwf_plan   	plan;
   fftwf_complex	*fdata;
   int			npix2;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
241
242
243
244
245
246
247
248

/* Convert axis indexing to that of FFTW */
  npix2 = ((size[0]/2) + 1) * size[1];

/* Forward FFT "in place" for data1 */
#ifdef USE_THREADS
  QPTHREAD_MUTEX_LOCK(&fftmutex);
#endif
249
250
  QFFTWMALLOC(fdata, fftwf_complex, npix2);
  plan = fftwf_plan_dft_r2c_2d(size[1], size[0], data,
251
        fdata, FFTW_ESTIMATE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
252
253
254
#ifdef USE_THREADS
  QPTHREAD_MUTEX_UNLOCK(&fftmutex);
#endif
255
  fftwf_execute(plan);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
256
257
258
#ifdef USE_THREADS
  QPTHREAD_MUTEX_LOCK(&fftmutex);
#endif
259
  fftwf_destroy_plan(plan);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
260
261
262
263
#ifdef USE_THREADS
  QPTHREAD_MUTEX_UNLOCK(&fftmutex);
#endif

264
  return (float *)fdata;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
265
266
267
  }