Skip to content

Commit

Permalink
Merge pull request #21 from chalharu/fix4rand
Browse files Browse the repository at this point in the history
 Correction to specification change of std::rand::SeedableRng::from_seed
  • Loading branch information
chalharu committed May 26, 2018
2 parents 1a1ede3 + 4cb08a0 commit ae8ca88
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 128 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "chfft"
version = "0.2.3"
version = "0.2.4"
authors = ["Mitsuharu Seki <mitsu1986@gmail.com>"]
repository = "https://github.com/chalharu/chfft"
keywords = ["fft", "numerics", "dsp", "mathematics"]
Expand All @@ -19,7 +19,7 @@ num-traits = ">=0.0.0"
num-complex = ">=0.0.0"

[dev-dependencies]
rand = ">=0.0.0"
rand = ">=0.5.0"

[dev-dependencies.appro-eq]
version=">=0.0.0"
Expand Down
42 changes: 25 additions & 17 deletions src/cfft1d.rs
Expand Up @@ -5,15 +5,15 @@
//! version 2.0 (the "License"). You can obtain a copy of the License at
//! http://mozilla.org/MPL/2.0/ .

use chirpz;
use mixed_radix;
use num_complex::Complex;
use num_traits::{cast, NumAssign};
use num_traits::float::{Float, FloatConst};
use num_traits::identities::{one, zero};
use num_traits::{cast, NumAssign};
use precompute_utils;
use prime_factorization;
use prime_factorization::Factor;
use precompute_utils;
use chirpz;
use mixed_radix;

