-
Notifications
You must be signed in to change notification settings - Fork 58
/
pffft.hpp
1060 lines (873 loc) · 30.8 KB
/
pffft.hpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2020 Dario Mambro ( [email protected] )
Copyright (c) 2020 Hayati Ayguen ( [email protected] )
Redistribution and use of the Software in source and binary forms,
with or without modification, is permitted provided that the
following conditions are met:
- Neither the names of PFFFT, nor the names of its
sponsors or contributors may be used to endorse or promote products
derived from this Software without specific prior written permission.
- Redistributions of source code must retain the above copyright
notices, this list of conditions, and the disclaimer below.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer below in the
documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
SOFTWARE.
*/
#pragma once
#include <complex>
#include <vector>
#include <limits>
#include <cassert>
namespace pffft {
namespace detail {
#if defined(PFFFT_ENABLE_FLOAT) || ( !defined(PFFFT_ENABLE_FLOAT) && !defined(PFFFT_ENABLE_DOUBLE) )
#include "pffft.h"
#endif
#if defined(PFFFT_ENABLE_DOUBLE)
#include "pffft_double.h"
#endif
}
}
namespace pffft {
// enum { PFFFT_REAL, PFFFT_COMPLEX }
typedef detail::pffft_transform_t TransformType;
// define 'Scalar' and 'Complex' (in namespace pffft) with template Types<>
// and other type specific helper functions
template<typename T> struct Types {};
#if defined(PFFFT_ENABLE_FLOAT) || ( !defined(PFFFT_ENABLE_FLOAT) && !defined(PFFFT_ENABLE_DOUBLE) )
template<> struct Types<float> {
typedef float Scalar;
typedef std::complex<Scalar> Complex;
static int simd_size() { return detail::pffft_simd_size(); }
static const char * simd_arch() { return detail::pffft_simd_arch(); }
static int minFFtsize() { return pffft_min_fft_size(detail::PFFFT_REAL); }
static bool isValidSize(int N) { return pffft_is_valid_size(N, detail::PFFFT_REAL); }
static int nearestTransformSize(int N, bool higher) { return pffft_nearest_transform_size(N, detail::PFFFT_REAL, higher ? 1 : 0); }
};
template<> struct Types< std::complex<float> > {
typedef float Scalar;
typedef std::complex<float> Complex;
static int simd_size() { return detail::pffft_simd_size(); }
static const char * simd_arch() { return detail::pffft_simd_arch(); }
static int minFFtsize() { return pffft_min_fft_size(detail::PFFFT_COMPLEX); }
static bool isValidSize(int N) { return pffft_is_valid_size(N, detail::PFFFT_COMPLEX); }
static int nearestTransformSize(int N, bool higher) { return pffft_nearest_transform_size(N, detail::PFFFT_COMPLEX, higher ? 1 : 0); }
};
#endif
#if defined(PFFFT_ENABLE_DOUBLE)
template<> struct Types<double> {
typedef double Scalar;
typedef std::complex<Scalar> Complex;
static int simd_size() { return detail::pffftd_simd_size(); }
static const char * simd_arch() { return detail::pffftd_simd_arch(); }
static int minFFtsize() { return pffftd_min_fft_size(detail::PFFFT_REAL); }
static bool isValidSize(int N) { return pffftd_is_valid_size(N, detail::PFFFT_REAL); }
static int nearestTransformSize(int N, bool higher) { return pffftd_nearest_transform_size(N, detail::PFFFT_REAL, higher ? 1 : 0); }
};
template<> struct Types< std::complex<double> > {
typedef double Scalar;
typedef std::complex<double> Complex;
static int simd_size() { return detail::pffftd_simd_size(); }
static const char * simd_arch() { return detail::pffftd_simd_arch(); }
static int minFFtsize() { return pffftd_min_fft_size(detail::PFFFT_COMPLEX); }
static bool isValidSize(int N) { return pffftd_is_valid_size(N, detail::PFFFT_COMPLEX); }
static int nearestTransformSize(int N, bool higher) { return pffftd_nearest_transform_size(N, detail::PFFFT_COMPLEX, higher ? 1 : 0); }
};
#endif
// Allocator
template<typename T> class PFAlloc;
namespace detail {
template<typename T> class Setup;
}
#if (__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900))
// define AlignedVector<T> utilizing 'using' in C++11
template<typename T>
using AlignedVector = typename std::vector< T, PFAlloc<T> >;
#else
// define AlignedVector<T> having to derive std::vector<>
template <typename T>
struct AlignedVector : public std::vector< T, PFAlloc<T> > {
AlignedVector() : std::vector< T, PFAlloc<T> >() { }
AlignedVector(int N) : std::vector< T, PFAlloc<T> >(N) { }
};
#endif
// T can be float, double, std::complex<float> or std::complex<double>
// define PFFFT_ENABLE_DOUBLE before include this file for double and std::complex<double>
template<typename T>
class Fft
{
public:
// define types value_type, Scalar and Complex
typedef T value_type;
typedef typename Types<T>::Scalar Scalar;
typedef typename Types<T>::Complex Complex;
// static retrospection functions
static bool isComplexTransform() { return sizeof(T) == sizeof(Complex); }
static bool isFloatScalar() { return sizeof(Scalar) == sizeof(float); }
static bool isDoubleScalar() { return sizeof(Scalar) == sizeof(double); }
// simple helper to determine next power of 2 - without inexact/rounding floating point operations
static int nextPowerOfTwo(int N) { return detail::pffft_next_power_of_two(N); }
static bool isPowerOfTwo(int N) { return detail::pffft_is_power_of_two(N) ? true : false; }
static int simd_size() { return Types<T>::simd_size(); }
static const char * simd_arch() { return Types<T>::simd_arch(); }
// simple helper to get minimum possible fft length
static int minFFtsize() { return Types<T>::minFFtsize(); }
// helper to determine nearest transform size - factorizable to minFFtsize() with factors 2, 3, 5
static bool isValidSize(int N) { return Types<T>::isValidSize(N); }
static int nearestTransformSize(int N, bool higher=true) { return Types<T>::nearestTransformSize(N, higher); }
//////////////////
/*
* Contructor, with transformation length, preparing transforms.
*
* For length <= stackThresholdLen, the stack is used for the internal
* work memory. for bigger length', the heap is used.
*
* Using the stack is probably the best strategy for small
* FFTs, say for N <= 4096). Threads usually have a small stack, that
* there's no sufficient amount of memory, usually leading to a crash!
*/
Fft( int length, int stackThresholdLen = 4096 );
/*
* constructor or prepareLength() produced a valid FFT instance?
* delivers false for invalid FFT sizes
*/
bool isValid() const;
~Fft();
/*
* prepare for transformation length 'newLength'.
* length is identical to forward()'s input vector's size,
* and also equals inverse()'s output vector size.
* this function is no simple setter. it pre-calculates twiddle factors.
* returns true if newLength is >= minFFtsize, false otherwise
*/
bool prepareLength(int newLength);
/*
* retrieve the transformation length.
*/
int getLength() const { return length; }
/*
* retrieve size of complex spectrum vector,
* the output of forward()
*/
int getSpectrumSize() const { return isComplexTransform() ? length : ( length / 2 ); }
/*
* retrieve size of spectrum vector - in internal layout;
* the output of forwardToInternalLayout()
*/
int getInternalLayoutSize() const { return isComplexTransform() ? ( 2 * length ) : length; }
////////////////////////////////////////////
////
//// API 1, with std::vector<> based containers,
//// which free the allocated memory themselves (RAII).
////
//// uses an Allocator for the alignment of SIMD data.
////
////////////////////////////////////////////
// create suitably preallocated aligned vector for one FFT
AlignedVector<T> valueVector() const;
AlignedVector<Complex> spectrumVector() const;
AlignedVector<Scalar> internalLayoutVector() const;
////////////////////////////////////////////
// although using Vectors for output ..
// they need to have resize() applied before!
// core API, having the spectrum in canonical order
/*
* Perform the forward Fourier transform.
*
* Transforms are not scaled: inverse(forward(x)) = N*x.
* Typically you will want to scale the backward transform by 1/N.
*
* The output 'spectrum' is canonically ordered - as expected.
*
* a) for complex input isComplexTransform() == true,
* and transformation length N the output array is complex:
* index k in 0 .. N/2 -1 corresponds to frequency k * Samplerate / N
* index k in N/2 .. N -1 corresponds to frequency (k -N) * Samplerate / N,
* resulting in negative frequencies
*
* b) for real input isComplexTransform() == false,
* and transformation length N the output array is 'mostly' complex:
* index k in 1 .. N/2 -1 corresponds to frequency k * Samplerate / N
* index k == 0 is a special case:
* the real() part contains the result for the DC frequency 0,
* the imag() part contains the result for the Nyquist frequency Samplerate/2
* both 0-frequency and half frequency components, which are real,
* are assembled in the first entry as F(0)+i*F(N/2).
* with the output size N/2 complex values, it is obvious, that the
* result for negative frequencies are not output, cause of symmetry.
*
* input and output may alias - if you do nasty type conversion.
* return is just the given output parameter 'spectrum'.
*/
AlignedVector<Complex> & forward(const AlignedVector<T> & input, AlignedVector<Complex> & spectrum);
/*
* Perform the inverse Fourier transform, see forward().
* return is just the given output parameter 'output'.
*/
AlignedVector<T> & inverse(const AlignedVector<Complex> & spectrum, AlignedVector<T> & output);
// provide additional functions with spectrum in some internal Layout.
// these are faster, cause the implementation omits the reordering.
// these are useful in special applications, like fast convolution,
// where inverse() is following anyway ..
/*
* Perform the forward Fourier transform - similar to forward(), BUT:
*
* The z-domain data is stored in the most efficient order
* for transforming it back, or using it for convolution.
* If you need to have its content sorted in the "usual" canonical order,
* either use forward(), or call reorderSpectrum() after calling
* forwardToInternalLayout(), and before the backward fft
*
* return is just the given output parameter 'spectrum_internal_layout'.
*/
AlignedVector<Scalar> & forwardToInternalLayout(
const AlignedVector<T> & input,
AlignedVector<Scalar> & spectrum_internal_layout );
/*
* Perform the inverse Fourier transform, see forwardToInternalLayout()
*
* return is just the given output parameter 'output'.
*/
AlignedVector<T> & inverseFromInternalLayout(
const AlignedVector<Scalar> & spectrum_internal_layout,
AlignedVector<T> & output );
/*
* Reorder the spectrum from internal layout to have the
* frequency components in the correct "canonical" order.
* see forward() for a description of the canonical order.
*
* input and output should not alias.
*/
void reorderSpectrum(
const AlignedVector<Scalar> & input,
AlignedVector<Complex> & output );
/*
* Perform a multiplication of the frequency components of
* spectrum_internal_a and spectrum_internal_b
* into spectrum_internal_ab.
* The arrays should have been obtained with forwardToInternalLayout)
* and should *not* have been reordered with reorderSpectrum().
*
* the operation performed is:
* spectrum_internal_ab = (spectrum_internal_a * spectrum_internal_b)*scaling
*
* The spectrum_internal_[a][b], pointers may alias.
* return is just the given output parameter 'spectrum_internal_ab'.
*/
AlignedVector<Scalar> & convolve(
const AlignedVector<Scalar> & spectrum_internal_a,
const AlignedVector<Scalar> & spectrum_internal_b,
AlignedVector<Scalar> & spectrum_internal_ab,
const Scalar scaling );
/*
* Perform a multiplication and accumulation of the frequency components
* - similar to convolve().
*
* the operation performed is:
* spectrum_internal_ab += (spectrum_internal_a * spectrum_internal_b)*scaling
*
* The spectrum_internal_[a][b], pointers may alias.
* return is just the given output parameter 'spectrum_internal_ab'.
*/
AlignedVector<Scalar> & convolveAccumulate(
const AlignedVector<Scalar> & spectrum_internal_a,
const AlignedVector<Scalar> & spectrum_internal_b,
AlignedVector<Scalar> & spectrum_internal_ab,
const Scalar scaling );
////////////////////////////////////////////
////
//// API 2, dealing with raw pointers,
//// which need to be deallocated using alignedFree()
////
//// the special allocation is required cause SIMD
//// implementations require aligned memory
////
//// Method descriptions are equal to the methods above,
//// having AlignedVector<T> parameters - instead of raw pointers.
//// That is why following methods have no documentation.
////
////////////////////////////////////////////
static void alignedFree(void* ptr);
static T * alignedAllocType(int length);
static Scalar* alignedAllocScalar(int length);
static Complex* alignedAllocComplex(int length);
// core API, having the spectrum in canonical order
Complex* forward(const T* input, Complex* spectrum);
T* inverse(const Complex* spectrum, T* output);
// provide additional functions with spectrum in some internal Layout.
// these are faster, cause the implementation omits the reordering.
// these are useful in special applications, like fast convolution,
// where inverse() is following anyway ..
Scalar* forwardToInternalLayout(const T* input,
Scalar* spectrum_internal_layout);
T* inverseFromInternalLayout(const Scalar* spectrum_internal_layout, T* output);
void reorderSpectrum(const Scalar* input, Complex* output );
Scalar* convolve(const Scalar* spectrum_internal_a,
const Scalar* spectrum_internal_b,
Scalar* spectrum_internal_ab,
const Scalar scaling);
Scalar* convolveAccumulate(const Scalar* spectrum_internal_a,
const Scalar* spectrum_internal_b,
Scalar* spectrum_internal_ab,
const Scalar scaling);
private:
detail::Setup<T> setup;
Scalar* work;
int length;
int stackThresholdLen;
};
template<typename T>
inline T* alignedAlloc(int length) {
return (T*)detail::pffft_aligned_malloc( length * sizeof(T) );
}
inline void alignedFree(void *ptr) {
detail::pffft_aligned_free(ptr);
}
// simple helper to determine next power of 2 - without inexact/rounding floating point operations
inline int nextPowerOfTwo(int N) {
return detail::pffft_next_power_of_two(N);
}
inline bool isPowerOfTwo(int N) {
return detail::pffft_is_power_of_two(N) ? true : false;
}
////////////////////////////////////////////////////////////////////
// implementation
namespace detail {
template<typename T>
class Setup
{};
#if defined(PFFFT_ENABLE_FLOAT) || ( !defined(PFFFT_ENABLE_FLOAT) && !defined(PFFFT_ENABLE_DOUBLE) )
template<>
class Setup<float>
{
PFFFT_Setup* self;
public:
typedef float value_type;
typedef Types< value_type >::Scalar Scalar;
Setup()
: self(NULL)
{}
~Setup() { pffft_destroy_setup(self); }
void prepareLength(int length)
{
if (self) {
pffft_destroy_setup(self);
}
self = pffft_new_setup(length, PFFFT_REAL);
}
bool isValid() const { return (self); }
void transform_ordered(const Scalar* input,
Scalar* output,
Scalar* work,
pffft_direction_t direction)
{
pffft_transform_ordered(self, input, output, work, direction);
}
void transform(const Scalar* input,
Scalar* output,
Scalar* work,
pffft_direction_t direction)
{
pffft_transform(self, input, output, work, direction);
}
void reorder(const Scalar* input, Scalar* output, pffft_direction_t direction)
{
pffft_zreorder(self, input, output, direction);
}
void convolveAccumulate(const Scalar* dft_a,
const Scalar* dft_b,
Scalar* dft_ab,
const Scalar scaling)
{
pffft_zconvolve_accumulate(self, dft_a, dft_b, dft_ab, scaling);
}
void convolve(const Scalar* dft_a,
const Scalar* dft_b,
Scalar* dft_ab,
const Scalar scaling)
{
pffft_zconvolve_no_accu(self, dft_a, dft_b, dft_ab, scaling);
}
};
template<>
class Setup< std::complex<float> >
{
PFFFT_Setup* self;
public:
typedef std::complex<float> value_type;
typedef Types< value_type >::Scalar Scalar;
Setup()
: self(NULL)
{}
~Setup() { pffft_destroy_setup(self); }
void prepareLength(int length)
{
if (self) {
pffft_destroy_setup(self);
}
self = pffft_new_setup(length, PFFFT_COMPLEX);
}
bool isValid() const { return (self); }
void transform_ordered(const Scalar* input,
Scalar* output,
Scalar* work,
pffft_direction_t direction)
{
pffft_transform_ordered(self, input, output, work, direction);
}
void transform(const Scalar* input,
Scalar* output,
Scalar* work,
pffft_direction_t direction)
{
pffft_transform(self, input, output, work, direction);
}
void reorder(const Scalar* input, Scalar* output, pffft_direction_t direction)
{
pffft_zreorder(self, input, output, direction);
}
void convolve(const Scalar* dft_a,
const Scalar* dft_b,
Scalar* dft_ab,
const Scalar scaling)
{
pffft_zconvolve_no_accu(self, dft_a, dft_b, dft_ab, scaling);
}
};
#endif /* defined(PFFFT_ENABLE_FLOAT) || ( !defined(PFFFT_ENABLE_FLOAT) && !defined(PFFFT_ENABLE_DOUBLE) ) */
#if defined(PFFFT_ENABLE_DOUBLE)
template<>
class Setup<double>
{
PFFFTD_Setup* self;
public:
typedef double value_type;
typedef Types< value_type >::Scalar Scalar;
Setup()
: self(NULL)
{}
~Setup() { pffftd_destroy_setup(self); }
void prepareLength(int length)
{
if (self) {
pffftd_destroy_setup(self);
self = NULL;
}
if (length > 0) {
self = pffftd_new_setup(length, PFFFT_REAL);
}
}
bool isValid() const { return (self); }
void transform_ordered(const Scalar* input,
Scalar* output,
Scalar* work,
pffft_direction_t direction)
{
pffftd_transform_ordered(self, input, output, work, direction);
}
void transform(const Scalar* input,
Scalar* output,
Scalar* work,
pffft_direction_t direction)
{
pffftd_transform(self, input, output, work, direction);
}
void reorder(const Scalar* input, Scalar* output, pffft_direction_t direction)
{
pffftd_zreorder(self, input, output, direction);
}
void convolveAccumulate(const Scalar* dft_a,
const Scalar* dft_b,
Scalar* dft_ab,
const Scalar scaling)
{
pffftd_zconvolve_accumulate(self, dft_a, dft_b, dft_ab, scaling);
}
void convolve(const Scalar* dft_a,
const Scalar* dft_b,
Scalar* dft_ab,
const Scalar scaling)
{
pffftd_zconvolve_no_accu(self, dft_a, dft_b, dft_ab, scaling);
}
};
template<>
class Setup< std::complex<double> >
{
PFFFTD_Setup* self;
public:
typedef std::complex<double> value_type;
typedef Types< value_type >::Scalar Scalar;
Setup()
: self(NULL)
{}
~Setup() { pffftd_destroy_setup(self); }
void prepareLength(int length)
{
if (self) {
pffftd_destroy_setup(self);
}
self = pffftd_new_setup(length, PFFFT_COMPLEX);
}
bool isValid() const { return (self); }
void transform_ordered(const Scalar* input,
Scalar* output,
Scalar* work,
pffft_direction_t direction)
{
pffftd_transform_ordered(self, input, output, work, direction);
}
void transform(const Scalar* input,
Scalar* output,
Scalar* work,
pffft_direction_t direction)
{
pffftd_transform(self, input, output, work, direction);
}
void reorder(const Scalar* input, Scalar* output, pffft_direction_t direction)
{
pffftd_zreorder(self, input, output, direction);
}
void convolveAccumulate(const Scalar* dft_a,
const Scalar* dft_b,
Scalar* dft_ab,
const Scalar scaling)
{
pffftd_zconvolve_accumulate(self, dft_a, dft_b, dft_ab, scaling);
}
void convolve(const Scalar* dft_a,
const Scalar* dft_b,
Scalar* dft_ab,
const Scalar scaling)
{
pffftd_zconvolve_no_accu(self, dft_a, dft_b, dft_ab, scaling);
}
};
#endif /* defined(PFFFT_ENABLE_DOUBLE) */
} // end of anonymous namespace for Setup<>
template<typename T>
inline Fft<T>::Fft(int length, int stackThresholdLen)
: work(NULL)
, length(0)
, stackThresholdLen(stackThresholdLen)
{
#if (__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900))
static_assert( sizeof(Complex) == 2 * sizeof(Scalar), "pffft requires sizeof(std::complex<>) == 2 * sizeof(Scalar)" );
#elif defined(__GNUC__)
char static_assert_like[(sizeof(Complex) == 2 * sizeof(Scalar)) ? 1 : -1]; // pffft requires sizeof(std::complex<>) == 2 * sizeof(Scalar)
#endif
prepareLength(length);
}
template<typename T>
inline Fft<T>::~Fft()
{
alignedFree(work);
}
template<typename T>
inline bool
Fft<T>::isValid() const
{
return setup.isValid();
}
template<typename T>
inline bool
Fft<T>::prepareLength(int newLength)
{
if(newLength < minFFtsize())
return false;
const bool wasOnHeap = ( work != NULL );
const bool useHeap = newLength > stackThresholdLen;
if (useHeap == wasOnHeap && newLength == length) {
return true;
}
length = 0;
setup.prepareLength(newLength);
if (!setup.isValid())
return false;
length = newLength;
if (work) {
alignedFree(work);
work = NULL;
}
if (useHeap) {
work = reinterpret_cast<Scalar*>( alignedAllocType(length) );
}
return true;
}
template<typename T>
inline AlignedVector<T>
Fft<T>::valueVector() const
{
return AlignedVector<T>(length);
}
template<typename T>
inline AlignedVector< typename Fft<T>::Complex >
Fft<T>::spectrumVector() const
{
return AlignedVector<Complex>( getSpectrumSize() );
}
template<typename T>
inline AlignedVector< typename Fft<T>::Scalar >
Fft<T>::internalLayoutVector() const
{
return AlignedVector<Scalar>( getInternalLayoutSize() );
}
template<typename T>
inline AlignedVector< typename Fft<T>::Complex > &
Fft<T>::forward(const AlignedVector<T> & input, AlignedVector<Complex> & spectrum)
{
forward( input.data(), spectrum.data() );
return spectrum;
}
template<typename T>
inline AlignedVector<T> &
Fft<T>::inverse(const AlignedVector<Complex> & spectrum, AlignedVector<T> & output)
{
inverse( spectrum.data(), output.data() );
return output;
}
template<typename T>
inline AlignedVector< typename Fft<T>::Scalar > &
Fft<T>::forwardToInternalLayout(
const AlignedVector<T> & input,
AlignedVector<Scalar> & spectrum_internal_layout )
{
forwardToInternalLayout( input.data(), spectrum_internal_layout.data() );
return spectrum_internal_layout;
}
template<typename T>
inline AlignedVector<T> &
Fft<T>::inverseFromInternalLayout(
const AlignedVector<Scalar> & spectrum_internal_layout,
AlignedVector<T> & output )
{
inverseFromInternalLayout( spectrum_internal_layout.data(), output.data() );
return output;
}
template<typename T>
inline void
Fft<T>::reorderSpectrum(
const AlignedVector<Scalar> & input,
AlignedVector<Complex> & output )
{
reorderSpectrum( input.data(), output.data() );
}
template<typename T>
inline AlignedVector< typename Fft<T>::Scalar > &
Fft<T>::convolveAccumulate(
const AlignedVector<Scalar> & spectrum_internal_a,
const AlignedVector<Scalar> & spectrum_internal_b,
AlignedVector<Scalar> & spectrum_internal_ab,
const Scalar scaling )
{
convolveAccumulate( spectrum_internal_a.data(), spectrum_internal_b.data(),
spectrum_internal_ab.data(), scaling );
return spectrum_internal_ab;
}
template<typename T>
inline AlignedVector< typename Fft<T>::Scalar > &
Fft<T>::convolve(
const AlignedVector<Scalar> & spectrum_internal_a,
const AlignedVector<Scalar> & spectrum_internal_b,
AlignedVector<Scalar> & spectrum_internal_ab,
const Scalar scaling )
{
convolve( spectrum_internal_a.data(), spectrum_internal_b.data(),
spectrum_internal_ab.data(), scaling );
return spectrum_internal_ab;
}
template<typename T>
inline typename Fft<T>::Complex *
Fft<T>::forward(const T* input, Complex * spectrum)
{
assert(isValid());
setup.transform_ordered(reinterpret_cast<const Scalar*>(input),
reinterpret_cast<Scalar*>(spectrum),
work,
detail::PFFFT_FORWARD);
return spectrum;
}
template<typename T>
inline T*
Fft<T>::inverse(Complex const* spectrum, T* output)
{
assert(isValid());
setup.transform_ordered(reinterpret_cast<const Scalar*>(spectrum),
reinterpret_cast<Scalar*>(output),
work,
detail::PFFFT_BACKWARD);
return output;
}
template<typename T>
inline typename pffft::Fft<T>::Scalar*
Fft<T>::forwardToInternalLayout(const T* input, Scalar* spectrum_internal_layout)
{
assert(isValid());
setup.transform(reinterpret_cast<const Scalar*>(input),
spectrum_internal_layout,
work,
detail::PFFFT_FORWARD);
return spectrum_internal_layout;
}
template<typename T>
inline T*
Fft<T>::inverseFromInternalLayout(const Scalar* spectrum_internal_layout, T* output)
{
assert(isValid());
setup.transform(spectrum_internal_layout,
reinterpret_cast<Scalar*>(output),
work,
detail::PFFFT_BACKWARD);
return output;
}
template<typename T>
inline void
Fft<T>::reorderSpectrum( const Scalar* input, Complex* output )
{
assert(isValid());
setup.reorder(input, reinterpret_cast<Scalar*>(output), detail::PFFFT_FORWARD);
}
template<typename T>
inline typename pffft::Fft<T>::Scalar*
Fft<T>::convolveAccumulate(const Scalar* dft_a,
const Scalar* dft_b,
Scalar* dft_ab,
const Scalar scaling)
{
assert(isValid());
setup.convolveAccumulate(dft_a, dft_b, dft_ab, scaling);
return dft_ab;
}
template<typename T>
inline typename pffft::Fft<T>::Scalar*
Fft<T>::convolve(const Scalar* dft_a,
const Scalar* dft_b,
Scalar* dft_ab,
const Scalar scaling)
{
assert(isValid());
setup.convolve(dft_a, dft_b, dft_ab, scaling);
return dft_ab;
}
template<typename T>
inline void
Fft<T>::alignedFree(void* ptr)
{
pffft::alignedFree(ptr);
}
template<typename T>
inline T*
pffft::Fft<T>::alignedAllocType(int length)
{
return alignedAlloc<T>(length);
}
template<typename T>
inline typename pffft::Fft<T>::Scalar*
pffft::Fft<T>::alignedAllocScalar(int length)
{
return alignedAlloc<Scalar>(length);
}
template<typename T>
inline typename Fft<T>::Complex *
Fft<T>::alignedAllocComplex(int length)
{
return alignedAlloc<Complex>(length);
}
////////////////////////////////////////////////////////////////////
// Allocator - for std::vector<>:
// origin: http://www.josuttis.com/cppcode/allocator.html
// http://www.josuttis.com/cppcode/myalloc.hpp
//
// minor renaming and utilizing of pffft (de)allocation functions
// are applied to Jossutis' allocator
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
template <class T>
class PFAlloc {
public:
// type definitions
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
// rebind allocator to type U
template <class U>
struct rebind {
typedef PFAlloc<U> other;
};
// return address of values
pointer address (reference value) const {
return &value;
}
const_pointer address (const_reference value) const {
return &value;