From fd0c497c90d946085aa53388f8d4f16ba0552119 Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 7 Feb 2019 19:58:09 -0600 Subject: [PATCH] Added valid method --- examples/convolution.rs | 123 ++++++++++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 30 deletions(-) diff --git a/examples/convolution.rs b/examples/convolution.rs index f2ab42cc..4619d8db 100644 --- a/examples/convolution.rs +++ b/examples/convolution.rs @@ -6,57 +6,120 @@ use std::cmp; -// evum CovvolveMode{ -// Full, -// Valid, -// Same -// } +enum ConvolveMode{ + Full, + Valid, + Same +} - - -#[allow(non_snake_case)] -fn Convolve1D, Q: Storage>( - Vector : Vector, - Kernel : Vector +fn convolve_full, 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); + let vec = vector.len(); + let ker = kernel.len(); + let len = vec + ker - 1; + let v = vec as i8; + let k = ker as i8; + let l = len as i8; + let mut conv = DVector::::zeros(len); 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]; + conv[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]; + conv[i as usize] += vector[u as usize] * kernel[(i - u ) as usize]; } } } } - C + conv + } + +fn convolve_valid, Q: Storage>( + vector : Vector, + kernel : Vector + ) -> Matrix> + { + let vec = vector.len(); + let ker = kernel.len(); + let len = vec - ker + 1; + let mut conv = DVector::::zeros(len); + + for i in 0..len { + for j in 0..ker { + conv[i] += vector[i + j] * kernel[ker - j - 1]; + } + } + + conv + } + +fn convolve_same, Q: Storage>( + vector : Vector, + kernel : Vector + ) -> Matrix> + { + + let vec = vector.len(); + let ker = kernel.len(); + let len = vec + ker - 1; + let v = vec as i8; + let k = ker as i8; + let l = len as i8; + let mut conv = DVector::::zeros(len); + + 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 { + conv[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{ + conv[i as usize] += vector[u as usize] * kernel[(i - u ) as usize]; + } + } + } + } + conv + } + +fn convolve, Q: Storage>( + vector : Vector, + kernel : Vector, + mode : Option + ) -> Matrix> + { + // + // vector is the vector, Kervel is the kervel + // C is the returv vector + // + if kernel.len() > vector.len(){ + return convolve(kernel, vector, mode); + } + + match mode.unwrap_or(ConvolveMode::Full) { + ConvolveMode::Full => return convolve_full(vector,kernel), + ConvolveMode::Valid => return convolve_valid(vector,kernel), + ConvolveMode::Same => return convolve_same(vector,kernel) + } } fn main() { - let v1 = Vector2::new(3.0,3.0); + let v1 = Vector2::new(3.0,1.0); let v2 = Vector4::new(1.0,2.0,5.0,9.0); - let x = Convolve1D(v1,v2); + let x = convolve(v1,v2,Some(ConvolveMode::Valid)); println!("{:?}",x) } \ No newline at end of file