Trait basic_dsp::CrossCorrelation
[−]
[src]
pub trait CrossCorrelation<T>: DataVector<T> where T: RealNumber { type FreqPartner; fn prepare_argument(self) -> VecResult<Self::FreqPartner>; fn prepare_argument_padded(self) -> VecResult<Self::FreqPartner>; fn correlate(self, other: &Self::FreqPartner) -> VecResult<Self>; }
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 basic_dsp::{ComplexTimeVector32, CrossCorrelation, DataVector}; let vector = ComplexTimeVector32::from_interleaved(&[1.0, 1.0, 2.0, 2.0, 3.0, 3.0]); let argument = ComplexTimeVector32::from_interleaved(&[3.0, 3.0, 2.0, 2.0, 1.0, 1.0]); let argument = argument.prepare_argument_padded().expect("Ignoring error handling in examples"); let result = vector.correlate(&argument).expect("Ignoring error handling in examples"); let expected = &[2.0, 0.0, 8.0, 0.0, 20.0, 0.0, 24.0, 0.0, 18.0, 0.0]; for i in 0..result.len() { assert!((result[i] - expected[i]).abs() < 1e-4); }
Unstable
This functionality has been recently added in order to find out if the definitions are consistent. However the actual implementation is lacking tests.
Failures
VecResult 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.
Associated Types
type FreqPartner
Required Methods
fn prepare_argument(self) -> VecResult<Self::FreqPartner>
Prepares an argument to be used for convolution. Preparing an argument includes two steps:
- Calculate the plain FFT
- Calculate the complex conjugate
fn prepare_argument_padded(self) -> VecResult<Self::FreqPartner>
Prepares an argument to be used for convolution. The argument is zero padded to length of 2 * self.points() - 1
and then the same operations are performed as described for prepare_argument
.
fn correlate(self, other: &Self::FreqPartner) -> VecResult<Self>
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.
Implementors
impl CrossCorrelation<f32> for GenericDataVector<f32>
impl CrossCorrelation<f64> for GenericDataVector<f64>
impl CrossCorrelation<f32> for ComplexTimeVector<f32>
impl CrossCorrelation<f64> for ComplexTimeVector<f64>