forked from arrayfire/arrayfire-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignal.py
More file actions
1650 lines (1170 loc) · 45.2 KB
/
signal.py
File metadata and controls
1650 lines (1170 loc) · 45.2 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
#######################################################
# Copyright (c) 2019, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
"""
Signal processing functions (fft, convolve, etc).
"""
from .array import Array
from .bcast import broadcast
from .library import backend, safe_call, CONV_DOMAIN, CONV_MODE, INTERP, PAD, c_dim_t, c_double_t, c_float_t, c_pointer, c_size_t
from .util import dim4, dim4_to_tuple
@broadcast
def _scale_pos_axis0(x_curr, x_orig):
x0 = x_orig[0, 0, 0, 0]
dx = x_orig[1, 0, 0, 0] - x0
return (x_curr - x0) / dx
@broadcast
def _scale_pos_axis1(y_curr, y_orig):
y0 = y_orig[0, 0, 0, 0]
dy = y_orig[0, 1, 0, 0] - y0
return (y_curr - y0) / dy
def approx1(signal, x, method=INTERP.LINEAR, off_grid=0.0, xp = None, output = None):
"""
Interpolate along a single dimension.Interpolation is performed along axis 0
of the input array.
Parameters
----------
signal: af.Array
Input signal array (signal = f(x))
x: af.Array
The x-coordinates of the interpolation points. The interpolation
function is queried at these set of points.
method: optional: af.INTERP. default: af.INTERP.LINEAR.
Interpolation method.
off_grid: optional: scalar. default: 0.0.
The value used for positions outside the range.
xp : af.Array
The x-coordinates of the input data points
output: None or af.Array
Optional preallocated output array. If it is a sub-array of an existing af_array,
only the corresponding portion of the af_array will be overwritten
Returns
-------
output: af.Array
Values calculated at interpolation points.
Note
-----
This holds applicable when x_input isn't provided:
The initial measurements are assumed to have taken place at equal steps between [0, N - 1],
where N is the length of the first dimension of `signal`.
"""
if output is None:
output = Array()
if(xp is not None):
pos0 = _scale_pos_axis0(x, xp)
else:
pos0 = x
safe_call(backend.get().af_approx1(c_pointer(output.arr), signal.arr, pos0.arr,
method.value, c_float_t(off_grid)))
else:
if(xp is not None):
pos0 = _scale_pos_axis0(x, xp)
else:
pos0 = x
safe_call(backend.get().af_approx1_v2(c_pointer(output.arr), signal.arr, pos0.arr,
method.value, c_float_t(off_grid)))
return output
def approx1_uniform(signal, x, interp_dim, idx_start, idx_step, method=INTERP.LINEAR, off_grid=0.0, output = None):
"""
Interpolation on one dimensional signals along specified dimension.
af_approx1_uniform() accepts the dimension to perform the interpolation along the input.
It also accepts start and step values which define the uniform range of corresponding indices.
Parameters
----------
signal: af.Array
Input signal array (signal = f(x))
x: af.Array
The x-coordinates of the interpolation points. The interpolation
function is queried at these set of points.
interp_dim: scalar
is the dimension to perform interpolation across.
idx_start: scalar
is the first index value along interp_dim.
idx_step: scalar
is the uniform spacing value between subsequent indices along interp_dim.
method: optional: af.INTERP. default: af.INTERP.LINEAR.
Interpolation method.
off_grid: optional: scalar. default: 0.0.
The value used for positions outside the range.
output: None or af.Array
Optional preallocated output array. If it is a sub-array of an existing af_array,
only the corresponding portion of the af_array will be overwritten
Returns
-------
output: af.Array
Values calculated at interpolation points.
"""
if output is None:
output = Array()
safe_call(backend.get().af_approx1_uniform(c_pointer(output.arr), signal.arr, x.arr,
c_dim_t(interp_dim), c_double_t(idx_start), c_double_t(idx_step),
method.value, c_float_t(off_grid)))
else:
safe_call(backend.get().af_approx1_uniform_v2(c_pointer(output.arr), signal.arr, x.arr,
c_dim_t(interp_dim), c_double_t(idx_start), c_double_t(idx_step),
method.value, c_float_t(off_grid)))
return output
def approx2(signal, x, y,
method=INTERP.LINEAR, off_grid=0.0, xp = None, yp = None, output = None):
"""
Interpolate along a two dimension.Interpolation is performed along axes 0 and 1
of the input array.
Parameters
----------
signal: af.Array
Input signal array (signal = f(x, y))
x : af.Array
The x-coordinates of the interpolation points. The interpolation
function is queried at these set of points.
y : af.Array
The y-coordinates of the interpolation points. The interpolation
function is queried at these set of points.
method: optional: af.INTERP. default: af.INTERP.LINEAR.
Interpolation method.
off_grid: optional: scalar. default: 0.0.
The value used for positions outside the range.
xp : af.Array
The x-coordinates of the input data points. The convention followed is that
the x-coordinates vary along axis 0
yp : af.Array
The y-coordinates of the input data points. The convention followed is that
the y-coordinates vary along axis 1
output: None or af.Array
Optional preallocated output array. If it is a sub-array of an existing af_array,
only the corresponding portion of the af_array will be overwritten
Returns
-------
output: af.Array
Values calculated at interpolation points.
Note
-----
This holds applicable when x_input/y_input isn't provided:
The initial measurements are assumed to have taken place at equal steps between [(0,0) - [M - 1, N - 1]]
where M is the length of the first dimension of `signal`,
and N is the length of the second dimension of `signal`.
"""
if output is None:
output = Array()
if(xp is not None):
pos0 = _scale_pos_axis0(x, xp)
else:
pos0 = x
if(yp is not None):
pos1 = _scale_pos_axis1(y, yp)
else:
pos1 = y
safe_call(backend.get().af_approx2(c_pointer(output.arr), signal.arr,
pos0.arr, pos1.arr, method.value, c_float_t(off_grid)))
else:
if(xp is not None):
pos0 = _scale_pos_axis0(x, xp)
else:
pos0 = x
if(yp is not None):
pos1 = _scale_pos_axis1(y, yp)
else:
pos1 = y
safe_call(backend.get().af_approx2_v2(c_pointer(output.arr), signal.arr,
pos0.arr, pos1.arr, method.value, c_float_t(off_grid)))
return output
def approx2_uniform(signal, pos0, interp_dim0, idx_start0, idx_step0, pos1, interp_dim1, idx_start1, idx_step1,
method=INTERP.LINEAR, off_grid=0.0, output = None):
"""
Interpolate along two uniformly spaced dimensions of the input array.
af_approx2_uniform() accepts two dimensions to perform the interpolation along the input.
It also accepts start and step values which define the uniform range of corresponding indices.
Parameters
----------
signal: af.Array
Input signal array (signal = f(x, y))
pos0 : af.Array
positions of the interpolation points along interp_dim0.
interp_dim0: scalar
is the first dimension to perform interpolation across.
idx_start0: scalar
is the first index value along interp_dim0.
idx_step0: scalar
is the uniform spacing value between subsequent indices along interp_dim0.
pos1 : af.Array
positions of the interpolation points along interp_dim1.
interp_dim1: scalar
is the second dimension to perform interpolation across.
idx_start1: scalar
is the first index value along interp_dim1.
idx_step1: scalar
is the uniform spacing value between subsequent indices along interp_dim1.
method: optional: af.INTERP. default: af.INTERP.LINEAR.
Interpolation method.
off_grid: optional: scalar. default: 0.0.
The value used for positions outside the range.
output: None or af.Array
Optional preallocated output array. If it is a sub-array of an existing af_array,
only the corresponding portion of the af_array will be overwritten
Returns
-------
output: af.Array
Values calculated at interpolation points.
Note
-----
This holds applicable when x_input/y_input isn't provided:
The initial measurements are assumed to have taken place at equal steps between [(0,0) - [M - 1, N - 1]]
where M is the length of the first dimension of `signal`,
and N is the length of the second dimension of `signal`.
"""
if output is None:
output = Array()
safe_call(backend.get().af_approx2_uniform(c_pointer(output.arr), signal.arr,
pos0.arr, c_dim_t(interp_dim0), c_double_t(idx_start0), c_double_t(idx_step0),
pos1.arr, c_dim_t(interp_dim1), c_double_t(idx_start1), c_double_t(idx_step1),
method.value, c_float_t(off_grid)))
else:
safe_call(backend.get().af_approx2_uniform_v2(c_pointer(output.arr), signal.arr,
pos0.arr, c_dim_t(interp_dim0), c_double_t(idx_start0), c_double_t(idx_step0),
pos1.arr, c_dim_t(interp_dim1), c_double_t(idx_start1), c_double_t(idx_step1),
method.value, c_float_t(off_grid)))
return output
def fft(signal, dim0 = None , scale = None):
"""
Fast Fourier Transform: 1D
Parameters
----------
signal: af.Array
A 1 dimensional signal or a batch of 1 dimensional signals.
dim0: optional: int. default: None.
- Specifies the size of the output.
- If None, dim0 is calculated to be the first dimension of `signal`.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.
Returns
-------
output: af.Array
A complex af.Array containing the full output of the fft.
"""
if dim0 is None:
dim0 = 0
if scale is None:
scale = 1.0
output = Array()
safe_call(backend.get().af_fft(c_pointer(output.arr), signal.arr, c_double_t(scale), c_dim_t(dim0)))
return output
def fft2(signal, dim0=None, dim1=None, scale=None):
"""
Fast Fourier Transform: 2D
Parameters
----------
signal: af.Array
A 2 dimensional signal or a batch of 2 dimensional signals.
dim0: optional: int. default: None.
- Specifies the size of the output.
- If None, dim0 is calculated to be the first dimension of `signal`.
dim1: optional: int. default: None.
- Specifies the size of the output.
- If None, dim1 is calculated to be the second dimension of `signal`.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.
Returns
-------
output: af.Array
A complex af.Array containing the full output of the fft.
"""
if dim0 is None:
dim0 = 0
if dim1 is None:
dim1 = 0
if scale is None:
scale = 1.0
output = Array()
safe_call(backend.get().af_fft2(
c_pointer(output.arr), signal.arr, c_double_t(scale), c_dim_t(dim0), c_dim_t(dim1)))
return output
def fft3(signal, dim0=None, dim1=None, dim2=None, scale=None):
"""
Fast Fourier Transform: 3D
Parameters
----------
signal: af.Array
A 3 dimensional signal or a batch of 3 dimensional signals.
dim0: optional: int. default: None.
- Specifies the size of the output.
- If None, dim0 is calculated to be the first dimension of `signal`.
dim1: optional: int. default: None.
- Specifies the size of the output.
- If None, dim1 is calculated to be the second dimension of `signal`.
dim2: optional: int. default: None.
- Specifies the size of the output.
- If None, dim2 is calculated to be the third dimension of `signal`.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.
Returns
-------
output: af.Array
A complex af.Array containing the full output of the fft.
"""
if dim0 is None:
dim0 = 0
if dim1 is None:
dim1 = 0
if dim2 is None:
dim2 = 0
if scale is None:
scale = 1.0
output = Array()
safe_call(backend.get().af_fft3(
c_pointer(output.arr), signal.arr, c_double_t(scale), c_dim_t(dim0), c_dim_t(dim1), c_dim_t(dim2)))
return output
def ifft(signal, dim0=None, scale=None):
"""
Inverse Fast Fourier Transform: 1D
Parameters
----------
signal: af.Array
A 1 dimensional signal or a batch of 1 dimensional signals.
dim0: optional: int. default: None.
- Specifies the size of the output.
- If None, dim0 is calculated to be the first dimension of `signal`.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.0 / (dim0)
Returns
-------
output: af.Array
A complex af.Array containing the full output of the inverse fft.
Note
----
The output is always complex.
"""
if dim0 is None:
dim0 = signal.dims()[0]
if scale is None:
scale = 1.0/float(dim0)
output = Array()
safe_call(backend.get().af_ifft(c_pointer(output.arr), signal.arr, c_double_t(scale), c_dim_t(dim0)))
return output
def ifft2(signal, dim0=None, dim1=None, scale=None):
"""
Inverse Fast Fourier Transform: 2D
Parameters
----------
signal: af.Array
A 2 dimensional signal or a batch of 2 dimensional signals.
dim0: optional: int. default: None.
- Specifies the size of the output.
- If None, dim0 is calculated to be the first dimension of `signal`.
dim1: optional: int. default: None.
- Specifies the size of the output.
- If None, dim1 is calculated to be the second dimension of `signal`.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.0 / (dim0 * dim1)
Returns
-------
output: af.Array
A complex af.Array containing the full output of the inverse fft.
Note
----
The output is always complex.
"""
dims = signal.dims()
if dim0 is None:
dim0 = dims[0]
if dim1 is None:
dim1 = dims[1]
if scale is None:
scale = 1.0/float(dim0 * dim1)
output = Array()
safe_call(backend.get().af_ifft2(
c_pointer(output.arr), signal.arr, c_double_t(scale), c_dim_t(dim0), c_dim_t(dim1)))
return output
def ifft3(signal, dim0=None, dim1=None, dim2=None, scale=None):
"""
Inverse Fast Fourier Transform: 3D
Parameters
----------
signal: af.Array
A 3 dimensional signal or a batch of 3 dimensional signals.
dim0: optional: int. default: None.
- Specifies the size of the output.
- If None, dim0 is calculated to be the first dimension of `signal`.
dim1: optional: int. default: None.
- Specifies the size of the output.
- If None, dim1 is calculated to be the second dimension of `signal`.
dim2: optional: int. default: None.
- Specifies the size of the output.
- If None, dim2 is calculated to be the third dimension of `signal`.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.0 / (dim0 * dim1 * dim2).
Returns
-------
output: af.Array
A complex af.Array containing the full output of the inverse fft.
Note
----
The output is always complex.
"""
dims = signal.dims()
if dim0 is None:
dim0 = dims[0]
if dim1 is None:
dim1 = dims[1]
if dim2 is None:
dim2 = dims[2]
if scale is None:
scale = 1.0 / float(dim0 * dim1 * dim2)
output = Array()
safe_call(backend.get().af_ifft3(
c_pointer(output.arr), signal.arr, c_double_t(scale), c_dim_t(dim0), c_dim_t(dim1), c_dim_t(dim2)))
return output
def fft_inplace(signal, scale=None):
"""
In-place Fast Fourier Transform: 1D
Parameters
----------
signal: af.Array
A 1 dimensional signal or a batch of 1 dimensional signals.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.
"""
if scale is None:
scale = 1.0
safe_call(backend.get().af_fft_inplace(signal.arr, c_double_t(scale)))
def fft2_inplace(signal, scale=None):
"""
In-place Fast Fourier Transform: 2D
Parameters
----------
signal: af.Array
A 2 dimensional signal or a batch of 2 dimensional signals.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.
"""
if scale is None:
scale = 1.0
safe_call(backend.get().af_fft2_inplace(signal.arr, c_double_t(scale)))
def fft3_inplace(signal, scale=None):
"""
In-place Fast Fourier Transform: 3D
Parameters
----------
signal: af.Array
A 3 dimensional signal or a batch of 3 dimensional signals.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.
"""
if scale is None:
scale = 1.0
# FIXME: output is assigned, but not used in function
output = Array()
safe_call(backend.get().af_fft3_inplace(signal.arr, c_double_t(scale)))
def ifft_inplace(signal, scale=None):
"""
Inverse In-place Fast Fourier Transform: 1D
Parameters
----------
signal: af.Array
A 1 dimensional signal or a batch of 1 dimensional signals.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.0 / (signal.dims()[0])
"""
if scale is None:
dim0 = signal.dims()[0]
scale = 1.0/float(dim0)
safe_call(backend.get().af_ifft_inplace(signal.arr, c_double_t(scale)))
def ifft2_inplace(signal, scale=None):
"""
Inverse In-place Fast Fourier Transform: 2D
Parameters
----------
signal: af.Array
A 2 dimensional signal or a batch of 2 dimensional signals.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.0 / (signal.dims()[0] * signal.dims()[1])
"""
dims = signal.dims()
if scale is None:
dim0 = dims[0]
dim1 = dims[1]
scale = 1.0/float(dim0 * dim1)
safe_call(backend.get().af_ifft2_inplace(signal.arr, c_double_t(scale)))
def ifft3_inplace(signal, scale=None):
"""
Inverse In-place Fast Fourier Transform: 3D
Parameters
----------
signal: af.Array
A 3 dimensional signal or a batch of 3 dimensional signals.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.0 / (signal.dims()[0] * signal.dims()[1] * signal.dims()[2]).
"""
dims = signal.dims()
if scale is None:
dim0 = dims[0]
dim1 = dims[1]
dim2 = dims[2]
scale = 1.0 / float(dim0 * dim1 * dim2)
safe_call(backend.get().af_ifft3_inplace(signal.arr, c_double_t(scale)))
def fft_r2c(signal, dim0=None, scale=None):
"""
Real to Complex Fast Fourier Transform: 1D
Parameters
----------
signal: af.Array
A 1 dimensional signal or a batch of 1 dimensional signals.
dim0: optional: int. default: None.
- Specifies the size of the output.
- If None, dim0 is calculated to be the first dimension of `signal`.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.
Returns
-------
output: af.Array
A complex af.Array containing the non-redundant parts of the full FFT.
"""
if dim0 is None:
dim0 = 0
if scale is None:
scale = 1.0
output = Array()
safe_call(backend.get().af_fft_r2c(c_pointer(output.arr), signal.arr, c_double_t(scale), c_dim_t(dim0)))
return output
def fft2_r2c(signal, dim0=None, dim1=None, scale=None):
"""
Real to Complex Fast Fourier Transform: 2D
Parameters
----------
signal: af.Array
A 2 dimensional signal or a batch of 2 dimensional signals.
dim0: optional: int. default: None.
- Specifies the size of the output.
- If None, dim0 is calculated to be the first dimension of `signal`.
dim1: optional: int. default: None.
- Specifies the size of the output.
- If None, dim1 is calculated to be the second dimension of `signal`.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.
Returns
-------
output: af.Array
A complex af.Array containing the non-redundant parts of the full FFT.
"""
if dim0 is None:
dim0 = 0
if dim1 is None:
dim1 = 0
if scale is None:
scale = 1.0
output = Array()
safe_call(backend.get().af_fft2_r2c(
c_pointer(output.arr), signal.arr, c_double_t(scale), c_dim_t(dim0), c_dim_t(dim1)))
return output
def fft3_r2c(signal, dim0=None, dim1=None, dim2=None, scale=None):
"""
Real to Complex Fast Fourier Transform: 3D
Parameters
----------
signal: af.Array
A 3 dimensional signal or a batch of 3 dimensional signals.
dim0: optional: int. default: None.
- Specifies the size of the output.
- If None, dim0 is calculated to be the first dimension of `signal`.
dim1: optional: int. default: None.
- Specifies the size of the output.
- If None, dim1 is calculated to be the second dimension of `signal`.
dim2: optional: int. default: None.
- Specifies the size of the output.
- If None, dim2 is calculated to be the third dimension of `signal`.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1.
Returns
-------
output: af.Array
A complex af.Array containing the non-redundant parts of the full FFT.
"""
if dim0 is None:
dim0 = 0
if dim1 is None:
dim1 = 0
if dim2 is None:
dim2 = 0
if scale is None:
scale = 1.0
output = Array()
safe_call(backend.get().af_fft3_r2c(
c_pointer(output.arr), signal.arr, c_double_t(scale), c_dim_t(dim0), c_dim_t(dim1), c_dim_t(dim2)))
return output
def _get_c2r_dim(dim, is_odd):
return 2 * (dim - 1) + int(is_odd)
def fft_c2r(signal, is_odd=False, scale=None):
"""
Real to Complex Fast Fourier Transform: 1D
Parameters
----------
signal: af.Array
A 1 dimensional signal or a batch of 1 dimensional signals.
is_odd: optional: Boolean. default: False.
- Specifies if the first dimension of output should be even or odd.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1 / (signal.dims()[0]).
Returns
-------
output: af.Array
A real af.Array containing the full output of the fft.
"""
if scale is None:
dim0 = _get_c2r_dim(signal.dims()[0], is_odd)
scale = 1.0/float(dim0)
output = Array()
safe_call(backend.get().af_fft_c2r(c_pointer(output.arr), signal.arr, c_double_t(scale), is_odd))
return output
def fft2_c2r(signal, is_odd=False, scale=None):
"""
Real to Complex Fast Fourier Transform: 2D
Parameters
----------
signal: af.Array
A 2 dimensional signal or a batch of 2 dimensional signals.
is_odd: optional: Boolean. default: False.
- Specifies if the first dimension of output should be even or odd.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1 / (signal.dims()[0] * signal.dims()[1]).
Returns
-------
output: af.Array
A real af.Array containing the full output of the fft.
"""
dims = signal.dims()
if scale is None:
dim0 = _get_c2r_dim(dims[0], is_odd)
dim1 = dims[1]
scale = 1.0/float(dim0 * dim1)
output = Array()
safe_call(backend.get().af_fft2_c2r(c_pointer(output.arr), signal.arr, c_double_t(scale), is_odd))
return output
def fft3_c2r(signal, is_odd=False, scale=None):
"""
Real to Complex Fast Fourier Transform: 3D
Parameters
----------
signal: af.Array
A 3 dimensional signal or a batch of 3 dimensional signals.
is_odd: optional: Boolean. default: False.
- Specifies if the first dimension of output should be even or odd.
scale: optional: scalar. default: None.
- Specifies the scaling factor.
- If None, scale is set to 1 / (signal.dims()[0] * signal.dims()[1] * signal.dims()[2]).
Returns
-------
output: af.Array
A real af.Array containing the full output of the fft.
"""
dims = signal.dims()
if scale is None:
dim0 = _get_c2r_dim(dims[0], is_odd)
dim1 = dims[1]
dim2 = dims[2]
scale = 1.0/float(dim0 * dim1 * dim2)
output = Array()
safe_call(backend.get().af_fft3_c2r(c_pointer(output.arr), signal.arr, c_double_t(scale), is_odd))
return output
def dft(signal, odims=(None, None, None, None), scale=None):
"""
Non batched Fourier transform.
This function performs n-dimensional fourier transform depending on the input dimensions.
Parameters
----------
signal: af.Array
- A multi dimensional arrayfire array.
odims: optional: tuple of ints. default: (None, None, None, None).
- If None, calculated to be `signal.dims()`
scale: optional: scalar. default: None.
- Scale factor for the fourier transform.
- If none, calculated to be 1.0.
Returns
-------
output: af.Array
- A complex array that is the ouput of n-dimensional fourier transform.
"""
# FIXME: odims4 is assigned, but not used in function
odims4 = dim4_to_tuple(odims, default=None)