1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2025-06-18 23:37:05 +01:00

general interface for filtering data

This commit is contained in:
2024-01-14 20:23:02 -03:00
parent 6739019c83
commit 145155f733
5 changed files with 216 additions and 19 deletions

View File

@ -0,0 +1,87 @@
/*
* DataFilter.cpp, part of LatAnalyze 3
*
* Copyright (C) 2013 - 2020 Antonin Portelli
*
* LatAnalyze 3 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.
*
* LatAnalyze 3 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 LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
*/
#include <LatAnalyze/Physics/DataFilter.hpp>
#include <LatAnalyze/includes.hpp>
#include <LatAnalyze/Numerical/DWT.hpp>
using namespace std;
using namespace Latan;
DataFilter::DataFilter(const vector<double> &filter, const bool downsample)
: filter_(filter), downsample_(downsample)
{}
template <typename MatType>
void filter(MatType &out, const MatType &in, const vector<double> &filter,
const bool downsample, MatType &buf)
{
if (!downsample)
{
out.resizeLike(in);
DWT::filterConvolution(out, in, filter, filter.size()/2);
}
else
{
out.resize(in.rows()/2, in.cols());
buf.resizeLike(in);
DWT::filterConvolution(buf, in, filter, filter.size()/2);
DWT::downsample(out, buf);
}
}
void DataFilter::operator()(DVec &out, const DVec &in)
{
filter(out, in, filter_, downsample_, vBuf_);
}
void DataFilter::operator()(DMat &out, const DMat &in)
{
filter(out, in, filter_, downsample_, mBuf_);
}
void DataFilter::operator()(DMatSample &out, const DMatSample &in)
{
FOR_STAT_ARRAY(in, s)
{
(*this)(out[s], in[s]);
}
}
LaplaceDataFilter::LaplaceDataFilter(const bool downsample)
: DataFilter({1., -2. , 1.}, downsample)
{}
void LaplaceDataFilter::operator()(DVec &out, const DVec &in, const double lambda)
{
filter_[1] = -2. - lambda;
DataFilter::operator()(out, in);
}
void LaplaceDataFilter::operator()(DMat &out, const DMat &in, const double lambda)
{
filter_[1] = -2. - lambda;
DataFilter::operator()(out, in);
}
void LaplaceDataFilter::operator()(DMatSample &out, const DMatSample &in, const double lambda)
{
filter_[1] = -2. - lambda;
DataFilter::operator()(out, in);
}

View File

@ -0,0 +1,58 @@
/*
* DataFilter.hpp, part of LatAnalyze 3
*
* Copyright (C) 2013 - 2020 Antonin Portelli
*
* LatAnalyze 3 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.
*
* LatAnalyze 3 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 LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Latan_DataFilter_hpp_
#define Latan_DataFilter_hpp_
#include <LatAnalyze/Global.hpp>
#include <LatAnalyze/Statistics/MatSample.hpp>
BEGIN_LATAN_NAMESPACE
class DataFilter
{
public:
// constructor
DataFilter(const std::vector<double> &filter, const bool downsample = false);
// filtering
virtual void operator()(DVec &out, const DVec &in);
virtual void operator()(DMat &out, const DMat &in);
virtual void operator()(DMatSample &out, const DMatSample &in);
protected:
std::vector<double> filter_;
private:
bool downsample_;
DVec vBuf_;
DMat mBuf_;
};
class LaplaceDataFilter: public DataFilter
{
public:
// constructor
LaplaceDataFilter(const bool downsample = false);
// filtering
virtual void operator()(DVec &out, const DVec &in, const double lambda = 0.);
virtual void operator()(DMat &out, const DMat &in, const double lambda = 0.);
virtual void operator()(DMatSample &out, const DMatSample &in, const double lambda = 0.);
};
END_LATAN_NAMESPACE
#endif // Latan_DataFilter_hpp_