Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  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 functions: Vec<_> =
  12. approximation_coefficients.into_iter()
  13. .map(move |i| {move |t: f32| t.sin()*i})
  14. .collect();
  15. let predict: Complex<f32> = functions.iter().map(|f| f(1.0)).sum();
  16. dbg!(predict); // Should be 1 + 0i, because approximating linear, right?
  17. }
  18.  
  19. fn dft(x: &[Complex<f32>]) -> Vec<Complex<f32>> {
  20. (0..x.len()).map(|k| dft_k(x, k as u32)).collect()
  21. }
  22.  
  23. fn dft_k(x: &[Complex<f32>], k: u32) -> Complex<f32> {
  24. let n = x.len();
  25. (0..n-1).map(|t| {
  26. let value = x[t];
  27. let power = Complex::new(0.0, 1.0)
  28. * -2.0
  29. * PI
  30. * (k as f32)
  31. / (n as f32);
  32. value * power.exp()
  33. }).sum()
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement