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
|
/*****************************************************************************
FFTReal.hpp
Copyright (c) 2005 Laurent de Soras
--- Legal stuff ---
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*Tab=3***********************************************************************/
#if defined (FFTReal_CURRENT_CODEHEADER)
#error Recursive inclusion of FFTReal code header.
#endif
#define FFTReal_CURRENT_CODEHEADER
#if ! defined (FFTReal_CODEHEADER_INCLUDED)
#define FFTReal_CODEHEADER_INCLUDED
/*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
#include <cassert>
#include <cmath>
static inline bool FFTReal_is_pow2 (long x)
{
assert (x > 0);
return ((x & -x) == x);
}
static inline int FFTReal_get_next_pow2 (long x)
{
--x;
int p = 0;
while ((x & ~0xFFFFL) != 0)
{
p += 16;
x >>= 16;
}
while ((x & ~0xFL) != 0)
{
p += 4;
x >>= 4;
}
while (x > 0)
{
++p;
x >>= 1;
}
return (p);
}
/*\\\ PUBLIC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
/*
==============================================================================
Name: ctor
Input parameters:
- length: length of the array on which we want to do a FFT. Range: power of
2 only, > 0.
Throws: std::bad_alloc
==============================================================================
*/
template <class DT>
FFTReal <DT>::FFTReal (long length)
: _length (length)
, _nbr_bits (FFTReal_get_next_pow2 (length))
, _br_lut ()
, _trigo_lut ()
, _buffer (length)
, _trigo_osc ()
{
assert (FFTReal_is_pow2 (length));
assert (_nbr_bits <= MAX_BIT_DEPTH);
init_br_lut ();
init_trigo_lut ();
init_trigo_osc ();
}
/*
==============================================================================
Name: get_length
Description:
Returns the number of points processed by this FFT object.
Returns: The number of points, power of 2, > 0.
Throws: Nothing
==============================================================================
*/
template <class DT>
long FFTReal <DT>::get_length () const
{
return (_length);
}
/*
==============================================================================
Name: do_fft
Description:
Compute the FFT of the array.
Input parameters:
- x: pointer on the source array (time).
Output parameters:
- f: pointer on the destination array (frequencies).
f [0...length(x)/2] = real values,
f [length(x)/2+1...length(x)-1] = negative imaginary values of
coefficents 1...length(x)/2-1.
Throws: Nothing
==============================================================================
*/
template <class DT>
void FFTReal <DT>::do_fft (DataType f [], const DataType x []) const
{
assert (f != 0);
assert (f != use_buffer ());
assert (x != 0);
assert (x != use_buffer ());
assert (x != f);
// General case
if (_nbr_bits > 2)
{
compute_fft_general (f, x);
}
// 4-point FFT
else if (_nbr_bits == 2)
{
f [1] = x [0] - x [2];
f [3] = x [1] - x [3];
const DataType b_0 = x [0] + x [2];
const DataType b_2 = x [1] + x [3];
f [0] = b_0 + b_2;
f [2] = b_0 - b_2;
}
// 2-point FFT
else if (_nbr_bits == 1)
{
f [0] = x [0] + x [1];
f [1] = x [0] - x [1];
}
// 1-point FFT
else
{
f [0] = x [0];
}
}
/*
==============================================================================
Name: do_ifft
Description:
Compute the inverse FFT of the array. Note that data must be post-scaled:
IFFT (FFT (x)) = x * length (x).
Input parameters:
- f: pointer on the source array (frequencies).
f [0...length(x)/2] = real values
f [length(x)/2+1...length(x)-1] = negative imaginary values of
coefficents 1...length(x)/2-1.
Output parameters:
- x: pointer on the destination array (time).
Throws: Nothing
==============================================================================
*/
template <class DT>
void FFTReal <DT>::do_ifft (const DataType f [], DataType x []) const
{
assert (f != 0);
assert (f != use_buffer ());
assert (x != 0);
assert (x != use_buffer ());
assert (x != f);
// General case
if (_nbr_bits > 2)
{
compute_ifft_general (f, x);
}
// 4-point IFFT
else if (_nbr_bits == 2)
{
const DataType b_0 = f [0] + f [2];
const DataType b_2 = f [0] - f [2];
x [0] = b_0 + f [1] * 2;
x [2] = b_0 - f [1] * 2;
x [1] = b_2 + f [3] * 2;
x [3] = b_2 - f [3] * 2;
}
// 2-point IFFT
else if (_nbr_bits == 1)
{
x [0] = f [0] + f [1];
x [1] = f [0] - f [1];
}
// 1-point IFFT
else
{
x [0] = f [0];
}
}
/*
==============================================================================
Name: rescale
Description:
Scale an array by divide each element by its length. This function should
be called after FFT + IFFT.
Input parameters:
- x: pointer on array to rescale (time or frequency).
Throws: Nothing
==============================================================================
*/
template <class DT>
void FFTReal <DT>::rescale (DataType x []) const
{
const DataType mul = DataType (1.0 / _length);
if (_length < 4)
{
long i = _length - 1;
do
{
x [i] *= mul;
--i;
}
while (i >= 0);
}
else
{
assert ((_length & 3) == 0);
// Could be optimized with SIMD instruction sets (needs alignment check)
long i = _length - 4;
do
{
x [i + 0] *= mul;
x [i + 1] *= mul;
x [i + 2] *= mul;
x [i + 3] *= mul;
i -= 4;
}
while (i >= 0);
}
}
/*
==============================================================================
Name: use_buffer
Description:
Access the internal buffer, whose length is the FFT one.
Buffer content will be erased at each do_fft() / do_ifft() call!
This buffer cannot be used as:
- source for FFT or IFFT done with this object
- destination for FFT or IFFT done with this object
Returns:
Buffer start address
Throws: Nothing
==============================================================================
*/
template <class DT>
typename FFTReal <DT>::DataType * FFTReal <DT>::use_buffer () const
{
return (&_buffer [0]);
}
/*\\\ PROTECTED \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
/*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
template <class DT>
void FFTReal <DT>::init_br_lut ()
{
const long length = 1L << _nbr_bits;
_br_lut.resize (length);
_br_lut [0] = 0;
long br_index = 0;
for (long cnt = 1; cnt < length; ++cnt)
{
// ++br_index (bit reversed)
long bit = length >> 1;
while (((br_index ^= bit) & bit) == 0)
{
bit >>= 1;
}
_br_lut [cnt] = br_index;
}
}
template <class DT>
void FFTReal <DT>::init_trigo_lut ()
{
using namespace std;
if (_nbr_bits > 3)
{
const long total_len = (1L << (_nbr_bits - 1)) - 4;
_trigo_lut.resize (total_len);
for (int level = 3; level < _nbr_bits; ++level)
{
const long level_len = 1L << (level - 1);
DataType * const level_ptr =
&_trigo_lut [get_trigo_level_index (level)];
const double mul = PI / (level_len << 1);
for (long i = 0; i < level_len; ++ i)
{
level_ptr [i] = static_cast <DataType> (cos (i * mul));
}
}
}
}
template <class DT>
void FFTReal <DT>::init_trigo_osc ()
{
const int nbr_osc = _nbr_bits - TRIGO_BD_LIMIT;
if (nbr_osc > 0)
{
_trigo_osc.resize (nbr_osc);
for (int osc_cnt = 0; osc_cnt < nbr_osc; ++osc_cnt)
{
OscType & osc = _trigo_osc [osc_cnt];
const long len = 1L << (TRIGO_BD_LIMIT + osc_cnt);
const double mul = (0.5 * PI) / len;
osc.set_step (mul);
}
}
}
template <class DT>
const long * FFTReal <DT>::get_br_ptr () const
{
return (&_br_lut [0]);
}
template <class DT>
const typename FFTReal <DT>::DataType * FFTReal <DT>::get_trigo_ptr (int level) const
{
assert (level >= 3);
return (&_trigo_lut [get_trigo_level_index (level)]);
}
template <class DT>
long FFTReal <DT>::get_trigo_level_index (int level) const
{
assert (level >= 3);
return ((1L << (level - 1)) - 4);
}
// Transform in several passes
template <class DT>
void FFTReal <DT>::compute_fft_general (DataType f [], const DataType x []) const
{
assert (f != 0);
assert (f != use_buffer ());
assert (x != 0);
assert (x != use_buffer ());
assert (x != f);
DataType * sf;
DataType * df;
if ((_nbr_bits & 1) != 0)
{
df = use_buffer ();
sf = f;
}
else
{
df = f;
sf = use_buffer ();
}
compute_direct_pass_1_2 (df, x);
compute_direct_pass_3 (sf, df);
for (int pass = 3; pass < _nbr_bits; ++ pass)
{
compute_direct_pass_n (df, sf, pass);
DataType * const temp_ptr = df;
df = sf;
sf = temp_ptr;
}
}
template <class DT>
void FFTReal <DT>::compute_direct_pass_1_2 (DataType df [], const DataType x []) const
{
assert (df != 0);
assert (x != 0);
assert (df != x);
const long * const bit_rev_lut_ptr = get_br_ptr ();
long coef_index = 0;
do
{
const long rev_index_0 = bit_rev_lut_ptr [coef_index];
const long rev_index_1 = bit_rev_lut_ptr [coef_index + 1];
const long rev_index_2 = bit_rev_lut_ptr [coef_index + 2];
const long rev_index_3 = bit_rev_lut_ptr [coef_index + 3];
DataType * const df2 = df + coef_index;
df2 [1] = x [rev_index_0] - x [rev_index_1];
df2 [3] = x [rev_index_2] - x [rev_index_3];
const DataType sf_0 = x [rev_index_0] + x [rev_index_1];
const DataType sf_2 = x [rev_index_2] + x [rev_index_3];
df2 [0] = sf_0 + sf_2;
df2 [2] = sf_0 - sf_2;
coef_index += 4;
}
while (coef_index < _length);
}
template <class DT>
void FFTReal <DT>::compute_direct_pass_3 (DataType df [], const DataType sf []) const
{
assert (df != 0);
assert (sf != 0);
assert (df != sf);
const DataType sqrt2_2 = DataType (SQRT2 * 0.5);
long coef_index = 0;
do
{
DataType v;
df [coef_index] = sf [coef_index] + sf [coef_index + 4];
df [coef_index + 4] = sf [coef_index] - sf [coef_index + 4];
df [coef_index + 2] = sf [coef_index + 2];
df [coef_index + 6] = sf [coef_index + 6];
v = (sf [coef_index + 5] - sf [coef_index + 7]) * sqrt2_2;
df [coef_index + 1] = sf [coef_index + 1] + v;
df [coef_index + 3] = sf [coef_index + 1] - v;
v = (sf [coef_index + 5] + sf [coef_index + 7]) * sqrt2_2;
df [coef_index + 5] = v + sf [coef_index + 3];
df [coef_index + 7] = v - sf [coef_index + 3];
coef_index += 8;
}
while (coef_index < _length);
}
template <class DT>
void FFTReal <DT>::compute_direct_pass_n (DataType df [], const DataType sf [], int pass) const
{
assert (df != 0);
assert (sf != 0);
assert (df != sf);
assert (pass >= 3);
assert (pass < _nbr_bits);
if (pass <= TRIGO_BD_LIMIT)
{
compute_direct_pass_n_lut (df, sf, pass);
}
else
{
compute_direct_pass_n_osc (df, sf, pass);
}
}
template <class DT>
void FFTReal <DT>::compute_direct_pass_n_lut (DataType df [], const DataType sf [], int pass) const
{
assert (df != 0);
assert (sf != 0);
assert (df != sf);
assert (pass >= 3);
assert (pass < _nbr_bits);
const long nbr_coef = 1 << pass;
const long h_nbr_coef = nbr_coef >> 1;
const long d_nbr_coef = nbr_coef << 1;
long coef_index = 0;
const DataType * const cos_ptr = get_trigo_ptr (pass);
do
{
const DataType * const sf1r = sf + coef_index;
const DataType * const sf2r = sf1r + nbr_coef;
DataType * const dfr = df + coef_index;
DataType * const dfi = dfr + nbr_coef;
// Extreme coefficients are always real
dfr [0] = sf1r [0] + sf2r [0];
dfi [0] = sf1r [0] - sf2r [0]; // dfr [nbr_coef] =
dfr [h_nbr_coef] = sf1r [h_nbr_coef];
dfi [h_nbr_coef] = sf2r [h_nbr_coef];
// Others are conjugate complex numbers
const DataType * const sf1i = sf1r + h_nbr_coef;
const DataType * const sf2i = sf1i + nbr_coef;
for (long i = 1; i < h_nbr_coef; ++ i)
{
const DataType c = cos_ptr [i]; // cos (i*PI/nbr_coef);
const DataType s = cos_ptr [h_nbr_coef - i]; // sin (i*PI/nbr_coef);
DataType v;
v = sf2r [i] * c - sf2i [i] * s;
dfr [i] = sf1r [i] + v;
dfi [-i] = sf1r [i] - v; // dfr [nbr_coef - i] =
v = sf2r [i] * s + sf2i [i] * c;
dfi [i] = v + sf1i [i];
dfi [nbr_coef - i] = v - sf1i [i];
}
coef_index += d_nbr_coef;
}
while (coef_index < _length);
}
template <class DT>
void FFTReal <DT>::compute_direct_pass_n_osc (DataType df [], const DataType sf [], int pass) const
{
assert (df != 0);
assert (sf != 0);
assert (df != sf);
assert (pass > TRIGO_BD_LIMIT);
assert (pass < _nbr_bits);
const long nbr_coef = 1 << pass;
const long h_nbr_coef = nbr_coef >> 1;
const long d_nbr_coef = nbr_coef << 1;
long coef_index = 0;
OscType & osc = _trigo_osc [pass - (TRIGO_BD_LIMIT + 1)];
do
{
const DataType * const sf1r = sf + coef_index;
const DataType * const sf2r = sf1r + nbr_coef;
DataType * const dfr = df + coef_index;
DataType * const dfi = dfr + nbr_coef;
osc.clear_buffers ();
// Extreme coefficients are always real
dfr [0] = sf1r [0] + sf2r [0];
dfi [0] = sf1r [0] - sf2r [0]; // dfr [nbr_coef] =
dfr [h_nbr_coef] = sf1r [h_nbr_coef];
dfi [h_nbr_coef] = sf2r [h_nbr_coef];
// Others are conjugate complex numbers
const DataType * const sf1i = sf1r + h_nbr_coef;
const DataType * const sf2i = sf1i + nbr_coef;
for (long i = 1; i < h_nbr_coef; ++ i)
{
osc.step ();
const DataType c = osc.get_cos ();
const DataType s = osc.get_sin ();
DataType v;
v = sf2r [i] * c - sf2i [i] * s;
dfr [i] = sf1r [i] + v;
dfi [-i] = sf1r [i] - v; // dfr [nbr_coef - i] =
v = sf2r [i] * s + sf2i [i] * c;
dfi [i] = v + sf1i [i];
dfi [nbr_coef - i] = v - sf1i [i];
}
coef_index += d_nbr_coef;
}
while (coef_index < _length);
}
// Transform in several pass
template <class DT>
void FFTReal <DT>::compute_ifft_general (const DataType f [], DataType x []) const
{
assert (f != 0);
assert (f != use_buffer ());
assert (x != 0);
assert (x != use_buffer ());
assert (x != f);
DataType * sf = const_cast <DataType *> (f);
DataType * df;
DataType * df_temp;
if (_nbr_bits & 1)
{
df = use_buffer ();
df_temp = x;
}
else
{
df = x;
df_temp = use_buffer ();
}
for (int pass = _nbr_bits - 1; pass >= 3; -- pass)
{
compute_inverse_pass_n (df, sf, pass);
if (pass < _nbr_bits - 1)
{
DataType * const temp_ptr = df;
df = sf;
sf = temp_ptr;
}
else
{
sf = df;
df = df_temp;
}
}
compute_inverse_pass_3 (df, sf);
compute_inverse_pass_1_2 (x, df);
}
template <class DT>
void FFTReal <DT>::compute_inverse_pass_n (DataType df [], const DataType sf [], int pass) const
{
assert (df != 0);
assert (sf != 0);
assert (df != sf);
assert (pass >= 3);
assert (pass < _nbr_bits);
if (pass <= TRIGO_BD_LIMIT)
{
compute_inverse_pass_n_lut (df, sf, pass);
}
else
{
compute_inverse_pass_n_osc (df, sf, pass);
}
}
template <class DT>
void FFTReal <DT>::compute_inverse_pass_n_lut (DataType df [], const DataType sf [], int pass) const
{
assert (df != 0);
assert (sf != 0);
assert (df != sf);
assert (pass >= 3);
assert (pass < _nbr_bits);
const long nbr_coef = 1 << pass;
const long h_nbr_coef = nbr_coef >> 1;
const long d_nbr_coef = nbr_coef << 1;
long coef_index = 0;
const DataType * const cos_ptr = get_trigo_ptr (pass);
do
{
const DataType * const sfr = sf + coef_index;
const DataType * const sfi = sfr + nbr_coef;
DataType * const df1r = df + coef_index;
DataType * const df2r = df1r + nbr_coef;
// Extreme coefficients are always real
df1r [0] = sfr [0] + sfi [0]; // + sfr [nbr_coef]
df2r [0] = sfr [0] - sfi [0]; // - sfr [nbr_coef]
df1r [h_nbr_coef] = sfr [h_nbr_coef] * 2;
df2r [h_nbr_coef] = sfi [h_nbr_coef] * 2;
// Others are conjugate complex numbers
DataType * const df1i = df1r + h_nbr_coef;
DataType * const df2i = df1i + nbr_coef;
for (long i = 1; i < h_nbr_coef; ++ i)
{
df1r [i] = sfr [i] + sfi [-i]; // + sfr [nbr_coef - i]
df1i [i] = sfi [i] - sfi [nbr_coef - i];
const DataType c = cos_ptr [i]; // cos (i*PI/nbr_coef);
const DataType s = cos_ptr [h_nbr_coef - i]; // sin (i*PI/nbr_coef);
const DataType vr = sfr [i] - sfi [-i]; // - sfr [nbr_coef - i]
const DataType vi = sfi [i] + sfi [nbr_coef - i];
df2r [i] = vr * c + vi * s;
df2i [i] = vi * c - vr * s;
}
coef_index += d_nbr_coef;
}
while (coef_index < _length);
}
template <class DT>
void FFTReal <DT>::compute_inverse_pass_n_osc (DataType df [], const DataType sf [], int pass) const
{
assert (df != 0);
assert (sf != 0);
assert (df != sf);
assert (pass > TRIGO_BD_LIMIT);
assert (pass < _nbr_bits);
const long nbr_coef = 1 << pass;
const long h_nbr_coef = nbr_coef >> 1;
const long d_nbr_coef = nbr_coef << 1;
long coef_index = 0;
OscType & osc = _trigo_osc [pass - (TRIGO_BD_LIMIT + 1)];
do
{
const DataType * const sfr = sf + coef_index;
const DataType * const sfi = sfr + nbr_coef;
DataType * const df1r = df + coef_index;
DataType * const df2r = df1r + nbr_coef;
osc.clear_buffers ();
// Extreme coefficients are always real
df1r [0] = sfr [0] + sfi [0]; // + sfr [nbr_coef]
df2r [0] = sfr [0] - sfi [0]; // - sfr [nbr_coef]
df1r [h_nbr_coef] = sfr [h_nbr_coef] * 2;
df2r [h_nbr_coef] = sfi [h_nbr_coef] * 2;
// Others are conjugate complex numbers
DataType * const df1i = df1r + h_nbr_coef;
DataType * const df2i = df1i + nbr_coef;
for (long i = 1; i < h_nbr_coef; ++ i)
{
df1r [i] = sfr [i] + sfi [-i]; // + sfr [nbr_coef - i]
df1i [i] = sfi [i] - sfi [nbr_coef - i];
osc.step ();
const DataType c = osc.get_cos ();
const DataType s = osc.get_sin ();
const DataType vr = sfr [i] - sfi [-i]; // - sfr [nbr_coef - i]
const DataType vi = sfi [i] + sfi [nbr_coef - i];
df2r [i] = vr * c + vi * s;
df2i [i] = vi * c - vr * s;
}
coef_index += d_nbr_coef;
}
while (coef_index < _length);
}
template <class DT>
void FFTReal <DT>::compute_inverse_pass_3 (DataType df [], const DataType sf []) const
{
assert (df != 0);
assert (sf != 0);
assert (df != sf);
const DataType sqrt2_2 = DataType (SQRT2 * 0.5);
long coef_index = 0;
do
{
df [coef_index] = sf [coef_index] + sf [coef_index + 4];
df [coef_index + 4] = sf [coef_index] - sf [coef_index + 4];
df [coef_index + 2] = sf [coef_index + 2] * 2;
df [coef_index + 6] = sf [coef_index + 6] * 2;
df [coef_index + 1] = sf [coef_index + 1] + sf [coef_index + 3];
df [coef_index + 3] = sf [coef_index + 5] - sf [coef_index + 7];
const DataType vr = sf [coef_index + 1] - sf [coef_index + 3];
const DataType vi = sf [coef_index + 5] + sf [coef_index + 7];
df [coef_index + 5] = (vr + vi) * sqrt2_2;
df [coef_index + 7] = (vi - vr) * sqrt2_2;
coef_index += 8;
}
while (coef_index < _length);
}
template <class DT>
void FFTReal <DT>::compute_inverse_pass_1_2 (DataType x [], const DataType sf []) const
{
assert (x != 0);
assert (sf != 0);
assert (x != sf);
const long * bit_rev_lut_ptr = get_br_ptr ();
const DataType * sf2 = sf;
long coef_index = 0;
do
{
{
const DataType b_0 = sf2 [0] + sf2 [2];
const DataType b_2 = sf2 [0] - sf2 [2];
const DataType b_1 = sf2 [1] * 2;
const DataType b_3 = sf2 [3] * 2;
x [bit_rev_lut_ptr [0]] = b_0 + b_1;
x [bit_rev_lut_ptr [1]] = b_0 - b_1;
x [bit_rev_lut_ptr [2]] = b_2 + b_3;
x [bit_rev_lut_ptr [3]] = b_2 - b_3;
}
{
const DataType b_0 = sf2 [4] + sf2 [6];
const DataType b_2 = sf2 [4] - sf2 [6];
const DataType b_1 = sf2 [5] * 2;
const DataType b_3 = sf2 [7] * 2;
x [bit_rev_lut_ptr [4]] = b_0 + b_1;
x [bit_rev_lut_ptr [5]] = b_0 - b_1;
x [bit_rev_lut_ptr [6]] = b_2 + b_3;
x [bit_rev_lut_ptr [7]] = b_2 - b_3;
}
sf2 += 8;
coef_index += 8;
bit_rev_lut_ptr += 8;
}
while (coef_index < _length);
}
#endif // FFTReal_CODEHEADER_INCLUDED
#undef FFTReal_CURRENT_CODEHEADER
/*\\\ EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
|