# Самое ровное окно записи: RMS окнами по 0,5 с. Аргументы: pcm f32le моно 8 кГц, длина окна, с.
import array, math, statistics as st, sys

a = array.array("f")
a.frombytes(open(sys.argv[1], "rb").read())
win = int(float(sys.argv[2]) * 2)
n = 4000
db = [10 * math.log10(sum(x * x for x in a[i:i + n]) / n + 1e-12) for i in range(0, len(a) - n, n)]
print("окон", len(db), "σ всей записи", round(st.pstdev(db), 2))
best = sorted((max(db[o:o + win]) - min(db[o:o + win]), st.pstdev(db[o:o + win]), o / 2) for o in range(2, len(db) - win))
print("лучшие (разброс дБ, σ, смещение с):", [(round(x, 1), round(y, 2), c) for x, y, c in best[:5]])
print("худшие:", [(round(x, 1), round(y, 2), c) for x, y, c in best[-3:]])
