summaryrefslogtreecommitdiff
path: root/gr-fft/lib/fft.cc
blob: 6074236e182c7969f352593b38b1d9401ca2c80e (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* -*- c++ -*- */
/*
 * Copyright 2003,2008,2011,2012 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio 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, or (at your option)
 * any later version.
 *
 * GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#include <fft/fft.h>
#include <gr_sys_paths.h>
#include <fftw3.h>

#ifdef _MSC_VER //http://www.fftw.org/install/windows.html#DLLwisdom
static void my_fftw_write_char(char c, void *f) { fputc(c, (FILE *) f); }
#define fftw_export_wisdom_to_file(f) fftw_export_wisdom(my_fftw_write_char, (void*) (f))
#define fftwf_export_wisdom_to_file(f) fftwf_export_wisdom(my_fftw_write_char, (void*) (f))
#define fftwl_export_wisdom_to_file(f) fftwl_export_wisdom(my_fftw_write_char, (void*) (f))

static int my_fftw_read_char(void *f) { return fgetc((FILE *) f); }
#define fftw_import_wisdom_from_file(f) fftw_import_wisdom(my_fftw_read_char, (void*) (f))
#define fftwf_import_wisdom_from_file(f) fftwf_import_wisdom(my_fftw_read_char, (void*) (f))
#define fftwl_import_wisdom_from_file(f) fftwl_import_wisdom(my_fftw_read_char, (void*) (f))
#endif //_MSC_VER

#include <gr_complex.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <cassert>
#include <stdexcept>

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
namespace fs = boost::filesystem;

namespace gr {
  namespace fft {

    gr_complex *
    malloc_complex(int size)
    {
      return (gr_complex*)fftwf_malloc(sizeof(gr_complex)*size);
    }

    float *
    malloc_float(int size)
    {
      return (float*)fftwf_malloc(sizeof(float)*size);
    }

    void
    free(void *b)
    {
      fftwf_free(b);
    }

    boost::mutex &
    planner::mutex()
    {
      static boost::mutex  s_planning_mutex;

      return s_planning_mutex;
    }

    static const char *
    wisdom_filename()
    {
      static fs::path path;
      path = fs::path(gr_appdata_path()) / ".gr_fftw_wisdom";
      return path.string().c_str();
    }

    static void
    import_wisdom()
    {
      const char *filename = wisdom_filename ();
      FILE *fp = fopen (filename, "r");
      if (fp != 0){
	int r = fftwf_import_wisdom_from_file (fp);
	fclose (fp);
	if (!r){
	  fprintf (stderr, "gr::fft: can't import wisdom from %s\n", filename);
	}
      }
    }

    static void
    config_threading(int nthreads)
    {
      static int fftw_threads_inited = 0;

#ifdef FFTW3F_THREADS
      if (fftw_threads_inited == 0)
	{
	  fftw_threads_inited = 1;
	  fftwf_init_threads();
	}

      fftwf_plan_with_nthreads(nthreads);
#endif
    }

    static void
    export_wisdom()
    {
      const char *filename = wisdom_filename ();
      FILE *fp = fopen (filename, "w");
      if (fp != 0){
	fftwf_export_wisdom_to_file (fp);
	fclose (fp);
      }
      else {
	fprintf (stderr, "fft_impl_fftw: ");
	perror (filename);
      }
    }

// ----------------------------------------------------------------

    fft_complex::fft_complex(int fft_size, bool forward, int nthreads)
    {
      // Hold global mutex during plan construction and destruction.
      planner::scoped_lock lock(planner::mutex());

      assert (sizeof (fftwf_complex) == sizeof (gr_complex));

      if (fft_size <= 0)
	throw std::out_of_range ("fft_impl_fftw: invalid fft_size");

      d_fft_size = fft_size;
      d_inbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * inbuf_length ());
      if (d_inbuf == 0)
	throw std::runtime_error ("fftwf_malloc");
      
      d_outbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * outbuf_length ());
      if (d_outbuf == 0){
	fftwf_free (d_inbuf);
	throw std::runtime_error ("fftwf_malloc");
      }
      
      d_nthreads = nthreads;
      config_threading(nthreads);
      import_wisdom();	// load prior wisdom from disk
      
      d_plan = fftwf_plan_dft_1d (fft_size,
				  reinterpret_cast<fftwf_complex *>(d_inbuf),
				  reinterpret_cast<fftwf_complex *>(d_outbuf),
				  forward ? FFTW_FORWARD : FFTW_BACKWARD,
				  FFTW_MEASURE);

      if (d_plan == NULL) {
	fprintf(stderr, "gr::fft: error creating plan\n");
	throw std::runtime_error ("fftwf_plan_dft_1d failed");
      }
      export_wisdom();	// store new wisdom to disk
    }

    fft_complex::~fft_complex()
    {
      // Hold global mutex during plan construction and destruction.
      planner::scoped_lock lock(planner::mutex());

      fftwf_destroy_plan ((fftwf_plan) d_plan);
      fftwf_free (d_inbuf);
      fftwf_free (d_outbuf);
    }

    void
    fft_complex::set_nthreads(int n)
    {
      if (n <= 0)
	throw std::out_of_range ("gr::fft: invalid number of threads");
      d_nthreads = n;

#ifdef FFTW3F_THREADS
      fftwf_plan_with_nthreads(d_nthreads);
#endif
    }

    void
    fft_complex::execute()
    {
      fftwf_execute((fftwf_plan) d_plan);
    }

// ----------------------------------------------------------------

    fft_real_fwd::fft_real_fwd (int fft_size, int nthreads)
    {
      // Hold global mutex during plan construction and destruction.
      planner::scoped_lock lock(planner::mutex());

      assert (sizeof (fftwf_complex) == sizeof (gr_complex));

      if (fft_size <= 0)
	throw std::out_of_range ("gr::fft: invalid fft_size");

      d_fft_size = fft_size;
      d_inbuf = (float *) fftwf_malloc (sizeof (float) * inbuf_length ());
      if (d_inbuf == 0)
	throw std::runtime_error ("fftwf_malloc");

      d_outbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * outbuf_length ());
      if (d_outbuf == 0){
	fftwf_free (d_inbuf);
	throw std::runtime_error ("fftwf_malloc");
      }

      d_nthreads = nthreads;
      config_threading(nthreads);
      import_wisdom();	// load prior wisdom from disk

      d_plan = fftwf_plan_dft_r2c_1d (fft_size,
				      d_inbuf,
				      reinterpret_cast<fftwf_complex *>(d_outbuf),
				      FFTW_MEASURE);

      if (d_plan == NULL) {
	fprintf(stderr, "gr::fft::fft_real_fwd: error creating plan\n");
	throw std::runtime_error ("fftwf_plan_dft_r2c_1d failed");
      }
      export_wisdom();	// store new wisdom to disk
    }

    fft_real_fwd::~fft_real_fwd()
    {
      // Hold global mutex during plan construction and destruction.
      planner::scoped_lock lock(planner::mutex());
      
      fftwf_destroy_plan ((fftwf_plan) d_plan);
      fftwf_free (d_inbuf);
      fftwf_free (d_outbuf);
    }

    void
    fft_real_fwd::set_nthreads(int n)
    {
      if (n <= 0)
	throw std::out_of_range ("gr::fft::fft_real_fwd::set_nthreads: invalid number of threads");
      d_nthreads = n;

#ifdef FFTW3F_THREADS
      fftwf_plan_with_nthreads(d_nthreads);
#endif
    }

    void
    fft_real_fwd::execute()
    {
      fftwf_execute ((fftwf_plan) d_plan);
    }

    // ----------------------------------------------------------------

    fft_real_rev::fft_real_rev(int fft_size, int nthreads)
    {
      // Hold global mutex during plan construction and destruction.
      planner::scoped_lock lock(planner::mutex());

      assert (sizeof (fftwf_complex) == sizeof (gr_complex));
      
      if (fft_size <= 0)
	throw std::out_of_range ("gr::fft::fft_real_rev: invalid fft_size");

      d_fft_size = fft_size;
      d_inbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * inbuf_length ());
      if (d_inbuf == 0)
	throw std::runtime_error ("fftwf_malloc");

      d_outbuf = (float *) fftwf_malloc (sizeof (float) * outbuf_length ());
      if (d_outbuf == 0){
	fftwf_free (d_inbuf);
	throw std::runtime_error ("fftwf_malloc");
      }

      d_nthreads = nthreads;
      config_threading(nthreads);
      import_wisdom();	// load prior wisdom from disk

      // FIXME If there's ever a chance that the planning functions
      // will be called in multiple threads, we've got to ensure single
      // threaded access.  They are not thread-safe.
      d_plan = fftwf_plan_dft_c2r_1d (fft_size,
				      reinterpret_cast<fftwf_complex *>(d_inbuf),
				      d_outbuf,
				      FFTW_MEASURE);

      if (d_plan == NULL) {
	fprintf(stderr, "gr::fft::fft_real_rev: error creating plan\n");
	throw std::runtime_error ("fftwf_plan_dft_c2r_1d failed");
      }
      export_wisdom ();	// store new wisdom to disk
    }

    fft_real_rev::~fft_real_rev ()
    {
      // Hold global mutex during plan construction and destruction.
      planner::scoped_lock lock(planner::mutex());
      
      fftwf_destroy_plan ((fftwf_plan) d_plan);
      fftwf_free (d_inbuf);
      fftwf_free (d_outbuf);
    }

    void
    fft_real_rev::set_nthreads(int n)
    {
      if (n <= 0)
	throw std::out_of_range ("gr::fft::fft_real_rev::set_nthreads: invalid number of threads");
      d_nthreads = n;
      
#ifdef FFTW3F_THREADS
      fftwf_plan_with_nthreads(d_nthreads);
#endif
    }

    void
    fft_real_rev::execute ()
    {
      fftwf_execute ((fftwf_plan) d_plan);
    }

  } /* namespace fft */
} /* namespace gr */