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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
extern crate libc;
extern crate num;

use self::libc::{c_double, c_float, c_int, c_longlong, c_uint, size_t};
use self::num::Complex;
use crate::array::Array;
use crate::defines::{AfError, ConvDomain, ConvMode, InterpType};
use crate::error::HANDLE_ERROR;
use crate::util::{AfArray, MutAfArray};
use crate::util::{ComplexFloating, FloatingPoint, HasAfEnum, RealFloating};

#[allow(dead_code)]
extern "C" {
    fn af_approx1(
        out: MutAfArray,
        inp: AfArray,
        pos: AfArray,
        method: c_int,
        off_grid: c_float,
    ) -> c_int;

    fn af_approx1_v2(
        out: MutAfArray,
        inp: AfArray,
        pos: AfArray,
        method: c_int,
        off_grid: c_float,
    ) -> c_int;

    fn af_approx1_uniform(
        out: MutAfArray,
        inp: AfArray,
        pos: AfArray,
        interp_dim: c_int,
        idx_start: c_double,
        idx_step: c_double,
        method: c_int,
        off_grid: c_float,
    ) -> c_int;

    fn af_approx1_uniform_v2(
        out: MutAfArray,
        inp: AfArray,
        pos: AfArray,
        interp_dim: c_int,
        idx_start: c_double,
        idx_step: c_double,
        method: c_int,
        off_grid: c_float,
    ) -> c_int;

    fn af_approx2(
        out: MutAfArray,
        inp: AfArray,
        pos0: AfArray,
        pos1: AfArray,
        method: c_int,
        off_grid: c_float,
    ) -> c_int;

    fn af_approx2_v2(
        out: MutAfArray,
        inp: AfArray,
        pos0: AfArray,
        pos1: AfArray,
        method: c_int,
        off_grid: c_float,
    ) -> c_int;

    fn af_approx2_uniform(
        out: MutAfArray,
        inp: AfArray,
        pos0: AfArray,
        interp_dim0: c_int,
        idx_start_dim0: c_double,
        idx_step_dim0: c_double,
        pos1: AfArray,
        interp_dim1: c_int,
        idx_start_dim1: c_double,
        idx_step_dim1: c_double,
        method: c_int,
        off_grid: c_float,
    ) -> c_int;

    fn af_approx2_uniform_v2(
        out: MutAfArray,
        inp: AfArray,
        pos0: AfArray,
        interp_dim0: c_int,
        idx_start_dim0: c_double,
        idx_step_dim0: c_double,
        pos1: AfArray,
        interp_dim1: c_int,
        idx_start_dim1: c_double,
        idx_step_dim1: c_double,
        method: c_int,
        off_grid: c_float,
    ) -> c_int;

    fn af_set_fft_plan_cache_size(cache_size: size_t) -> c_int;

    fn af_fft(out: MutAfArray, arr: AfArray, nfac: c_double, odim0: c_longlong) -> c_int;

    fn af_fft2(
        out: MutAfArray,
        arr: AfArray,
        nfac: c_double,
        odim0: c_longlong,
        odim1: c_longlong,
    ) -> c_int;

    fn af_fft3(
        out: MutAfArray,
        arr: AfArray,
        nfac: c_double,
        odim0: c_longlong,
        odim1: c_longlong,
        odim2: c_longlong,
    ) -> c_int;

    fn af_ifft(out: MutAfArray, arr: AfArray, nfac: c_double, odim0: c_longlong) -> c_int;

    fn af_ifft2(
        out: MutAfArray,
        arr: AfArray,
        nfac: c_double,
        odim0: c_longlong,
        odim1: c_longlong,
    ) -> c_int;

    fn af_ifft3(
        out: MutAfArray,
        arr: AfArray,
        nfac: c_double,
        odim0: c_longlong,
        odim1: c_longlong,
        odim2: c_longlong,
    ) -> c_int;

    fn af_fft_inplace(arr: AfArray, nfac: c_double) -> c_int;
    fn af_fft2_inplace(arr: AfArray, nfac: c_double) -> c_int;
    fn af_fft3_inplace(arr: AfArray, nfac: c_double) -> c_int;
    fn af_ifft_inplace(arr: AfArray, nfac: c_double) -> c_int;
    fn af_ifft2_inplace(arr: AfArray, nfac: c_double) -> c_int;
    fn af_ifft3_inplace(arr: AfArray, nfac: c_double) -> c_int;

    fn af_fft_r2c(out: MutAfArray, arr: AfArray, nfac: c_double, pad0: c_longlong) -> c_int;
    fn af_fft2_r2c(
        out: MutAfArray,
        arr: AfArray,
        nfac: c_double,
        pad0: c_longlong,
        pad1: c_longlong,
    ) -> c_int;
    fn af_fft3_r2c(
        out: MutAfArray,
        arr: AfArray,
        nfac: c_double,
        pad0: c_longlong,
        pad1: c_longlong,
        pad2: c_longlong,
    ) -> c_int;

    fn af_fft_c2r(out: MutAfArray, input: AfArray, nfac: c_double, is_odd: c_int) -> c_int;
    fn af_fft2_c2r(out: MutAfArray, input: AfArray, nfac: c_double, is_odd: c_int) -> c_int;
    fn af_fft3_c2r(out: MutAfArray, input: AfArray, nfac: c_double, is_odd: c_int) -> c_int;

    fn af_convolve1(out: MutAfArray, s: AfArray, f: AfArray, m: c_uint, d: c_uint) -> c_int;
    fn af_convolve2(out: MutAfArray, s: AfArray, f: AfArray, m: c_uint, d: c_uint) -> c_int;
    fn af_convolve3(out: MutAfArray, s: AfArray, f: AfArray, m: c_uint, d: c_uint) -> c_int;
    fn af_convolve2_sep(o: MutAfArray, c: AfArray, r: AfArray, s: AfArray, m: c_uint) -> c_int;
    fn af_fft_convolve1(out: MutAfArray, s: AfArray, f: AfArray, m: c_uint) -> c_int;
    fn af_fft_convolve2(out: MutAfArray, s: AfArray, f: AfArray, m: c_uint) -> c_int;
    fn af_fft_convolve3(out: MutAfArray, s: AfArray, f: AfArray, m: c_uint) -> c_int;
    fn af_fir(out: MutAfArray, b: AfArray, x: AfArray) -> c_int;
    fn af_iir(out: MutAfArray, b: AfArray, a: AfArray, x: AfArray) -> c_int;
}

