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
//! Rust Fourier Transform.
//!
//! The length of the input must not be a power of two, it picks the proper
//! algorithm automatically.
//!
//! Cooley-Tukey is used for power of two sizes, it has an O(N log N) time complexity
//! and O(N) space complexity.
//!
//! Bluestein is used for non-power of two sizes, it has an O(5N log N) time complexity
//! and O(5N) space complexity.
//!
//! If it isn't already obvious, try to use power of two sizes.

#![allow(non_snake_case)]
#![warn(missing_docs)]

extern crate num;

extern crate strided;
use strided::{Strided, MutStrided};

/// The float precision all operations use.
#[cfg(all(not(feature = "f64"), not(feature = "f32")))]
pub type Precision = f32;
#[cfg(feature = "f32")]
pub type Precision = f32;
#[cfg(feature = "f64")]
pub type Precision = f64;

mod sample;
pub use sample::{Sample, SampleMut};

mod complex;
pub use complex::{Complex, ComplexMut};

/// Various algorithms to compute the fourier transform.
pub mod transform;
use transform::{cooley_tukey, bluestein};

/// Window function application.
pub mod window;
pub use window::Window;

/// Spectrum computations.
pub mod spectrum;

/// Applies a forward fourier transform to the given input and returns a vector
/// of complex numbers.
#[inline(always)]
pub fn forward<CI, CO, I>(input: I) -> Vec<CO>
	where CI: Complex,
	      CO: ComplexMut,
	      I:  Strided<Elem=CI>
{
	let mut output = vec![CO::zero(); input.as_stride().len()];
	forward_in(input, &mut *output);

	output
}

/// Applies a forward fourier transform to the given input and puts it into the
/// given output.
#[inline]
pub fn forward_in<CI, CO, I, O>(input: I, mut output: O)
	where CI: Complex,
	      CO: ComplexMut,
	      I:  Strided<Elem=CI>,
	      O:  MutStrided<Elem=CO>
{
	let input  = input.as_stride();
	let output = output.as_stride_mut();

	if input.len().is_power_of_two() {
		cooley_tukey::forward(input, output);
	}
	else {
		bluestein::forward(input, output);
	}
}

/// Applies an inverse fourier transform to the given input and returns a
/// vector of complex numbers.
#[inline(always)]
pub fn inverse<CI, CO, I>(input: I) -> Vec<CO>
	where CI: Complex,
	      CO: ComplexMut,
	      I:  Strided<Elem=CI>
{
	let mut output = vec![CO::zero(); input.as_stride().len()];
	inverse_in(input, &mut *output);

	output
}

/// Applies an inverse fourier transform to the given input and puts it into
/// the given output.
#[inline]
pub fn inverse_in<CI, CO, I, O>(input: I, mut output: O)
	where CI: Complex,
	      CO: ComplexMut,
	      I:  Strided<Elem=CI>,
	      O:  MutStrided<Elem=CO>
{
	let     input  = input.as_stride();
	let mut output = output.as_stride_mut();

	if input.len().is_power_of_two() {
		cooley_tukey::inverse(input, output.reborrow());
	}
	else {
		bluestein::inverse(input, output.reborrow());
	}

	// the implementations do no scaling internally
	let length = input.len() as Precision;

	for output in output.iter_mut() {
		output.unscale(length);
	}
}