Skip to content

Commit

Permalink
Merge pull request #2 from dpscience/dpscience-local
Browse files Browse the repository at this point in the history
version 1.03 release
  • Loading branch information
dpscience authored Apr 23, 2018
2 parents 11506fa + b3714a1 commit 9af3fce
Show file tree
Hide file tree
Showing 23 changed files with 1,229 additions and 106 deletions.
2 changes: 2 additions & 0 deletions DLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "DLib/DThirdParty/Crypt/tinyaes.h"
#include "DLib/DThirdParty/Miniz/miniz.h"
#include "DLib/DThirdParty/MedianFilter/medflt.h"

#include "DLib/DGUI/slider.h"
#include "DLib/DGUI/svgbutton.h"
Expand All @@ -51,5 +52,6 @@
#include "DLib/DPlot/plot2DXAxis.h"

#include "DLib/DMath/constantexplanations.h"
#include "DLib/DMath/dmedianfilter.h"

#endif // DLIB_H
90 changes: 90 additions & 0 deletions DLib/DMath/dmedianfilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "dmedianfilter.h"

DMedianFilter::DMedianFilter() {}
DMedianFilter::~DMedianFilter() {}

#ifdef __DMEDIANFILTER_VARIANT_1
bool DMedianFilter::apply(float *data_1d, int size, int windowSize)
{
if (!data_1d || size < 3 || windowSize < 3)
return false;

const int windowSizeHalf = windowSize/2;
const int offset = windowSizeHalf + (windowSize%2);

if (!autocastToInteger) {
DMediator<float> mediator(windowSize);
for (int i = 0 ; i < windowSizeHalf ; ++i) {
mediator.insert(data_1d[0]);
data_1d[i] = mediator.getMedian();
}

for (int i = 0 ; i < offset ; ++i) {
mediator.insert(data_1d[i]);
}

const int nDec = (size - offset);
const int nMinus1 = (size - 1);

for (int i = 0 ; i < nDec ; ++i) {
data_1d[i] = mediator.getMedian();
mediator.insert(data_1d[i + offset]);
}

for (int i = nDec ; i < size ; ++i) {
data_1d[i] = mediator.getMedian();
mediator.insert(data_1d[nMinus1]);
}
}
else {
DMediator<int> mediator(windowSize);
for (int i = 0 ; i < windowSizeHalf ; ++i) {
mediator.insert(data_1d[0]);
data_1d[i] = mediator.getMedian();
}

for (int i = 0 ; i < offset ; ++i) {
mediator.insert(data_1d[i]);
}

const int nDec = (size - offset);
const int nMinus1 = (size - 1);

for (int i = 0 ; i < nDec ; ++i) {
data_1d[i] = mediator.getMedian();
mediator.insert(data_1d[i + offset]);
}

for (int i = nDec ; i < size ; ++i) {
data_1d[i] = mediator.getMedian();
mediator.insert(data_1d[nMinus1]);
}
}

return true;
}
#endif

bool DMedianFilter::apply(float *data_1d, float *data_1d_source, int size, int windowSize)
{
if (!data_1d || size <= 3 || windowSize < 3)
return false;

TMedianFilter1D<float> filter(windowSize);

std::vector<float> data(size);
for ( int i = 0 ; i < size ; ++ i ) {
data[i] = data_1d[i];
data_1d_source[i] = data_1d[i]; /* original data */
}

filter.Execute(data);

for ( int i = 0 ; i < filter.Count() ; ++ i )
data_1d[i] = filter[i];


return true;
}


160 changes: 160 additions & 0 deletions DLib/DMath/dmedianfilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#ifndef DMEDIANFILTER_H
#define DMEDIANFILTER_H

#include "DLib/DThirdParty/MedianFilter/medflt.h"

