SDDSlib
Loading...
Searching...
No Matches
medianfilter.c File Reference

Core MEX routine implementing a fast version of the classical 1D running median filter of size W, where W is odd. More...

#include "mdb.h"

Go to the source code of this file.

Macros

#define SWAP(a, b)
 

Functions

double quickSelect (double *arr, int n)
 
void median_filter (double *x, double *m, long n, long W)
 Applies a median filter to an input signal.
 

Detailed Description

Core MEX routine implementing a fast version of the classical 1D running median filter of size W, where W is odd.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M.A. Little, N.S. Jones, H. Shang, R. Soliday

Definition in file medianfilter.c.

Macro Definition Documentation

◆ SWAP

#define SWAP ( a,
b )
Value:
{ \
register double t = (a); \
(a) = (b); \
(b) = t; \
}

Definition at line 78 of file medianfilter.c.

78#define SWAP(a, b) \
79 { \
80 register double t = (a); \
81 (a) = (b); \
82 (b) = t; \
83 }

Function Documentation

◆ median_filter()

void median_filter ( double * x,
double * m,
long n,
long W )

Applies a median filter to an input signal.

This function processes the input signal using a sliding window approach to compute the median value for each position. It handles boundary conditions by replicating the edge values to maintain the window size.

Parameters
xInput signal array.
mOutput signal buffer array where the median filtered signal will be stored.
nSize of the input signal.
WSize of the sliding window (must be an odd number, W = 2*W2 + 1).

Definition at line 157 of file medianfilter.c.

157 {
158 /* x -- input signal
159 m -- output signal buffer
160 n -- size of input signal
161 W -- size of slding window (must be odd number) W = 2*W2 + 1) */
162 long i, k, idx, W2;
163 double *w;
164 if (W % 2 == 0)
165 W = W + 1;
166 W2 = (W - 1) / 2;
167
168 w = calloc(sizeof(*w), W);
169 for (i = 0; i < n; i++) {
170 for (k = 0; k < W; k++) {
171 idx = i - W2 + k;
172 if (idx < 0) {
173 w[k] = x[0];
174 } else if (idx >= n) {
175 w[k] = x[n - 1];
176 } else {
177 w[k] = x[idx];
178 }
179 }
180 m[i] = quickSelect(w, W);
181 }
182 free(w);
183}

◆ quickSelect()

double quickSelect ( double * arr,
int n )

Definition at line 85 of file medianfilter.c.

85 {
86 long low, high;
87 long median;
88 long middle, ll, hh;
89
90 low = 0;
91 high = n - 1;
92 median = (low + high) / 2;
93 while (1) {
94 /* One element only */
95 if (high <= low)
96 return arr[median];
97
98 /* Two elements only */
99 if (high == low + 1) {
100 if (arr[low] > arr[high])
101 SWAP(arr[low], arr[high]);
102 return arr[median];
103 }
104
105 /* Find median of low, middle and high items; swap to low position */
106 middle = (low + high) / 2;
107 if (arr[middle] > arr[high])
108 SWAP(arr[middle], arr[high]);
109 if (arr[low] > arr[high])
110 SWAP(arr[low], arr[high]);
111 if (arr[middle] > arr[low])
112 SWAP(arr[middle], arr[low]);
113
114 /* Swap low item (now in position middle) into position (low+1) */
115 SWAP(arr[middle], arr[low + 1]);
116
117 /* Work from each end towards middle, swapping items when stuck */
118 ll = low + 1;
119 hh = high;
120 while (1) {
121 do {
122 ll++;
123 } while (arr[low] > arr[ll]);
124
125 do {
126 hh--;
127 } while (arr[hh] > arr[low]);
128
129 if (hh < ll)
130 break;
131
132 SWAP(arr[ll], arr[hh]);
133 }
134
135 /* Swap middle item (in position low) back into correct position */
136 SWAP(arr[low], arr[hh]);
137
138 /* Reset active partition */
139 if (hh <= median)
140 low = ll;
141 if (hh >= median)
142 high = hh - 1;
143 }
144}