Repository navigation
Expand file tree
/
Copy pathmsignals.cpp
More file actions
2091 lines (1736 loc) · 55.9 KB
/
Copy pathmsignals.cpp
File metadata and controls
2091 lines (1736 loc) · 55.9 KB
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
/**
* @file signals.cpp
* @author Jens Munk Hansen <jens.munk.hansen@gmail.com>
* @date Fri May 29 02:43:18 2015
*
* @brief
*
*
*/
/*
* This file is part of SOFUS.
*
* SOFUS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SOFUS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SOFUS. If not, see <http://www.gnu.org/licenses/>.
*/
// TODO: Implement pruned FFTs
// Use FFTW's h2h
// fftw_plan_r2r_1d(N, out, in, FFTW_HC2R / FFTW_R2HC, FFTW_PATIENT);
// fftw_execute_r2r(plan_r2hc, in, out);
// For a halfcomplex array, hc[n], the kth component thus has its real
// part in hc[k] and its imaginary part in hc[n-k], with the exception
// of k == 0 or n/2 (the latter only if n is even)—in these two cases,
// the imaginary part is zero due to symmetries of the real-input DFT
#include <algorithm> // min/max
#include <cstring> // memset
#include <sps/cenv.h>
#include <sps/debug.h>
#include <sps/mm_malloc.h>
#include <sps/msignals.hpp>
#include <sps/extintrin.h>
#include <sps/math.h>
#include <emmintrin.h>
#include <stdint.h>
#include <cstring>
#include <fftw3.h>
#include <algorithm>
#include <cassert>
#include <complex>
#include <iostream>
#include <mutex>
std::mutex g_plan_mutex;
// TODO: Solve order of destruction sequence
#include <cstdio>
#define DEBUG_PLAN 0
// TODO: Avoid static const members with in-class initializers
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4127)
#endif
namespace sps
{
// Consider using FFTW_MEASURE or FFTW_PATIENT (overwrites input)
// Erases the input arrays
// FFTW_MEASURE
// Even more optimal
//#define SPS_FFTW_PLAN FFTW_PATIENT
//#define SPS_FFTW_PLAN FFTW_EXHAUSTIVE
#define SPS_FFTW_FAST FFTW_ESTIMATE
#define SPS_FFTW_ACCURATE FFTW_PATIENT
template <typename T>
class Signal1DPlan
{
public:
static Signal1DPlan& Instance();
// static void Wisdom(size_t** nFFTSamples, size_t* nFFTs);
private:
Signal1DPlan();
~Signal1DPlan();
Signal1DPlan(Signal1DPlan const&) = default;
Signal1DPlan& operator=(Signal1DPlan const&) = default;
};
template <>
class Signal1DPlan<float>
{
public:
// TODO: Consider storing plans for non-aligned memory
static const size_t nFFTLengths = 31;
static __THREAD fftwf_plan forward[Signal1DPlan<float>::nFFTLengths]; // NOLINT
static __THREAD fftwf_plan backward[Signal1DPlan<float>::nFFTLengths]; // NOLINT
static const bool measure = false;
static const bool reuse = true;
static void DestroyPlans()
{
for (size_t i = 0; i < nFFTLengths; i++)
{
if (forward[i])
{
fftwf_destroy_plan(forward[i]);
forward[i] = nullptr;
}
if (backward[i])
{
fftwf_destroy_plan(backward[i]);
backward[i] = nullptr;
}
}
}
static Signal1DPlan& Instance()
{
// A singleton cannot be made thread_local
static Signal1DPlan singleton;
return singleton;
}
fftwf_plan& Forward(size_t size, float* in, std::complex<float>* out)
{
// Check power of two
assert(size && ((size & (size - 1))) == 0);
size_t index = static_cast<size_t>(log2(size));
if (Signal1DPlan<float>::reuse == false)
{
fftwf_destroy_plan(forward[index]);
forward[index] = nullptr;
}
if (forward[index])
{
return forward[index];
}
else
{
std::lock_guard<std::mutex> guard(g_plan_mutex);
if (Signal1DPlan<float>::measure)
{
float* dummy = static_cast<float*>(_mm_malloc(size * sizeof(float), 16));
memcpy(dummy, in, size * sizeof(float));
forward[index] = fftwf_plan_dft_r2c_1d(
static_cast<int>(size), dummy, reinterpret_cast<fftwf_complex*>(out), SPS_FFTW_ACCURATE);
_mm_free(dummy);
}
else
{
forward[index] = fftwf_plan_dft_r2c_1d(
static_cast<int>(size), in, reinterpret_cast<fftwf_complex*>(out), SPS_FFTW_FAST);
}
return forward[index];
}
}
fftwf_plan& Backward(size_t size, std::complex<float>* in, float* out)
{
// Check power of two
assert(size && ((size & (size - 1))) == 0);
size_t index = static_cast<size_t>(log2(size));
if (Signal1DPlan<float>::reuse == false)
{
fftwf_destroy_plan(backward[index]);
backward[index] = nullptr;
}
if (backward[index])
{
return backward[index];
}
else
{
std::lock_guard<std::mutex> guard(g_plan_mutex);
if (Signal1DPlan<float>::measure)
{
size_t nComplex = size / 2 + 1;
size_t nbytes = 16 * ((nComplex + 1) * sizeof(std::complex<float>) + 15) / 16;
std::complex<float>* dummy = static_cast<std::complex<float>*>(_mm_malloc(nbytes, 16));
memcpy(dummy, in, nComplex * sizeof(std::complex<float>));
backward[index] = fftwf_plan_dft_c2r_1d(
static_cast<int>(size), reinterpret_cast<fftwf_complex*>(dummy), out, SPS_FFTW_ACCURATE);
_mm_free(dummy);
}
else
{
backward[index] = fftwf_plan_dft_c2r_1d(
static_cast<int>(size), reinterpret_cast<fftwf_complex*>(in), out, SPS_FFTW_FAST);
}
return backward[index];
}
}
private:
Signal1DPlan()
{
#if USE_FFTW_THREADS
int state = fftw_init_threads();
fftw_plan_with_nthreads(4);
#endif
}
~Signal1DPlan()
{
#if USE_FFTW_THREADS
fftw_cleanup_threads();
#else
fftw_cleanup();
#endif
#if DEBUG_PLAN
FILE* fp = fopen("out.txt", "w");
#endif
for (size_t i = 0; i < nFFTLengths; i++)
{
if (forward[i])
{
#if DEBUG_PLAN
fftwf_fprint_plan(forward[i], fp);
#endif
fftwf_destroy_plan(forward[i]);
forward[i] = nullptr;
}
if (backward[i])
{
fftwf_destroy_plan(backward[i]);
backward[i] = nullptr;
}
}
#if DEBUG_PLAN
fclose(fp);
#endif
}
Signal1DPlan(Signal1DPlan const&) = default;
Signal1DPlan& operator=(Signal1DPlan const&) = default;
};
__THREAD fftwf_plan Signal1DPlan<float>::forward[Signal1DPlan<float>::nFFTLengths] = { NULL };
__THREAD fftwf_plan Signal1DPlan<float>::backward[Signal1DPlan<float>::nFFTLengths] = { nullptr };
template <>
class Signal1DPlan<double>
{
public:
static const size_t nFFTLengths = 31;
static __THREAD fftw_plan forward[Signal1DPlan<double>::nFFTLengths];
static __THREAD fftw_plan backward[Signal1DPlan<double>::nFFTLengths];
static Signal1DPlan& Instance()
{
// Apparently, the singleton cannot be made thread_local
static Signal1DPlan singleton;
return singleton;
}
/*
void Wisdom(size_t** nFFTSamples, size_t* nFFTs) {
*nFFTs = nFFTLengths;
size_t* _nFFTSamples = (size_t*) malloc(Signal1DPlan<double>::nFFTLengths * sizeof(size_t));
for (size_t i = 0 ; i < nFFTLengths ; i++) {
if (forward[i] != nullptr) {
(*nFFTSamples)[i] = pow(2.0f, (double)i);
}
}
*nFFTSamples = _nFFTSamples;
}
*/
fftw_plan& Forward(size_t size, double* in, std::complex<double>* out)
{
// Check power of two
assert(size && ((size & (size - 1))) == 0);
size_t index = static_cast<size_t>(log2(size));
if (forward[index])
{
return forward[index];
}
else
{
std::lock_guard<std::mutex> guard(g_plan_mutex);
forward[index] = fftw_plan_dft_r2c_1d(
static_cast<int>(size), in, reinterpret_cast<fftw_complex*>(out), SPS_FFTW_FAST);
return forward[index];
}
}
fftw_plan& Backward(size_t size, std::complex<double>* in, double* out)
{
// Check power of two
assert(size && ((size & (size - 1))) == 0);
size_t index = static_cast<size_t>(log2(size));
if (backward[index])
{
return backward[index];
}
else
{
std::lock_guard<std::mutex> guard(g_plan_mutex);
backward[index] = fftw_plan_dft_c2r_1d(
static_cast<int>(size), reinterpret_cast<fftw_complex*>(in), out, SPS_FFTW_FAST);
return backward[index];
}
}
private:
Signal1DPlan() {}
~Signal1DPlan()
{
for (size_t i = 0; i < nFFTLengths; i++)
{
if (forward[i])
{
fftw_destroy_plan(forward[i]);
forward[i] = nullptr;
}
if (backward[i])
{
fftw_destroy_plan(backward[i]);
backward[i] = nullptr;
}
}
}
Signal1DPlan(Signal1DPlan const&) = default;
Signal1DPlan& operator=(Signal1DPlan const&) = default;
};
__THREAD fftw_plan Signal1DPlan<double>::forward[Signal1DPlan<double>::nFFTLengths] = { nullptr };
__THREAD fftw_plan Signal1DPlan<double>::backward[Signal1DPlan<double>::nFFTLengths] = { nullptr };
// These are explicit specializations (not instantiations), so no need to instantiate
// template class Signal1DPlan<float>;
// template class Signal1DPlan<double>;
template <typename T>
STATIC_INLINE_BEGIN T* _mm_padarray(const T* input, const size_t len, const size_t newlen)
{
T* output = static_cast<T*>(SPS_MM_MALLOC(newlen * sizeof(T), 16));
#if 0
memset(output, 0, newlen*sizeof(T));
memcpy(output, reinterpret_cast<const void*>(input), len*sizeof(T));
#else
memcpy(reinterpret_cast<char*>(output), reinterpret_cast<const void*>(input), len * sizeof(T));
memset(reinterpret_cast<char*>(&output[len]), 0, (newlen - len) * sizeof(T));
#endif
return output;
}
template <typename T>
void DivideArray(T* Data, size_t NumEl, T Divisor)
{
size_t n;
for (n = 0; n < NumEl; n++)
Data[n] /= Divisor;
}
template <>
void DivideArray<float>(float* Data, size_t NumEl, float Divisor)
{
// TODO: Consider using _mm_loadu_ps for the first (if any)
const size_t align = 16;
const uintptr_t mask = ~static_cast<uintptr_t>(align - 1);
float* aligned_ptr = reinterpret_cast<float*>(
(reinterpret_cast<uintptr_t>(Data) + align - 1) & mask); // mask is 0x0F
// Offset before first aligned address
size_t offset = static_cast<size_t>(
(reinterpret_cast<uintptr_t>(aligned_ptr) - reinterpret_cast<uintptr_t>(Data)) / sizeof(float));
const float inv_Divisor = 1.0f / Divisor;
// Number of unaligned values before alignment (if any)
int nCount = static_cast<int>(std::min<size_t>(NumEl, offset));
int n = 0;
for (n = 0; n < nCount; n++)
{
Data[n] *= inv_Divisor;
}
const __m128 divisor = _mm_set1_ps(inv_Divisor);
// Number of aligned values after first aligned address (TODO: Verify)
nCount = static_cast<int>(NumEl - (align / sizeof(float) - 1) - offset);
// Shouldn't it be
nCount = static_cast<int>((align / sizeof(float)) * ((NumEl - offset) / (align / sizeof(float))));
// Only works for alignment equal to 16 (SSE2,SSE4)
for (; n < nCount; n += static_cast<int>(align / sizeof(float)))
{
__m128 va = _mm_load_ps(&aligned_ptr[n]);
va = _mm_mul_ps(va, divisor);
_mm_store_ps(&aligned_ptr[n], va);
}
// Remaining unaligned values (if any)
for (; n < static_cast<int>(NumEl); n++)
Data[n] *= inv_Divisor;
}
template <>
void DivideArray<double>(double* Data, size_t NumEl, double Divisor)
{
const size_t align = 16;
const uintptr_t mask = ~static_cast<uintptr_t>(align - 1);
double* aligned_ptr =
reinterpret_cast<double*>((reinterpret_cast<uintptr_t>(Data) + align - 1) & mask);
int n = 0;
size_t offset = static_cast<size_t>(
(reinterpret_cast<uintptr_t>(aligned_ptr) - reinterpret_cast<uintptr_t>(Data)) /
sizeof(double));
const double inv_Divisor = 1.0 / Divisor;
// Number of unaligned values before alignment (if any)
int nCount = static_cast<int>(std::min<size_t>(NumEl, offset));
// Shouldn't it be
nCount =
static_cast<int>((align / sizeof(double)) * ((NumEl - offset) / (align / sizeof(double))));
for (n = 0; n < nCount; n++)
{
Data[n] *= inv_Divisor;
}
const __m128d divisor = _mm_set1_pd(inv_Divisor);
// Number of aligned values after first aligned address
nCount = static_cast<int>(NumEl) - static_cast<int>(align / sizeof(double) - 1) -
static_cast<int>(offset);
// Divide aligned values (continue from above)
for (; n < nCount; n += static_cast<int>(align / sizeof(double)))
{
__m128d va = _mm_load_pd(&aligned_ptr[n]);
va = _mm_mul_pd(va, divisor);
_mm_store_pd(&aligned_ptr[n], va);
}
// Remaining unaligned values (if any)
for (; n < static_cast<int>(NumEl); n++)
Data[n] *= inv_Divisor;
}
template <class T>
signal1D<T>::signal1D()
: data(NULL)
, offset(0)
, ndata(0)
, nbytes(0)
{
}
template <class T>
signal1D<T>::signal1D(size_t n)
: data(NULL)
, offset(0)
, ndata(n)
{
nbytes = 16 * (n * sizeof(T) + 15) / 16;
data = static_cast<T*>(_mm_malloc(nbytes, 16));
#ifndef NDEBUG
SPS_RELAXED_MEMSET_BEGIN
memset(data, 0, nbytes);
SPS_RELAXED_MEMSET_END
#endif
}
template <class T>
signal1D<T>::signal1D(size_t n, size_t bytes)
: data(NULL)
, offset(0)
, ndata(n)
, nbytes(0)
{
nbytes = 16 * (n * sizeof(T) + 15) / 16;
nbytes = std::max<size_t>(bytes, nbytes);
data = static_cast<T*>(_mm_malloc(nbytes, 16));
#ifndef NDEBUG
SPS_RELAXED_MEMSET_BEGIN
memset(data, 0, nbytes);
SPS_RELAXED_MEMSET_END
#endif
}
template <class T>
signal1D<T>::~signal1D()
{
if (data)
{
_mm_free(data);
data = nullptr;
}
}
#if defined(__GNUG__) || (defined(_MSC_VER) && (_MSC_VER >= 1800))
template <typename T>
bool pack_r2c(const sps::signal1D<T>& a, sps::signal1D<std::complex<T>>& c)
{
size_t nbytes = 16 * (a.ndata * sizeof(T) + 15) / 16;
c.ndata = a.ndata / 2;
if (c.nbytes < nbytes)
{
c.nbytes = nbytes;
if (c.data)
{
_mm_free(c.data);
c.data = nullptr;
}
c.data = static_cast<std::complex<T>*>(SPS_MM_MALLOC(c.nbytes, 16));
}
// New warning -Wclass-memaccess
memcpy(c.data, reinterpret_cast<const char*>(a.data), a.ndata * sizeof(T));
return true;
}
template <typename T>
bool unpack_c2r(const sps::signal1D<std::complex<T>>& a, sps::signal1D<T>& c)
{
size_t nbytes = 16 * (a.ndata * sizeof(std::complex<T>) + 15) / 16;
c.ndata = 2 * a.ndata;
if (c.nbytes < nbytes)
{
c.nbytes = nbytes;
if (c.data)
{
_mm_free(c.data);
c.data = nullptr;
}
c.data = static_cast<T*>(SPS_MM_MALLOC(c.nbytes, 16));
}
memcpy(c.data, a.data, a.ndata * sizeof(std::complex<T>));
return true;
}
template <>
bool fft<float>(
const sps::signal1D<float>& a, const size_t& n, sps::signal1D<std::complex<float>>& c)
{
c.ndata = n / 2 + 1;
c.offset = 0;
// TODO: Make 2 a divisor (pair-wise multiplication)
// 4 a divisor (four-wise multiplication - filters)
// One extra sample for pair-wise multiplication of complex data
size_t nbytes = 16 * ((c.ndata + 1) * sizeof(std::complex<float>) + 15) / 16;
bool retval = true, pad_a = false;
float* _a = nullptr;
while (retval)
{
// Allocate if necessary
if (c.data)
{
if (c.nbytes < nbytes)
{
_mm_free(c.data);
c.nbytes = nbytes;
c.data = static_cast<std::complex<float>*>(_mm_malloc(c.nbytes, 16));
}
}
else
{
c.nbytes = nbytes;
c.data = static_cast<std::complex<float>*>(_mm_malloc(c.nbytes, 16));
}
// Pad array if necessary
pad_a = a.nbytes < n * sizeof(float);
_a = pad_a ? _mm_padarray<float>(a.data, a.ndata, n) : a.data;
assert((reinterpret_cast<uintptr_t>(_a) & 0x0F) == 0 && "Data must be aligned");
// No need for zeroing output (TEST - NOW WE ZERO OUTPUT)
// memset(c.data,0,c.ndata * sizeof(std::complex<float>));
Signal1DPlan<float>& p = Signal1DPlan<float>::Instance();
fftwf_execute_dft_r2c(p.Forward(n, _a, c.data), _a, reinterpret_cast<fftwf_complex*>(c.data));
break;
}
return true;
}
template <>
bool ifft<float>(
const sps::signal1D<std::complex<float>>& a, const size_t& n, sps::signal1D<float>& c)
{
c.ndata = n;
c.offset = 0;
size_t nbytes = 16 * (n * sizeof(float) + 15) / 16;
bool retval = true, pad_a = false;
std::complex<float>* _a = nullptr;
while (retval)
{
// Allocate if necessary
if (c.data)
{
if (c.nbytes != nbytes)
{
_mm_free(c.data);
c.nbytes = nbytes;
c.data = static_cast<float*>(_mm_malloc(c.nbytes, 16));
}
}
else
{
c.nbytes = nbytes;
c.data = static_cast<float*>(_mm_malloc(c.nbytes, 16));
}
// Pad array if necessary
pad_a = a.nbytes < (n / 2 + 1) * sizeof(std::complex<float>);
_a = pad_a ? _mm_padarray<std::complex<float>>(a.data, a.ndata, n / 2 + 1) : a.data;
assert((reinterpret_cast<uintptr_t>(_a) & 0x0F) == 0 && "Data must be aligned");
// Not necessary
// memset(c.data,0,sizeof(float) * n);
Signal1DPlan<float>& p = Signal1DPlan<float>::Instance();
fftwf_execute_dft_c2r(p.Backward(n, _a, c.data), reinterpret_cast<fftwf_complex*>(_a), c.data);
DivideArray<float>(c.data, n, float(n));
break;
}
return true;
}
template <>
bool fft<double>(
const sps::signal1D<double>& a, const size_t& n, sps::signal1D<std::complex<double>>& c)
{
c.ndata = n / 2 + 1;
c.offset = 0;
// One extra sample for pair-wise multiplication of complex data
size_t nbytes = 16 * ((c.ndata + 1) * sizeof(std::complex<double>) + 15) / 16;
bool retval = true, pad_a = false;
double* _a = nullptr;
while (retval)
{
if (c.data)
{
if (c.nbytes != nbytes)
{
_mm_free(c.data);
c.nbytes = nbytes;
c.data = static_cast<std::complex<double>*>(_mm_malloc(c.nbytes, 16));
}
}
else
{
c.nbytes = nbytes;
c.data = static_cast<std::complex<double>*>(_mm_malloc(c.nbytes, 16));
}
pad_a = a.nbytes < n * sizeof(double);
_a = pad_a ? _mm_padarray<double>(a.data, a.ndata, n) : a.data;
assert((reinterpret_cast<uintptr_t>(_a) & 0x0F) == 0 && "Data must be aligned");
// Not necessary
// memset(c.data,0,c.ndata * sizeof(std::complex<double>));
Signal1DPlan<double>& p = Signal1DPlan<double>::Instance();
fftw_execute_dft_r2c(p.Forward(n, _a, c.data), _a, reinterpret_cast<fftw_complex*>(c.data));
break;
}
return true;
}
template <>
bool ifft<double>(
const sps::signal1D<std::complex<double>>& a, const size_t& n, sps::signal1D<double>& c)
{
c.ndata = n;
c.offset = 0;
size_t nbytes = 16 * (n * sizeof(double) + 15) / 16;
bool retval = true, pad_a = false;
std::complex<double>* _a = nullptr;
while (retval)
{
if (c.data)
{
if (c.nbytes != nbytes)
{
_mm_free(c.data);
c.nbytes = nbytes;
c.data = static_cast<double*>(_mm_malloc(c.nbytes, 16));
}
}
else
{
c.nbytes = nbytes;
c.data = static_cast<double*>(_mm_malloc(c.nbytes, 16));
}
pad_a = a.nbytes < (n / 2 + 1) * sizeof(std::complex<double>);
_a = pad_a ? _mm_padarray<std::complex<double>>(a.data, a.ndata, n / 2 + 1) : a.data;
assert((reinterpret_cast<uintptr_t>(_a) & 0x0F) == 0 && "Data must be aligned");
// Not necessary
// memset(c.data,0,sizeof(double) * n);
Signal1DPlan<double>& p = Signal1DPlan<double>::Instance();
fftw_execute_dft_c2r(p.Backward(n, _a, c.data), reinterpret_cast<fftw_complex*>(_a), c.data);
DivideArray<double>(c.data, n, double(n));
break;
}
return true;
}
#endif
template <>
bool conv_fft<float>(
const sps::signal1D<float>& a, const sps::signal1D<float>& b, sps::signal1D<float>& c)
{
float *_a = NULL, *_b = nullptr;
fftwf_complex *fft_a = NULL, *fft_b = nullptr;
bool pad_a = false, pad_b = false;
size_t n_a = a.ndata;
size_t n_b = b.ndata;
size_t n = next_power_two<size_t>(n_a + n_b - 1);
debug_print("na: %zu, nb: %zu, n: %zu\n", n_a, n_b, n);
bool retval = true;
if ((!a.data || !b.data) || (a.ndata == 0 || b.ndata == 0))
{
debug_print("Uninitialized data\n");
retval = false;
}
while (retval)
{
// If the arrays are long enough, we don't need to copy data
pad_a = a.nbytes < n * sizeof(float);
// Ups
_a = pad_a ? _mm_padarray<float>(a.data, n_a, n) : a.data;
assert((reinterpret_cast<uintptr_t>(_a) & 0x0F) == 0 && "Data must be aligned");
pad_b = b.nbytes < n * sizeof(float);
_b = pad_b ? _mm_padarray<float>(b.data, n_b, n) : b.data;
assert((reinterpret_cast<uintptr_t>(_b) & 0x0F) == 0 && "Data must be aligned");
// One extra complex point added to use fast complex multiply (we multiply two at a time)
fft_a =
static_cast<fftwf_complex*>(SPS_MM_MALLOC(sizeof(fftwf_complex) * ((n / 2 + 1) + 1), 16));
fft_b =
static_cast<fftwf_complex*>(SPS_MM_MALLOC(sizeof(fftwf_complex) * ((n / 2 + 1) + 1), 16));
// TEST TO ZERO ARRAYS
// memset(fft_a, 0, sizeof(fftwf_complex) * ((n/2+1) + 1));
// memset(fft_b, 0, sizeof(fftwf_complex) * ((n/2+1) + 1));
// No need to zero output array
c.ndata = n_a + n_b - 1;
c.offset = a.offset + b.offset;
size_t nbytes = 16 * (n * sizeof(float) + 15) / 16; // Added 16 for complex multiply (removed)
// IS IT AN ISSUE THAT WE REALLOC OUTPUT
if (c.data)
{
assert((reinterpret_cast<uintptr_t>(c.data) & 0x0F) == 0 && "Data must be aligned");
if (c.nbytes < nbytes)
{ // Was != nbytes
_mm_free(c.data);
c.nbytes = nbytes;
c.data = static_cast<float*>(_mm_malloc(c.nbytes, 16));
}
}
else
{
c.nbytes = nbytes;
c.data = static_cast<float*>(_mm_malloc(c.nbytes, 16));
}
SPS_RELAXED_MEMSET_BEGIN
memset(c.data, 0, c.nbytes);
SPS_RELAXED_MEMSET_END
Signal1DPlan<float>& p = Signal1DPlan<float>::Instance();
fftwf_plan forward = p.Forward(n, _a, reinterpret_cast<std::complex<float>*>(fft_a));
fftwf_plan backward = p.Backward(n, reinterpret_cast<std::complex<float>*>(fft_a), c.data);
fftwf_execute_dft_r2c(forward, _a, fft_a);
fftwf_execute_dft_r2c(forward, _b, fft_b); // Error here???
//#define CONV_FFT_USE_COMPLEX 1
#ifdef CONV_FFT_USE_COMPLEX
// It is incredible slow
std::complex<float>* cfft_a = reinterpret_cast<std::complex<float>*>(fft_a);
std::complex<float>* cfft_b = reinterpret_cast<std::complex<float>*>(fft_b);
for (size_t i = 0; i < (n / 2 + 1); i++)
{
cfft_a[i] = cfft_a[i] * cfft_b[i] / float(n);
}
fftwf_execute_dft_c2r(backward, fft_a, c.data);
#elif defined(CONV_FFT_USE_SCALARS)
for (size_t i = 0; i < (n / 2 + 1); i++)
{
// For some reason this is way faster than using <complex>
float real = (fft_a[i][0] * fft_b[i][0] - fft_a[i][1] * fft_b[i][1]);
float imag = (fft_a[i][0] * fft_b[i][1] + fft_a[i][1] * fft_b[i][0]);
fft_a[i][0] = real;
fft_a[i][1] = imag;
}
fftwf_execute_dft_c2r(backward, fft_a, c.data);
// Why n-1
DivideArray<float>(c.data, n - 1, float(n));
#else
__m128 divisor = _mm_set1_ps(1.0f / static_cast<float>(n));
for (size_t i = 0; i < (n / 2 + 1) + 1; i += 2)
{
__m128 vec_a = _mm_load_ps(reinterpret_cast<float*>(&fft_a[i]));
__m128 vec_b = _mm_load_ps(reinterpret_cast<float*>(&fft_b[i]));
vec_b = _mm_mul_ps(_mm_mulcmplx_ps(vec_a, vec_b), divisor);
_mm_store_ps(reinterpret_cast<float*>(&fft_a[i][0]), vec_b);
}
#endif
fftwf_execute_dft_c2r(backward, fft_a, c.data);
break;
}
if (pad_a)
_mm_free(_a);
_a = nullptr;
if (pad_b)
_mm_free(_b);
_b = nullptr;
if (fft_a)
_mm_free(fft_a);
fft_a = nullptr;
if (fft_b)
_mm_free(fft_b);
fft_b = nullptr;
return retval;
}
template <>
bool conv_fft<double>(
const sps::signal1D<double>& a, const sps::signal1D<double>& b, sps::signal1D<double>& c)
{
double *_a = NULL, *_b = nullptr;
fftw_complex *fft_a = NULL, *fft_b = nullptr;
bool pad_a = false, pad_b = false;
size_t n_a = a.ndata;
size_t n_b = b.ndata;
size_t n = next_power_two<size_t>(n_a + n_b - 1);
bool retval = true;
if (!a.data || !b.data)
retval = false;
while (retval)
{
pad_a = a.nbytes < n * sizeof(double);
_a = pad_a ? _mm_padarray<double>(a.data, n_a, n) : a.data;
pad_b = b.nbytes < n * sizeof(double);
_b = pad_b ? _mm_padarray<double>(b.data, n_b, n) : b.data;
// Here FFTW is used (no need to memset)
fft_a = static_cast<fftw_complex*>(SPS_MM_MALLOC(sizeof(fftw_complex) * (n / 2 + 1), 16));
fft_b = static_cast<fftw_complex*>(_mm_malloc(sizeof(fftw_complex) * (n / 2 + 1), 16));
size_t nbytes = 16 * (n * sizeof(double) + 15) / 16;
c.ndata = n_a + n_b - 1;
c.offset = a.offset + b.offset;
// Allocate if needed
if (c.data)
{
if (c.nbytes != nbytes)
{
_mm_free(c.data);
c.nbytes = nbytes;
c.data = static_cast<double*>(_mm_malloc(c.nbytes, 16));
}
}
else
{
c.nbytes = nbytes;
c.data = static_cast<double*>(_mm_malloc(c.nbytes, 16));
}
Signal1DPlan<double>& p = Signal1DPlan<double>::Instance();
fftw_plan forward = p.Forward(n, _a, reinterpret_cast<std::complex<double>*>(fft_a));
fftw_plan backward = p.Backward(n, reinterpret_cast<std::complex<double>*>(fft_a), c.data);
fftw_execute_dft_r2c(forward, _a, fft_a);
fftw_execute_dft_r2c(forward, _b, fft_b);
// Complex multiplication
for (size_t i = 0; i < (n / 2 + 1); i++)
{
double real = fft_a[i][0] * fft_b[i][0] - fft_a[i][1] * fft_b[i][1];
double imag = fft_a[i][0] * fft_b[i][1] + fft_a[i][1] * fft_b[i][0];
fft_a[i][0] = real;
fft_a[i][1] = imag;
}
fftw_execute_dft_c2r(backward, fft_a, c.data);
// Why n-1
DivideArray<double>(c.data, n - 1, double(n));
break;
}
if (pad_a)
_mm_free(_a);
_a = nullptr;
if (pad_b)
_mm_free(_b);
_b = nullptr;
if (fft_a)
_mm_free(fft_a);
fft_a = nullptr;
if (fft_b)
_mm_free(fft_b);
fft_b = nullptr;
return retval;
}
template <typename T>
bool conv_fft_fs(