/// Perform signal interpolation for 1d signals
///
/// # Parameters
///
/// - `input` is the input Array
/// - `pos` Array contains the interpolation locations
/// - `method` indicates the type of interpolation method that be used. It is of type enum
/// [InterpType](./enum.InterpType.html)
/// - `off_grid` is the value that will set in the output Array when certain index is out of bounds
///
/// # Return Values
///
/// An Array with interpolated values
pub fn approx1<T, P>(
    input: &Array<T>,
    pos: &Array<P>,
    method: InterpType,
    off_grid: f32,
) -> Array<T>
where
    T: HasAfEnum + FloatingPoint,
    P: HasAfEnum + RealFloating,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_approx1(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            pos.get() as AfArray,
            method as c_int,
            off_grid as c_float,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// Same as [approx1](./fn.approx1.html) but uses existing Array as output
pub fn approx1_v2<T, P>(
    output: &mut Array<T>,
    input: &Array<T>,
    pos: &Array<P>,
    method: InterpType,
    off_grid: f32,
) where
    T: HasAfEnum + FloatingPoint,
    P: HasAfEnum + RealFloating,
{
    unsafe {
        let err_val = af_approx1_v2(
            output.get() as MutAfArray,
            input.get() as AfArray,
            pos.get() as AfArray,
            method as c_int,
            off_grid as c_float,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
}

/// Perform signal interpolation for 1d signals along specified dimension
///
/// # Parameters
///
/// - `input` is the input Array
/// - `pos` Array contains the interpolation locations
/// - `interp_dim` is the dimension along which interpolation is performed
/// - `start` is the first index along `interp_dim`
/// - `step` is the uniform spacing value between subsequent indices along `interp_dim`
/// - `method` indicates the type of interpolation method that be used. It is of type enum
/// [InterpType](./enum.InterpType.html)
/// - `off_grid` is the value that will set in the output Array when certain index is out of bounds
///
/// # Return Values
///
/// An Array with interpolated values
pub fn approx1_uniform<T, P>(
    input: &Array<T>,
    pos: &Array<P>,
    interp_dim: i32,
    start: f64,
    step: f64,
    method: InterpType,
    off_grid: f32,
) -> Array<T>
where
    T: HasAfEnum + FloatingPoint,
    P: HasAfEnum + RealFloating,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_approx1_uniform(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            pos.get() as AfArray,
            interp_dim,
            start,
            step,
            method as c_int,
            off_grid,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// Same as [approx1_uniform](./fn.approx1_uniform.html) but uses existing Array as output
#[allow(clippy::too_many_arguments)]
pub fn approx1_uniform_v2<T, P>(
    output: &mut Array<T>,
    input: &Array<T>,
    pos: &Array<P>,
    interp_dim: i32,
    start: f64,
    step: f64,
    method: InterpType,
    off_grid: f32,
) where
    T: HasAfEnum + FloatingPoint,
    P: HasAfEnum + RealFloating,
{
    unsafe {
        let err_val = af_approx1_uniform_v2(
            output.get() as MutAfArray,
            input.get() as AfArray,
            pos.get() as AfArray,
            interp_dim,
            start,
            step,
            method as c_int,
            off_grid,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
}

/// Perform signal interpolation for 2d signals
///
/// # Parameters
///
/// - `input` is the input Array
/// - `pos0` Array contains the interpolation locations for first dimension
/// - `pos1` Array contains the interpolation locations for second dimension
/// - `method` indicates the type of interpolation method that be used. It is of type enum
/// [InterpType](./enum.InterpType.html)
/// - `off_grid` is the value that will set in the output Array when certain index is out of bounds
///
/// # Return Values
///
/// An Array with interpolated values
pub fn approx2<T, P>(
    input: &Array<T>,
    pos0: &Array<P>,
    pos1: &Array<P>,
    method: InterpType,
    off_grid: f32,
) -> Array<T>
where
    T: HasAfEnum + FloatingPoint,
    P: HasAfEnum + RealFloating,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_approx2(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            pos0.get() as AfArray,
            pos1.get() as AfArray,
            method as c_int,
            off_grid as c_float,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// Same as [approx2](./fn.approx2.html) but uses existing Array as output
pub fn approx2_v2<T, P>(
    output: &mut Array<T>,
    input: &Array<T>,
    pos0: &Array<P>,
    pos1: &Array<P>,
    method: InterpType,
    off_grid: f32,
) where
    T: HasAfEnum + FloatingPoint,
    P: HasAfEnum + RealFloating,
{
    unsafe {
        let err_val = af_approx2_v2(
            output.get() as MutAfArray,
            input.get() as AfArray,
            pos0.get() as AfArray,
            pos1.get() as AfArray,
            method as c_int,
            off_grid as c_float,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
}

/// Perform signal interpolation for 2d signals along a specified dimension
///
/// # Parameters
///
/// - `input` is the input Array
/// - `pos0` Array contains the interpolation locations for first dimension
/// - `interp_dim0` is the dimension along which interpolation is performed
/// - `start0` is the first index along `interp_dim0`
/// - `step0` is the uniform spacing value between subsequent indices along `interp_dim0`
/// - `pos1` Array contains the interpolation locations for second dimension
/// - `interp_dim0` is the dimension along which interpolation is performed
/// - `start0` is the first index along `interp_dim1`
/// - `step0` is the uniform spacing value between subsequent indices along `interp_dim1`
/// - `method` indicates the type of interpolation method that be used. It is of type enum
/// [InterpType](./enum.InterpType.html)
/// - `off_grid` is the value that will set in the output Array when certain index is out of bounds
///
/// # Return Values
///
/// An Array with interpolated values
#[allow(clippy::too_many_arguments)]
pub fn approx2_uniform<T, P>(
    input: &Array<T>,
    pos0: &Array<P>,
    interp_dim0: i32,
    start0: f64,
    step0: f64,
    pos1: &Array<P>,
    interp_dim1: i32,
    start1: f64,
    step1: f64,
    method: InterpType,
    off_grid: f32,
) -> Array<T>
where
    T: HasAfEnum + FloatingPoint,
    P: HasAfEnum + RealFloating,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_approx2_uniform(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            pos0.get() as AfArray,
            interp_dim0,
            start0,
            step0,
            pos1.get() as AfArray,
            interp_dim1,
            start1,
            step1,
            method as c_int,
            off_grid as c_float,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// Same as [approx2_uniform](./fn.approx2_uniform.html) but uses existing Array as output
#[allow(clippy::too_many_arguments)]
pub fn approx2_uniform_v2<T, P>(
    output: &mut Array<T>,
    input: &Array<T>,
    pos0: &Array<P>,
    interp_dim0: i32,
    start0: f64,
    step0: f64,
    pos1: &Array<P>,
    interp_dim1: i32,
    start1: f64,
    step1: f64,
    method: InterpType,
    off_grid: f32,
) where
    T: HasAfEnum + FloatingPoint,
    P: HasAfEnum + RealFloating,
{
    unsafe {
        let err_val = af_approx2_uniform_v2(
            output.get() as MutAfArray,
            input.get() as AfArray,
            pos0.get() as AfArray,
            interp_dim0,
            start0,
            step0,
            pos1.get() as AfArray,
            interp_dim1,
            start1,
            step1,
            method as c_int,
            off_grid as c_float,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
}

/// Set fft plan cache size
///
/// Though this is a low overhead function, it is advised not to change
/// the fft plan cache size a mid program execution unless that is what
/// you intend to do.
pub fn set_fft_plan_cache_size(cache_size: usize) {
    unsafe {
        let err_val = af_set_fft_plan_cache_size(cache_size as size_t);
        HANDLE_ERROR(AfError::from(err_val));
    }
}

/// Fast fourier transform for 1d signals
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor with which the input is scaled before the
/// transformation is applied
/// - `odim0` is the length of output signals - used for either truncating or padding the input
/// signals
///
/// # Return Values
///
/// Transformed Array
#[allow(unused_mut)]
pub fn fft<T>(input: &Array<T>, norm_factor: f64, odim0: i64) -> Array<T::ComplexOutType>
where
    T: HasAfEnum + FloatingPoint,
    <T as HasAfEnum>::ComplexOutType: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_fft(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            norm_factor as c_double,
            odim0 as c_longlong,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// Fast fourier transform for 2d signals
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor with which the input is scaled before the
/// transformation is applied
/// - `odim0` is the length of output signal first dimension - used for either truncating or padding the input
/// - `odim1` is the length of output signal second dimension - used for either truncating or padding the input
///
/// # Return Values
///
/// Transformed Array
#[allow(unused_mut)]
pub fn fft2<T>(
    input: &Array<T>,
    norm_factor: f64,
    odim0: i64,
    odim1: i64,
) -> Array<T::ComplexOutType>
where
    T: HasAfEnum + FloatingPoint,
    <T as HasAfEnum>::ComplexOutType: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_fft2(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            norm_factor as c_double,
            odim0 as c_longlong,
            odim1 as c_longlong,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// Fast fourier transform for 3d signals
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor with which the input is scaled before the
/// transformation is applied
/// - `odim0` is the length of output signal first dimension - used for either truncating or padding the input
/// - `odim1` is the length of output signal second dimension - used for either truncating or padding the input
/// - `odim2` is the length of output signal third dimension - used for either truncating or padding the input
///
/// # Return Values
///
/// Transformed Array
#[allow(unused_mut)]
pub fn fft3<T>(
    input: &Array<T>,
    norm_factor: f64,
    odim0: i64,
    odim1: i64,
    odim2: i64,
) -> Array<T::ComplexOutType>
where
    T: HasAfEnum + FloatingPoint,
    <T as HasAfEnum>::ComplexOutType: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_fft3(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            norm_factor as c_double,
            odim0 as c_longlong,
            odim1 as c_longlong,
            odim2 as c_longlong,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// Inverse fast fourier transform for 1d signals
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor with which the input is scaled before the
/// transformation is applied
/// - `odim0` is the length of output signals - used for either truncating or padding the input
/// signals
///
/// # Return Values
///
/// Transformed Array
#[allow(unused_mut)]
pub fn ifft<T>(input: &Array<T>, norm_factor: f64, odim0: i64) -> Array<T::ComplexOutType>
where
    T: HasAfEnum + FloatingPoint,
    <T as HasAfEnum>::ComplexOutType: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_ifft(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            norm_factor as c_double,
            odim0 as c_longlong,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// Inverse fast fourier transform for 2d signals
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor with which the input is scaled before the
/// transformation is applied
/// - `odim0` is the length of output signal first dimension - used for either truncating or padding the input
/// - `odim1` is the length of output signal second dimension - used for either truncating or padding the input
///
/// # Return Values
///
/// Transformed Array
#[allow(unused_mut)]
pub fn ifft2<T>(
    input: &Array<T>,
    norm_factor: f64,
    odim0: i64,
    odim1: i64,
) -> Array<T::ComplexOutType>
where
    T: HasAfEnum + FloatingPoint,
    <T as HasAfEnum>::ComplexOutType: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_ifft2(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            norm_factor as c_double,
            odim0 as c_longlong,
            odim1 as c_longlong,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// Inverse fast fourier transform for 3d signals
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor with which the input is scaled before the
/// transformation is applied
/// - `odim0` is the length of output signal first dimension - used for either truncating or padding the input
/// - `odim1` is the length of output signal second dimension - used for either truncating or padding the input
/// - `odim2` is the length of output signal third dimension - used for either truncating or padding the input
///
/// # Return Values
///
/// Transformed Array
#[allow(unused_mut)]
pub fn ifft3<T>(
    input: &Array<T>,
    norm_factor: f64,
    odim0: i64,
    odim1: i64,
    odim2: i64,
) -> Array<T::ComplexOutType>
where
    T: HasAfEnum + FloatingPoint,
    <T as HasAfEnum>::ComplexOutType: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_ifft3(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            norm_factor as c_double,
            odim0 as c_longlong,
            odim1 as c_longlong,
            odim2 as c_longlong,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

macro_rules! conv_func_def {
    ($doc_str: expr, $fn_name:ident, $ffi_name: ident) => {
        #[doc=$doc_str]
        ///
        ///# Parameters
        ///
        /// - `signal` is the input signal
        /// - `filter` is the signal that shall be flipped for convolution operation
        /// - `mode` indicates if the convolution should be expanded or not(where output size
        /// equals input). It takes a value of type [ConvMode](./enum.ConvMode.html)
        /// - `domain` indicates if the convolution should be performed in frequencey or spatial
        /// domain. It takes a value of type [ConvDomain](./enum.ConvDomain.html)
        ///
        ///# Return Values
        ///
        /// Convolved Array
        #[allow(unused_mut)]
        pub fn $fn_name<T, F>(
            signal: &Array<T>,
            filter: &Array<F>,
            mode: ConvMode,
            domain: ConvDomain,
        ) -> Array<T>
        where
            T: HasAfEnum,
            F: HasAfEnum,
        {
            let mut temp: i64 = 0;
            unsafe {
                let err_val = $ffi_name(
                    &mut temp as MutAfArray,
                    signal.get() as AfArray,
                    filter.get() as AfArray,
                    mode as c_uint,
                    domain as c_uint,
                );
                HANDLE_ERROR(AfError::from(err_val));
            }
            temp.into()
        }
    };
}

conv_func_def!("1d convolution", convolve1, af_convolve1);
conv_func_def!("2d convolution", convolve2, af_convolve2);
conv_func_def!("3d convolution", convolve3, af_convolve3);

/// Separable convolution for 2d signals
///
/// # Parameters
///
/// - `cfilt` is the filter to be applied along coloumns
/// - `rfilt` is the filter to be applied along rows
/// - `signal` is the input signal
/// - `mode` indicates if the convolution should be expanded or not(where output size equals input)
///
/// # Return Values
///
/// The convolved Array
#[allow(unused_mut)]
pub fn convolve2_sep<T, F>(
    cfilt: &Array<F>,
    rfilt: &Array<F>,
    signal: &Array<T>,
    mode: ConvMode,
) -> Array<T>
where
    T: HasAfEnum,
    F: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_convolve2_sep(
            &mut temp as MutAfArray,
            cfilt.get() as AfArray,
            rfilt.get() as AfArray,
            signal.get() as AfArray,
            mode as c_uint,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

macro_rules! fft_conv_func_def {
    ($doc_str: expr, $fn_name:ident, $ffi_name: ident) => {
        #[doc=$doc_str]
        ///
        ///# Parameters
        ///
        /// - `signal` is the input signal
        /// - `filter` is the signal that shall be used for convolution operation
        /// - `mode` indicates if the convolution should be expanded or not(where output size
        /// equals input). It takes values of type [ConvMode](./enum.ConvMode.html)
        ///
        ///# Return Values
        ///
        /// Convolved Array
        #[allow(unused_mut)]
        pub fn $fn_name<T, F>(signal: &Array<T>, filter: &Array<F>, mode: ConvMode) -> Array<T>
        where
            T: HasAfEnum,
            F: HasAfEnum,
        {
            let mut temp: i64 = 0;
            unsafe {
                let err_val = $ffi_name(
                    &mut temp as MutAfArray,
                    signal.get() as AfArray,
                    filter.get() as AfArray,
                    mode as c_uint,
                );
                HANDLE_ERROR(AfError::from(err_val));
            }
            temp.into()
        }
    };
}

fft_conv_func_def!(
    "1d convolution using fast-fourier transform",
    fft_convolve1,
    af_fft_convolve1
);
fft_conv_func_def!(
    "2d convolution using fast-fourier transform",
    fft_convolve2,
    af_fft_convolve2
);
fft_conv_func_def!(
    "3d convolution using fast-fourier transform",
    fft_convolve3,
    af_fft_convolve3
);

/// Finite impulse filter
///
/// # Parameters
///
/// - `b` is the Array containing the coefficients of the filter
/// - `x` is the input signal to filter
///
/// # Return Values
///
/// Filtered Array
#[allow(unused_mut)]
pub fn fir<B, X>(b: &Array<B>, x: &Array<X>) -> Array<X>
where
    B: HasAfEnum,
    X: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_fir(
            &mut temp as MutAfArray,
            b.get() as AfArray,
            x.get() as AfArray,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// Infinite impulse response filter
///
/// # Parameters
///
/// - `b` is the Array containing the feedforward coefficients
/// - `a` is the Array containing the feedback coefficients
/// - `x` is the input signal to filter
///
/// # Return Values
///
/// Filtered Array
#[allow(unused_mut)]
pub fn iir<T: HasAfEnum>(b: &Array<T>, a: &Array<T>, x: &Array<T>) -> Array<T> {
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_iir(
            &mut temp as MutAfArray,
            b.get() as AfArray,
            a.get() as AfArray,
            x.get() as AfArray,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// In place 1d dimensional Fast fourier transform
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor
pub fn fft_inplace<T>(input: &mut Array<T>, norm_factor: f64)
where
    T: HasAfEnum + ComplexFloating,
{
    unsafe {
        let err_val = af_fft_inplace(input.get() as AfArray, norm_factor as c_double);
        HANDLE_ERROR(AfError::from(err_val));
    }
}

/// In place 2d dimensional Fast fourier transform
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor
pub fn fft2_inplace<T>(input: &mut Array<T>, norm_factor: f64)
where
    T: HasAfEnum + ComplexFloating,
{
    unsafe {
        let err_val = af_fft2_inplace(input.get() as AfArray, norm_factor as c_double);
        HANDLE_ERROR(AfError::from(err_val));
    }
}

/// In place 3d dimensional Fast fourier transform
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor
pub fn fft3_inplace<T>(input: &mut Array<T>, norm_factor: f64)
where
    T: HasAfEnum + ComplexFloating,
{
    unsafe {
        let err_val = af_fft3_inplace(input.get() as AfArray, norm_factor as c_double);
        HANDLE_ERROR(AfError::from(err_val));
    }
}

/// In place 1d dimensional inverse fast fourier transform
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor
pub fn ifft_inplace<T>(input: &mut Array<T>, norm_factor: f64)
where
    T: HasAfEnum + ComplexFloating,
{
    unsafe {
        let err_val = af_ifft_inplace(input.get() as AfArray, norm_factor as c_double);
        HANDLE_ERROR(AfError::from(err_val));
    }
}

/// In place 2d dimensional inverse fast fourier transform
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor
pub fn ifft2_inplace<T>(input: &mut Array<T>, norm_factor: f64)
where
    T: HasAfEnum + ComplexFloating,
{
    unsafe {
        let err_val = af_ifft2_inplace(input.get() as AfArray, norm_factor as c_double);
        HANDLE_ERROR(AfError::from(err_val));
    }
}

/// In place 3d dimensional inverse fast fourier transform
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor
pub fn ifft3_inplace<T>(input: &mut Array<T>, norm_factor: f64)
where
    T: HasAfEnum + ComplexFloating,
{
    unsafe {
        let err_val = af_ifft3_inplace(input.get() as AfArray, norm_factor as c_double);
        HANDLE_ERROR(AfError::from(err_val));
    }
}

/// 1d Real to Complex fast fourier transform
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor to be applied before fft is applied
/// - `pad0` is the padding along 0th dimension of Array
///
/// # Return Values
///
/// Complex Array
pub fn fft_r2c<T>(input: &Array<T>, norm_factor: f64, pad0: i64) -> Array<Complex<T>>
where
    T: HasAfEnum + RealFloating,
    Complex<T>: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_fft_r2c(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            norm_factor as c_double,
            pad0 as c_longlong,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// 2d Real to Complex fast fourier transform
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor to be applied before fft is applied
/// - `pad0` is the padding along 0th dimension of Array
/// - `pad1` is the padding along 1st dimension of Array
///
/// # Return Values
///
/// Complex Array
pub fn fft2_r2c<T>(input: &Array<T>, norm_factor: f64, pad0: i64, pad1: i64) -> Array<Complex<T>>
where
    T: HasAfEnum + RealFloating,
    Complex<T>: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_fft2_r2c(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            norm_factor as c_double,
            pad0 as c_longlong,
            pad1 as c_longlong,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// 3d Real to Complex fast fourier transform
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor to be applied before fft is applied
/// - `pad0` is the padding along 0th dimension of Array
/// - `pad1` is the padding along 1st dimension of Array
/// - `pad2` is the padding along 2nd dimension of Array
///
/// # Return Values
///
/// Complex Array
pub fn fft3_r2c<T>(
    input: &Array<T>,
    norm_factor: f64,
    pad0: i64,
    pad1: i64,
    pad2: i64,
) -> Array<Complex<T>>
where
    T: HasAfEnum + RealFloating,
    Complex<T>: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_fft3_r2c(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            norm_factor as c_double,
            pad0 as c_longlong,
            pad1 as c_longlong,
            pad2 as c_longlong,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// 1d Complex to Real fast fourier transform
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor to be applied before fft is applied
/// - `is_odd` signifies if the output should be even or odd size
///
/// # Return Values
///
/// Complex Array
pub fn fft_c2r<T>(input: &Array<T>, norm_factor: f64, is_odd: bool) -> Array<T::BaseType>
where
    T: HasAfEnum + ComplexFloating,
    <T as HasAfEnum>::BaseType: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_fft_c2r(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            norm_factor as c_double,
            is_odd as c_int,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// 2d Complex to Real fast fourier transform
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor to be applied before fft is applied
/// - `is_odd` signifies if the output should be even or odd size
///
/// # Return Values
///
/// Complex Array
pub fn fft2_c2r<T>(input: &Array<T>, norm_factor: f64, is_odd: bool) -> Array<T::BaseType>
where
    T: HasAfEnum + ComplexFloating,
    <T as HasAfEnum>::BaseType: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_fft2_c2r(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            norm_factor as c_double,
            is_odd as c_int,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}

/// 3d Complex to Real fast fourier transform
///
/// # Parameters
///
/// - `input` is the input Array
/// - `norm_factor` is the normalization factor to be applied before fft is applied
/// - `is_odd` signifies if the output should be even or odd size
///
/// # Return Values
///
/// Complex Array
pub fn fft3_c2r<T>(input: &Array<T>, norm_factor: f64, is_odd: bool) -> Array<T::BaseType>
where
    T: HasAfEnum + ComplexFloating,
    <T as HasAfEnum>::BaseType: HasAfEnum,
{
    let mut temp: i64 = 0;
    unsafe {
        let err_val = af_fft3_c2r(
            &mut temp as MutAfArray,
            input.get() as AfArray,
            norm_factor as c_double,
            is_odd as c_int,
        );
        HANDLE_ERROR(AfError::from(err_val));
    }
    temp.into()
}