From 2aec8edfdb7c0f775d9ff19f89151f88edaa79c0 Mon Sep 17 00:00:00 2001 From: ReservedField Date: Tue, 18 Oct 2016 02:25:04 +0200 Subject: [PATCH] Optimize median filter We're only interested in sorting the first half of the sample buffer. --- src/atomizer/Atomizer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/atomizer/Atomizer.c b/src/atomizer/Atomizer.c index 289dcea..c98f689 100755 --- a/src/atomizer/Atomizer.c +++ b/src/atomizer/Atomizer.c @@ -337,9 +337,9 @@ static uint16_t Atomizer_MedianFilter(uint16_t value, Atomizer_MedianFilterCtx_t ctx->buf[ctx->idx] = value; ctx->idx = (ctx->idx + 1) % ATOMIZER_MEDIANFILTER_WINDOW; - // Selection sort + // Selection sort. We only need to sort the first half. memcpy(sortBuf, ctx->buf, sizeof(sortBuf)); - for(i = 0; i < ATOMIZER_MEDIANFILTER_WINDOW; i++) { + for(i = 0; i < (ATOMIZER_MEDIANFILTER_WINDOW + 1) / 2; i++) { minIdx = i; for(j = i + 1; j < ATOMIZER_MEDIANFILTER_WINDOW; j++) { if(sortBuf[j] < sortBuf[minIdx]) {