-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathMedianFilter.cpp
More file actions
55 lines (48 loc) · 1.24 KB
/
MedianFilter.cpp
File metadata and controls
55 lines (48 loc) · 1.24 KB
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
/****************************************************
/* 7Bot class for Arduino platform
/* Author: Jerry Peng
/* Date: 26 April 2016
/*
/* Version 1.00
/* www.7bot.cc
/*
/* Description:
/*
/*
/***************************************************/
#include "MedianFilter.h"
MedianFilter::MedianFilter() {
for(int i=0; i<filterSize; i++){
filerElements[i] = 0;
}
}
int MedianFilter::filter(int dataIn) {
// 1- in put data
for(int i=filterSize-1; i>0; i--){
filerElements[i] = filerElements[i-1];
}
filerElements[0] = dataIn;
// 2- copy data
//float[] rankingElements = filerElements;
int rankingElements[filterSize];
for(int i=0; i<filterSize; i++){
rankingElements[i] = filerElements[i];
}
// 3- ranking
int temp;
for(int k=0;k<filterSize;k++){
for(int j=k+1;j<filterSize;j++){
if(rankingElements[j]<rankingElements[k])
{
temp=rankingElements[k];
rankingElements[k]=rankingElements[j];
rankingElements[j]=temp;
}
}
}
// 4- Drop highest & lowest 20% then calculate the mean
temp = 0;
for (int i=8;i<filterSize-8;i++) {temp += rankingElements[i];}
return (temp/(filterSize-16));
//return rankingElements[(filterSize-1)/2];
}