| 1 | /* Copyright (C) 2004 Jean-Marc Valin |
| 2 | File medfilter.c |
| 3 | Median filter |
| 4 | |
| 5 | |
| 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions |
| 8 | are met: |
| 9 | |
| 10 | - Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
| 12 | |
| 13 | - Redistributions in binary form must reproduce the above copyright |
| 14 | notice, this list of conditions and the following disclaimer in the |
| 15 | documentation and/or other materials provided with the distribution. |
| 16 | |
| 17 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 18 | contributors may be used to endorse or promote products derived from |
| 19 | this software without specific prior written permission. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | |
| 33 | */ |
| 34 | |
| 35 | #include "medfilter.h" |
| 36 | #include "misc.h" |
| 37 | |
| 38 | MedianFilter *median_filter_new(int N) |
| 39 | { |
| 40 | MedianFilter *f = speex_alloc(sizeof(MedianFilter)); |
| 41 | f->N = N; |
| 42 | f->ids = speex_alloc(sizeof(int)*N); |
| 43 | f->val = speex_alloc(sizeof(float)*N); |
| 44 | f->filled = 0; |
| 45 | return f; |
| 46 | } |
| 47 | |
| 48 | void median_filter_update(MedianFilter *f, float val) |
| 49 | { |
| 50 | int i=0; |
| 51 | int insert = 0; |
| 52 | while (insert<f->filled && f->val[insert] < val) |
| 53 | { |
| 54 | insert++; |
| 55 | } |
| 56 | if (f->filled == f->N) |
| 57 | { |
| 58 | int remove; |
| 59 | for (remove=0;remove<f->N;remove++) |
| 60 | if (f->ids[remove] == 0) |
| 61 | break; |
| 62 | if (insert>remove) |
| 63 | insert--; |
| 64 | if (insert > remove) |
| 65 | { |
| 66 | for (i=remove;i<insert;i++) |
| 67 | { |
| 68 | f->val[i] = f->val[i+1]; |
| 69 | f->ids[i] = f->ids[i+1]; |
| 70 | } |
| 71 | } else if (insert < remove) |
| 72 | { |
| 73 | for (i=remove;i>insert;i--) |
| 74 | { |
| 75 | f->val[i] = f->val[i-1]; |
| 76 | f->ids[i] = f->ids[i-1]; |
| 77 | } |
| 78 | } |
| 79 | for (i=0;i<f->filled;i++) |
| 80 | f->ids[i]--; |
| 81 | } else { |
| 82 | for (i=f->filled;i>insert;i--) |
| 83 | { |
| 84 | f->val[i] = f->val[i-1]; |
| 85 | f->ids[i] = f->ids[i-1]; |
| 86 | } |
| 87 | f->filled++; |
| 88 | } |
| 89 | f->val[insert]=val; |
| 90 | f->ids[insert]=f->filled-1; |
| 91 | } |
| 92 | |
| 93 | float median_filter_get(MedianFilter *f) |
| 94 | { |
| 95 | return f->val[f->filled>>1]; |
| 96 | } |
| 97 |