diff options
author | Martin Braun <martin.braun@ettus.com> | 2020-08-13 17:06:36 +0200 |
---|---|---|
committer | Martin Braun <martin@gnuradio.org> | 2020-08-14 04:11:26 -0700 |
commit | 487da72790363937c20f93500a1883ac0fc93582 (patch) | |
tree | 5db582d67942a606458d573a7181f0b7d8ccb7c1 /gr-fft/lib/window.cc | |
parent | 3adb522b0ddf6736605beea797016353a1d4a2b8 (diff) |
fft: window: Allow normalizing windows
In some applications, it is useful to generate windows that have unit
power. The boxcar window (rectangle) is always of unit power, but the
other windows are not, leading to apple-to-oranges comparisons, e.g.,
when switching between window types in a live spectrum estimation
application.
Diffstat (limited to 'gr-fft/lib/window.cc')
-rw-r--r-- | gr-fft/lib/window.cc | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/gr-fft/lib/window.cc b/gr-fft/lib/window.cc index 430b435bd4..faebdd4f56 100644 --- a/gr-fft/lib/window.cc +++ b/gr-fft/lib/window.cc @@ -14,6 +14,8 @@ #include <gnuradio/fft/window.h> #include <gnuradio/math.h> +#include <algorithm> +#include <numeric> #include <stdexcept> namespace gr { @@ -355,8 +357,27 @@ std::vector<float> window::gaussian(int ntaps, float sigma) return taps; } -std::vector<float> window::build(win_type type, int ntaps, double beta) +std::vector<float> +window::build(win_type type, int ntaps, double beta, const bool normalize) { + // If we want a normalized window, we get a non-normalized one first, then + // normalize it here: + if (normalize) { + auto win = build(type, ntaps, beta, false); + const double pwr_acc = // sum(win**2) / len(win) + std::accumulate(win.cbegin(), + win.cend(), + 0.0, + [](const double a, const double b) { return a + b * b; }) / + win.size(); + const float norm_fac = static_cast<float>(std::sqrt(pwr_acc)); + std::transform(win.begin(), win.end(), win.begin(), [norm_fac](const float tap) { + return tap / norm_fac; + }); + return win; + } + + // Create non-normalized window: switch (type) { case WIN_RECTANGULAR: return rectangular(ntaps); |