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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/* filter/impulse.c
*
* Impulse detecting filters
*
* Copyright (C) 2018 Patrick Alken
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <stdlib.h>
#include <math.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_filter.h>
static int filter_impulse(const double scale, const double epsilon, const double t, const gsl_vector * x, const gsl_vector * xmedian,
gsl_vector * y, gsl_vector * xsigma, size_t * noutlier, gsl_vector_int * ioutlier);
/*
gsl_filter_impulse_alloc()
Allocate a workspace for impulse detection filtering.
Inputs: K - number of samples in window; if even, it is rounded up to
the next odd, to have a symmetric window
Return: pointer to workspace
*/
gsl_filter_impulse_workspace *
gsl_filter_impulse_alloc(const size_t K)
{
gsl_filter_impulse_workspace *w;
w = calloc(1, sizeof(gsl_filter_impulse_workspace));
if (w == 0)
{
GSL_ERROR_NULL ("failed to allocate space for workspace", GSL_ENOMEM);
}
w->movstat_workspace_p = gsl_movstat_alloc(K);
if (w->movstat_workspace_p == 0)
{
gsl_filter_impulse_free(w);
return NULL;
}
return w;
}
void
gsl_filter_impulse_free(gsl_filter_impulse_workspace * w)
{
if (w->movstat_workspace_p)
gsl_movstat_free(w->movstat_workspace_p);
free(w);
}
/*
gsl_filter_impulse()
Apply an impulse detection filter to an input vector. The filter output is
y_i = { x_i, |x_i - m_i| <= t * S_i
{ m_i, |x_i - m_i| > t * S_i
where m_i is the median of the window W_i^H and S_i is the scale estimate (MAD, IQR, S_n, Q_n)
Inputs: endtype - how to handle signal end points
scale_type - which statistic to use for scale estimate (MAD, IQR, etc)
t - number of standard deviations required to identity outliers (>= 0)
x - input vector, size n
y - (output) filtered vector, size n
xmedian - (output) vector of median values of x, size n
xmedian_i = median of window centered on x_i
xsigma - (output) vector of estimated local standard deviations of x, size n
xsigma_i = sigma for i-th window: scale*MAD
noutlier - (output) number of outliers detected
ioutlier - (output) boolean array indicating outliers identified, size n; may be NULL
ioutlier_i = 1 if outlier detected, 0 if not
w - workspace
Notes:
*/
int
gsl_filter_impulse(const gsl_filter_end_t endtype, const gsl_filter_scale_t scale_type, const double t,
const gsl_vector * x, gsl_vector * y, gsl_vector * xmedian, gsl_vector * xsigma, size_t * noutlier,
gsl_vector_int * ioutlier, gsl_filter_impulse_workspace * w)
{
const size_t n = x->size;
if (n != y->size)
{
GSL_ERROR("input and output vectors must have same length", GSL_EBADLEN);
}
else if (xmedian->size != n)
{
GSL_ERROR("xmedian vector must match input size", GSL_EBADLEN);
}
else if (xsigma->size != n)
{
GSL_ERROR("xsigma vector must match input size", GSL_EBADLEN);
}
else if ((ioutlier != NULL) && (ioutlier->size != n))
{
GSL_ERROR("ioutlier vector must match input size", GSL_EBADLEN);
}
else if (t < 0.0)
{
GSL_ERROR("t must be non-negative", GSL_EDOM);
}
else
{
int status;
double scale = 1.0;
switch (scale_type)
{
case GSL_FILTER_SCALE_MAD:
{
/* compute window medians and MADs */
gsl_movstat_mad(endtype, x, xmedian, xsigma, w->movstat_workspace_p);
break;
}
case GSL_FILTER_SCALE_IQR:
{
/* multiplication factor for IQR to estimate stddev for Gaussian signal */
scale = 0.741301109252801;
/* calculate the window medians */
gsl_movstat_median(endtype, x, xmedian, w->movstat_workspace_p);
/* calculate window IQRs */
gsl_movstat_qqr(endtype, x, 0.25, xsigma, w->movstat_workspace_p);
break;
}
case GSL_FILTER_SCALE_SN:
{
/* calculate the window medians */
gsl_movstat_median(endtype, x, xmedian, w->movstat_workspace_p);
/* calculate window S_n values */
gsl_movstat_Sn(endtype, x, xsigma, w->movstat_workspace_p);
break;
}
case GSL_FILTER_SCALE_QN:
{
/* calculate the window medians */
gsl_movstat_median(endtype, x, xmedian, w->movstat_workspace_p);
/* calculate window Q_n values */
gsl_movstat_Qn(endtype, x, xsigma, w->movstat_workspace_p);
break;
}
default:
GSL_ERROR("unknown scale type", GSL_EDOM);
break;
}
/* apply impulse detecting filter using previously computed scale estimate */
status = filter_impulse(scale, 0.0, t, x, xmedian, y, xsigma, noutlier, ioutlier);
return status;
}
}
/*
filter_impulse()
Apply an impulse detection filter to an input vector. The filter output is
y_i = { x_i, |x_i - m_i| <= t * S_i OR S_i < epsilon
{ m_i, |x_i - m_i| > t * S_i
where m_i is the median of the window W_i^H and S_i is the scale estimate (MAD, IQR, etc)
Inputs: scale - scale factor to multiply xsigma to get unbiased estimate of stddev for Gaussian data
epsilon - minimum allowed scale estimate for identifying outliers
t - number of standard deviations required to identity outliers (>= 0)
x - input vector, size n
xmedian - vector of median values of x, size n
xmedian_i = median of window centered on x_i
y - (output) filtered vector, size n
xsigma - (output) vector of estimated local standard deviations of x, size n
xsigma_i = S_n for i-th window
noutlier - (output) number of outliers detected
ioutlier - (output) boolean array indicating outliers identified, size n; may be NULL
ioutlier_i = 1 if outlier detected, 0 if not
Notes:
1) If S_i = 0 or is very small for a particular sample, then the filter may erroneously flag the
sample as an outlier, since it will act as a standard median filter. To avoid this scenario, the
parameter epsilon specifies the minimum value of S_i which can be used in the filter test. Any
samples for which S_i < epsilon are passed through unchanged.
*/
static int
filter_impulse(const double scale, const double epsilon, const double t, const gsl_vector * x, const gsl_vector * xmedian,
gsl_vector * y, gsl_vector * xsigma, size_t * noutlier, gsl_vector_int * ioutlier)
{
const size_t n = x->size;
if (n != y->size)
{
GSL_ERROR("input and output vectors must have same length", GSL_EBADLEN);
}
else if (xmedian->size != n)
{
GSL_ERROR("xmedian vector must match input size", GSL_EBADLEN);
}
else if (xsigma->size != n)
{
GSL_ERROR("xsigma vector must match input size", GSL_EBADLEN);
}
else if ((ioutlier != NULL) && (ioutlier->size != n))
{
GSL_ERROR("ioutlier vector must match input size", GSL_EBADLEN);
}
else if (t < 0.0)
{
GSL_ERROR("t must be non-negative", GSL_EDOM);
}
else
{
size_t i;
*noutlier = 0;
/* build output vector */
for (i = 0; i < n; ++i)
{
double xi = gsl_vector_get(x, i);
double xmedi = gsl_vector_get(xmedian, i);
double absdevi = fabs(xi - xmedi); /* absolute deviation for this sample */
double *xsigmai = gsl_vector_ptr(xsigma, i);
/* multiply by scale factor to get estimate of standard deviation */
*xsigmai *= scale;
/*
* If the absolute deviation for this sample is more than t stddevs
* for this window (and S_i is sufficiently large to avoid scale implosion),
* set the output value to the window median, otherwise use the original sample
*/
if ((*xsigmai >= epsilon) && (absdevi > t * (*xsigmai)))
{
gsl_vector_set(y, i, xmedi);
++(*noutlier);
if (ioutlier)
gsl_vector_int_set(ioutlier, i, 1);
}
else
{
gsl_vector_set(y, i, xi);
if (ioutlier)
gsl_vector_int_set(ioutlier, i, 0);
}
}
return GSL_SUCCESS;
}
}
|