Flat Top window (impl.)

This is the implementation guide for the Flat Top window. For theoretical background, including mathematical formula, frequency response, and spectral analysis, see the the article on Flat Top window.

All code examples are written in C for maximum performance and portability.


Direct Implementation

The following function computes the Flat Top window value for a given sample index n and total window length N. This is a 5-term cosine window designed for amplitude accuracy in spectral analysis.


#include <math.h>

/**
 * Compute Flat Top 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 flat_top_window_sample(int n, int N) {
    if (N <= 1) return 1.0;
    if (n < 0 || n >= N) return 0.0;
    
    /* Coefficients for Flat Top window (5-term) */
    const double a0 = 1.0;
    const double a1 = 1.93;
    const double a2 = 1.29;
    const double a3 = 0.388;
    const double a4 = 0.032;
    
    double angle = 2 * M_PI * n / (N - 1);
    
    return a0
           - a1 * cos(angle)
           + a2 * cos(2 * angle)
           - a3 * cos(3 * angle)
           + a4 * cos(4 * angle);
}

Alternative Implementation (Standard Form)

Many implementations use slightly different coefficients with positive-cosine form:


/**
 * Compute Flat Top window using standard positive-cosine coefficients.
 *
 * @param n     Sample index (0 to N-1)
 * @param N     Total window length
 * @return      Window coefficient
 */
double flat_top_window_standard(int n, int N) {
    if (N <= 1) return 1.0;
    if (n < 0 || n >= N) return 0.0;
    
    /* Standard Flat Top coefficients */
    const double a0 = 0.21557895;
    const double a1 = 0.41663158;
    const double a2 = 0.27726316;
    const double a3 = 0.08357895;
    const double a4 = 0.00694737;
    
    double angle = 2 * M_PI * n / (N - 1);
    
    return a0
           - a1 * cos(angle)
           + a2 * cos(2 * angle)
           - a3 * cos(3 * angle)
           + a4 * cos(4 * angle);
}

Alternative Implementation (Simplified 3-Term)

For applications where less accuracy is acceptable, a simplified 3-term version exists:


/**
 * Compute 3-term Flat Top window (simplified).
 *
 * @param n     Sample index (0 to N-1)
 * @param N     Total window length
 * @return      Window coefficient
 */
double flat_top_window_3term(int n, int N) {
    if (N <= 1) return 1.0;
    if (n < 0 || n >= N) return 0.0;
    
    const double a0 = 0.2810639;
    const double a1 = 0.5208972;
    const double a2 = 0.1980399;
    
    double angle = 2 * M_PI * n / (N - 1);
    
    return a0 - a1 * cos(angle) + a2 * cos(2 * angle);
}

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 Flat Top window array (5-term standard form).
 *
 * @param N     Length of the window (number of samples)
 * @param out   Output array of length N (must be pre-allocated)
 */
void flat_top_window_array(int N, double *out) {
    if (N <= 0) return;
    
    if (N == 1) {
        out[0] = 1.0;
        return;
    }
    
    const double a0 = 0.21557895;
    const double a1 = 0.41663158;
    const double a2 = 0.27726316;
    const double a3 = 0.08357895;
    const double a4 = 0.00694737;
    
    for (int i = 0; i < N; i++) {
        double angle = 2 * M_PI * i / (N - 1);
        out[i] = a0
               - a1 * cos(angle)
               + a2 * cos(2 * angle)
               - a3 * cos(3 * angle)
               + a4 * cos(4 * angle);
    }
}

Usage Example

The following example demonstrates how to apply the Flat Top window to a real signal.


#include <stdio.h>
#include <math.h>

#define N 64

int main() {
    double window[N];
    double signal[N];
    double windowed[N];
    
    /* Generate Flat Top window */
    flat_top_window_array(N, 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)

n    window      signal      windowed
0    -0.000060   0.000000    -0.000000
1    0.000019    0.647387    0.000012
2    0.000165    0.998351    0.000165
3    0.000475    0.907575    0.000431
4    0.001034    0.460073    0.000476

Implementation Notes

  • Amplitude accuracy: The Flat Top window is specifically designed for accurate amplitude measurements in spectral analysis •
  • 5-term cosine sum: Unlike other windows, Flat Top uses 5 cosine terms to achieve its flat passband response.
  • Negative coefficients: Some coefficients are negative, which can cause the window to go slightly negative at the ends (for N large enough).
  • Scallop loss: Flat Top window has extremely low scallop loss (~0.01 dB), meaning amplitude accuracy is excellent even when signal frequency falls between FFT bins .
  • Widest main lobe: This window has the widest main lobe of any standard window (∼0.20 normalized frequency).
  • Sidelobe level: Approximately -80 dB to -90 dB – excellent suppression.
  • Symmetry: The window is perfectly symmetric.
  • 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

static const double FLAT_TOP_WINDOW[N] = {
    -0.000060, 0.000019, 0.000165, 0.000475, 0.001034, /* ... */
    /* Full table would be generated by a script */
};

/* Usage */
for (int i = 0; i < N; i++) {
    windowed[i] = signal[i] * FLAT_TOP_WINDOW[i];
}

Flat Top vs Other Windows

The Flat Top window is unique in its focus on amplitude accuracy rather than resolution:

Window              Scallop loss    Main lobe width    Best for
Rectangular         3.92 dB         ~0.04              Resolution
Hann                1.42 dB         ~0.08              General purpose
Hamming             1.78 dB         ~0.08              General purpose
Blackman            1.10 dB         ~0.12              Leakage suppression
Flat Top            ~0.01 dB        ~0.20              Amplitude accuracy

Comparison of Flat Top Coefficients

There are several common Flat Top coefficient sets:

Coefficient    Standard      Alternate      Description
a0             0.21557895    1.0            Center coefficient
a1             0.41663158    1.93           First cosine term
a2             0.27726316    1.29           Second cosine term
a3             0.08357895    0.388          Third cosine term
a4             0.00694737    0.032          Fourth cosine term

Practical Applications

Application                       Why Flat Top?
Calibration measurements          Requires accurate amplitude readings
Calibration systems               Minimizes amplitude errors
Testing                          Needs precise peak detection
Metrology                        Demands highest accuracy

See also: The Flat Top Window in DSP