normalize

Function normalize 

Source
pub fn normalize<T>(x: &[T], norm: &str) -> Result<Vec<f64>, &'static str>
where T: Float + NumCast + Debug,
Expand description

Normalize a vector to have unit energy or unit peak amplitude.

§Arguments

  • x - Input vector
  • norm - Normalization type: energy, “peak”, “sum”, or “max”

§Returns

  • Normalized vector as Vec<f64>

§Examples

use scirs2_core::utils::normalize;

// Normalize a vector to unit energy
let signal = vec![1.0, 2.0, 3.0, 4.0];
let normalized = normalize(&signal, "energy").unwrap();

// Sum of squares should be 1.0
let sum_of_squares: f64 = normalized.iter().map(|&x| x * x).sum();
assert!((sum_of_squares - 1.0).abs() < 1e-10);

§Errors

Returns an error if the input signal is empty, has zero energy/peak/sum, or if a conversion fails.