use scirs2_core::numeric::Complex64;
use std::fmt::Debug;
#[allow(dead_code)]
pub fn try_as_complex<T: 'static + Copy>(val: T) -> Option<Complex64> {
use std::any::Any;
if let Some(complex) = (&val as &dyn Any).downcast_ref::<Complex64>() {
return Some(*complex);
}
if let Some(complex32) = (&val as &dyn Any).downcast_ref::<scirs2_core::numeric::Complex<f32>>()
{
return Some(Complex64::new(complex32.re as f64, complex32.im as f64));
}
None
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SparsityEstimationMethod {
Manual,
Threshold,
Adaptive,
FrequencyPruning,
SpectralFlatness,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SparseFFTAlgorithm {
Sublinear,
CompressedSensing,
Iterative,
Deterministic,
FrequencyPruning,
SpectralFlatness,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WindowFunction {
None,
Hann,
Hamming,
Blackman,
FlatTop,
Kaiser,
}
#[derive(Debug, Clone)]
pub struct SparseFFTConfig {
pub estimation_method: SparsityEstimationMethod,
pub sparsity: usize,
pub algorithm: SparseFFTAlgorithm,
pub threshold: f64,
pub iterations: usize,
pub seed: Option<u64>,
pub max_signal_size: usize,
pub adaptivity_factor: f64,
pub pruning_sensitivity: f64,
pub flatness_threshold: f64,
pub window_size: usize,
pub window_function: WindowFunction,
pub kaiser_beta: f64,
}
impl Default for SparseFFTConfig {
fn default() -> Self {
Self {
estimation_method: SparsityEstimationMethod::Threshold,
sparsity: 10,
algorithm: SparseFFTAlgorithm::Sublinear,
threshold: 0.01,
iterations: 3,
seed: None,
max_signal_size: 1024, adaptivity_factor: 0.25,
pruning_sensitivity: 0.05,
flatness_threshold: 0.3,
window_size: 16,
window_function: WindowFunction::None,
kaiser_beta: 14.0, }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = SparseFFTConfig::default();
assert_eq!(config.sparsity, 10);
assert_eq!(config.threshold, 0.01);
assert_eq!(config.max_signal_size, 1024);
}
#[test]
fn test_try_as_complex() {
let val = Complex64::new(1.0, 2.0);
assert_eq!(try_as_complex(val), Some(val));
let val32 = scirs2_core::numeric::Complex::new(1.0f32, 2.0f32);
assert_eq!(try_as_complex(val32), Some(Complex64::new(1.0, 2.0)));
}
}