gpu_fft/psd.rs
1/// Computes the Power Spectral Density (PSD) from the real and imaginary components of a signal.
2///
3/// The Power Spectral Density is a measure of the power of a signal per unit frequency. This function
4/// calculates the PSD by first computing the magnitude of the complex numbers represented by the
5/// real and imaginary components, and then calculating the power from the magnitude. The result is
6/// normalized by the number of points in the input vectors.
7///
8/// # Parameters
9///
10/// - `real`: A vector of `f32` values representing the real parts of the complex signal.
11/// - `imag`: A vector of `f32` values representing the imaginary parts of the complex signal.
12///
13/// # Returns
14///
15/// A vector of `f32` values representing the Power Spectral Density of the input signal. The length
16/// of the output vector will be the same as the input vectors.
17///
18/// # Example
19///
20/// ```rust
21/// let real = vec![1.0, 0.0, 0.0, 0.0]; // Example real input
22/// let imag = vec![0.0, 0.0, 0.0, 0.0]; // Example imaginary input
23/// let psd_values = psd(real, imag);
24/// ```
25///
26/// # Note
27///
28/// The normalization step (dividing by `n`) is optional and can be adjusted based on specific
29/// requirements or conventions in your application.
30pub fn psd(real: Vec<f32>, imag: Vec<f32>) -> Vec<f32> {
31 let n = real.len();
32 let mut psd = Vec::with_capacity(n);
33
34 for i in 0..n {
35 // Calculate the magnitude
36 let magnitude = (real[i] * real[i] + imag[i] * imag[i]).sqrt();
37 // Calculate the power
38 let power = magnitude * magnitude;
39 // Normalize the power (optional, depending on your needs)
40 psd.push(power / n as f32); // Normalization by the number of points
41 }
42
43 psd
44}