forked from nemomobile/mce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
median_filter.c
180 lines (156 loc) · 4.51 KB
/
median_filter.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* @file median_filter.c
* median filter -- this implements a median filter
* <p>
* Copyright © 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
* <p>
* @author David Weinehall <[email protected]>
* @author Semi Malinen <[email protected]>
*
* mce is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* mce is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mce. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include "median_filter.h"
/**
* Initialise median filter
* @param filter The median filter to initialise
* @param window_size The window size to use
*
* @return FALSE if window_size is too big or filter is NULL,
* TRUE on success
*/
gboolean median_filter_init(median_filter_struct *filter, gsize window_size)
{
gboolean status = FALSE;
guint i;
if ((filter == NULL) || (window_size > MEDIAN_FILTER_MAX_WINDOW_SIZE))
goto EXIT;
filter->window_size = window_size;
for (i = 0; i < filter->window_size; i++) {
filter->window[i] = 0;
filter->ordered_window[i] = 0;
}
filter->samples = 0;
filter->oldest = 0;
status = TRUE;
EXIT:
return status;
}
/**
* Insert a new sample into the median filter
*
* @param filter The median filter to insert the value into
* @param value The value to insert
* @param oldest The oldest value
* @return The filtered value
*/
static gint insert_ordered(median_filter_struct *filter,
gint value, gint oldest)
{
guint i;
/* If the filter window hasn't been filled yet, insert the new value */
if (filter->samples < filter->window_size) {
/* Find insertion point */
for (i = 0; i < filter->samples; i++) {
if (filter->ordered_window[i] >= value) {
/* Found the insertion point */
for ( ; i < filter->samples; ++i) {
gint tmp;
/* Swap the value at insertion point
* with the new value
*/
tmp = filter->ordered_window[i];
filter->ordered_window[i] = value;
value = tmp;
}
break;
}
}
/* Do the insertion */
filter->ordered_window[i] = value;
filter->samples++;
goto EXIT;
} else {
/* The filter window is full;
* remove the oldest value and insert new
*/
if (value == oldest) {
/* Do nothing */
goto EXIT;
}
/* Find either the insertion point
* and/or the deletion point
*/
for (i = 0; i < filter->window_size; i++) {
if (filter->ordered_window[i] >= value) {
/* Found the insertion point
* (it might be the deletion point
* as well!)
*/
for ( ; i < filter->window_size; i++) {
/* Swap value at insertion
* point and the new value
*/
int tmp = filter->ordered_window[i];
filter->ordered_window[i] = value;
value = tmp;
if (value == oldest) {
/* Found the deletion point */
goto EXIT;
}
}
goto EXIT;
} else if (filter->ordered_window[i] == oldest) {
/* Found the deletion point */
for ( ; i < filter->window_size - 1; i++) {
if (filter->ordered_window[i + 1] >= value) {
/* Found the insertion point */
break;
}
/* Shift the window,
* overwriting the value to delete
*/
filter->ordered_window[i] = filter->ordered_window[i + 1];
}
/* Insert */
filter->ordered_window[i] = value;
goto EXIT;
}
}
}
EXIT:
/* For odd number of samples return the middle one
* For even number of samples return the average
* of the two middle ones
*/
return (filter->ordered_window[(filter->samples - 1) / 2] +
filter->ordered_window[filter->samples / 2]) / 2;
}
/**
* Do a complete insertion of a sample into the median filter
*
* @param filter The median filter to insert the value into
* @param value The value to insert
* @return The filtered value
*/
gint median_filter_map(median_filter_struct *filter, gint value)
{
gint filtered_value;
/* Insert into the ordered array (deleting the oldest value) */
filtered_value = insert_ordered(filter, value,
filter->window[filter->oldest]);
/* Insert into the ring buffer (overwriting the oldest value) */
filter->window[filter->oldest] = value;
filter->oldest = (filter->oldest + 1) % filter->window_size;
return filtered_value;
}