SHARE
TWEET

Untitled

a guest Jul 18th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extern crate num;
  2.  
  3. use num::complex::Complex;
  4.  
  5. use std::f32::consts::PI;
  6.  
  7. fn main() {
  8.     let linear: Vec<_> = (0..100).map(|i|
  9.             Complex::new(i as f32, 0.0)).collect();
  10.     let approximation_coefficients = dft(&linear);
  11.     let n = linear.len() as f32;
  12.  
  13.     let functions: Vec<_> =
  14.         approximation_coefficients.into_iter().enumerate()
  15.               .map(move |(i,Xi)| {move |t: f32| Xi*Complex::new(0.0, 2.*PI*(i as f32)*t/n).exp()})
  16.               .collect();
  17.     let predict: Complex<f32> = functions.iter().map(|f| f(1.0)).sum();
  18.     dbg!(predict/n); // Should be 1 + 0i, because approximating linear, right?
  19. }
  20.  
  21. fn dft(x: &[Complex<f32>]) -> Vec<Complex<f32>> {
  22.     (0..x.len()).map(|k| dft_k(x, k as u32)).collect()
  23. }
  24.  
  25. fn dft_k(x: &[Complex<f32>], k: u32) -> Complex<f32> {
  26.     let n = x.len();
  27.     (0..n).map(|t| {
  28.         let value = x[t];
  29.         let power = Complex::new(0.0, 1.0)
  30.                     * -2.0
  31.                     * PI
  32.                     * (t as f32)
  33.                     * (k as f32)
  34.                     / (n as f32);
  35.         value * power.exp()
  36.     }).sum()
  37. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top