This is the implementation guide for the Bartlett window (also known as the triangular window). For theoretical background, including mathematical formula, frequency response, and spectral analysis, see the the article on Bartlett window.
All code examples are written in C for maximum performance and portability.
Direct Implementation
The following function computes the Bartlett window value for a given sample index n and total window length N. The Bartlett window is a triangular-shaped window with zero values at both endpoints [citation:9].
#include <math.h>
/**
* Compute Bartlett window coefficient for a single sample.
*
* @param n Sample index (0 to N-1)
* @param N Total window length
* @return Window coefficient (0.0 to 1.0)
*/
double bartlett_window_sample(int n, int N) {
if (N <= 1) return 1.0;
if (n < 0 || n >= N) return 0.0;
double m = (N - 1) / 2.0;
return 1.0 - fabs((n - m) / m);
}
Alternative Implementation (Piecewise)
The Bartlett window can also be expressed as a piecewise linear function [citation:1][citation:5]:
/**
* Compute Bartlett window coefficient using piecewise formula.
*
* @param n Sample index (0 to N-1)
* @param N Total window length
* @return Window coefficient (0.0 to 1.0)
*/
double bartlett_window_piecewise(int n, int N) {
if (N <= 1) return 1.0;
if (n < 0 || n >= N) return 0.0;
double x = (double)n / (N - 1);
if (x <= 0.5) {
return 2.0 * x; // Rising edge
} else {
return 2.0 * (1.0 - x); // Falling edge
}
}
Full Array Implementation
For better performance when the entire window is needed, the following function fills a pre-allocated array with all coefficients at once.
#include <math.h>
#include <stdlib.h>
/**
* Generate full Bartlett window array.
*
* @param N Length of the window (number of samples)
* @param out Output array of length N (must be pre-allocated)
*/
void bartlett_window_array(int N, double *out) {
if (N <= 0) return;
if (N == 1) {
out[0] = 1.0;
return;
}
double m = (N - 1) / 2.0;
for (int i = 0; i < N; i++) {
out[i] = 1.0 - fabs((i - m) / m);
}
}
Usage Example
The following example demonstrates how to apply the Bartlett window to a real signal.
#include <stdio.h>
#include <math.h>
#define N 512
int main() {
double window[N];
double signal[N];
double windowed[N];
// Generate Bartlett window
bartlett_window_array(N, window);
// Generate test signal (sinusoid with non-integer period)
for (int i = 0; i < N; i++) {
signal[i] = sin(2 * M_PI * i / 9.5);
}
// Apply window to signal
for (int i = 0; i < N; i++) {
windowed[i] = signal[i] * window[i];
}
// Print first 10 samples
printf("n\twindow\tsignal\t\twindowed\n");
for (int i = 0; i < 10; i++) {
printf("%d\t%.6f\t%.6f\t%.6f\n",
i, window[i], signal[i], windowed[i]);
}
return 0;
}
Expected Output (first 5 samples)
n\twindow\tsignal\t\twindowed
0\t0.000000\t0.000000\t0.000000
1\t0.003922\t0.647387\t0.002539
2\t0.007843\t0.998351\t0.007829
3\t0.011765\t0.907575\t0.010675
4\t0.015686\t0.460073\t0.007217
Implementation Notes
- Triangular shape: The Bartlett window is a simple triangular (tent-shaped) function that increases linearly to the centre and then decreases back to zero [citation:1][citation:5].
- Zero endpoints: Unlike the general triangular window, the Bartlett window always has zero values at both endpoints [citation:8][citation:9].
- Symmetry: The window is perfectly symmetric:
w[n] = w[N-1-n]for all n. - Sidelobe level: The first sidelobe is approximately -26 dB – significantly higher (worse) than Hann (-31 dB) or Hamming (-41 dB) [citation:10].
- Main lobe width: Approximately 8π/N normalized frequency – wider than rectangular but narrower than some cosine windows [citation:10].
- Convolution property: The Bartlett window can be obtained by convolving two rectangular windows of equal length [citation:7][citation:8].
- Edge case N=1: The only coefficient is 1.0.
- Precomputation: For real-time applications, precompute the window once and reuse it for multiple signal blocks.
Alternative: Precomputed Lookup Table
For embedded systems or when speed is critical, precompute the window at compile time:
#define N 512
static const double BARTLETT_WINDOW[N] = {
0.000000, 0.003922, 0.007843, 0.011765, 0.015686, /* ... */
/* Full table would be generated by a script */
};
// Usage
for (int i = 0; i < N; i++) {
windowed[i] = signal[i] * BARTLETT_WINDOW[i];
}
Comparison with Other Windows
The following table compares the Bartlett window with other common windows:
Window First sidelobe Main lobe width Endpoint value
Rectangular -13 dB ~0.04 1.0
Bartlett -26 dB ~0.07–0.08 0.0
Hann -31 dB ~0.08 0.0
Hamming -41 dB ~0.08 0.08
Blackman -58 dB ~0.12–0.13 0.0
Blackman–Harris -92 dB ~0.14–0.16 ~0.000
See also: The Bartlett Window in DSP | Triangular Window Implementation