"""Data for the plots on this page.

    python data.py     writes the .dat files the .tex drawings read

2204: a 1000 Hz sinusoid sampled at 8000 Hz, 8 samples (one full cycle) and 9 samples.
2205: the Hamming window, M = 64.
"""
import numpy as np

fs, f = 8000, 1000
for N in (8, 9):
    n = np.arange(N)
    x = np.sin(2 * np.pi * f * n / fs)
    np.savetxt(f"leak{N}-signal.dat", np.column_stack([n + 1, x]), fmt="%.5g", header="n x", comments="")
    X = np.fft.fftshift(np.abs(np.fft.fft(x)) / N)
    k = np.arange(N) - N // 2
    np.savetxt(f"leak{N}-spectrum.dat", np.column_stack([k * fs / N, X]), fmt="%.5g", header="f X", comments="")

M = 64
np.savetxt("hamming.dat", np.column_stack([np.arange(1, M + 1), np.hamming(M)]), fmt="%.5g", header="n w", comments="")

# 2106: periodogram of white Gaussian noise, smoothed a little (8 averaged frames), in dB/Hz
rng = np.random.default_rng(2)
fs3, L = 8000, 256
w = rng.standard_normal(8 * L) * np.sqrt(fs3 * 10**3.9 / 2)
P = np.mean([np.abs(np.fft.rfft(w[i*L:(i+1)*L]))**2 / (L * fs3) * 2 for i in range(8)], axis=0)
np.savetxt("whitepsd.dat", np.column_stack([np.arange(len(P)) * fs3 / L, 10 * np.log10(P)])[1:-1],
           fmt="%.5g", header="f P", comments="")
