-
Notifications
You must be signed in to change notification settings - Fork 0
/
median.c
139 lines (115 loc) · 3.25 KB
/
median.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "median.h"
static void MedianBufferClear(median* this);
static void MedianBufferShellSort(median* this);
static void MedianBufferInit(median* this);
static float MedianValueGet(median* this);
static void SwapPtPt(float **a, float **b);
static void MedianSortingGapInit(median* this);
extern void MedianInit(median* this, float* buffer,float** ptBufferSorted, uint16_t size)
{
if (this == NULL)
{
return;
}
else if ((buffer == NULL)||(ptBufferSorted == NULL)||(size < 3)||(size > UINT8_MAX))
{
this->init=false;
return;
}
this->buffer=buffer;
this->ptBufferSorted=ptBufferSorted;
this->size=size;
this->index=0;
this->initialKnuthGap=1;
this->init=true;
MedianBufferClear(this);
MedianBufferInit(this);
MedianSortingGapInit(this);
}
extern float MedianFilter(median* this,float input)
{
if ((this == NULL)||(this->init==false))
return 0;
this->buffer[this->index] = input;
/*increment index*/
this->index = (this->index + 1) % this->size;
MedianBufferShellSort(this);
return MedianValueGet(this);
}
extern uint8_t MedianIterationGet(median* this)
{
if ((this == NULL)||(this->init==false))
return 0;
return this->iterationCount;
}
static void MedianSortingGapInit(median* this)
{
/* calculate initial gap using Knuth sequence for Shell-Sort */
this->initialKnuthGap=1;
while (this->initialKnuthGap < this->size / 3)
{
this->initialKnuthGap = this->initialKnuthGap * 3 + 1;
}
}
static float MedianValueGet(median* this)
{
if (this->size % 2)
{ /* return the middle value */
return *this->ptBufferSorted[(this->size / 2)];
}
else
{ /* return the average of the two middle values */
return (*this->ptBufferSorted[(this->size / 2)] + *this->ptBufferSorted[(this->size / 2) - 1]) / 2;
}
}
static void MedianBufferClear(median* this)
{
if (this == NULL)
return;
for (uint8_t i = 0; i < this->size; i++)
this->buffer[i] = 0;
}
static void MedianBufferInit(median* this)
{
for (uint8_t i = 0; i < this->size; i++)
this->ptBufferSorted[i] = &this->buffer[i];
}
static void MedianBufferShellSort(median* this)
{
if (this == NULL)
return;
this->iterationCount = 0;
uint8_t gap=this->initialKnuthGap;
while (gap > 0)
{
for (uint8_t index = gap; index < this->size; index++)
{
float* currentElement = this->ptBufferSorted[index];
/* If left element is larger, swap until correct position is found. */
while (index >=gap && *this->ptBufferSorted[index - gap] > *currentElement)
{
SwapPtPt(&this->ptBufferSorted[index - gap],&this->ptBufferSorted[index]);
index-= gap;
this->iterationCount++;
}
this->ptBufferSorted[index] = currentElement;
}
/* calculate next gap, using Knuth sequence */
if (gap!=1)
{
gap = (gap - 1) / 3;
}else
{
gap=0;
}
}
}
static void SwapPtPt(float **a, float **b)
{
float *temp = *a;
*a = *b;
*b = temp;
}