Trait basic_dsp::CrossCorrelationOps
source · pub trait CrossCorrelationOps<A, S, T, N, D>{
// Required method
fn correlate<B>(
&mut self,
buffer: &mut B,
other: &A
) -> Result<(), ErrorReason>
where B: for<'a> Buffer<'a, S, T>;
}
Expand description
Cross-correlation of data vectors. See also https://en.wikipedia.org/wiki/Cross-correlation.
The correlation is calculated in two steps. This is done to give you more control over two things:
- Should the correlation use zero padding or not? This is done by calling either
prepare_argument
orprepare_argument_padded
. - The lifetime of the argument. The argument needs to be transformed for the correlation and depending on the application that might be just fine, or a clone needs to be created or it’s okay to use one argument for multiple correlations.
To get the same behavior like GNU Octave or MATLAB prepare_argument_padded
needs to be
called before doing the correlation. See also the example section for how to do this.
§Example
use std::f32;
use basic_dsp_vector::*;
let mut vector = vec!(Complex::new(1.0, 1.0), Complex::new(2.0, 2.0), Complex::new(3.0, 3.0)).to_complex_time_vec();
let argument = vec!(Complex::new(3.0, 3.0), Complex::new(2.0, 2.0), Complex::new(1.0, 1.0)).to_complex_time_vec();
let mut buffer = SingleBuffer::new();
let argument = argument.prepare_argument_padded(&mut buffer);
vector.correlate(&mut buffer, &argument).expect("Ignoring error handling in examples");
let expected = &[Complex::new(2.0, 0.0), Complex::new(8.0, 0.0), Complex::new(20.0, 0.0), Complex::new(24.0, 0.0), Complex::new(18.0, 0.0)];
for i in 0..vector.len() {
assert!((vector[i] - expected[i]).norm() < 1e-4);
}
§Failures
TransRes
may report the following ErrorReason
members:
VectorMustBeComplex
: ifself
is in real number space.VectorMetaDataMustAgree
: in caseself
andfunction
are not in the same number space and same domain.
Required Methods§
sourcefn correlate<B>(&mut self, buffer: &mut B, other: &A) -> Result<(), ErrorReason>where
B: for<'a> Buffer<'a, S, T>,
fn correlate<B>(&mut self, buffer: &mut B, other: &A) -> Result<(), ErrorReason>where
B: for<'a> Buffer<'a, S, T>,
Calculates the correlation between self
and other
. other
needs to be a time vector which
went through one of the prepare functions prepare_argument
or prepare_argument_padded
.
See also the trait description for more details.
Object Safety§
This trait is not object safe.