forked from M-Labs/nalgebra
Feedback updates round 1
This commit is contained in:
parent
bca385ea6b
commit
b08c2ad70d
5
examples/convolution.rs
Normal file
5
examples/convolution.rs
Normal file
@ -0,0 +1,5 @@
|
||||
fn main(){
|
||||
let (x,y) = (1,2);
|
||||
|
||||
println!("{}", x);
|
||||
}
|
@ -1,45 +1,63 @@
|
||||
use storage::Storage;
|
||||
use {zero, DVector, Dim, Dynamic, Matrix, Real, VecStorage, Vector, U1};
|
||||
use {zero, DVector, Dim, Dynamic, Matrix, Real, VecStorage, Vector, U1, Add};
|
||||
use std::cmp;
|
||||
|
||||
///
|
||||
/// The output is the full discrete linear convolution of the inputs
|
||||
///
|
||||
pub fn convolve_full<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E>>(
|
||||
vector: Vector<R, D, S>,
|
||||
kernel: Vector<R, E, Q>,
|
||||
) -> Matrix<R, Dynamic, U1, VecStorage<R, Dynamic, U1>> {
|
||||
let vec = vector.len();
|
||||
impl<N: Real, D1: Dim, S1: Storage<N,D1>> Vector<N,D1,S1>{
|
||||
|
||||
/// Returns the convolution of the vector and a kernel
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `self` - A DVector with size D > 0
|
||||
/// * `kernel` - A DVector with size D > 0
|
||||
///
|
||||
/// # Note:
|
||||
/// This function is commutative. If D_kernel > D_vector,
|
||||
/// they will swap their roles as in
|
||||
/// (self, kernel) = (kernel,self)
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
///
|
||||
/// ```
|
||||
pub fn convolve_full<D2: Dim, S2: Storage<N, D2>>(&self, kernel: Vector<N, D2, S2>) -> Vector<N,Add<D1,D2>,Add<S1,S2>>
|
||||
{
|
||||
let vec = self.len();
|
||||
let ker = kernel.len();
|
||||
|
||||
if vec == 0 || ker == 0 {
|
||||
panic!("Convolve's inputs must not be 0-sized. ");
|
||||
}
|
||||
// if vec == 0 || ker == 0 {
|
||||
// panic!("Convolve's inputs must not be 0-sized. ");
|
||||
// }
|
||||
|
||||
if ker > vec {
|
||||
return convolve_full(kernel, vector);
|
||||
}
|
||||
// if ker > vec {
|
||||
// return kernel::convolve_full(vector);
|
||||
// }
|
||||
|
||||
let newlen = vec + ker - 1;
|
||||
|
||||
let mut conv = DVector::<R>::zeros(newlen);
|
||||
let mut conv = DVector::<N>::zeros(newlen);
|
||||
|
||||
for i in 0..newlen {
|
||||
let u_i = if i > ker { i - ker } else { 0 };
|
||||
let u_f = cmp::min(i, vec - 1);
|
||||
|
||||
if u_i == u_f {
|
||||
conv[i] += vector[u_i] * kernel[(i - u_i)];
|
||||
conv[i] += self[u_i] * kernel[(i - u_i)];
|
||||
} else {
|
||||
for u in u_i..(u_f + 1) {
|
||||
if i - u < ker {
|
||||
conv[i] += vector[u] * kernel[(i - u)];
|
||||
conv[i] += self[u] * kernel[(i - u)];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
conv
|
||||
// conv
|
||||
}
|
||||
}
|
||||
///
|
||||
/// The output is the full discrete linear convolution of the inputs
|
||||
///
|
||||
|
||||
|
||||
///
|
||||
/// The output convolution consists only of those elements that do not rely on the zero-padding.
|
||||
@ -103,3 +121,4 @@ pub fn convolve_same<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E>
|
||||
}
|
||||
conv
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user