cepstrum_extractor/
conversions.rs

1use rustfft::num_complex::Complex;
2use rustfft::num_traits::Num;
3
4/// Used to convert a slice of `Real` into a slice of `Complex`.
5pub trait RealToComplex<T> {
6    /// Produces a vector of complex numbers, given a slice of real numbers.
7    fn to_complex_vec(&self) -> Vec<Complex<T>>;
8}
9
10/// Used to convert a slice of `Complex` into a slice of `Real`.
11pub trait ComplexToReal<T> {
12    /// Produces a vector of real numbers, given a slice of complex numbers.
13    fn to_real_vec(&self) -> Vec<T>;
14}
15
16/// Produces a vector of complex numbers, given a slice of real numbers.
17#[inline(always)]
18pub fn real_to_complex<T: Copy + Num>(this: &[T]) -> Vec<Complex<T>> {
19    this.iter().map(|r| Complex::from(r)).collect()
20}
21
22#[inline(always)]
23/// Produces a vector of real numbers, given a slice of complex numbers.
24pub fn complex_to_real<T: Copy + Num>(this: &[Complex<T>]) -> Vec<T> {
25    this.iter().map(|c| c.re).collect()
26}
27
28impl<T: Copy + Num> RealToComplex<T> for [T] {
29    #[inline(always)]
30    fn to_complex_vec(&self) -> Vec<Complex<T>> {
31        real_to_complex(self)
32    }
33}
34impl<T: Copy + Num> ComplexToReal<T> for [Complex<T>] {
35    #[inline(always)]
36    fn to_real_vec(&self) -> Vec<T> {
37        complex_to_real(self)
38    }
39}