ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
fft_real.hpp
Go to the documentation of this file.
1/*
2 * $Id: fft_real.hpp 283 2012-08-26 15:36:43Z jdl3 $
3 * Copyright (C) 2012, 2020 John D Lamb
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#ifndef CCGSL_FFT_REAL_HPP
21#define CCGSL_FFT_REAL_HPP
22
23#include <gsl/gsl_fft_real.h>
24#include "vector.hpp"
25#include "vector_complex.hpp"
26
27namespace gsl {
28 namespace fft {
32 namespace real {
36 namespace radix2 {
44 inline int transform( double data[], size_t const stride, size_t const n ){
45 return gsl_fft_real_radix2_transform( data, stride, n ); }
46
54 template<typename DATA>
55 inline int transform( DATA& data, size_t const stride = 1 ){
56 return gsl_fft_real_radix2_transform( data.data(), stride, data.size() / stride ); }
57
64 inline int transform( double data[], size_t const n ){
65 return gsl_fft_real_radix2_transform( data, 1, n ); }
66 }
67
71 class wavetable {
72 public:
77 ccgsl_pointer = 0;
78 count = 0; // initially nullptr will do
79 }
80 // Refines random access container
81 // Refines assignable
86 explicit wavetable( size_t const n ){
87 ccgsl_pointer = gsl_fft_real_wavetable_alloc( n );
88 // just plausibly we could allocate wavetable but not count
89 try { count = new size_t; } catch( std::bad_alloc& e ){
90 // try to tidy up before rethrowing
91 gsl_fft_real_wavetable_free( ccgsl_pointer );
92 throw e;
93 }
94 *count = 1; // initially there is just one reference to ccgsl_pointer
95 }
102 explicit wavetable( gsl_fft_real_wavetable* v ){
103 ccgsl_pointer = v;
104 // just plausibly we could fail to allocate count: no further action needed.
105 count = new size_t;
106 *count = 1; // initially there is just one reference to ccgsl_pointer
107 }
108 // copy constructor
114 count = v.count; if( count != 0 ) ++*count; }
115 // assignment operator
121 // first, possibly delete anything pointed to by this
122 if( count == 0 or --*count == 0 ){
123 if( ccgsl_pointer != 0 ) gsl_fft_real_wavetable_free( ccgsl_pointer );
124 delete count;
125 } // Then copy
126 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
127 }
128 // destructor
133 if( count == 0 or --*count == 0 ){
134 // could have allocated null pointer
135 if( ccgsl_pointer != 0 ) gsl_fft_real_wavetable_free( ccgsl_pointer );
136 delete count;
137 }
138 }
139#ifdef __GXX_EXPERIMENTAL_CXX0X__
145 std::swap( count, v.count );
146 v.ccgsl_pointer = nullptr;
147 }
154 wavetable( std::move( v ) ).swap( *this );
155 return *this;
156 }
157#endif
158 // Refines equality comparable
159 // == operator
166 bool operator==( wavetable const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
167 // != operator
174 bool operator!=( wavetable const& v ) const { return not operator==( v ); }
175 // Refines forward container
176 // Refines less than comparable
177 // operator<
186 bool operator<( wavetable const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
187 // operator>
196 bool operator>( wavetable const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
197 // operator<=
206 bool operator<=( wavetable const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
207 // operator>=
216 bool operator>=( wavetable const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
221 bool empty() const { return ccgsl_pointer == 0; }
222 // swap() --- should work even if sizes don't match
228 void swap( wavetable& v ){
229 std::swap( ccgsl_pointer, v.ccgsl_pointer );
230 std::swap( count, v.count );
231 }
232 private:
236 gsl_fft_real_wavetable* ccgsl_pointer;
240 size_t* count;
241 public:
242 // shared reference functions
247 gsl_fft_real_wavetable* get() const { return ccgsl_pointer; }
253 bool unique() const { return count != 0 and *count == 1; }
258 size_t use_count() const { return count == 0 ? 0 : *count; }
264#ifdef __GXX_EXPERIMENTAL_CXX0X__
265 explicit
266#endif
267 operator bool() const { return ccgsl_pointer != 0; }
268 };
269
273 class workspace {
274 public:
279 ccgsl_pointer = 0;
280 count = 0; // initially nullptr will do
281 }
282 // Refines random access container
283 // Refines assignable
288 explicit workspace( size_t const n ){
289 ccgsl_pointer = gsl_fft_real_workspace_alloc( n );
290 // just plausibly we could allocate workspace but not count
291 try { count = new size_t; } catch( std::bad_alloc& e ){
292 // try to tidy up before rethrowing
293 gsl_fft_real_workspace_free( ccgsl_pointer );
294 throw e;
295 }
296 *count = 1; // initially there is just one reference to ccgsl_pointer
297 }
304 explicit workspace( gsl_fft_real_workspace* v ){
305 ccgsl_pointer = v;
306 // just plausibly we could fail to allocate count: no further action needed.
307 count = new size_t;
308 *count = 1; // initially there is just one reference to ccgsl_pointer
309 }
310 // copy constructor
316 count = v.count; if( count != 0 ) ++*count; }
317 // assignment operator
323 // first, possibly delete anything pointed to by this
324 if( count == 0 or --*count == 0 ){
325 if( ccgsl_pointer != 0 ) gsl_fft_real_workspace_free( ccgsl_pointer );
326 delete count;
327 } // Then copy
328 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
329 }
330 // destructor
335 if( count == 0 or --*count == 0 ){
336 // could have allocated null pointer
337 if( ccgsl_pointer != 0 ) gsl_fft_real_workspace_free( ccgsl_pointer );
338 delete count;
339 }
340 }
341#ifdef __GXX_EXPERIMENTAL_CXX0X__
347 std::swap( count, v.count );
348 v.ccgsl_pointer = nullptr;
349 }
356 workspace( std::move( v ) ).swap( *this );
357 return *this;
358 }
359#endif
360 // Refines equality comparable
361 // == operator
368 bool operator==( workspace const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
369 // != operator
376 bool operator!=( workspace const& v ) const { return not operator==( v ); }
377 // Refines forward container
378 // Refines less than comparable
379 // operator<
388 bool operator<( workspace const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
389 // operator>
398 bool operator>( workspace const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
399 // operator<=
408 bool operator<=( workspace const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
409 // operator>=
418 bool operator>=( workspace const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
423 bool empty() const { return ccgsl_pointer == 0; }
424 // swap() --- should work even if sizes don't match
430 void swap( workspace& v ){
431 std::swap( ccgsl_pointer, v.ccgsl_pointer );
432 std::swap( count, v.count );
433 }
434 private:
438 gsl_fft_real_workspace* ccgsl_pointer;
442 size_t* count;
443 public:
444 // shared reference functions
449 gsl_fft_real_workspace* get() const { return ccgsl_pointer; }
455 bool unique() const { return count != 0 and *count == 1; }
460 size_t use_count() const { return count == 0 ? 0 : *count; }
466#ifdef __GXX_EXPERIMENTAL_CXX0X__
467 explicit
468#endif
469 operator bool() const { return ccgsl_pointer != 0; }
470 };
471
481 inline int transform( double data[], size_t const stride, size_t const n,
482 wavetable const& wavetable, workspace& work ){
483 return gsl_fft_real_transform( data, stride, n, wavetable.get(), work.get() ); }
484
493 inline int unpack( double const real_coefficient[], double complex_coefficient[],
494 size_t const stride, size_t const n ){
495 return gsl_fft_real_unpack( real_coefficient, complex_coefficient, stride, n ); }
496
506 template<typename DATA>
507 inline int transform( DATA& data, size_t const stride, wavetable const& wavetable, workspace& work ){
508 return gsl_fft_real_transform( data.data(), stride, data.size() / stride,
509 wavetable.get(), work.get() ); }
510
519 template<typename R, typename C>
520 inline int unpack( R const& real_coefficient, C& complex_coefficient, size_t const stride ){
521 size_t n = std::max( real_coefficient.size(), complex_coefficient.size() / 2) / stride;
522 return gsl_fft_real_unpack( real_coefficient.data(), complex_coefficient.data(),
523 stride, n ); }
524 // Partial specialisation
525
534 template<>
535 inline int unpack( vector const& real_coefficient,
536 vector_complex& complex_coefficient, size_t const stride ){
537 size_t n = std::max( real_coefficient.size(), complex_coefficient.size() ) / stride;
538 return gsl_fft_real_unpack( real_coefficient.data(), complex_coefficient.data(),
539 stride, n ); }
540
541 // stride-free versions
542
551 inline int transform( double data[], size_t const n,
552 wavetable const& wavetable, workspace& work ){
553 return gsl_fft_real_transform( data, 1, n, wavetable.get(), work.get() ); }
554
562 inline int unpack( double const real_coefficient[], double complex_coefficient[], size_t const n ){
563 return gsl_fft_real_unpack( real_coefficient, complex_coefficient, 1, n ); }
564
573 template<typename DATA>
574 inline int transform( DATA& data, wavetable const& wavetable, workspace& work ){
575 return gsl_fft_real_transform( data.data(), 1, data.size(),
576 wavetable.get(), work.get() ); }
577
585 template<typename R, typename C>
586 inline int unpack( R const& real_coefficient, C& complex_coefficient ){
587 size_t n = std::max( real_coefficient.size(), complex_coefficient.size() / 2);
588 return gsl_fft_real_unpack( real_coefficient.data(), complex_coefficient.data(),
589 1, n ); }
590 // Partial specialisation
591
599 template<>
600 inline int unpack( vector const& real_coefficient,
601 vector_complex& complex_coefficient ){
602 size_t n = std::max( real_coefficient.size(), complex_coefficient.size() );
603 return gsl_fft_real_unpack( real_coefficient.data(), complex_coefficient.data(),
604 1, n ); }
605
606 }
607 }
608}
609
610#endif
C++ version of gsl_fft_real_wavetable functions.
Definition: fft_real.hpp:71
bool empty() const
Find if the wavetable is empty.
Definition: fft_real.hpp:221
void swap(wavetable &v)
Swap two wavetable.
Definition: fft_real.hpp:228
wavetable(wavetable &&v)
Move constructor.
Definition: fft_real.hpp:144
size_t use_count() const
Find how many workspace objects share this pointer.
Definition: fft_real.hpp:258
wavetable(gsl_fft_real_wavetable *v)
Could construct from a gsl_fft_real_wavetable.
Definition: fft_real.hpp:102
gsl_fft_real_wavetable * ccgsl_pointer
The shared pointer.
Definition: fft_real.hpp:236
bool operator<=(wavetable const &v) const
A container needs to define an ordering for sorting.
Definition: fft_real.hpp:206
bool operator<(wavetable const &v) const
A container needs to define an ordering for sorting.
Definition: fft_real.hpp:186
wavetable()
The default constructor is only really useful for assigning to.
Definition: fft_real.hpp:76
bool operator>=(wavetable const &v) const
A container needs to define an ordering for sorting.
Definition: fft_real.hpp:216
bool operator!=(wavetable const &v) const
Two wavetable are different equal if their elements are not identical.
Definition: fft_real.hpp:174
wavetable(wavetable const &v)
The copy constructor.
Definition: fft_real.hpp:113
wavetable & operator=(wavetable &&v)
Move operator.
Definition: fft_real.hpp:153
gsl_fft_real_wavetable * get() const
Get the gsl_fft_real_wavetable.
Definition: fft_real.hpp:247
wavetable & operator=(wavetable const &v)
The assignment operator.
Definition: fft_real.hpp:120
size_t * count
The shared reference count.
Definition: fft_real.hpp:240
bool unique() const
Find if this is the only object sharing the gsl_fft_real_wavetable.
Definition: fft_real.hpp:253
~wavetable()
The destructor only deletes the pointers if count reaches zero.
Definition: fft_real.hpp:132
wavetable(size_t const n)
The default constructor creates a new workspace of size n.
Definition: fft_real.hpp:86
bool operator>(wavetable const &v) const
A container needs to define an ordering for sorting.
Definition: fft_real.hpp:196
bool operator==(wavetable const &v) const
Two wavetable are identically equal if their elements are identical.
Definition: fft_real.hpp:166
Workspace for real fast fourier transforms.
Definition: fft_real.hpp:273
bool unique() const
Find if this is the only object sharing the gsl_fft_real_workspace.
Definition: fft_real.hpp:455
bool empty() const
Find if the workspace is empty.
Definition: fft_real.hpp:423
workspace(gsl_fft_real_workspace *v)
Could construct from a gsl_fft_real_workspace.
Definition: fft_real.hpp:304
gsl_fft_real_workspace * ccgsl_pointer
The shared pointer.
Definition: fft_real.hpp:438
size_t * count
The shared reference count.
Definition: fft_real.hpp:442
bool operator==(workspace const &v) const
Two workspace are identically equal if their elements are identical.
Definition: fft_real.hpp:368
bool operator<(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: fft_real.hpp:388
workspace & operator=(workspace &&v)
Move operator.
Definition: fft_real.hpp:355
workspace()
The default constructor is only really useful for assigning to.
Definition: fft_real.hpp:278
bool operator>(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: fft_real.hpp:398
gsl_fft_real_workspace * get() const
Get the gsl_fft_real_workspace.
Definition: fft_real.hpp:449
bool operator<=(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: fft_real.hpp:408
workspace(workspace &&v)
Move constructor.
Definition: fft_real.hpp:346
workspace & operator=(workspace const &v)
The assignment operator.
Definition: fft_real.hpp:322
bool operator!=(workspace const &v) const
Two workspace are different equal if their elements are not identical.
Definition: fft_real.hpp:376
~workspace()
The destructor only deletes the pointers if count reaches zero.
Definition: fft_real.hpp:334
workspace(size_t const n)
The default constructor creates a new workspace of size n.
Definition: fft_real.hpp:288
void swap(workspace &v)
Swap two workspace.
Definition: fft_real.hpp:430
workspace(workspace const &v)
The copy constructor.
Definition: fft_real.hpp:315
bool operator>=(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: fft_real.hpp:418
size_t use_count() const
Find how many workspace objects share this pointer.
Definition: fft_real.hpp:460
This class handles vector_complex objects as shared handles.
size_type size() const
The size (number of elements) of the vector_complex.
double * data()
Give access to the data block.
This class handles vector objects as shared handles.
Definition: vector.hpp:74
size_type size() const
The size (number of elements) of the vector.
Definition: vector.hpp:1169
double * data()
Give access to the data block.
Definition: vector.hpp:1177
int transform(double data[], size_t const stride, size_t const n)
C++ version of gsl_fft_real_radix2_transform().
Definition: fft_real.hpp:44
int transform(double data[], size_t const stride, size_t const n, wavetable const &wavetable, workspace &work)
C++ version of gsl_fft_real_transform().
Definition: fft_real.hpp:481
int unpack(double const real_coefficient[], double complex_coefficient[], size_t const stride, size_t const n)
C++ version of gsl_fft_real_unpack().
Definition: fft_real.hpp:493
int max(movstat::end_t const endtype, vector const &x, vector &y, workspace &w)
C++ version of gsl_movstat_max().
Definition: movstat.hpp:592
size_t n(workspace const &w)
C++ version of gsl_rstat_n().
Definition: rstat.hpp:299
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34