torch.hamming_window
function hamming_window(window_length: number, options?: WindowOptions): Tensorfunction hamming_window(window_length: number, periodic: boolean, options?: WindowOptions): TensorHamming window: cosine window with optimized coefficients for low side lobes.
Similar to Hann but with coefficients (α=0.54, β=0.46) optimized to minimize the first side lobe. Slightly different behavior: unlike Hann, doesn't go to zero at endpoints. Essential for:
- Spectral analysis: When minimizing first side lobe is important
- Audio processing: STFT with lower spectral leakage than rectangular
- Speech recognition: Standard window for acoustic feature extraction
- Vibration monitoring: Detecting weak signals near strong components
- Radar signal processing: Reducing clutter from strong reflections
- Noise reduction: Windowing for time-domain processing
- Music information retrieval: Extracting timbral features
Hamming vs Hann:
- Hamming: Non-zero endpoints (~0.08), slightly better first side lobe suppression
- Hann: Zero endpoints, better overall side lobe suppression
- Hamming better when you need to preserve edge information
- Hann better when DC discontinuity causes problems
- Non-zero endpoints: Values at edges are ~0.08 (unlike Hann's zero)
- Optimized coefficients: 0.54, 0.46 minimize first side lobe
- Symmetric: Window is symmetric around center
- Unity peak: Maximum value is 1.0 (at center)
- First side lobe: -43 dB (vs -32 dB for Hann)
- Main lobe width: Similar to Hann (~4π/N)
- Not zero at edges: Discontinuity remains (less severe than rectangular)
- Specific coefficients: Standard 0.54/0.46 for Hamming; don't change
- Side lobe tradeoff: Lower first side lobe, but higher subsequent ones
- Amplitude loss: Still reduces signal amplitude; compensate if needed
Parameters
window_lengthnumber- Size of the window (number of samples)
optionsWindowOptionsoptional- Window options: -
periodic: If true, uses N in denominator (default: true) -dtype: Output data type (default: 'float32') -device: Where to place tensor (default: 'webgpu')
Returns
Tensor– 1D tensor of shape [window_length] with values approximately in [0.08, 1]Examples
// Basic Hamming window
const window = torch.hamming_window(10);
// [0.08, 0.188, 0.46, 0.77, 0.972, 0.972, 0.77, 0.46, 0.188, 0.08]
// Note: doesn't reach zero at endpoints (unlike Hann)// Speech feature extraction with Hamming windowing
const speech_sample = torch.randn(20000); // 500ms at 40kHz
const frame_length = 512;
const hop_length = 160;
const window = torch.hamming_window(frame_length, { periodic: true });
// Extract frames with 75% overlap
const frames = [];
for (let i = 0; i < speech_sample.shape[0] - frame_length; i += hop_length) {
const frame = speech_sample.slice(0, i, i + frame_length);
frames.push(frame.mul(window));
}
// Standard approach in speech recognition (MFCC, spectrograms)// Comparing Hamming vs Hann side lobe behavior
const n = 1024;
const hamming = torch.hamming_window(n);
const hann = torch.hann_window(n);
// Hamming: flatter top, doesn't go to zero at edges
// Hann: zero at edges, better overall side lobe
const hann_spectrum = torch.abs(torch.fft(hann));
const hamming_spectrum = torch.abs(torch.fft(hamming));
// Compare spectral leakage characteristics// Real-time audio processing with Hamming window
const audio_buffer = [];
const window = torch.hamming_window(2048, { periodic: true });
function process_frame(new_samples) {
audio_buffer.push(...new_samples);
if (audio_buffer.length >= 2048) {
const frame = torch.tensor(audio_buffer.slice(0, 2048));
const windowed = frame.mul(window);
const spectrum = torch.fft(windowed);
audio_buffer.splice(0, 512); // Hop by 512 (25% overlap)
}
}// Spectral analysis: compare window shapes
const windows = {
rect: torch.ones(256),
hamming: torch.hamming_window(256),
hann: torch.hann_window(256),
blackman: torch.blackman_window(256)
};
// Plot windows to visualize shape and spectral properties
for (const [name, w] of Object.entries(windows)) {
console.log(name, w.shape, w.max(), w.min());
}See Also
- PyTorch torch.hamming_window(window_length, periodic=True, dtype=None, layout=torch.strided, device=None, requires_grad=False)
- hann_window - Similar with zero endpoints, better overall sidelobes
- blackman_window - Much lower sidelobes but wider main lobe
- bartlett_window - Simpler triangular window
- kaiser_window - Tunable window for custom trade-offs