2019-02-19 09:01:18 +08:00
|
|
|
use na::{Vector2,Vector3,Vector4,Vector5,DVector};
|
2019-02-25 09:53:09 +08:00
|
|
|
use std::panic;
|
2019-02-11 03:40:32 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Should mimic calculations in Python's scipy library
|
|
|
|
// >>>from scipy.signal import convolve
|
|
|
|
//
|
|
|
|
|
|
|
|
// >>> convolve([1,2,3,4],[1,2],"same")
|
|
|
|
// array([ 1, 4, 7, 10])
|
|
|
|
#[test]
|
|
|
|
fn convolve_same_check(){
|
2019-02-25 09:53:09 +08:00
|
|
|
// Static Tests
|
2019-02-19 09:01:18 +08:00
|
|
|
let actual_s = Vector4::from_vec(vec![1.0,4.0,7.0,10.0]);
|
2019-03-03 05:00:40 +08:00
|
|
|
let expected_s = Vector4::new(1.0,2.0,3.0,4.0).convolve_same(Vector2::new(1.0,2.0));
|
2019-02-11 03:40:32 +08:00
|
|
|
|
2019-02-19 09:01:18 +08:00
|
|
|
assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
|
|
|
|
|
2019-02-25 09:53:09 +08:00
|
|
|
// Dynamic Tests
|
|
|
|
let actual_d = DVector::from_vec(vec![1.0,4.0,7.0,10.0]);
|
2019-03-03 05:00:40 +08:00
|
|
|
let expected_d = DVector::from_vec(vec![1.0,2.0,3.0,4.0]).convolve_same(DVector::from_vec(vec![1.0,2.0]));
|
2019-02-19 09:01:18 +08:00
|
|
|
|
|
|
|
assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
|
2019-02-25 09:53:09 +08:00
|
|
|
|
|
|
|
// Panic Tests
|
|
|
|
// These really only apply to dynamic sized vectors
|
|
|
|
assert!(
|
|
|
|
panic::catch_unwind(|| {
|
2019-03-03 05:00:40 +08:00
|
|
|
DVector::from_vec(vec![1.0,2.0]).convolve_same(DVector::from_vec(vec![1.0,2.0,3.0,4.0]));
|
2019-02-25 09:53:09 +08:00
|
|
|
}).is_err()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
panic::catch_unwind(|| {
|
2019-03-03 05:00:40 +08:00
|
|
|
DVector::<f32>::from_vec(vec![]).convolve_same(DVector::from_vec(vec![1.0,2.0,3.0,4.0]));
|
2019-02-25 09:53:09 +08:00
|
|
|
}).is_err()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
panic::catch_unwind(|| {
|
2019-03-03 05:00:40 +08:00
|
|
|
DVector::from_vec(vec![1.0,2.0,3.0,4.0]).convolve_same(DVector::<f32>::from_vec(vec![]));
|
2019-02-25 09:53:09 +08:00
|
|
|
}).is_err()
|
|
|
|
);
|
2019-02-11 03:40:32 +08:00
|
|
|
}
|
|
|
|
|
2019-02-19 09:01:18 +08:00
|
|
|
// >>> convolve([1,2,3,4],[1,2],"full")
|
2019-02-11 03:40:32 +08:00
|
|
|
// array([ 1, 4, 7, 10, 8])
|
|
|
|
#[test]
|
|
|
|
fn convolve_full_check(){
|
2019-02-25 09:53:09 +08:00
|
|
|
// Static Tests
|
2019-02-19 09:01:18 +08:00
|
|
|
let actual_s = Vector5::new(1.0,4.0,7.0,10.0,8.0);
|
2019-03-03 05:00:40 +08:00
|
|
|
let expected_s = Vector4::new(1.0,2.0,3.0,4.0).convolve_full(Vector2::new(1.0,2.0));
|
2019-02-11 03:40:32 +08:00
|
|
|
|
2019-02-19 09:01:18 +08:00
|
|
|
assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
|
2019-02-11 03:40:32 +08:00
|
|
|
|
2019-02-25 09:53:09 +08:00
|
|
|
// Dynamic Tests
|
|
|
|
let actual_d = DVector::from_vec(vec![1.0,4.0,7.0,10.0,8.0]);
|
2019-03-03 05:00:40 +08:00
|
|
|
let expected_d = DVector::from_vec(vec![1.0,2.0,3.0,4.0]).convolve_full(DVector::from_vec(vec![1.0,2.0]));
|
2019-02-19 09:01:18 +08:00
|
|
|
|
|
|
|
assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
|
2019-02-25 09:53:09 +08:00
|
|
|
|
|
|
|
// Panic Tests
|
|
|
|
// These really only apply to dynamic sized vectors
|
|
|
|
assert!(
|
|
|
|
panic::catch_unwind(|| {
|
2019-03-03 05:00:40 +08:00
|
|
|
DVector::from_vec(vec![1.0,2.0]).convolve_full(DVector::from_vec(vec![1.0,2.0,3.0,4.0]));
|
2019-02-25 09:53:09 +08:00
|
|
|
}).is_err()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
panic::catch_unwind(|| {
|
2019-03-03 05:00:40 +08:00
|
|
|
DVector::<f32>::from_vec(vec![]).convolve_full(DVector::from_vec(vec![1.0,2.0,3.0,4.0]));
|
2019-02-25 09:53:09 +08:00
|
|
|
}).is_err()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
panic::catch_unwind(|| {
|
2019-03-03 05:00:40 +08:00
|
|
|
DVector::from_vec(vec![1.0,2.0,3.0,4.0]).convolve_full(DVector::<f32>::from_vec(vec![]));
|
2019-02-25 09:53:09 +08:00
|
|
|
}).is_err()
|
|
|
|
);
|
2019-02-11 03:40:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// >>> convolve([1,2,3,4],[1,2],"valid")
|
|
|
|
// array([ 4, 7, 10])
|
2019-02-25 09:53:09 +08:00
|
|
|
#[test]
|
|
|
|
fn convolve_valid_check(){
|
|
|
|
// Static Tests
|
|
|
|
let actual_s = Vector3::from_vec(vec![4.0,7.0,10.0]);
|
2019-03-03 05:00:40 +08:00
|
|
|
let expected_s = Vector4::new(1.0,2.0,3.0,4.0).convolve_valid( Vector2::new(1.0,2.0));
|
2019-02-19 09:01:18 +08:00
|
|
|
|
2019-02-25 09:53:09 +08:00
|
|
|
assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
|
2019-02-11 03:40:32 +08:00
|
|
|
|
2019-02-25 09:53:09 +08:00
|
|
|
// Dynamic Tests
|
|
|
|
let actual_d = DVector::from_vec(vec![4.0,7.0,10.0]);
|
2019-03-03 05:00:40 +08:00
|
|
|
let expected_d = DVector::from_vec(vec![1.0,2.0,3.0,4.0]).convolve_valid(DVector::from_vec(vec![1.0,2.0]));
|
2019-02-11 03:40:32 +08:00
|
|
|
|
2019-02-25 09:53:09 +08:00
|
|
|
assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
|
2019-02-11 03:40:32 +08:00
|
|
|
|
2019-02-25 09:53:09 +08:00
|
|
|
// Panic Tests
|
|
|
|
// These really only apply to dynamic sized vectors
|
|
|
|
assert!(
|
|
|
|
panic::catch_unwind(|| {
|
2019-03-03 05:00:40 +08:00
|
|
|
DVector::from_vec(vec![1.0,2.0]).convolve_valid(DVector::from_vec(vec![1.0,2.0,3.0,4.0]));
|
2019-02-25 09:53:09 +08:00
|
|
|
}).is_err()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
panic::catch_unwind(|| {
|
2019-03-03 05:00:40 +08:00
|
|
|
DVector::<f32>::from_vec(vec![]).convolve_valid(DVector::from_vec(vec![1.0,2.0,3.0,4.0]));
|
2019-02-25 09:53:09 +08:00
|
|
|
}).is_err()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
panic::catch_unwind(|| {
|
2019-03-03 05:00:40 +08:00
|
|
|
DVector::from_vec(vec![1.0,2.0,3.0,4.0]).convolve_valid(DVector::<f32>::from_vec(vec![]));
|
2019-02-25 09:53:09 +08:00
|
|
|
}).is_err()
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|