Add reset method to AvPowerSpectra

This commit is contained in:
Anne de Jong 2024-07-10 10:49:06 +02:00
parent d695b31426
commit 14dae88d48
2 changed files with 27 additions and 10 deletions

View File

@ -47,13 +47,14 @@ pub enum ApsMode {
/// where new data is weighted with old data, and old data exponentially /// where new data is weighted with old data, and old data exponentially
/// backs off. This mode only makes sense when `tau >> nfft/fs` /// backs off. This mode only makes sense when `tau >> nfft/fs`
ExponentialWeighting { ExponentialWeighting {
/// Sampling frequency in [Hz] /// Sampling frequency in [Hz], used for computing IIR filter coefficient.
fs: Flt, fs: Flt,
/// Time weighting constant, its inverse is its approximate -3 dB point /// Time weighting constant, follows convention of Sound Level Meters.
/// for forgetting old history. /// Means the data is approximately low-pass filtered with a cut-off
/// frequency f_c of s/tau ≅ 1 → f_c = (2 * pi * tau)^-1.
tau: Flt, tau: Flt,
}, },
/// Spectrogram mode. Only returns the latest estimates. /// Spectrogram mode. Only returns the latest estimate(s).
Spectrogram, Spectrogram,
} }
impl Default for ApsMode { impl Default for ApsMode {
@ -119,15 +120,29 @@ impl AvPowerSpectra {
Ok(overlap_keep) Ok(overlap_keep)
} }
/// Resets all state, starting with a clean sleave. After this step, also
/// the number of channels can be different on the input.
pub fn reset(&mut self) {
self.N = 0;
self.timebuf.reset();
self.cur_est = CPS::zeros((0,0,0));
}
/// Create new averaged power spectra estimator for weighing over the full /// Create new averaged power spectra estimator for weighing over the full
/// amount of data supplied (no exponential spectra weighting) using /// amount of data supplied (no exponential spectra weighting) using
/// sensible defaults (Hann window, 50% overlap). /// sensible defaults (Hann window, 50% overlap). This is a simpler method
/// than [AvPowerSpectra.build]. But use with caution, it might panic on
/// invalid nfft values!
/// ///
/// # Args /// # Args
/// ///
/// * `nfft` - FFT Length /// * `nfft` - FFT Length
pub fn new_simple(nfft: usize) -> Result<AvPowerSpectra> { ///
return AvPowerSpectra::build(nfft, None, None, None); /// # Panics
///
/// - When nfft is not even, or 0.
///
pub fn new_simple(nfft: usize) -> AvPowerSpectra {
AvPowerSpectra::build(nfft, None, None, None).unwrap()
} }
/// Create power spectra estimator which weighs either all data /// Create power spectra estimator which weighs either all data
@ -147,7 +162,7 @@ impl AvPowerSpectra {
/// - `nfft` - The discrete Fourier Transform length used in the estimation. /// - `nfft` - The discrete Fourier Transform length used in the estimation.
/// - `windowtype` - Window Type. The window type to use Hann, etc. /// - `windowtype` - Window Type. The window type to use Hann, etc.
/// - `overlap` - Amount of overlap in /// - `overlap` - Amount of overlap in
/// - `mode` - The mode in which the /// - `mode` - The mode in which the [AvPowerSpectra] runs. See [ApsMode].
/// ///
pub fn build( pub fn build(
nfft: usize, nfft: usize,
@ -393,7 +408,8 @@ mod test {
assert!(false, "Should return one value"); assert!(false, "Should return one value");
} }
let overlap_keep = AvPowerSpectra::get_overlap_keep(nfft, Overlap::NoOverlap).unwrap(); let overlap_keep = AvPowerSpectra::get_overlap_keep(nfft, Overlap::NoOverlap).unwrap();
if let ApsResult::OnlyLastResult(v) = aps.compute_last(&timedata_zeros) { if let ApsResult::OnlyLastResult(_) = aps.compute_last(&timedata_zeros) {
// Do nothing with it.
} else { } else {
assert!(false, "Should return one value"); assert!(false, "Should return one value");
} }

View File

@ -68,7 +68,8 @@ fn hamming(N: usize) -> Dcol {
) )
} }
/// Window type descriptors. Used for storage /// Window type descriptors. Used for computing the actual window with private
/// functions. See [Window], for the actual window (taper).
#[derive(Display, Clone, Debug)] #[derive(Display, Clone, Debug)]
pub enum WindowType { pub enum WindowType {
/// Von Hann window /// Von Hann window