enum WorkData<T> {
MixedRadix {
Expand Down Expand Up @@ -510,23 +510,23 @@ impl<T: Float + FloatConst + NumAssign> CFft1D<T> {
#[cfg(test)]
mod tests {
use super::*;
use assert_appro_eq;
use FloatEps;
use appro_eq::AbsError;
use rand::{Rand, Rng, SeedableRng, XorShiftRng};
use assert_appro_eq;
use rand::distributions::{Distribution, Standard};
use rand::{Rng, SeedableRng, XorShiftRng};
use std::fmt::Debug;

fn convert<T: Float + FloatConst>(source: &[Complex<T>], scalar: T) -> Vec<Complex<T>> {
(0..source.len())
.map(|i| {
(1..source.len()).fold(source[0], |x, j| {
x
+ source[j]
* Complex::<T>::from_polar(
&one(),
&(-cast::<_, T>(2 * i * j).unwrap() * T::PI()
/ cast(source.len()).unwrap()),
)
x + source[j]
* Complex::<T>::from_polar(
&one(),
&(-cast::<_, T>(2 * i * j).unwrap() * T::PI()
/ cast(source.len()).unwrap()),
)
}) * scalar
})
.collect::<Vec<_>>()
Expand Down Expand Up @@ -572,11 +572,16 @@ mod tests {
assert_appro_eq(source, &actual);
}

fn test_with_len<T: Float + Rand + FloatConst + NumAssign + AbsError + Debug + FloatEps>(
fn test_with_len<T: Float + FloatConst + NumAssign + AbsError + Debug + FloatEps>(
fft: &mut CFft1D<T>,
len: usize,
) {
let mut rng = XorShiftRng::from_seed([189522394, 1694417663, 1363148323, 4087496301]);
) where
Standard: Distribution<T>,
{
let mut rng = XorShiftRng::from_seed([
0xDA, 0xE1, 0x4B, 0x0B, 0xFF, 0xC2, 0xFE, 0x64, 0x23, 0xFE, 0x3F, 0x51, 0x6D, 0x3E,
0xA2, 0xF3,
]);

// 10パターンのテスト
for _ in 0..10 {
Expand Down Expand Up @@ -619,7 +624,10 @@ mod tests {
#[test]
fn f64_primes() {
let mut dft = CFft1D::<f64>::new();
for &i in &[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] {
for &i in &[
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
89, 97,
] {
test_with_len(&mut dft, i);
}
}
Expand Down
44 changes: 26 additions & 18 deletions src/cfft2d.rs
Expand Up @@ -5,11 +5,11 @@
//! version 2.0 (the "License"). You can obtain a copy of the License at
//! http://mozilla.org/MPL/2.0/ .

use CFft1D;
use num_complex::Complex;
use num_traits::{cast, NumAssign};
use num_traits::float::{Float, FloatConst};
use num_traits::identities::{one, zero};
use CFft1D;
use num_traits::{cast, NumAssign};

/// Perform a complex-to-complex two-dimensional Fourier transform
///
Expand Down Expand Up @@ -369,29 +369,32 @@ impl<T: Float + FloatConst + NumAssign> CFft2D<T> {
#[cfg(test)]
mod tests {
use super::*;
use assert_appro_eq;
use FloatEps;
use appro_eq::AbsError;
use rand::{Rand, Rng, SeedableRng, XorShiftRng};
use assert_appro_eq;
use rand::distributions::{Distribution, Standard};
use rand::{Rng, SeedableRng, XorShiftRng};
use std::fmt::Debug;

fn convert<T: Float + FloatConst>(source: &[Vec<Complex<T>>], scalar: T) -> Vec<Vec<Complex<T>>> {
fn convert<T: Float + FloatConst>(
source: &[Vec<Complex<T>>],
scalar: T,
) -> Vec<Vec<Complex<T>>> {
(0..source.len())
.map(|i| {
(0..source[0].len())
.map(|k| {
(0..source.len()).fold(zero(), |x: Complex<T>, j| {
x + (0..source[0].len()).fold(zero(), |y: Complex<T>, l| {
y
+ source[j][l]
* Complex::<T>::from_polar(
&one(),
&(-cast::<_, T>(2).unwrap() * T::PI()
* ((cast::<_, T>(i * j).unwrap()
/ cast(source.len()).unwrap())
+ cast::<_, T>(k * l).unwrap()
/ cast(source[0].len()).unwrap())),
)
y + source[j][l]
* Complex::<T>::from_polar(
&one(),
&(-cast::<_, T>(2).unwrap() * T::PI()
* ((cast::<_, T>(i * j).unwrap()
/ cast(source.len()).unwrap())
+ cast::<_, T>(k * l).unwrap()
/ cast(source[0].len()).unwrap())),
)
})
}) * scalar
})
Expand All @@ -411,12 +414,17 @@ mod tests {
assert_appro_eq(source, &actual_source);
}

fn test_with_len<T: Float + Rand + FloatConst + NumAssign + Debug + AbsError + FloatEps>(
fn test_with_len<T: Float + FloatConst + NumAssign + Debug + AbsError + FloatEps>(
fft: &mut CFft2D<T>,
len_m: usize,
len_n: usize,
) {
let mut rng = XorShiftRng::from_seed([189522394, 1694417663, 1363148323, 4087496301]);
) where
Standard: Distribution<T>,
{
let mut rng = XorShiftRng::from_seed([
0xDA, 0xE1, 0x4B, 0x0B, 0xFF, 0xC2, 0xFE, 0x64, 0x23, 0xFE, 0x3F, 0x51, 0x6D, 0x3E,
0xA2, 0xF3,
]);

// 10パターンのテスト
for _ in 0..10 {
Expand Down
26 changes: 15 additions & 11 deletions src/chirpz.rs
Expand Up @@ -5,11 +5,11 @@
//! version 2.0 (the "License"). You can obtain a copy of the License at
//! http://mozilla.org/MPL/2.0/ .

use mixed_radix;
use num_complex::Complex;
use num_traits::NumAssign;
use num_traits::float::Float;
use num_traits::identities::{one, zero};
use mixed_radix;

pub fn convert_rad2_inplace<T: Float + NumAssign>(
source: &mut [Complex<T>],
Expand Down Expand Up @@ -88,17 +88,21 @@ pub fn convert_chirpz<T: Float + NumAssign>(

// Multiply phase factor
(0..srclen)
.map(move |i| if i == 0 {
0
} else if is_back {
srclen - i
} else {
i
.map(move |i| {
if i == 0 {
0
} else if is_back {
srclen - i
} else {
i
}
})
.map(move |i| if scaler != one() {
a[i] * rot_conj[i].scale(scaler)
} else {
a[i] * rot_conj[i]
.map(move |i| {
if scaler != one() {
a[i] * rot_conj[i].scale(scaler)
} else {
a[i] * rot_conj[i]
}
})
.collect::<Vec<_>>()
}
Expand Down
37 changes: 21 additions & 16 deletions src/dct1d.rs
Expand Up @@ -5,12 +5,12 @@
//! version 2.0 (the "License"). You can obtain a copy of the License at
//! http://mozilla.org/MPL/2.0/ .

use CFft1D;
use num_complex::Complex;
use num_traits::{cast, NumAssign};
use num_traits::float::{Float, FloatConst};
use num_traits::identities::{one, zero};
use num_traits::{cast, NumAssign};
use precompute_utils;
use CFft1D;

/// Perform a discrete cosine transform
///
Expand Down Expand Up @@ -199,7 +199,8 @@ impl<T: Float + FloatConst + NumAssign> DctWorker1D<T> for Dct2Worker1D<T> {
.iter()
.take(len >> 1)
.map(|w| {
(Complex::<T>::new(one(), zero()) + Complex::<T>::i() * w).scale(cast(0.5).unwrap())
(Complex::<T>::new(one(), zero()) + Complex::<T>::i() * w)
.scale(cast(0.5).unwrap())
})
.collect();
self.work = vec![zero(); len >> 1];
Expand Down Expand Up @@ -320,7 +321,8 @@ impl<T: Float + FloatConst + NumAssign> DctWorker1D<T> for Dct3Worker1D<T> {
.rev()
.take(len >> 1)
.map(|w| {
(Complex::<T>::new(one(), zero()) - Complex::<T>::i() * w).scale(cast(0.5).unwrap())
(Complex::<T>::new(one(), zero()) - Complex::<T>::i() * w)
.scale(cast(0.5).unwrap())
})
.collect();
self.work = vec![zero(); len >> 1];
Expand Down Expand Up @@ -399,21 +401,21 @@ impl<T: Float + FloatConst + NumAssign> DctWorker1D<T> for Dct3Worker1D<T> {
#[cfg(test)]
mod tests {
use super::*;
use assert_appro_eq;
use FloatEps;
use appro_eq::AbsError;
use rand::{Rand, Rng, SeedableRng, XorShiftRng};
use assert_appro_eq;
use rand::distributions::{Distribution, Standard};
use rand::{Rng, SeedableRng, XorShiftRng};
use std::fmt::Debug;

fn convert<T: Float + FloatConst>(source: &[T], scalar: T) -> Vec<T> {
(0..source.len())
.map(|i| {
(0..source.len()).fold(zero(), |x: T, j| {
x
+ source[j]
* (T::PI() / cast(source.len() * 2).unwrap()
* cast::<_, T>((j * 2 + 1) * i).unwrap())
.cos()
x + source[j]
* (T::PI() / cast(source.len() * 2).unwrap()
* cast::<_, T>((j * 2 + 1) * i).unwrap())
.cos()
}) * scalar
})
.collect::<Vec<_>>()
Expand Down Expand Up @@ -447,12 +449,17 @@ mod tests {
assert_appro_eq(source, &actual_source);
}

fn test_with_len<T: Float + Rand + FloatConst + NumAssign + Debug + AbsError + FloatEps>(
fn test_with_len<T: Float + FloatConst + NumAssign + Debug + AbsError + FloatEps>(
dct2: &mut Dct1D<T>,
dct3: &mut Dct1D<T>,
len: usize,
) {
let mut rng = XorShiftRng::from_seed([189522394, 1694417663, 1363148323, 4087496301]);
) where
Standard: Distribution<T>,
{
let mut rng = XorShiftRng::from_seed([
0xDA, 0xE1, 0x4B, 0x0B, 0xFF, 0xC2, 0xFE, 0x64, 0x23, 0xFE, 0x3F, 0x51, 0x6D, 0x3E,
0xA2, 0xF3,
]);

// 10パターンのテスト
for _ in 0..10 {
Expand All @@ -472,7 +479,6 @@ mod tests {
}
}


#[test]
fn f32_new() {
for i in 1..100 {
Expand All @@ -495,7 +501,6 @@ mod tests {
}
}


#[test]
fn f32_with_setup() {
for i in 1..100 {
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Expand Up @@ -17,22 +17,22 @@ extern crate rand;
#[macro_use]
extern crate appro_eq;

mod precompute_utils;
mod prime_factorization;
mod chirpz;
mod mixed_radix;
mod precompute_utils;
mod prime_factorization;

mod cfft1d;
mod rfft1d;
mod cfft2d;
mod dct1d;
mod mdct1d;
mod cfft2d;
mod rfft1d;

pub use cfft1d::CFft1D;
pub use rfft1d::RFft1D;
pub use cfft2d::CFft2D;
pub use dct1d::Dct1D;
pub use mdct1d::Mdct1D;
pub use cfft2d::CFft2D;
pub use rfft1d::RFft1D;

#[cfg(test)]
trait FloatEps {
Expand Down

0 comments on commit ae8ca88

Please sign in to comment.