How do I account for Hann-window amplitude loss?

How do I account for Hann-window amplitude loss?

For np.hanning(128), divide the FFT by sum(w)=63.5; mean gain is 0.49609375. Double the interior one-sided amplitudes.

On this page

Published 22 November 2026. Updated 27 November 2026.

Which normalization should I use with a Hann window?

For this real, 128-sample signal, use 2*abs(rfft(signal*w))/sum(w) for bins 1–63, and abs(rfft(signal*w))/sum(w) at DC and Nyquist. NumPy's default forward FFT uses an unscaled sum. The two-sided spectrum of a real signal has matching positive and negative frequency magnitudes; the factor of two combines each interior pair.

I use the same 128 samples in both plots: a sine with amplitude 1 at 5.3 Hz, sampled at 128 Hz. This article uses simulated example data.

Rectangular-window spectrum: the 5 Hz bin has amplitude 0.8601; the dashed line marks input amplitude 1.
Before windowing: divide by the record length, 128. The largest sampled amplitude is 0.8601 at 5 Hz.
Hann-window spectrum normalized by weight sum 63.5: the 5 Hz bin has amplitude 0.9441.
With the symmetric Hann window: divide by the weight sum, 63.5. The sampled peak is 0.9441 at 5 Hz. The dashed reference line shows the input amplitude of 1 in both plots.

What does the NumPy Hann function calculate?

np.hanning(N) returns a symmetric window with weights w[j] = 0.5 - 0.5*cos(2*pi*j/(N-1)) for j=0,...,N-1. For N=128, both endpoint weights are zero and the weight sum is (N-1)/2 = 63.5. Dividing by N gives the mean gain, 0.49609375.

I use the actual weight sum in the script so the divisor follows the window I created. The window type, length and FFT normalization mode belong together when checking an amplitude calculation.

Why does the corrected peak remain below 1?

The 5.3 Hz sine lies between the FFT bins, whose spacing is fs/N = 1 Hz. The maximum sampled bin is at 5 Hz, where the corrected amplitude is about 0.9441. Coherent-gain correction accounts for the average window weight; an off-bin tone retains scalloping loss. To estimate this tone's amplitude more closely, evaluate the spectrum near 5.3 Hz or fit a sine model at the known frequency.

How can I run and check the example?

Run the complete script in a working folder with NumPy and Matplotlib installed. I include spectrum.csv so you can compare all 65 nonnegative frequency bins.

The executed environment is Python 3.12.4, NumPy 1.26.4 and Matplotlib 3.8.4. PNG files are 1152 × 720 pixels at 160 PPI. The PDF page is 7.2 × 4.5 inches. The DejaVu Sans font terms are included with the downloads.

# Bopbun example: CC0-1.0. Data and font terms: LICENSE.txt.
from pathlib import Path
import json, csv
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

out = Path(__file__).resolve().parent
n, fs = 128, 128
t = np.arange(n) / fs
signal = np.sin(2 * np.pi * 5.3 * t)
w = np.hanning(n)  # Symmetric Hann window, including both endpoints.
f = np.fft.rfftfreq(n, d=1/fs)
with (out/'data.csv').open('w',newline='',encoding='utf-8') as stream:
    writer=csv.writer(stream)
    writer.writerow(['time_s','signal','hann_weight'])
    writer.writerows(zip(t,signal,w))

def amplitude(samples, divisor):
    a = 2 * np.abs(np.fft.rfft(samples)) / divisor
    a[0] /= 2
    a[-1] /= 2  # n is even: the final bin is Nyquist.
    return a

raw = amplitude(signal, n)
corrected = amplitude(signal*w, w.sum())
plt.rcParams.update({'font.family':'DejaVu Sans', 'font.size':11,
                     'pdf.fonttype':42, 'savefig.dpi':160})
for name, a, title in [
    ('before', raw, 'Rectangular window: 5.3 Hz signal'),
    ('after', corrected, 'Symmetric Hann: FFT divided by sum(w) = 63.5')
]:
    fig, ax = plt.subplots(figsize=(7.2,4.5), layout='constrained')
    ax.plot(f, a, 'o-', label='FFT-bin amplitude')
    ax.axhline(1, color='#666666', linestyle='--', label='Input sine amplitude = 1')
    k = int(np.argmax(a))
    ax.annotate(f'{f[k]:.0f} Hz: {a[k]:.4f}', (f[k],a[k]),
                xytext=(15,-24 if a[k]>.9 else 12), textcoords='offset points')
    ax.set(xlim=(0,16), ylim=(0,1.22), xlabel='Frequency (Hz)',
           ylabel='Amplitude (a.u.)', title=title)
    ax.legend(loc='upper right', fontsize=9)
    fig.savefig(out/f'{name}.png')
    if name == 'after':
        fig.savefig(out/'after.pdf', metadata={'CreationDate':None,'ModDate':None})
    plt.close(fig)
np.savetxt(out/'spectrum.csv', np.column_stack((f,raw,corrected)),
           delimiter=',', header='frequency_hz,rectangular_amplitude,hann_corrected_amplitude',comments='')
(out/'run.json').write_text(json.dumps({'n':n,'fs_hz':fs,'frequency_hz':5.3,
    'window_sum':float(w.sum()),'window_mean':float(w.mean()),
    'raw_peak':float(raw.max()),'corrected_peak':float(corrected.max()),
    'peak_frequency_hz':float(f[np.argmax(corrected)]),
    'versions':{'numpy':np.__version__,'matplotlib':matplotlib.__version__}},indent=2))

These are the code, data and result files used in this article. Choose a file below to run the example or compare your results.

  • example.pyComplete example code to run on your computer.
  • data.csvThe input data used in the example; open it as a table.
  • spectrum.csvFrequencies and the corresponding amplitudes.
  • before.pngThe figure before the change, for comparison.
  • after.pngThe figure after applying the settings in this article.
  • after.pdfThe result in PDF format, for viewing and page layout.
  • Usage license · LICENSE.txtTerms for reusing the code, data and figures, with source and font notices.

For the separate question of time-frequency windows and overlap, see the spectrogram answer.

NumPy 1.26: hanning · NumPy 1.26: rfft