-
Notifications
You must be signed in to change notification settings - Fork 14
/
meow_fft.h
2343 lines (2064 loc) · 64.3 KB
/
meow_fft.h
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
/*
meow_fft. My Easy Oresome Wonderful Fast Fourier Transform.
Copyright (C) 2017 Richard Maxwell <jodi.the.tigger@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef MEOW_FFT
#define MEOW_FFT
#include <stdlib.h>
// for size_t, abort
#ifdef __cplusplus
extern "C" {
#endif
// C-API -----------------------------------------------------------------------
typedef struct Meow_FFT_Complex
{
float r;
float j;
}
Meow_FFT_Complex;
struct Meow_FFT_Workset;
struct Meow_FFT_Workset_Real;
size_t meow_fft_generate_workset
(
int N
, struct Meow_FFT_Workset* workset
);
// returns the size of the workset if null is passed. 0 if N is invalid.
size_t meow_fft_generate_workset_real
(
int N
, struct Meow_FFT_Workset_Real* workset
);
// returns the size of the workset if null is passed. 0 if N is invalid.
unsigned meow_fft_is_slow (const struct Meow_FFT_Workset* workset);
unsigned meow_fft_is_slow_real(const struct Meow_FFT_Workset_Real* workset);
// returns non-zero if the fft has a slow dft in any one of its stages.
// C-API (ffts) ----------------------------------------------------------------
// NOTES:
// countof(out) == countof(in).
// In order to do that I have mixed out[0] with out[N/2]. That is:
// out[0].r == out[0].r, out[0].j = out[N/2].r
void meow_fft_real
(
const struct Meow_FFT_Workset_Real* workset
, const float* in
, Meow_FFT_Complex* out
);
void meow_fft_real_i
(
const struct Meow_FFT_Workset_Real* workset
, const Meow_FFT_Complex* in
, Meow_FFT_Complex* temp
, float* out
);
void meow_fft
(
const struct Meow_FFT_Workset* data
, const Meow_FFT_Complex* in
, Meow_FFT_Complex* out
);
void meow_fft_i
(
const struct Meow_FFT_Workset* data
, const Meow_FFT_Complex* in
, Meow_FFT_Complex* out
);
// -----------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
#endif // MEOW_FFT
#ifdef MEOW_FFT_IMPLEMENTATION
// Reading List ----------------------------------------------------------------
//
// It's a circle! -> How FFTs _actually_ work
// http://betterexplained.com/articles/an-interactive-guide-to-the-fourier-transform/
//
// How to get radix-2, 3, 4, and 5 formulas:
// http://www.briangough.com/fftalgorithms.pdf pages 18 and 19
//
// How do make a faster fft when only dealing with real (non-complex) inputs.
// (Warning, the maths is confusing due to inconsisten formulas and assumptions)
// http://www.engineeringproductivitytools.com/stuff/T0001/PT10.HTM
//
// Finally, know that ffts are pretty much as fast as you can get, you need
// to start making them cache friendly to get any extra speed.
// https://math.mit.edu/~stevenj/18.335/FFTW-Alan-2008.pdf
//
// -----------------------------------------------------------------------------
#include <math.h>
#include <stdint.h>
typedef const Meow_FFT_Complex Complex;
#define MEOW_TAU 6.283185307179586476925286766559005768394338798750211641949889
// Plumbing --------------------------------------------------------------------
typedef struct Meow_Fft_Stages
{
unsigned count;
unsigned* radix;
unsigned* remainder;
unsigned* offsets;
}
Meow_Fft_Stages;
typedef struct Meow_FFT_Workset
{
int N;
Meow_FFT_Complex* wn;
// Non-null only defined if there is a slow-dft as one of the radix stages.
Meow_FFT_Complex* wn_ordered;
// Sequentially ordered per stage, will be duplicates between stages.
Meow_Fft_Stages stages;
}
Meow_FFT_Workset;
typedef struct Meow_FFT_Workset_Real
{
Meow_FFT_Complex* w_2n;
Meow_FFT_Workset half;
}
Meow_FFT_Workset_Real;
typedef struct Meow_Stage_Info
{
unsigned is_slow;
unsigned stage_count;
unsigned w_count;
}
Meow_Stage_Info;
// -----------------------------------------------------------------------------
unsigned meow_is_codelet(unsigned radix)
{
return ((radix <= 5) || (radix == 8));
}
void meow_make_twiddles
(
unsigned n
, unsigned count
, Meow_FFT_Complex* w
)
{
const double ni = 1.0f / n;
for (unsigned i = 0; i < count; ++i)
{
w[i].r = (float) cos(MEOW_TAU * i * ni);
w[i].j = (float) sin(MEOW_TAU * i * ni);
}
}
unsigned meow_make_twiddles_sequential
(
unsigned n
, Meow_FFT_Complex* w
, Meow_Fft_Stages* stages
)
// Returns number of W constants needed.
{
// Figure out the tiddle offsets.
unsigned w_count = 0;
{
unsigned offset = 0;
for (unsigned s = 0; s < stages->count; s++)
{
unsigned r = stages->radix[s];
unsigned count = stages->remainder[s];
unsigned amount = meow_is_codelet(r) ? (r - 1) * (count - 1) : 0u;
stages->offsets[s] = amount;
offset += amount;
}
w_count = offset;
for (unsigned s = 0; s < stages->count; s++)
{
unsigned count = stages->offsets[s];
offset -= count;
stages->offsets[s] = offset;
}
}
// Fill in the twiddles so that they are accessed sequentially in the radix
// code for best cacheline use.
if (w)
{
unsigned w_mul = 1;
double ni = 1.0 / n;
for (unsigned s = 0; s < stages->count; s++)
{
const unsigned radix = stages->radix[s];
const unsigned count = stages->remainder[s];
unsigned offset = stages->offsets[s];
if (meow_is_codelet(radix))
{
for (unsigned i = 1 ; i < count; i++)
{
for (unsigned j = 1; j < radix; j++)
{
const unsigned w_x = i * j * w_mul;
w[offset].r = (float) cos(MEOW_TAU * w_x * ni);
w[offset].j = (float) sin(MEOW_TAU * w_x * ni);
offset++;
}
}
}
w_mul *= radix;
}
}
return w_count;
}
Meow_Stage_Info meow_calculate_stages(unsigned n, Meow_FFT_Workset* workset)
{
unsigned is_slow = 0u;
unsigned stage = 0u;
unsigned w_count = 0u;
while (n > 1)
{
// premade codelets 2, 3, 4, 5, 8
unsigned i = 8;
for (; i > 1; i--)
{
if ((i == 7) || (i == 6))
{
// don't have radix-7 or radix-6.
continue;
}
if (!(n % i))
{
w_count += ((i - 1) * (n - 1));
break;
}
}
// bah, plain slow dft instead
if (i == 1)
{
is_slow = 1;
i = 7;
for (; i <= n; i++)
{
if (!(n % i))
{
break;
}
}
}
n /= i;
if (workset)
{
workset->stages.radix[stage] = i;
workset->stages.remainder[stage] = n;
}
stage++;
}
Meow_Stage_Info result =
{
is_slow
, stage
, w_count
};
return result;
}
size_t meow_fft_generate_workset(int N, Meow_FFT_Workset* workset)
{
if (N < 2)
{
// Too small.
return 0;
}
Meow_Stage_Info info = meow_calculate_stages(N, NULL);
const size_t size_workset = sizeof(Meow_FFT_Workset);
const size_t size_radix = info.stage_count * sizeof(int);
const size_t size_remainder = info.stage_count * sizeof(int);
const size_t size_offsets = info.stage_count * sizeof(int);
const size_t size_twiddles_ordered =
info.w_count * sizeof(Meow_FFT_Complex);
const size_t size_twiddles =
N * sizeof(Meow_FFT_Complex) * info.is_slow;
const size_t size_total =
size_workset
+ size_twiddles
+ size_radix
+ size_remainder
+ size_offsets
+ size_twiddles_ordered;
if (workset)
{
uint8_t* data = (uint8_t*)(workset);
uint8_t* data_wn = data + size_workset;
uint8_t* data_radix = data_wn + size_twiddles;
uint8_t* data_remainder = data_radix + size_radix;
uint8_t* data_offsets = data_remainder + size_offsets;
uint8_t* data_wn_ordered = data_offsets + size_remainder;
workset->wn =
(info.is_slow)
? (Meow_FFT_Complex*)(data_wn)
: NULL;
workset->stages.radix = (unsigned*)(data_radix);
workset->stages.remainder = (unsigned*)(data_remainder);
workset->stages.offsets = (unsigned*)(data_offsets);
workset->stages.count = info.stage_count;
workset->wn_ordered = (Meow_FFT_Complex*)(data_wn_ordered);
workset->N = N;
if (workset->wn)
{
meow_make_twiddles(N, N, workset->wn);
}
meow_calculate_stages(N, workset);
meow_make_twiddles_sequential
(
N
, workset->wn_ordered
, &workset->stages
);
}
return size_total;
}
size_t meow_fft_generate_workset_real
(
const int N
, Meow_FFT_Workset_Real* workset
)
{
if ((N < 4) || (N % 2))
{
// Too small or not divisible by two.
return 0;
}
const unsigned N_2 = N / 2;
const unsigned N_4 = N / 4;
const size_t size_workset = sizeof(Meow_FFT_Workset_Real);
const size_t size_w_2n = (N_4 + 1) * sizeof(Meow_FFT_Complex);
const size_t size_half = meow_fft_generate_workset(N_2, NULL);
if (workset)
{
meow_fft_generate_workset(N_2, &workset->half);
uint8_t* data = (uint8_t*)(&workset->half);
uint8_t* data_w_2n = data + size_half;
workset->w_2n = (Meow_FFT_Complex*)(data_w_2n);
meow_make_twiddles(N, N_4 + 1, workset->w_2n);
}
return size_workset + size_w_2n + size_half;
}
// -----------------------------------------------------------------------------
unsigned meow_fft_is_slow(const Meow_FFT_Workset* workset)
{
return !!(workset->wn);
}
unsigned meow_fft_is_slow_real(const Meow_FFT_Workset_Real* workset)
{
return meow_fft_is_slow(&workset->half);
}
// -----------------------------------------------------------------------------
inline Meow_FFT_Complex meow_add
(
const Meow_FFT_Complex lhs
, const Meow_FFT_Complex rhs
)
{
Meow_FFT_Complex result =
{
lhs.r + rhs.r
, lhs.j + rhs.j
};
return result;
}
inline Meow_FFT_Complex meow_sub
(
const Meow_FFT_Complex lhs
, const Meow_FFT_Complex rhs
)
{
Meow_FFT_Complex result =
{
lhs.r - rhs.r
, lhs.j - rhs.j
};
return result;
}
inline Meow_FFT_Complex meow_negate(const Meow_FFT_Complex lhs)
{
Meow_FFT_Complex result =
{
-lhs.r
, -lhs.j
};
return result;
}
inline Meow_FFT_Complex meow_conjugate(const Meow_FFT_Complex lhs)
{
Meow_FFT_Complex result =
{
lhs.r
, -lhs.j
};
return result;
}
inline Meow_FFT_Complex meow_mul
(
const Meow_FFT_Complex lhs
, const Meow_FFT_Complex rhs
)
{
Meow_FFT_Complex result =
{
(lhs.r * rhs.r) - (lhs.j * rhs.j)
, (lhs.r * rhs.j) + (lhs.j * rhs.r)
};
return result;
}
inline Meow_FFT_Complex meow_mul_by_conjugate
(
const Meow_FFT_Complex lhs
, const Meow_FFT_Complex rhs
)
{
Meow_FFT_Complex result =
{
(lhs.r * rhs.r) + (lhs.j * rhs.j)
, (lhs.j * rhs.r) - (lhs.r * rhs.j)
};
return result;
}
inline Meow_FFT_Complex meow_mul_by_j(const Meow_FFT_Complex lhs)
{
Meow_FFT_Complex result =
{
-lhs.j
, lhs.r
};
return result;
}
inline Meow_FFT_Complex meow_mulf
(
const Meow_FFT_Complex lhs
, float rhs
)
{
Meow_FFT_Complex result =
{
lhs.r * rhs
, lhs.j * rhs
};
return result;
}
// -----------------------------------------------------------------------------
// https://developercommunity.visualstudio.com/t/fatal-error-C1001:-Internal-compiler-err/1390698
// https://developercommunity.visualstudio.com/t/bug-in-visual-c-2019-and-below-i-think-it-is-relat/1119500
// https://developercommunity.visualstudio.com/t/optimized-compiler-bug/846597
#if defined(_MSC_VER) && (_MSC_VER < 1930)
#define MSVC_BUGFIX volatile
#else
#define MSVC_BUGFIX
#endif
void meow_dft_n_dit
(
const Meow_FFT_Complex* w_n
, Meow_FFT_Complex* out
, unsigned count
, unsigned w_multiplier
, unsigned radix
, unsigned N
, unsigned reverse
)
{
// Can I do something with the knowledge that n is always odd?
if (radix > 2048)
{
abort();
// removing VLAs, so set a hard limit we support.
}
Meow_FFT_Complex scratch[2048];
for (unsigned butterfly = 0; butterfly < count; ++butterfly)
{
for (unsigned i = 0; i < radix; i++)
{
scratch[i] = out[i * count + butterfly];
}
for (unsigned i = 0 ; i < radix ; ++i)
{
MSVC_BUGFIX const unsigned index_out = i * count + butterfly;
// W0 is always 1
Meow_FFT_Complex sum = scratch[0];
for (unsigned j = 1; j < radix; ++j )
{
const unsigned wi = (j * w_multiplier * index_out) % N;
Complex w = w_n[wi];
Complex in = scratch[j];
float rr;
float jj;
if (reverse)
{
rr = (in.r * w.r) - (in.j * w.j);
jj = (in.r * w.j) + (in.j * w.r);
}
else
{
rr = (in.r * w.r) + (in.j * w.j);
jj = (in.j * w.r) - (in.r * w.j);
}
sum.r += rr;
sum.j += jj;
}
out[index_out] = sum;
}
}
}
// -----------------------------------------------------------------------------
// Algorithms taken from
// http://www.briangough.com/fftalgorithms.pdf
// (equations 135 to 146)
// in, out and twiddle indicies taken from kiss_fft
// All twiddles are assumed to be ifft calculated. Conjugation is done in the
// maths.
// All twiddle input arrays are assumed to be sequentiall accessed. Twiddle
// indicies are pre-calculated.
// -----------------------------------------------------------------------------
// Forward
// -----------------------------------------------------------------------------
void meow_radix_2_dit
(
const Meow_FFT_Complex* w_n
, Meow_FFT_Complex* out
, unsigned count
)
{
// butteryfly 0 always has the twiddle factor == 1.0f
// so special case that one.
{
Complex z0 = out[0];
Complex z1 = out[count];
out[0] = meow_add(z0, z1);
out[count] = meow_sub(z0, z1);
}
for (unsigned butterfly = 1; butterfly < count; ++butterfly)
{
Complex w = w_n[butterfly - 1];
const unsigned i0 = butterfly;
const unsigned i1 = butterfly + count;
Complex z0 = out[i0];
Complex z1 = meow_mul_by_conjugate(out[i1], w);
out[i0] = meow_add(z0, z1);
out[i1] = meow_sub(z0, z1);
// Equation 135
}
}
#define MEOW_SIN_PI_3 -0.866025403784438646763723170752936183471402626905190314f
void meow_radix_3_dit
(
const Meow_FFT_Complex* w_n
, Meow_FFT_Complex* out
, unsigned count
)
{
// W[0] is always 1.0f;
{
const unsigned i0 = 0 * count;
const unsigned i1 = 1 * count;
const unsigned i2 = 2 * count;
Complex z0 = out[i0];
Complex z1 = out[i1];
Complex z2 = out[i2];
Complex t1 = meow_add(z1, z2);
Complex t2 = meow_sub(z0, meow_mulf(t1, 0.5));
Complex t3j = meow_mul_by_j(meow_mulf(meow_sub(z1, z2), MEOW_SIN_PI_3));
out[i0] = meow_add(z0, t1);
out[i1] = meow_add(t2, t3j);
out[i2] = meow_sub(t2, t3j);
}
unsigned wi = 0;
for (unsigned butterfly = 1; butterfly < count; butterfly++, wi+=2)
{
Complex w1 = w_n[wi + 0];
Complex w2 = w_n[wi + 1];
const unsigned i0 = butterfly;
const unsigned i1 = butterfly + count;
const unsigned i2 = butterfly + 2 * count;
Complex z0 = out[i0];
Complex z1 = meow_mul_by_conjugate(out[i1], w1);
Complex z2 = meow_mul_by_conjugate(out[i2], w2);
Complex t1 = meow_add(z1, z2);
Complex t2 = meow_sub(z0, meow_mulf(t1, 0.5));
Complex t3j = meow_mul_by_j(meow_mulf(meow_sub(z1, z2), MEOW_SIN_PI_3));
// Equation 136
out[i0] = meow_add(z0, t1);
out[i1] = meow_add(t2, t3j);
out[i2] = meow_sub(t2, t3j);
// Equation 137
}
}
void meow_radix_4_dit
(
const Meow_FFT_Complex* w_n
, Meow_FFT_Complex* out
, unsigned count
)
{
// W[0] is always 1.0f;
{
const unsigned i0 = 0 * count;
const unsigned i1 = 1 * count;
const unsigned i2 = 2 * count;
const unsigned i3 = 3 * count;
Complex z0 = out[i0];
Complex z1 = out[i1];
Complex z2 = out[i2];
Complex z3 = out[i3];
Complex t1 = meow_add(z0, z2);
Complex t2 = meow_add(z1, z3);
Complex t3 = meow_sub(z0, z2);
Complex t4j = meow_negate(meow_mul_by_j(meow_sub(z1, z3)));
out[i0] = meow_add(t1, t2);
out[i1] = meow_add(t3, t4j);
out[i2] = meow_sub(t1, t2);
out[i3] = meow_sub(t3, t4j);
}
unsigned wi = 0u;
for (unsigned butterfly = 1; butterfly < count; ++butterfly, wi+=3)
{
Complex w1 = w_n[wi + 0];
Complex w2 = w_n[wi + 1];
Complex w3 = w_n[wi + 2];
const unsigned i0 = butterfly + 0 * count;
const unsigned i1 = butterfly + 1 * count;
const unsigned i2 = butterfly + 2 * count;
const unsigned i3 = butterfly + 3 * count;
Complex z0 = out[i0];
Complex z1 = meow_mul_by_conjugate(out[i1], w1);
Complex z2 = meow_mul_by_conjugate(out[i2], w2);
Complex z3 = meow_mul_by_conjugate(out[i3], w3);
Complex t1 = meow_add(z0, z2);
Complex t2 = meow_add(z1, z3);
Complex t3 = meow_sub(z0, z2);
Complex t4j = meow_negate(meow_mul_by_j(meow_sub(z1, z3)));
// Equations 138
// Also instead of conjugating the input and multplying with the
// twiddles for the ifft, we invert the twiddles instead. This works
// fine except here, the mul_by_j is assuming that it's the forward
// fft twiddle we are multiplying with, not the conjugated one we
// actually have. So we have to conjugate it _back_ if we are doing the
// ifft.
// Also, had to multiply by -j, not j for reasons I am yet to grasp.
out[i0] = meow_add(t1, t2);
out[i1] = meow_add(t3, t4j);
out[i2] = meow_sub(t1, t2);
out[i3] = meow_sub(t3, t4j);
// Equations 139
}
}
#define MEOW_SQR_5_DIV_4 0.5590169943749474241022934171828190588601545899028814f
#define MEOW_SIN_2PI_5 -0.9510565162951535721164393333793821434056986341257502f
#define MEOW_SIN_2PI_10 -0.5877852522924731291687059546390727685976524376431459f
void meow_radix_5_dit
(
const Meow_FFT_Complex* w_n
, Meow_FFT_Complex* out
, unsigned count
)
{
// W[0] is always 1.0f;
{
const unsigned i0 = 0 * count;
const unsigned i1 = 1 * count;
const unsigned i2 = 2 * count;
const unsigned i3 = 3 * count;
const unsigned i4 = 4 * count;
Complex z0 = out[i0];
Complex z1 = out[i1];
Complex z2 = out[i2];
Complex z3 = out[i3];
Complex z4 = out[i4];
Complex t1 = meow_add(z1, z4);
Complex t2 = meow_add(z2, z3);
Complex t3 = meow_sub(z1, z4);
Complex t4 = meow_sub(z2, z3);
// Equations 140
Complex t5 = meow_add(t1, t2);
Complex t6 = meow_mulf(meow_sub(t1, t2), MEOW_SQR_5_DIV_4);
Complex t7 = meow_sub(z0, meow_mulf(t5, 0.25f));
// Equation 141
Complex t8 = meow_add(t7, t6);
Complex t9 = meow_sub(t7, t6);
// Equation 142
Complex t10j = meow_mul_by_j
(
meow_add
(
meow_mulf(t3, MEOW_SIN_2PI_5)
, meow_mulf(t4, MEOW_SIN_2PI_10)
)
);
Complex t11j = meow_mul_by_j
(
meow_sub
(
meow_mulf(t3, MEOW_SIN_2PI_10)
, meow_mulf(t4, MEOW_SIN_2PI_5)
)
);
// Equation 143
out[i0] = meow_add(z0, t5);
// Equation 144
out[i1] = meow_add(t8, t10j);
out[i2] = meow_add(t9, t11j);
// Equation 145
out[i3] = meow_sub(t9, t11j);
out[i4] = meow_sub(t8, t10j);
// Equation 146
}
unsigned wi = 0u;
for (unsigned butterfly = 1; butterfly < count; ++butterfly, wi+=4)
{
Complex w1 = w_n[wi + 0];
Complex w2 = w_n[wi + 1];
Complex w3 = w_n[wi + 2];
Complex w4 = w_n[wi + 3];
unsigned i0 = butterfly + 0 * count;
unsigned i1 = butterfly + 1 * count;
unsigned i2 = butterfly + 2 * count;
unsigned i3 = butterfly + 3 * count;
unsigned i4 = butterfly + 4 * count;
Complex z0 = out[i0];
Complex z1 = meow_mul_by_conjugate(out[i1], w1);
Complex z2 = meow_mul_by_conjugate(out[i2], w2);
Complex z3 = meow_mul_by_conjugate(out[i3], w3);
Complex z4 = meow_mul_by_conjugate(out[i4], w4);
Complex t1 = meow_add(z1, z4);
Complex t2 = meow_add(z2, z3);
Complex t3 = meow_sub(z1, z4);
Complex t4 = meow_sub(z2, z3);
// Equations 140
Complex t5 = meow_add(t1, t2);
Complex t6 = meow_mulf(meow_sub(t1, t2), MEOW_SQR_5_DIV_4);
Complex t7 = meow_sub(z0, meow_mulf(t5, 0.25f));
// Equation 141
Complex t8 = meow_add(t7, t6);
Complex t9 = meow_sub(t7, t6);
// Equation 142
Complex t10j = meow_mul_by_j
(
meow_add
(
meow_mulf(t3, MEOW_SIN_2PI_5)
, meow_mulf(t4, MEOW_SIN_2PI_10)
)
);
Complex t11j = meow_mul_by_j
(
meow_sub
(
meow_mulf(t3, MEOW_SIN_2PI_10)
, meow_mulf(t4, MEOW_SIN_2PI_5)
)
);
// Equation 143
out[i0] = meow_add(z0, t5);
// Equation 144
out[i1] = meow_add(t8, t10j);
out[i2] = meow_add(t9, t11j);
// Equation 145
out[i3] = meow_sub(t9, t11j);
out[i4] = meow_sub(t8, t10j);
// Equation 146
}
}
#define MEOW_1_DIV_SQR_2 0.707106781186547524400844362104849039284835938f
static void meow_radix_8_dit
(
const Meow_FFT_Complex* w_n
, Meow_FFT_Complex* out
, unsigned count
)
{
const float* W = &w_n[0].r;
{
float T3;
float T23;
float T18;
float T38;
float T6;
float T37;
float T21;
float T24;
float T13;
float T49;
float T35;
float T43;
float T10;
float T48;
float T30;
float T42;
{
float T1;
float T2;
float T19;
float T20;
T1 = out[0].r;
T2 = out[count * 4].r;
T3 = T1 + T2;
T23 = T1 - T2;
{
float T16;
float T17;
float T4;
float T5;
T16 = out[0].j;
T17 = out[count * 4].j;
T18 = T16 + T17;
T38 = T16 - T17;
T4 = out[count * 2].r;
T5 = out[count * 6].r;
T6 = T4 + T5;
T37 = T4 - T5;
}
T19 = out[count * 2].j;
T20 = out[count * 6].j;
T21 = T19 + T20;
T24 = T19 - T20;
{
float T11;
float T12;
float T31;
float T32;
float T33;
float T34;
T11 = out[count * 7].r;
T12 = out[count * 3].r;
T31 = T11 - T12;
T32 = out[count * 7].j;
T33 = out[count * 3].j;
T34 = T32 - T33;
T13 = T11 + T12;
T49 = T32 + T33;
T35 = T31 - T34;
T43 = T31 + T34;