17 medianBuffer.resize(size);
18 medianFilter.numNodes = size;
19 medianFilter.medianBuffer = medianBuffer.data();
23 virtual T process(T in)
override {
return insert(&medianFilter, in); }
35 unsigned int numNodes = 0;
45 int init(MedianFilter_t *medianFilter) {
46 if (medianFilter && medianFilter->medianBuffer &&
47 (medianFilter->numNodes % 2) && (medianFilter->numNodes > 1)) {
49 for (
unsigned int i = 0; i < medianFilter->numNodes; i++) {
50 medianFilter->medianBuffer[i].value = 0;
51 medianFilter->medianBuffer[i].nextAge =
52 &medianFilter->medianBuffer[(i + 1) % medianFilter->numNodes];
53 medianFilter->medianBuffer[i].nextValue =
54 &medianFilter->medianBuffer[(i + 1) % medianFilter->numNodes];
55 medianFilter->medianBuffer[(i + 1) % medianFilter->numNodes].prevValue =
56 &medianFilter->medianBuffer[i];
59 medianFilter->ageHead = medianFilter->medianBuffer;
60 medianFilter->valueHead = medianFilter->medianBuffer;
61 medianFilter->medianHead =
62 &medianFilter->medianBuffer[medianFilter->numNodes / 2];
70 int insert(MedianFilter_t *medianFilter, T sample) {
72 MedianNode_t *newNode=
nullptr, *it=
nullptr;
74 if (medianFilter->ageHead == medianFilter->valueHead) {
76 medianFilter->valueHead = medianFilter->valueHead->nextValue;
79 if ((medianFilter->ageHead == medianFilter->medianHead) ||
80 (medianFilter->ageHead->value > medianFilter->medianHead->value)) {
82 medianFilter->medianHead = medianFilter->medianHead->prevValue;
86 newNode = medianFilter->ageHead;
87 newNode->value = sample;
90 medianFilter->ageHead->nextValue->prevValue =
91 medianFilter->ageHead->prevValue;
92 medianFilter->ageHead->prevValue->nextValue =
93 medianFilter->ageHead->nextValue;
95 medianFilter->ageHead = medianFilter->ageHead->nextAge;
98 it = medianFilter->valueHead;
99 for (i = 0; i < medianFilter->numNodes - 1; i++) {
100 if (sample < it->value) {
102 medianFilter->valueHead = newNode;
110 it->prevValue->nextValue = newNode;
111 newNode->prevValue = it->prevValue;
112 it->prevValue = newNode;
113 newNode->nextValue = it;
116 if (i >= (medianFilter->numNodes / 2)) {
117 medianFilter->medianHead = medianFilter->medianHead->nextValue;
120 return medianFilter->medianHead->value;