/* Copyright 2018 Michal Fratczak This file is part of habdec. habdec 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 (at your option) any later version. habdec 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 habdec. If not, see . */ #pragma once #include #include #include #include #include namespace habdec { template class Decimator { public: Decimator() {}; Decimator(int decimation_factor, const Tt* p_taps, const size_t taps_size); void setInput(const Ts* p_in, const size_t in_size); void setOutput(Ts* p_out); void setFilter(const Tt* p_taps, const size_t taps_size); size_t operator()(); void setFactor(const int i_f) {decimation_factor_ = i_f;} int factor() const {return decimation_factor_;} private: int decimation_factor_ = 0; const Ts* p_in_ = 0; size_t p_in_size_ = 0; Ts* p_out_ = 0; //size is p_in_size_/decimation_factor_ std::vector p_buff_; std::vector p_taps_; size_t decimated_samples_count_ = 0; }; template Decimator::Decimator(int decimation_factor, const Tt* p_taps, const size_t taps_size) { setFactor(decimation_factor); setFilter(p_taps, taps_size); } template void Decimator::setInput(const Ts* p_in, const size_t in_size) { p_in_ = p_in; p_in_size_ = in_size; const size_t new_buff_size = p_in_size_ + p_taps_.size() + decimation_factor_; if(p_buff_.size() < new_buff_size) { p_buff_.resize(new_buff_size); memset( p_buff_.data(), 0, sizeof(Ts) * p_taps_.size() ); } } template void Decimator::setFilter(const Tt *p_taps, const size_t taps_size) { p_taps_.resize(taps_size); memcpy( (void*) p_taps_.data(), p_taps, taps_size * sizeof(Tt)); } template void Decimator::setOutput(Ts* p_out) { p_out_ = p_out; } template size_t Decimator::operator()() { if(!decimation_factor_ || !p_in_ || !p_out_ || !p_buff_.size()) { std::cout<<"Decimator not fully initialised"<