// Copyright 2026 MoonBit DSP Authors
// Apache-2.0 License

///|
fn main {
  println("=== MoonBit DSP: STFT Spectrogram Demo ===")

  let fs = 1000.0
  let total_samples = 512
  let frame_size = 64
  let hop_size = 32

  let signal = Array::make(total_samples, 0.0)
  for i = 0; i < total_samples; i = i + 1 {
    let t = i.to_double() / fs
    // Frequency sweep / chirp from 50Hz to 200Hz
    let f_inst = 50.0 + 150.0 * (t / 0.512)
    signal[i] = @cmath.sin(2.0 * @cmath.PI * f_inst * t)
  }

  let stft_frames = @fft.stft(
    signal,
    frame_size,
    hop_size,
    @window.WindowType::hann(),
  )
  println(
    "Computed \{stft_frames.length()} STFT spectrogram frames (frame_size=\{frame_size}, hop=\{hop_size}).",
  )

  for idx = 0; idx < stft_frames.length(); idx = idx + 1 {
    let frame = stft_frames[idx]
    let dom_freq = @fft.find_dominant_frequency(frame, fs)
    println("Frame \{idx}: estimated center frequency = \{dom_freq} Hz")
  }
}