// Advanced digital signal processing for optical pulse diagnostics.
///|
/// Compute cross-correlation of two complex signals
pub fn cross_correlation(
x : Array[Complex],
y : Array[Complex],
) -> Array[Complex] {
let n = x.length()
let res = Array::make(n, Complex::zero())
let half = n / 2
for shift = -half; shift < half; shift = shift + 1 {
let mut sum = Complex::zero()
for i = 0; i < n; i = i + 1 {
let j = i - shift
if j >= 0 && j < n {
let prod = x[i].mul(y[j].conj())
sum = sum.add(prod)
}
}
let idx = shift + half
if idx >= 0 && idx < n {
res[idx] = sum
}
}
res
}
///|
/// Unwrap phase array to eliminate 2*pi phase discontinuities
pub fn unwrap_phase(phase : Array[Double]) -> Array[Double] {
let n = phase.length()
if n == 0 {
return []
}
let res = Array::make(n, 0.0)
res[0] = phase[0]
let mut correction = 0.0
for i = 1; i < n; i = i + 1 {
let diff = phase[i] - phase[i - 1]
if diff > PI {
correction = correction - TWO_PI
} else if diff < -PI {
correction = correction + TWO_PI
}
res[i] = phase[i] + correction
}
res
}