Skip to content

Commit 8e7c9f4

Browse files
committed
Added in-place ffts & fft real to complex & vice versa functions
1 parent 2167299 commit 8e7c9f4

File tree

2 files changed

+259
-0
lines changed

2 files changed

+259
-0
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ mod lapack;
7171

7272
pub use signal::{approx1, approx2};
7373
pub use signal::{fft, fft2, fft3, ifft, ifft2, ifft3};
74+
pub use signal::{fft_r2c, fft2_r2c, fft3_r2c, fft_c2r, fft2_c2r, fft3_c2r};
75+
pub use signal::{fft_inplace, fft2_inplace, fft3_inplace};
76+
pub use signal::{ifft_inplace, ifft2_inplace, ifft3_inplace};
7477
pub use signal::{convolve1, convolve2, convolve3, convolve2_sep};
7578
pub use signal::{fft_convolve1, fft_convolve2, fft_convolve3};
7679
pub use signal::{fir, iir};

src/signal/mod.rs

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ extern {
3636
fn af_ifft3(out: MutAfArray, arr: AfArray, nfac: c_double,
3737
odim0: c_longlong, odim1: c_longlong, odim2: c_longlong) -> c_int;
3838

39+
fn af_fft_inplace(arr: AfArray, nfac: c_double) -> c_int;
40+
fn af_fft2_inplace(arr: AfArray, nfac: c_double) -> c_int;
41+
fn af_fft3_inplace(arr: AfArray, nfac: c_double) -> c_int;
42+
fn af_ifft_inplace(arr: AfArray, nfac: c_double) -> c_int;
43+
fn af_ifft2_inplace(arr: AfArray, nfac: c_double) -> c_int;
44+
fn af_ifft3_inplace(arr: AfArray, nfac: c_double) -> c_int;
45+
46+
fn af_fft_r2c(out: MutAfArray, arr: AfArray,
47+
nfac: c_double, pad0: c_longlong) -> c_int;
48+
fn af_fft2_r2c(out: MutAfArray, arr: AfArray,
49+
nfac: c_double, pad0: c_longlong, pad1: c_longlong) -> c_int;
50+
fn af_fft3_r2c(out: MutAfArray, arr: AfArray,
51+
nfac: c_double, pad0: c_longlong, pad1: c_longlong, pad2: c_longlong) -> c_int;
52+
53+
fn af_fft_c2r(out: MutAfArray, input: AfArray, nfac: c_double, is_odd: c_int) -> c_int;
54+
fn af_fft2_c2r(out: MutAfArray, input: AfArray, nfac: c_double, is_odd: c_int) -> c_int;
55+
fn af_fft3_c2r(out: MutAfArray, input: AfArray, nfac: c_double, is_odd: c_int) -> c_int;
56+
3957
fn af_convolve1(out: MutAfArray, s: AfArray, f: AfArray, m: uint8_t, d: uint8_t) -> c_int;
4058
fn af_convolve2(out: MutAfArray, s: AfArray, f: AfArray, m: uint8_t, d: uint8_t) -> c_int;
4159
fn af_convolve3(out: MutAfArray, s: AfArray, f: AfArray, m: uint8_t, d: uint8_t) -> c_int;
@@ -426,3 +444,241 @@ pub fn iir(b: &Array, a: &Array, x: &Array) -> Result<Array, AfError> {
426444
}
427445
}
428446
}
447+
448+
/// In place 1d dimensional Fast fourier transform
449+
///
450+
/// # Parameters
451+
///
452+
/// - `input` is the input Array
453+
/// - `norm_factor` is the normalization factor
454+
pub fn fft_inplace(input: &Array, norm_factor: f64) -> Result<(), AfError> {
455+
unsafe {
456+
let err_val = af_fft_inplace(input.get() as AfArray, norm_factor as c_double);
457+
match err_val {
458+
0 => Ok(()),
459+
_ => Err(AfError::from(err_val)),
460+
}
461+
}
462+
}
463+
464+
/// In place 2d dimensional Fast fourier transform
465+
///
466+
/// # Parameters
467+
///
468+
/// - `input` is the input Array
469+
/// - `norm_factor` is the normalization factor
470+
pub fn fft2_inplace(input: &Array, norm_factor: f64) -> Result<(), AfError> {
471+
unsafe {
472+
let err_val = af_fft2_inplace(input.get() as AfArray, norm_factor as c_double);
473+
match err_val {
474+
0 => Ok(()),
475+
_ => Err(AfError::from(err_val)),
476+
}
477+
}
478+
}
479+
480+
/// In place 3d dimensional Fast fourier transform
481+
///
482+
/// # Parameters
483+
///
484+
/// - `input` is the input Array
485+
/// - `norm_factor` is the normalization factor
486+
pub fn fft3_inplace(input: &Array, norm_factor: f64) -> Result<(), AfError> {
487+
unsafe {
488+
let err_val = af_fft3_inplace(input.get() as AfArray, norm_factor as c_double);
489+
match err_val {
490+
0 => Ok(()),
491+
_ => Err(AfError::from(err_val)),
492+
}
493+
}
494+
}
495+
496+
/// In place 1d dimensional inverse fast fourier transform
497+
///
498+
/// # Parameters
499+
///
500+
/// - `input` is the input Array
501+
/// - `norm_factor` is the normalization factor
502+
pub fn ifft_inplace(input: &Array, norm_factor: f64) -> Result<(), AfError> {
503+
unsafe {
504+
let err_val = af_ifft_inplace(input.get() as AfArray, norm_factor as c_double);
505+
match err_val {
506+
0 => Ok(()),
507+
_ => Err(AfError::from(err_val)),
508+
}
509+
}
510+
}
511+
512+
/// In place 2d dimensional inverse fast fourier transform
513+
///
514+
/// # Parameters
515+
///
516+
/// - `input` is the input Array
517+
/// - `norm_factor` is the normalization factor
518+
pub fn ifft2_inplace(input: &Array, norm_factor: f64) -> Result<(), AfError> {
519+
unsafe {
520+
let err_val = af_ifft2_inplace(input.get() as AfArray, norm_factor as c_double);
521+
match err_val {
522+
0 => Ok(()),
523+
_ => Err(AfError::from(err_val)),
524+
}
525+
}
526+
}
527+
528+
/// In place 3d dimensional inverse fast fourier transform
529+
///
530+
/// # Parameters
531+
///
532+
/// - `input` is the input Array
533+
/// - `norm_factor` is the normalization factor
534+
pub fn ifft3_inplace(input: &Array, norm_factor: f64) -> Result<(), AfError> {
535+
unsafe {
536+
let err_val = af_ifft3_inplace(input.get() as AfArray, norm_factor as c_double);
537+
match err_val {
538+
0 => Ok(()),
539+
_ => Err(AfError::from(err_val)),
540+
}
541+
}
542+
}
543+
544+
/// 1d Real to Complex fast fourier transform
545+
///
546+
/// # Parameters
547+
///
548+
/// - `input` is the input Array
549+
/// - `norm_factor` is the normalization factor to be applied before fft is applied
550+
/// - `pad0` is the padding along 0th dimension of Array
551+
///
552+
/// # Return Values
553+
///
554+
/// Complex Array
555+
pub fn fft_r2c(input: &Array, norm_factor: f64, pad0: i64) -> Result<Array, AfError> {
556+
unsafe {
557+
let mut temp: i64 = 0;
558+
let err_val = af_fft_r2c(&mut temp as MutAfArray, input.get() as AfArray,
559+
norm_factor as c_double, pad0 as c_longlong);
560+
match err_val {
561+
0 => Ok(Array::from(temp)),
562+
_ => Err(AfError::from(err_val)),
563+
}
564+
}
565+
}
566+
567+
/// 2d Real to Complex fast fourier transform
568+
///
569+
/// # Parameters
570+
///
571+
/// - `input` is the input Array
572+
/// - `norm_factor` is the normalization factor to be applied before fft is applied
573+
/// - `pad0` is the padding along 0th dimension of Array
574+
/// - `pad1` is the padding along 1st dimension of Array
575+
///
576+
/// # Return Values
577+
///
578+
/// Complex Array
579+
pub fn fft2_r2c(input: &Array, norm_factor: f64, pad0: i64, pad1: i64) -> Result<Array, AfError> {
580+
unsafe {
581+
let mut temp: i64 = 0;
582+
let err_val = af_fft2_r2c(&mut temp as MutAfArray, input.get() as AfArray,
583+
norm_factor as c_double, pad0 as c_longlong, pad1 as c_longlong);
584+
match err_val {
585+
0 => Ok(Array::from(temp)),
586+
_ => Err(AfError::from(err_val)),
587+
}
588+
}
589+
}
590+
591+
/// 3d Real to Complex fast fourier transform
592+
///
593+
/// # Parameters
594+
///
595+
/// - `input` is the input Array
596+
/// - `norm_factor` is the normalization factor to be applied before fft is applied
597+
/// - `pad0` is the padding along 0th dimension of Array
598+
/// - `pad1` is the padding along 1st dimension of Array
599+
/// - `pad2` is the padding along 2nd dimension of Array
600+
///
601+
/// # Return Values
602+
///
603+
/// Complex Array
604+
pub fn fft3_r2c(input: &Array, norm_factor: f64, pad0: i64, pad1: i64, pad2: i64) -> Result<Array, AfError> {
605+
unsafe {
606+
let mut temp: i64 = 0;
607+
let err_val = af_fft3_r2c(&mut temp as MutAfArray, input.get() as AfArray,
608+
norm_factor as c_double, pad0 as c_longlong,
609+
pad1 as c_longlong, pad2 as c_longlong);
610+
match err_val {
611+
0 => Ok(Array::from(temp)),
612+
_ => Err(AfError::from(err_val)),
613+
}
614+
}
615+
}
616+
617+
/// 1d Complex to Real fast fourier transform
618+
///
619+
/// # Parameters
620+
///
621+
/// - `input` is the input Array
622+
/// - `norm_factor` is the normalization factor to be applied before fft is applied
623+
/// - `is_odd` signifies if the output should be even or odd size
624+
///
625+
/// # Return Values
626+
///
627+
/// Complex Array
628+
pub fn fft_c2r(input: &Array, norm_factor: f64, is_odd: bool) -> Result<Array, AfError> {
629+
unsafe {
630+
let mut temp: i64 = 0;
631+
let err_val = af_fft_c2r(&mut temp as MutAfArray, input.get() as AfArray,
632+
norm_factor as c_double, is_odd as c_int);
633+
match err_val {
634+
0 => Ok(Array::from(temp)),
635+
_ => Err(AfError::from(err_val)),
636+
}
637+
}
638+
}
639+
640+
/// 2d Complex to Real fast fourier transform
641+
///
642+
/// # Parameters
643+
///
644+
/// - `input` is the input Array
645+
/// - `norm_factor` is the normalization factor to be applied before fft is applied
646+
/// - `is_odd` signifies if the output should be even or odd size
647+
///
648+
/// # Return Values
649+
///
650+
/// Complex Array
651+
pub fn fft2_c2r(input: &Array, norm_factor: f64, is_odd: bool) -> Result<Array, AfError> {
652+
unsafe {
653+
let mut temp: i64 = 0;
654+
let err_val = af_fft2_c2r(&mut temp as MutAfArray, input.get() as AfArray,
655+
norm_factor as c_double, is_odd as c_int);
656+
match err_val {
657+
0 => Ok(Array::from(temp)),
658+
_ => Err(AfError::from(err_val)),
659+
}
660+
}
661+
}
662+
663+
/// 3d Complex to Real fast fourier transform
664+
///
665+
/// # Parameters
666+
///
667+
/// - `input` is the input Array
668+
/// - `norm_factor` is the normalization factor to be applied before fft is applied
669+
/// - `is_odd` signifies if the output should be even or odd size
670+
///
671+
/// # Return Values
672+
///
673+
/// Complex Array
674+
pub fn fft3_c2r(input: &Array, norm_factor: f64, is_odd: bool) -> Result<Array, AfError> {
675+
unsafe {
676+
let mut temp: i64 = 0;
677+
let err_val = af_fft3_c2r(&mut temp as MutAfArray, input.get() as AfArray,
678+
norm_factor as c_double, is_odd as c_int);
679+
match err_val {
680+
0 => Ok(Array::from(temp)),
681+
_ => Err(AfError::from(err_val)),
682+
}
683+
}
684+
}

0 commit comments

Comments
 (0)