1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-14 01:45:35 +00:00

DataFilter cleanup and Laplacian filter with CDR optimisation

This commit is contained in:
Antonin Portelli 2024-01-19 22:39:52 -03:00
parent 83e09b82fc
commit b938a855e3
2 changed files with 97 additions and 20 deletions

View File

@ -24,10 +24,15 @@
using namespace std; using namespace std;
using namespace Latan; using namespace Latan;
/******************************************************************************
* DataFilter implementation *
******************************************************************************/
// constructor ////////////////////////////////////////////////////////////////
DataFilter::DataFilter(const vector<double> &filter, const bool downsample) DataFilter::DataFilter(const vector<double> &filter, const bool downsample)
: filter_(filter), downsample_(downsample) : filter_(filter), downsample_(downsample)
{} {}
// filtering //////////////////////////////////////////////////////////////////
template <typename MatType> template <typename MatType>
void filter(MatType &out, const MatType &in, const vector<double> &filter, void filter(MatType &out, const MatType &in, const vector<double> &filter,
const bool downsample, MatType &buf) const bool downsample, MatType &buf)
@ -56,18 +61,15 @@ void DataFilter::operator()(DMat &out, const DMat &in)
filter(out, in, filter_, downsample_, mBuf_); filter(out, in, filter_, downsample_, mBuf_);
} }
void DataFilter::operator()(DMatSample &out, const DMatSample &in) /******************************************************************************
{ * LaplaceDataFilter implementation *
FOR_STAT_ARRAY(in, s) ******************************************************************************/
{ // constructor ////////////////////////////////////////////////////////////////
(*this)(out[s], in[s]);
}
}
LaplaceDataFilter::LaplaceDataFilter(const bool downsample) LaplaceDataFilter::LaplaceDataFilter(const bool downsample)
: DataFilter({1., -2. , 1.}, downsample) : DataFilter({1., -2. , 1.}, downsample)
{} {}
// filtering //////////////////////////////////////////////////////////////////
void LaplaceDataFilter::operator()(DVec &out, const DVec &in, const double lambda) void LaplaceDataFilter::operator()(DVec &out, const DVec &in, const double lambda)
{ {
filter_[1] = -2. - lambda; filter_[1] = -2. - lambda;
@ -79,9 +81,3 @@ void LaplaceDataFilter::operator()(DMat &out, const DMat &in, const double lambd
filter_[1] = -2. - lambda; filter_[1] = -2. - lambda;
DataFilter::operator()(out, in); 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

@ -21,19 +21,26 @@
#define Latan_DataFilter_hpp_ #define Latan_DataFilter_hpp_
#include <LatAnalyze/Global.hpp> #include <LatAnalyze/Global.hpp>
#include <LatAnalyze/Core/Math.hpp>
#include <LatAnalyze/Statistics/StatArray.hpp>
#include <LatAnalyze/Statistics/MatSample.hpp> #include <LatAnalyze/Statistics/MatSample.hpp>
#include <LatAnalyze/Numerical/Minimizer.hpp>
BEGIN_LATAN_NAMESPACE BEGIN_LATAN_NAMESPACE
/******************************************************************************
* Generic convolution filter class *
******************************************************************************/
class DataFilter class DataFilter
{ {
public: public:
// constructor // constructor
DataFilter(const std::vector<double> &filter, const bool downsample = false); DataFilter(const std::vector<double> &filter, const bool downsample = false);
// filtering // filtering
virtual void operator()(DVec &out, const DVec &in); void operator()(DVec &out, const DVec &in);
virtual void operator()(DMat &out, const DMat &in); void operator()(DMat &out, const DMat &in);
virtual void operator()(DMatSample &out, const DMatSample &in); template <typename MatType, Index o>
void operator()(StatArray<MatType, o> &out, const StatArray<MatType, o> &in);
protected: protected:
std::vector<double> filter_; std::vector<double> filter_;
private: private:
@ -42,17 +49,91 @@ private:
DMat mBuf_; DMat mBuf_;
}; };
/******************************************************************************
* Laplacian filter class *
******************************************************************************/
class LaplaceDataFilter: public DataFilter class LaplaceDataFilter: public DataFilter
{ {
public: public:
// constructor // constructor
LaplaceDataFilter(const bool downsample = false); LaplaceDataFilter(const bool downsample = false);
// filtering // filtering
virtual void operator()(DVec &out, const DVec &in, const double lambda = 0.); void operator()(DVec &out, const DVec &in, const double lambda = 0.);
virtual void operator()(DMat &out, const DMat &in, const double lambda = 0.); void operator()(DMat &out, const DMat &in, const double lambda = 0.);
virtual void operator()(DMatSample &out, const DMatSample &in, const double lambda = 0.); template <typename MatType, Index o>
void operator()(StatArray<MatType, o> &out, const StatArray<MatType, o> &in,
const double lambda = 0.);
// correlation optimisation
template <typename MatType, Index o>
double optimiseCdr(const StatArray<MatType, o> &data, Minimizer &min,
const unsigned int nPass = 3);
}; };
/******************************************************************************
* DataFilter class template implementation *
******************************************************************************/
// filtering //////////////////////////////////////////////////////////////////
template <typename MatType, Index o>
void DataFilter::operator()(StatArray<MatType, o> &out, const StatArray<MatType, o> &in)
{
FOR_STAT_ARRAY(in, s)
{
(*this)(out[s], in[s]);
}
}
/******************************************************************************
* LaplaceDataFilter class template implementation *
******************************************************************************/
// filtering //////////////////////////////////////////////////////////////////
template <typename MatType, Index o>
void LaplaceDataFilter::operator()(StatArray<MatType, o> &out,
const StatArray<MatType, o> &in, const double lambda)
{
FOR_STAT_ARRAY(in, s)
{
(*this)(out[s], in[s], lambda);
}
}
// correlation optimisation ///////////////////////////////////////////////////
template <typename MatType, Index o>
double LaplaceDataFilter::optimiseCdr(const StatArray<MatType, o> &data,
Minimizer &min, const unsigned int nPass)
{
StatArray<MatType, o> fdata(data.size());
DVec init(1);
double reg, prec;
DoubleFunction cdr([&data, &fdata, this](const double *x)
{
double res;
(*this)(fdata, data, x[0]);
res = Math::cdr(fdata.correlationMatrix());
return res;
}, 1);
min.setLowLimit(0., -0.1);
min.setHighLimit(0., 100000.);
init(0) = 0.1;
min.setInit(init);
prec = 0.1;
min.setPrecision(prec);
reg = min(cdr)(0);
for (unsigned int pass = 0; pass < nPass; pass++)
{
min.setLowLimit(0., (1.-10.*prec)*reg);
min.setHighLimit(0., (1.+10.*prec)*reg);
init(0) = reg;
min.setInit(init);
prec *= 0.1;
min.setPrecision(prec);
reg = min(cdr)(0);
}
return reg;
}
END_LATAN_NAMESPACE END_LATAN_NAMESPACE
#endif // Latan_DataFilter_hpp_ #endif // Latan_DataFilter_hpp_