This is the implementation guide for the Gaussian window. For theoretical background, including mathematical formula, frequency response, and spectral analysis, see the the article on Gaussian window.
All code examples are written in C for maximum performance and portability.
Direct Implementation
The following function computes the Gaussian window value for a given sample index n and total window length N. The Gaussian window uses a σ (sigma) parameter that controls the standard deviation of the Gaussian curve.
#include <math.h>
/**
* Compute Gaussian window coefficient for a single sample.
*
* @param n Sample index (0 to N-1)
* @param N Total window length
* @param sigma Standard deviation parameter (sigma > 0)
* Larger sigma = wider main lobe, better sidelobe suppression
* Typical sigma: 0.3 to 0.5
* @return Window coefficient (0.0 to 1.0)
*/
double gaussian_window_sample(int n, int N, double sigma) {
if (N <= 1) return 1.0;
if (n < 0 || n >= N) return 0.0;
if (sigma <= 0.0) return 1.0;
double m = (N - 1) / 2.0;
double x = (n - m) / (sigma * m);
return exp(-0.5 * x * x);
}
Alternative Implementation (with truncation)
The Gaussian window is theoretically infinite. In practice, the window is truncated at the ends where values become negligible.
/**
* Compute Gaussian window with truncation threshold.
*
* @param n Sample index (0 to N-1)
* @param N Total window length
* @param sigma Standard deviation parameter
* @param truncate Threshold (e.g., 1e-6) – values below are set to zero
* @return Window coefficient
*/
double gaussian_window_truncated(int n, int N, double sigma, double truncate) {
double val = gaussian_window_sample(n, N, sigma);
if (val < truncate) return 0.0;
return val;
}
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
#include
/**
* Generate full Gaussian window array.
*
* @param N Length of the window (number of samples)
* @param sigma Standard deviation parameter
* @param out Output array of length N (must be pre-allocated)
*/
void gaussian_window_array(int N, double sigma, double *out) {
if (N <= 0) return;
if (N == 1 || sigma <= 0.0) {
out[0] = 1.0;
return;
}
double m = (N - 1) / 2.0;
double denom = sigma * m;
for (int i = 0; i < N; i++) {
double x = (i - m) / denom;
out[i] = exp(-0.5 * x * x);
}
}
Helper Function: Alpha Parameter
Some implementations use an α (alpha) parameter instead of sigma:
/**
* Compute Gaussian window using alpha parameter.
* Alpha controls the width of the Gaussian curve.
*
* @param n Sample index (0 to N-1)
* @param N Total window length
* @param alpha Alpha parameter (alpha > 0)
* alpha = 2.5 → typical value
* @return Window coefficient
*/
double gaussian_window_alpha(int n, int N, double alpha) {
if (N <= 1) return 1.0;
if (n < 0 || n >= N) return 0.0;
if (alpha <= 0.0) return 1.0;
double m = (N - 1) / 2.0;
double x = (n - m) / m;
return exp(-0.5 * alpha * alpha * x * x);
}
Usage Example
The following example demonstrates how to apply the Gaussian window to a real signal with different sigma values.
#include
#include
#define N 64
#define SIGMA 0.4
int main() {
double window[N];
double signal[N];
double windowed[N];
// Generate Gaussian window (sigma = 0.4)
gaussian_window_array(N, SIGMA, window);
// Generate test signal (sinusoid)
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 window signal windowed\n");
for (int i = 0; i < 10; i++) {
printf("%d %.6f %.6f %.6f\n",
i, window[i], signal[i], windowed[i]);
}
return 0;
}
Expected Output (first 5 samples, N=64, σ=0.4)
n window signal windowed 0 0.000335 0.000000 0.000000 1 0.000444 0.647387 0.000287 2 0.000734 0.998351 0.000733 3 0.001197 0.907575 0.001087 4 0.001924 0.460073 0.000885
Implementation Notes
- Gaussian shape: The window follows a Gaussian (normal) distribution curve, providing the optimal time-frequency concentration.
- Sigma parameter:
- σ = 0.3 → Narrow main lobe, higher sidelobes (∼-30 dB)
- σ = 0.4 → Balanced (∼-40 dB)
- σ = 0.5 → Wider main lobe, lower sidelobes (∼-55 dB)
- σ = 0.6 → Very wide main lobe, excellent suppression (∼-70 dB)
- No zero endpoints: The Gaussian window never reaches exactly zero, only approaches it asymptotically.
- Symmetry: The window is perfectly symmetric:
w[n] = w[N-1-n]for all n. - Truncation: In practice, the window is truncated where values become negligible (typically < 1e-6).
- Fourier transform: The Fourier transform of a Gaussian window is also Gaussian – unique property!
- Uncertainty principle: Gaussian window achieves the minimum possible time-frequency product.
- 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 64
#define SIGMA 0.4
static const double GAUSSIAN_WINDOW[N] = {
0.000335, 0.000444, 0.000734, 0.001197, 0.001924, /* ... */
/* Full table would be generated by a script */
};
// Usage
for (int i = 0; i < N; i++) {
windowed[i] = signal[i] * GAUSSIAN_WINDOW[i];
}
Gaussian Window vs Other Windows
The Gaussian window is unique because the sigma parameter provides continuous control over performance:
σ value Sidelobes (dB) Main lobe width Notes 0.30 -30 ~0.06 Narrow main lobe 0.35 -38 ~0.07 0.40 -42 ~0.08 Similar to Hann 0.45 -48 ~0.09 Similar to Hamming 0.50 -55 ~0.10 Similar to Blackman 0.55 -62 ~0.12 0.60 -70 ~0.14 Excellent suppression
Comparison with Kaiser Window
Both Gaussian and Kaiser windows are adjustable:
Window Parameter Control Best for Gaussian σ Time-frequency uncertainty Gabor analysis, spectrograms Kaiser β Sidelobe level General purpose, filter design
See also: The Gaussian Window in DSP | Kaiser Window Implementation | Gabor Transform