Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate num;
- use num::complex::Complex;
- use std::f32::consts::PI;
- fn main() {
- let linear: Vec<_> = (0..100).map(|i|
- Complex::new(i as f32, 0.0)).collect();
- let approximation_coefficients = dft(&linear);
- let n = linear.len() as f32;
- let functions: Vec<_> =
- approximation_coefficients.into_iter().enumerate()
- .map(move |(i,Xi)| {move |t: f32| Xi*Complex::new(0.0, 2.*PI*(i as f32)*t/n).exp()})
- .collect();
- let predict: Complex<f32> = functions.iter().map(|f| f(1.0)).sum();
- dbg!(predict/n); // Should be 1 + 0i, because approximating linear, right?
- }
- fn dft(x: &[Complex<f32>]) -> Vec<Complex<f32>> {
- (0..x.len()).map(|k| dft_k(x, k as u32)).collect()
- }
- fn dft_k(x: &[Complex<f32>], k: u32) -> Complex<f32> {
- let n = x.len();
- (0..n).map(|t| {
- let value = x[t];
- let power = Complex::new(0.0, 1.0)
- * -2.0
- * PI
- * (t as f32)
- * (k as f32)
- / (n as f32);
- value * power.exp()
- }).sum()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement