torch.bartlett_window
function bartlett_window(window_length: number, options?: WindowOptions): Tensorfunction bartlett_window(window_length: number, periodic: boolean, options?: WindowOptions): TensorBartlett window: simple triangular window with moderate performance.
Linear taper from zero to peak at center, then back to zero. Simplest window implementation but with higher side lobes. Used when simplicity is more important than optimal frequency characteristics. Essential for:
- Real-time processing: Low computational cost (just linear taper)
- Educational purposes: Easy to understand and visualize
- Baseline comparisons: Reference for evaluating other windows
- Quick prototyping: Simple implementation for testing
- Memory-constrained systems: No need to pre-compute cosines
- Rapid signal processing: Where speed matters more than spectral purity
- Legacy compatibility: Used in some older signal processing systems
Bartlett characteristics:
- Main lobe: ~8π/N (widest of common windows)
- Side lobes: -26 dB (higher than Hann or Hamming)
- Simplicity: Linear formula, no transcendental functions
- Best when you don't know signal characteristics in advance
- Simplest window: Just linear interpolation from 0 to 1 to 0
- Symmetric: Identical rise and fall
- Zero endpoints: Goes exactly to zero at both ends
- Peak value: 1.0 at the center
- Computational efficiency: No need for cosine calculations
- Equivalent to convolution: Result of convolving two rectangular windows
- Poor side lobe performance: -26 dB (worse than Hann/Hamming/Blackman)
- Wide main lobe: ~8π/N (frequency resolution is worst of common windows)
- Not recommended for analysis: Use Hann, Hamming, or Blackman instead
- Spectral leakage: Higher leakage than cosine windows
- Not optimal choice: Only use when simplicity/speed is critical
Parameters
window_lengthnumber- Size of the window (number of samples)
optionsWindowOptionsoptional- Window options: -
periodic: If true, uses N instead of N-1 (default: true) -dtype: Output data type (default: 'float32') -device: Where to place tensor (default: 'webgpu')
Returns
Tensor– 1D tensor of shape [window_length] forming a triangular shapeExamples
// Basic Bartlett window (triangular)
const window = torch.bartlett_window(10);
// [0, 0.222, 0.444, 0.667, 0.889, 1, 0.889, 0.667, 0.444, 0.222]
// Linear ramp up to center, then down// Quick spectral analysis with minimal processing
const signal = torch.randn(1024);
const window = torch.bartlett_window(1024);
const windowed = signal.mul(window);
const spectrum = torch.abs(torch.fft(windowed));
// Fast computation with acceptable frequency characteristics// Comparing all common windows
const windows = {
rect: torch.ones(512),
bartlett: torch.bartlett_window(512),
hann: torch.hann_window(512),
hamming: torch.hamming_window(512),
blackman: torch.blackman_window(512)
};
// Bartlett is simplest but has worst side lobe performance
// Side lobe trade-off: bartlett < hann < hamming < blackman (worse -> better)
// Main lobe width trade-off: bartlett > hann = hamming > blackman (wider -> narrower)// Real-time streaming: lightweight window computation
let window;
function init_stream(frame_size) {
window = torch.bartlett_window(frame_size); // One-time computation
}
function process_frame(audio_data) {
const windowed = audio_data.mul(window); // Lightweight operation
return torch.fft(windowed); // Fast FFT
}// Signal with unknown characteristics: use Bartlett for flexibility
const unknown_signal = torch.randn(2048);
const window = torch.bartlett_window(2048); // No assumptions needed
const windowed = unknown_signal.mul(window);
// Works reasonably for any signal without optimizationSee Also
- PyTorch torch.bartlett_window(window_length, periodic=True, dtype=None, layout=torch.strided, device=None, requires_grad=False)
- hann_window - Better frequency characteristics, minimal additional cost
- hamming_window - Optimized first side lobe
- blackman_window - Excellent side lobe suppression
- kaiser_window - Tunable for custom performance needs