Expand description
ndarray-conv provides N-dimensional convolution operations for ndarray arrays.
This crate extends the ndarray library with both standard and
FFT-accelerated convolution methods.
§Getting Started
To start performing convolutions, you’ll interact with the following:
- Input Arrays: Use
ndarray’sArrayorArrayViewas your input data and convolution kernel. - Convolution Methods: Call
array.conv(...)orarray.conv_fft(...). These methods are added toArrayBasetypes via the traitsConvExt::convandConvFFTExt::conv_fft. - Convolution Mode:
ConvModespecifies the size of the output. - Padding Mode:
PaddingModespecifies how to handle array boundaries.
§Basic Example:
Here’s a simple example of how to perform a 2D convolution using ndarray-conv:
use ndarray::prelude::*;
use ndarray_conv::{ConvExt, ConvFFTExt, ConvMode, PaddingMode};
// Input data
let input = array![[1, 2, 3], [4, 5, 6], [7, 8, 9]];
// Convolution kernel
let kernel = array![[1, 1], [1, 1]];
// Perform standard convolution with "same" output size and zero padding
let output = input.conv(
&kernel,
ConvMode::Same,
PaddingMode::Zeros,
).unwrap();
println!("Standard Convolution Output:\n{:?}", output);
// Perform FFT-accelerated convolution with "same" output size and zero padding
let output_fft = input.map(|&x| x as f32).conv_fft(
&kernel.map(|&x| x as f32),
ConvMode::Same,
PaddingMode::Zeros,
).unwrap();
println!("FFT Convolution Output:\n{:?}", output_fft);§Choosing a convolution method
- Use
ConvExt::convfor standard convolution - Use
ConvFFTExt::conv_fftfor FFT accelerated convolution. FFT accelerated convolution is generally faster for larger kernels, but standard convolution may be faster for smaller kernels.
§Key Structs, Enums and Traits
ConvMode: Specifies how to determine the size of the convolution output (e.g.,Full,Same,Valid).PaddingMode: Specifies how to handle array boundaries (e.g.,Zeros,Reflect,Replicate). You can also usePaddingMode::CustomorPaddingMode::Explicitto combine differentBorderTypestrategies for each dimension or for each side of each dimension.BorderType: Used withPaddingModeforCustomandExplicit, specifies the padding strategy (e.g.,Zeros,Reflect,Replicate,Circular).ConvExt: The trait that adds theconvmethod, extendingndarrayarrays with standard convolution functionality.ConvFFTExt: The trait that adds theconv_fftmethod, extendingndarrayarrays with FFT-accelerated convolution functionality.
Enums§
- Border
Type - Used with
PaddingMode. Specifies the padding mode for a single dimension or a single side of a dimension. - Conv
Mode - Specifies the convolution mode, which determines the output size.
- Error
- Error type for convolution operations.
- Padding
Mode - Specifies the padding mode, which determines how to handle borders.
Traits§
- ConvExt
- Extends
ndarray’sArrayBasewith convolution operations. - ConvFFT
Ext - Extends
ndarray’sArrayBasewith FFT-accelerated convolution operations. - FftProcessor
- Trait for FFT processors that can perform forward and backward transforms.
- Reverse
Kernel - Trait for controlling kernel reversal behavior in convolution operations.
- With
Dilation - Trait for adding dilation information to a kernel.
Functions§
- get_
fft_ processor - Returns a processor instance for the given input element type.