From 4f9a3cf1e64f9bc7f4067309a6a178e6a3c5715f Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 6 Feb 2019 21:15:33 -0600 Subject: [PATCH] basic algorithm defined --- examples/convolution.rs | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 examples/convolution.rs diff --git a/examples/convolution.rs b/examples/convolution.rs new file mode 100644 index 00000000..f2ab42cc --- /dev/null +++ b/examples/convolution.rs @@ -0,0 +1,62 @@ +extern crate nalgebra as na; +#[allow(unused_imports)] +use na::{Vector,Dim,Real,Vector4,Vector3,Vector2,U1,Matrix,DVector,Dynamic,VecStorage}; +use na::storage::{Storage}; +use std::cmp; + + + +// evum CovvolveMode{ +// Full, +// Valid, +// Same +// } + + + +#[allow(non_snake_case)] +fn Convolve1D, Q: Storage>( + Vector : Vector, + Kernel : Vector + ) -> Matrix> + { + // + // Vector is the vector, Kervel is the kervel + // C is the returv vector + // + if Kernel.len() > Vector.len(){ + return Convolve1D(Kernel, Vector); + } + + let V = Vector.len(); + let K = Kernel.len(); + let L = V + K - 1; + let v = V as i8; + let k = K as i8; + let l = L as i8; + let mut C = DVector::::zeros(L); + + for i in 0..l{ + let u_i = cmp::max(0, i - k); + let u_f = cmp::min(i, v - 1); + if u_i == u_f{ + C[i as usize] += Vector[u_i as usize] * Kernel[(i - u_i) as usize]; + } + else{ + for u in u_i..(u_f+1){ + if i - u < k{ + C[i as usize] += Vector[u as usize] * Kernel[(i - u ) as usize]; + } + } + } + } + C + } + + +fn main() { + let v1 = Vector2::new(3.0,3.0); + let v2 = Vector4::new(1.0,2.0,5.0,9.0); + let x = Convolve1D(v1,v2); + println!("{:?}",x) +} \ No newline at end of file