Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/comms/window_designer: add normalization option #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions window/WindowDesigner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <Pothos/Framework.hpp>
#include <Poco/Logger.h>
#include <cmath>
#include <complex>
#include <algorithm>
#include <iostream>
Expand Down Expand Up @@ -44,10 +45,19 @@ using spuce::design_window;
* |default 51
* |widget SpinBox(minimum=1)
*
* |param normalization[Normalization] The option to normalize the output taps.
* When normalized, the output taps will be the window taps divided by the
* selected metric computed over the window taps.
* |option [None] "NONE"
* |option [Sum] "SUM"
* |option [Power] "POWER"
* |default "NONE"
*
* |factory /comms/window_designer()
* |setter setWindowType(window)
* |setter setWindowArgs(windowArgs)
* |setter setNumTaps(numTaps)
* |setter setNormalization(normalization)
**********************************************************************/
class WindowDesigner : public Pothos::Block
{
Expand All @@ -59,14 +69,17 @@ class WindowDesigner : public Pothos::Block

WindowDesigner(void):
_windowType("hann"),
_numTaps(51)
_numTaps(51),
_normalization("NONE")
{
this->registerCall(this, POTHOS_FCN_TUPLE(WindowDesigner, setWindowType));
this->registerCall(this, POTHOS_FCN_TUPLE(WindowDesigner, windowType));
this->registerCall(this, POTHOS_FCN_TUPLE(WindowDesigner, setWindowArgs));
this->registerCall(this, POTHOS_FCN_TUPLE(WindowDesigner, windowArgs));
this->registerCall(this, POTHOS_FCN_TUPLE(WindowDesigner, setNumTaps));
this->registerCall(this, POTHOS_FCN_TUPLE(WindowDesigner, numTaps));
this->registerCall(this, POTHOS_FCN_TUPLE(WindowDesigner, setNormalization));
this->registerCall(this, POTHOS_FCN_TUPLE(WindowDesigner, normalization));
this->registerSignal("tapsChanged");
this->recalculate();
}
Expand Down Expand Up @@ -104,6 +117,17 @@ class WindowDesigner : public Pothos::Block
return _numTaps;
}

void setNormalization(const std::string &normalization)
{
_normalization = normalization;
this->recalculate();
}

std::string normalization(void) const
{
return _normalization;
}

void activate(void)
{
this->recalculate();
Expand All @@ -116,6 +140,7 @@ class WindowDesigner : public Pothos::Block
std::string _windowType;
std::vector<double> _windowArgs;
size_t _numTaps;
std::string _normalization;
};

void WindowDesigner::recalculate(void)
Expand All @@ -126,7 +151,33 @@ void WindowDesigner::recalculate(void)
if (_numTaps == 0) throw Pothos::Exception("WindowDesigner()", "num taps must be positive");

//generate the window
const auto window = design_window(_windowType, _numTaps, _windowArgs.empty()?0.0:_windowArgs.at(0));
auto window = design_window(_windowType, _numTaps, _windowArgs.empty()?0.0:_windowArgs.at(0));

if (_normalization == "SUM")
{
double accumulator = 0.0;
for (size_t n = 0; n < _numTaps; n++)
{
accumulator += window[n];
}
for (size_t n = 0; n < _numTaps; n++)
{
window[n] /= accumulator;
}
}
else if (_normalization == "POWER")
{
double power = 0.0;
for (size_t n = 0; n < _numTaps; n++)
{
power += window[n]*window[n];
}
power = std::sqrt(power/_numTaps);
for (size_t n = 0; n < _numTaps; n++)
{
window[n] /= power;
}
}

this->emitSignal("tapsChanged", window);
}
Expand Down