/** A sliding/moving window. * * @file * @author Steffen Vogel * @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 . *********************************************************************************/ #pragma once #include #include #include namespace villas { namespace dsp { template class CosineWindow : public Window { public: using size_type = typename Window::size_type; protected: std::vector 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(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 using HannWindow = CosineWindow; template using HammingWindow = CosineWindow; template using FlattopWindow = CosineWindow; // based on MATLAB coeffs template using NuttallWindow = CosineWindow; template using BlackmanWindow = CosineWindow; } /* namespace dsp */ } /* namespace villas */