Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example for 1D convolution. #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ members = [
"./",
"linear_regression",
"k_means",
"convolution_1d",
]
11 changes: 11 additions & 0 deletions convolution_1d/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "convolution_1d"
version = "0.1.0"
authors = ["Christopher H. Jordan <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
float-cmp = "0.6.0"
ndarray = "0.13.0"
19 changes: 19 additions & 0 deletions convolution_1d/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
1D Convolution
==================

An implementation of traditional (i.e. not utilising FFTs) convolution with
ndarray. Designed to match `numpy`'s `convolve` with `mode='same'`.

The code could be made to work for multi-dimensional arrays, but at present, I
have not investigated how.

```
numpy example of convolve with mode='same':
>>> np.convolve([1,2,3],[0,1,0.5], 'same')
array([1. , 2.5, 4. ])

ndarray code follows:
data: [1.0, 2.0, 3.0], shape=[3], strides=[1], layout=C | F (0x3), const ndim=1
window: [0.0, 1.0, 0.5], shape=[3], strides=[1], layout=C | F (0x3), const ndim=1
convolution: [1.0, 2.5, 4.0], shape=[3], strides=[1], layout=C | F (0x3), const ndim=1
```
107 changes: 107 additions & 0 deletions convolution_1d/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#[macro_use]
extern crate ndarray;

use ndarray::prelude::*;

pub fn convolve(data: ArrayView1<f64>, window: ArrayView1<f64>) -> Array1<f64> {
let padded = stack![
Axis(0),
Array1::zeros(window.len() / 2),
data,
Array1::zeros(window.len() / 2)
];
let mut w = window.view();
w.invert_axis(Axis(0));

padded
.windows(w.len())
.into_iter()
.map(|x| (&x * &w).sum())
.collect()
}

#[cfg(test)]
mod tests {
use super::*;
use float_cmp::*;

#[test]
fn convolve_odd_odd() {
let data = array![1., 2., 3.];
let window = array![0., 1., 0.5];
let expected = array![1., 2.5, 4.];

for (exp, res) in expected.iter().zip(&convolve(data.view(), window.view())) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}

#[test]
fn convolve_odd_odd2() {
let data = array![1., 2., 3., 4., 5.];
let window = array![2., 1., 0., 1., 0.5];
let result = convolve(data.view(), window.view());
let expected = array![8., 12., 16.5, 9., 5.5];

for (exp, res) in expected.iter().zip(&result) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}

#[test]
fn convolve_even_odd() {
let data = array![1., 2., 3., 4.];
let window = array![0., 1., 0.5];
let expected = array![1., 2.5, 4., 5.5];

for (exp, res) in expected.iter().zip(&convolve(data.view(), window.view())) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}

#[test]
fn convolve_even_even() {
let data = array![1., 2., 3., 4.];
let window = array![1., 0.5];
let expected = array![1., 2.5, 4., 5.5];

for (exp, res) in expected.iter().zip(&convolve(data.view(), window.view())) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}

#[test]
fn convolve_even_even2() {
let data = array![1., 2., 3., 4.];
let window = array![1., 0., 1., 0.5];
let result = convolve(data.view(), window.view());
let expected = array![2., 4., 6.5, 4.];

for (exp, res) in expected.iter().zip(&result) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}

#[test]
fn convolve_odd_even() {
let data = array![1., 2., 3., 4., 5.];
let window = array![1., 0.5];
let expected = array![1., 2.5, 4., 5.5, 7.];

for (exp, res) in expected.iter().zip(&convolve(data.view(), window.view())) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}

#[test]
fn convolve_bigger_window() {
let data = array![1., 2., 3.];
let window = array![1., 0., 1., 0.5];
let result = convolve(data.view(), window.view());
let expected = array![2., 4., 2.5, 4.];

for (exp, res) in expected.iter().zip(&result) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}
}
16 changes: 16 additions & 0 deletions convolution_1d/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use ndarray::prelude::*;
use convolution_1d::convolve;

fn main() {
println!(r#"numpy example of convolve with mode='same':
>>> np.convolve([1,2,3],[0,1,0.5], 'same')
array([1. , 2.5, 4. ])

ndarray code follows:"#);

let data = array![1., 2., 3.];
let window = array![0., 1., 0.5];
println!("data: \t\t{:?}", data);
println!("window: \t{:?}", window);
println!("convolution: \t{:?}", convolve(data.view(), window.view()));
}