generateWindow function

Float32List generateWindow(
  1. int length, {
  2. WindowType type = WindowType.hann,
  3. bool periodic = true,
})

Returns a window of length for the selected type.

By default this uses the periodic form, which is typically what you want for FFT/STFT analysis. Set periodic to false to generate the symmetric form instead.

Implementation

Float32List generateWindow(
  int length, {
  WindowType type = WindowType.hann,
  bool periodic = true,
}) {
  _checkWindowLength(length);

  final window = Float32List(length);
  if (length == 1) {
    window[0] = 1.0;
    return window;
  }

  if (type == WindowType.rect) {
    for (var i = 0; i < length; i++) {
      window[i] = 1.0;
    }
    return window;
  }

  final denom = periodic ? length : length - 1;
  final phaseScale = (2.0 * math.pi) / denom;

  for (var n = 0; n < length; n++) {
    final phase = phaseScale * n;
    switch (type) {
      case WindowType.rect:
        window[n] = 1.0;
      case WindowType.hann:
        window[n] = 0.5 - 0.5 * math.cos(phase);
      case WindowType.hamming:
        window[n] = 0.54 - 0.46 * math.cos(phase);
      case WindowType.blackman:
        window[n] = 0.42 - 0.5 * math.cos(phase) + 0.08 * math.cos(2.0 * phase);
    }
  }

  return window;
}