mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
window: add support for cosine moving windows
This commit is contained in:
parent
765b195e5f
commit
b47e1294f3
2 changed files with 152 additions and 27 deletions
|
@ -23,57 +23,85 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <villas/utils.hpp>
|
||||
#include <ranges>
|
||||
#include <deque>
|
||||
|
||||
namespace villas {
|
||||
namespace dsp {
|
||||
|
||||
template<typename T>
|
||||
class Window {
|
||||
template<typename T, typename Container = std::deque<T>>
|
||||
class Window : protected Container {
|
||||
|
||||
public:
|
||||
typedef typename std::vector<T>::size_type size_type;
|
||||
using iterator = typename Container::iterator;
|
||||
using size_type = typename Container::size_type;
|
||||
|
||||
protected:
|
||||
std::vector<T> data;
|
||||
|
||||
T init;
|
||||
virtual
|
||||
T filter(T in, size_type i) const
|
||||
{
|
||||
return in;
|
||||
}
|
||||
|
||||
size_type steps;
|
||||
size_type mask;
|
||||
size_type pos;
|
||||
class transform_iterator : public Container::const_iterator {
|
||||
protected:
|
||||
const Window *window;
|
||||
|
||||
using base_iterator = typename Container::const_iterator;
|
||||
|
||||
public:
|
||||
transform_iterator(const Window<T> *w) :
|
||||
base_iterator(w->Container::begin()),
|
||||
window(w)
|
||||
{ }
|
||||
|
||||
T operator*()
|
||||
{
|
||||
auto i = (*this) - window->begin();
|
||||
const auto &v = base_iterator::operator*();
|
||||
|
||||
return window->filter(v, i);
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
Window(size_type s = 0, T i = 0) :
|
||||
init(i),
|
||||
steps(s)
|
||||
{
|
||||
size_type len = LOG2_CEIL(s);
|
||||
|
||||
/* Allocate memory for circular history buffer */
|
||||
data = std::vector<T>(len, i);
|
||||
|
||||
pos = len;
|
||||
mask = len - 1;
|
||||
}
|
||||
Window(size_type l = 0, T i = 0) :
|
||||
std::deque<T>(l, i)
|
||||
{ }
|
||||
|
||||
T update(T in)
|
||||
{
|
||||
T out = data[(pos - steps) & mask];
|
||||
Container::push_back(in);
|
||||
|
||||
data[pos++ & mask] = in;
|
||||
auto out = (*this)[0];
|
||||
|
||||
Container::pop_front();
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
size_type getLength() const
|
||||
// Expose a limited number of functions from deque
|
||||
|
||||
using Container::size;
|
||||
using Container::end;
|
||||
|
||||
transform_iterator begin() const noexcept
|
||||
{
|
||||
return steps;
|
||||
return transform_iterator(this);
|
||||
}
|
||||
|
||||
T operator[](int i) const
|
||||
T operator[](size_type i) const noexcept
|
||||
{
|
||||
return data[(pos + i) & mask];
|
||||
auto v = Container::operator[](i);
|
||||
|
||||
return filter(v, i);
|
||||
}
|
||||
|
||||
virtual
|
||||
T getCorrectionFactor() const
|
||||
{
|
||||
return 1.0;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
97
common/include/villas/dsp/window_cosine.hpp
Normal file
97
common/include/villas/dsp/window_cosine.hpp
Normal file
|
@ -0,0 +1,97 @@
|
|||
/** A sliding/moving window.
|
||||
*
|
||||
* @file
|
||||
* @author Steffen Vogel <svogel2@eonerc.rwth-aachen.de>
|
||||
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
||||
* @license GNU General Public License (version 3)
|
||||
*
|
||||
* VILLAScommon
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*********************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <villas/dsp/window.hpp>
|
||||
|
||||
namespace villas {
|
||||
namespace dsp {
|
||||
|
||||
template<typename T, double a0 = 1.0, double a1 = 0.0, double a2 = 0.0, double a3 = 0.0, double a4 = 0.0>
|
||||
class CosineWindow : public Window<T> {
|
||||
|
||||
public:
|
||||
using size_type = typename Window<T>::size_type;
|
||||
|
||||
protected:
|
||||
std::vector<T> coefficients;
|
||||
|
||||
T correctionFactor;
|
||||
|
||||
virtual
|
||||
T filter(T in, size_type i) const
|
||||
{
|
||||
return in * coefficients[i];
|
||||
}
|
||||
|
||||
public:
|
||||
CosineWindow(size_type len, T i = 0) :
|
||||
Window<T>(len, i),
|
||||
coefficients(len),
|
||||
correctionFactor(0)
|
||||
{
|
||||
for (unsigned i = 0; i < len; i++) {
|
||||
coefficients[i] = a0
|
||||
- a1 * cos(2 * M_PI * i / len)
|
||||
+ a2 * cos(4 * M_PI * i / len)
|
||||
- a3 * cos(6 * M_PI * i / len)
|
||||
+ a4 * cos(8 * M_PI * i / len);
|
||||
|
||||
correctionFactor += coefficients[i];
|
||||
}
|
||||
|
||||
correctionFactor /= len;
|
||||
}
|
||||
|
||||
|
||||
virtual
|
||||
T getCorrectionFactor() const
|
||||
{
|
||||
return correctionFactor;
|
||||
}
|
||||
};
|
||||
|
||||
// From: https://en.wikipedia.org/wiki/Window_function#Cosine-sum_windows
|
||||
|
||||
template<typename T>
|
||||
using HannWindow = CosineWindow<T, 0.5, 0.5>;
|
||||
|
||||
template<typename T>
|
||||
using HammingWindow = CosineWindow<T, 25./46, 1 - 25./46>;
|
||||
|
||||
template<typename T>
|
||||
using FlattopWindow = CosineWindow<T, 0.21557895, 0.41663158, 0.277263158, 0.083578947, 0.006947368>; // based on MATLAB coeffs
|
||||
|
||||
template<typename T>
|
||||
using NuttallWindow = CosineWindow<T, 0.355768, 0.487396, 0.144232, 0.012604>;
|
||||
|
||||
template<typename T>
|
||||
using BlackmanWindow = CosineWindow<T, 0.3635819, 0.4891775, 0.1365995, 0.0106411>;
|
||||
|
||||
} /* namespace dsp */
|
||||
} /* namespace villas */
|
Loading…
Add table
Reference in a new issue