template <typename DItem> class DMediator {
const int m_N;
DItem* m_data;
int* m_pos;
int* m_heap;
int* m_allocatedHeap;
int m_idx;
int m_minCt;
int m_maxCt;

public:
DMediator(int nItems) :
m_N(nItems) {
m_data = new DItem[nItems];
m_pos = new int[nItems];
m_allocatedHeap = new int[nItems];
m_heap = m_allocatedHeap + (nItems/2);
m_minCt = m_maxCt = m_idx = 0;

while (nItems--) {
m_pos[nItems] = ((nItems + 1)/2)*((nItems & 1) ? -1 : 1);
m_heap[m_pos[nItems]] = nItems;
}
}

virtual ~DMediator() {
delete[] m_data;
delete[] m_pos;
delete[] m_allocatedHeap;
}

void insert(const DItem& v) {
const int p = m_pos[m_idx];
const DItem old = m_data[m_idx];
m_data[m_idx] = v;
m_idx = (m_idx+1)%m_N;

if (p > 0) {
if (m_minCt < (m_N-1)/2) {
m_minCt ++;
}
else if (v > old) {
minSortDown( p );
return;
}

if (minSortUp(p) && mmCmpExch(0, -1)) {
maxSortDown(-1);
}
}
else if (p < 0) {
if (m_maxCt < m_N/2) {
m_maxCt++;
}
else if (v < old) {
maxSortDown( p );
return;
}

if (maxSortUp(p) && m_minCt && mmCmpExch(1, 0)) {
minSortDown(1);
}
}
else {
if (m_maxCt && maxSortUp(-1)) {
maxSortDown( -1 );
}

if (m_minCt && minSortUp(1)) {
minSortDown(1);
}
}
}

DItem getMedian() {
DItem v = m_data[m_heap[0]];

if (m_minCt < m_maxCt) {
v = (v + m_data[m_heap[-1]])/2;
}

return v;
}

private:
int mmexchange(const int i, const int j)
{
int t = m_heap[i];
m_heap[i] = m_heap[j];
m_heap[j] = t;
m_pos[m_heap[i]] = i;
m_pos[m_heap[j]] = j;

return 1;
}

void minSortDown(int i) {
for (i*=2; i <=m_minCt; i*=2) {
if (i < m_minCt && mmless(i+1, i)) {
++i;
}

if (!mmCmpExch(i, i/2)) {
break;
}
}
}

void maxSortDown(int i) {
for (i*=2; i >= -m_maxCt; i*=2) {
if (i > -m_maxCt && mmless(i, i-1)) {
--i;
}

if (!mmCmpExch(i/2, i)) {
break;
}
}
}

inline int mmless(const int i,const int j) {
return (m_data[m_heap[i]] < m_data[m_heap[j]]);
}

inline int mmCmpExch(const int i,const int j) {
return (mmless(i, j) && mmexchange(i, j));
}

inline int minSortUp(int i) {
while (i > 0 && mmCmpExch(i, i/2)) {
i /= 2;
}

return (i == 0);
}

inline int maxSortUp(int i) {
while (i < 0 && mmCmpExch(i/2, i)) {
i /= 2;
}

return (i==0);
}
};

class DMedianFilter
{
DMedianFilter();
virtual ~DMedianFilter();

public:
static bool apply(float *data_1d, float *data_1d_source, int size, int windowSize);
};

#endif // DMEDIANFILTER_H
10 changes: 10 additions & 0 deletions DLib/DPlot/plot2DXCurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,16 @@ int plot2DXCurve::setMaxContainerSize() const
return m_maxCount;
}

int plot2DXCurve::getDataSize() const
{
return m_dataContainer.size();
}

int plot2DXCurve::getCacheSize() const
{
return m_cache.size();
}

void plot2DXCurve::reset()
{
m_cache.clear();
Expand Down
3 changes: 3 additions & 0 deletions DLib/DPlot/plot2DXCurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public slots:

int setMaxContainerSize() const;

int getDataSize() const;
int getCacheSize() const;

private slots:
void reset();
void setLastValueBeforeReplot(const QPointF& lastValue);
Expand Down
Loading

0 comments on commit 9af3fce

Please sign in to comment.