Commit e27c65f9 authored by Pavel Vainerman's avatar Pavel Vainerman

(test): добавил основные тесты для DigitalFilter (но не всё пока-что).

parent aaf5a790
......@@ -68,8 +68,8 @@ class DigitalFilter
// Вторая ступень фильтра, математическая реализация RC фильтра
double secondLevel( double val );
double Ti; // Постоянная времени для апериодического звена в милисекундах
double val; // Текущее значение второй ступени фильтра
double Ti; // Постоянная времени для апериодического звена в милисекундах
double val; // Текущее значение второй ступени фильтра
double M; // Среднее арифметическое
double S; // Среднеквадратичное отклонение
PassiveTimer tmr;
......@@ -80,6 +80,7 @@ class DigitalFilter
typedef std::vector<int> MedianVector;
MedianVector mvec;
bool mvec_sorted; // флаг, что mvec остортирован (заполнен)
typedef std::vector<double> Coeff;
Coeff w; // Вектор коэффициентов для filterIIR
......
......@@ -19,6 +19,7 @@ DigitalFilter::DigitalFilter( unsigned int bufsize, double T, double lsq,
tmr(UniSetTimer::WaitUpTime),
maxsize(bufsize),
mvec(bufsize),
mvec_sorted(false),
w(bufsize),
lsparam(lsq),
ls(0),
......@@ -107,7 +108,7 @@ double DigitalFilter::firstLevel()
double val = 0; // Конечное среднее значение
for( auto &i: buf )
{
if( fabs(M-i) > S*2 )
if( fabs(M-i) < S*2 ) // откидываем
{
val += i;
n++;
......@@ -204,12 +205,20 @@ int DigitalFilter::median( int newval )
mvec.assign(buf.begin(),buf.end());
sort(mvec.begin(),mvec.end());
mvec_sorted = true;
return mvec[maxsize/2];
}
//--------------------------------------------------------------------------
int DigitalFilter::currentMedian()
{
if( !mvec_sorted )
{
mvec.assign(buf.begin(),buf.end());
sort(mvec.begin(),mvec.end());
// mvec_sorted = true; // специально не выставляю, чтобы если данные добавляются через add(), то тут надо каждый раз пересчитыать..
}
return mvec[maxsize/2];
}
//--------------------------------------------------------------------------
......
......@@ -2,7 +2,7 @@ SUBDIR=SMemoryTest
noinst_PROGRAMS = tests tests_with_conf tests_with_sm
tests_SOURCES = tests.cc calibration.cc digitalfilter.cc
tests_SOURCES = tests.cc digitalfilter.cc
tests_LDADD = $(top_builddir)/lib/libUniSet2.la $(top_builddir)/extensions/lib/libUniSet2Extensions.la
tests_CPPFLAGS = -I$(top_builddir)/include -I$(top_builddir)/extensions/include -I$(includedir)/Catch
......
......@@ -63,85 +63,3 @@ TEST_CASE("Calibration","[calibration]")
REQUIRE( cal->getRightRaw() == 1000 );
}
}
#if 0
#include <iostream>
#include "Exceptions.h"
#include "UniXML.h"
#include "UniSetTypes.h"
#include "Configuration.h"
#include "Extensions.h"
#include "Calibration.h"
#include <vector>
#include <deque>
using namespace std;
using namespace UniSetTypes;
using namespace UniSetExtensions;
int main( int argc, const char** argv )
{
try
{
uniset_init(argc,argv,"test.xml");
Calibration* cal = buildCalibrationDiagram("testcal");
cout << "diagram: " << cal << endl;
cout << "-1500 --> " << cal->getValue(-1500) << endl;
cout << "-933 --> " << cal->getValue(-933) << endl;
cout << "-844 --> " << cal->getValue(-844) << endl;
cout << "-540 --> " << cal->getValue(-540) << endl;
cout << "-320 --> " << cal->getValue(-320) << endl;
cout << "-200 --> " << cal->getValue(-200) << endl;
// проверка кэша....
cout << "-200 --> " << cal->getValue(-200) << endl;
cout << "-200 --> " << cal->getValue(-200) << endl;
cout << "-200 --> " << cal->getValue(-200) << endl;
cout << "-200 --> " << cal->getValue(-200) << endl;
// --------------
cout << "-100 --> " << cal->getValue(-100) << endl;
cout << "-200 --> " << cal->getValue(-200) << endl;
cout << "-100 --> " << cal->getValue(-100) << endl;
cout << " -75 --> " << cal->getValue(-75) << endl;
cout << " -50 --> " << cal->getValue(-50) << endl;
cout << " -25 --> " << cal->getValue(-25) << endl;
cout << " -0 --> " << cal->getValue(0) << endl;
cout << " 25 --> " << cal->getValue(25) << endl;
cout << " 50 --> " << cal->getValue(50) << endl;
cout << " 75 --> " << cal->getValue(75) << endl;
cout << " 100 --> " << cal->getValue(100) << endl;
cout << " 200 --> " << cal->getValue(200) << endl;
cout << " 310 --> " << cal->getValue(310) << endl;
cout << " 415 --> " << cal->getValue(415) << endl;
cout << " 556 --> " << cal->getValue(556) << endl;
cout << " 873 --> " << cal->getValue(873) << endl;
cout << " 1500 --> " << cal->getValue(1500) << endl;
cout << endl << " RAW VALUE.." << endl;
cout << "-1220 --> " << cal->getRawValue(-1220) << endl;
cout << " -200 --> " << cal->getRawValue(-200) << endl;
cout << " -60 --> " << cal->getRawValue(-60) << endl;
cout << " -40 --> " << cal->getRawValue(-40) << endl;
cout << " -20 --> " << cal->getRawValue(-20) << endl;
cout << " 0 --> " << cal->getRawValue(0) << endl;
cout << " 20 --> " << cal->getRawValue(20) << endl;
cout << " 40 --> " << cal->getRawValue(40) << endl;
cout << " 60 --> " << cal->getRawValue(60) << endl;
cout << " 200 --> " << cal->getRawValue(200) << endl;
cout << " 1500 --> " << cal->getRawValue(1500) << endl;
return 0;
}
catch( Exception& ex )
{
cerr << "(main): " << ex << std::endl;
}
catch(...)
{
cerr << "(main): catch ..." << std::endl;
}
return 1;
}
#endif
\ No newline at end of file
#include <catch.hpp>
#if 0
#include <iostream>
#include <vector>
#include <iomanip>
#include "Exceptions.h"
#include "Extensions.h"
#include "DigitalFilter.h"
using namespace std;
using namespace UniSetTypes;
using namespace UniSetExtensions;
int main( int argc, const char** argv )
TEST_CASE("DigitalFilter","[DigitalFilter]")
{
try
SECTION("..")
{
WARN("List of tests for [DigitalFilter] is not complete (is not sufficient)");
}
SECTION("Default constructor (const data)")
{
DigitalFilter df;
DigitalFilter df_m;
DigitalFilter df;
DigitalFilter df10(10);
vector<long> dat={0,234,356,344,234,320,250,250,250,250,250,250,250,251,252,251,252,252,250};
REQUIRE( df.size() == 5 );
REQUIRE( df10.size() == 10 );
for( auto v: dat )
{
df.add(v);
for( int i=0; i<20; i++ )
{
df.add(50);
df10.add(50);
}
cout << "[" << setw(4) << v << "]: "
<< " filter1: " << setw(4) << df.current1()
<< " filterRC: " << setw(4) << df.currentRC()
<< " median: " << setw(4) << df_m.median(v)
<< endl;
}
REQUIRE( df.current1() == 50 );
REQUIRE( df.currentRC() == 50 );
REQUIRE( df.currentMedian() == 50 );
return 0;
REQUIRE( df10.current1() == 50 );
REQUIRE( df10.currentRC() == 50 );
REQUIRE( df10.currentMedian() == 50 );
}
catch( Exception& ex )
{
cerr << "(main): " << ex << std::endl;
SECTION("Median filter")
{
DigitalFilter df;
for( int i=0; i<20; i++ )
df.median(50);
REQUIRE( df.currentMedian() == 50 );
DigitalFilter df1;
DigitalFilter df10;
vector<long> dat={0,234,356,344,234,320,250,250,250,250,250,250,250,251,252,251,252,252,250};
for( auto v: dat )
{
df1.median(v);
df10.median(v);
}
REQUIRE( df1.currentMedian() == 252 );
REQUIRE( df10.currentMedian() == 252 );
}
catch(...)
{
cerr << "(main): catch ..." << std::endl;
SECTION("filter1")
{
DigitalFilter df1;
DigitalFilter df10;
// "выброс" за СКО отсекается..
vector<long> dat={10,12,10,-8,10,10,-230,10,10};
for( auto v: dat )
{
df1.add(v);
df10.add(v);
}
REQUIRE( df1.current1() == 10 );
REQUIRE( df10.current1() == 10 );
}
return 1;
SECTION("filterRC")
{
double Ti = 0.09; // постоянная времени фильтра
DigitalFilter df1(5,Ti);
DigitalFilter df10(10,Ti);
vector<long> dat={10,12,10,-8,10,10,-230,10,10,12,12,10,-8,-8,10,11,9,11,11,11,9,12,12,10,10,11,-4560,12,10,10,11,10,10,10,10};
for( auto v: dat )
{
df1.add(v);
df10.add(v);
msleep(30);
}
REQUIRE( df1.currentRC() == 10 );
REQUIRE( df10.currentRC() == 10 );
}
}
#endif
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment