Qu

hann

Builtin index · Signal processing

result = hann(n)

Input: n · Output: result

What it is

n: a number (integer window length).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = hann(n)
n numbernot documented yet
result vector
Returns a length-n real vector. Raised-cosine window, w[i] = 0.5*(1 - cos(2*pi*i/(n-1))). Symmetric convention (denominator n-1, MATLAB/SciPy sym=True); w[0] == w[n-1] == 0 exactly.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

N = 256
Fs = 256
t = (0 to N - 1) / Fs
f0 = 20.5
x = sin(2*pi*f0*t)

mag_raw = abs(fft(x))[0:N/2]
w = hann(N)
mag_win = abs(fft(x .* w))[0:N/2]

far_band = 90 to 127
leak_raw = sum(mag_raw[far_band])
leak_win = sum(mag_win[far_band])

print("Peak magnitude, no window:   {max(mag_raw):.2f}")
print("Peak magnitude, hann window: {max(mag_win):.2f}")
print("Far-band (90-127 Hz) leakage energy, no window:   {leak_raw:.3f}")
print("Far-band (90-127 Hz) leakage energy, hann window: {leak_win:.3f}")

from tour

How it works

hann(n) — raised-cosine window, w[i] = 0.5*(1 - cos(2*pi*i/(n-1))). Symmetric convention (denominator n-1, matching MATLAB's/SciPy's sym=True default), not the periodic/DFT-even convention (denominator n, SciPy's sym=False) that STFT/spectrogram frameworks prefer for perfect overlap-add reconstruction — this codebase's own stft doesn't window at all yet, so there's no existing periodic-window caller to stay consistent with, and "symmetric" is the more common default a user typing hann(n) expects. w[0] == w[n-1] == 0 exactly (cos(0)=1).

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.