basic_dsp_vector/vector_types/time_freq/
time.rs

1use super::super::{
2    DspVec, MetaData, NumberSpace, RededicateForceOps, TimeDomain, ToFreqResult, ToSliceMut,
3};
4use crate::array_to_complex_mut;
5use crate::numbers::*;
6use crate::window_functions::*;
7
8/// Defines all operations which are valid on `DataVecs` containing time domain data.
9/// # Failures
10/// All operations in this trait set `self.len()` to `0` if the vector isn't in time domain.
11pub trait TimeDomainOperations<S, T>
12where
13    S: ToSliceMut<T>,
14    T: RealNumber,
15{
16    /// Applies a window to the data vector.
17    fn apply_window(&mut self, window: &dyn WindowFunction<T>);
18
19    /// Removes a window from the data vector.
20    fn unapply_window(&mut self, window: &dyn WindowFunction<T>);
21}
22
23impl<S, T, N, D> TimeDomainOperations<S, T> for DspVec<S, T, N, D>
24where
25    DspVec<S, T, N, D>: ToFreqResult,
26    <DspVec<S, T, N, D> as ToFreqResult>::FreqResult: RededicateForceOps<DspVec<S, T, N, D>>,
27    S: ToSliceMut<T>,
28    T: RealNumber,
29    N: NumberSpace,
30    D: TimeDomain,
31{
32    fn apply_window(&mut self, window: &dyn WindowFunction<T>) {
33        if self.is_complex() {
34            self.multiply_window_priv(
35                window.is_symmetric(),
36                |array| array_to_complex_mut(array),
37                window,
38                |f, i, p| Complex::<T>::new(f.window(i, p), T::zero()),
39            );
40        } else {
41            self.multiply_window_priv(
42                window.is_symmetric(),
43                |array| array,
44                window,
45                |f, i, p| f.window(i, p),
46            );
47        }
48    }
49
50    fn unapply_window(&mut self, window: &dyn WindowFunction<T>) {
51        if self.is_complex() {
52            self.multiply_window_priv(
53                window.is_symmetric(),
54                |array| array_to_complex_mut(array),
55                window,
56                |f, i, p| Complex::<T>::new(T::one() / f.window(i, p), T::zero()),
57            );
58        } else {
59            self.multiply_window_priv(
60                window.is_symmetric(),
61                |array| array,
62                window,
63                |f, i, p| T::one() / f.window(i, p),
64            );
65        }
66    }
67}