Last active
August 24, 2016 09:44
-
-
Save riversun/ee4d2966a23d8bda40bc66bb541fd6ae to your computer and use it in GitHub Desktop.
[Java]Lowpass Filter(Linear weight version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2006-2016 Tom Misawa(riversun.org@gmail.com) | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
package org.riversun; | |
/** | |
* Linear weight lowpass filter | |
* | |
* @author Tom Misawa (riversun.org@gmail.com) | |
* | |
*/ | |
public class LinearWeightLowpassFilter { | |
public static void main(String[] args) { | |
// example | |
final float[] data = new float[] { 100f, 100f, 200f, 200f }; | |
final int sampleRange = 20; | |
LinearWeightLowpassFilter filter = new LinearWeightLowpassFilter(); | |
filter.setSamplingRange(sampleRange); | |
for (int i = 0; i < data.length; i++) { | |
filter.putValue(data[i]); | |
System.out.println(data[i] + " added. current filtered val is " + filter.getFilteredValue()); | |
} | |
} | |
private int mSamplingRange; | |
private float[] mSamplingBuffer; | |
private float mCurrentFilterVal; | |
private int mCurrentDataNumber = 0; | |
public LinearWeightLowpassFilter() { | |
setSamplingRange(20); | |
} | |
public void setSamplingRange(int range) { | |
mSamplingRange = range; | |
mSamplingBuffer = new float[mSamplingRange]; | |
} | |
/** | |
* Get the latest filtered(linear weighted) value | |
* | |
* @return | |
*/ | |
public float getFilteredValue() { | |
float filteredVal = mCurrentFilterVal; | |
return filteredVal; | |
} | |
/** | |
* Put the current value to filter | |
* | |
* @param val | |
*/ | |
public void putValue(float val) { | |
mCurrentFilterVal = filter(val, mSamplingBuffer); | |
} | |
public float[] getSampingBuffer() { | |
return mSamplingBuffer; | |
} | |
public int getSamplingRange() { | |
return mSamplingRange; | |
} | |
public void fillValue(float val) { | |
for (int i = 0; i < mSamplingBuffer.length; i++) { | |
mSamplingBuffer[i] = val; | |
} | |
} | |
private float filter(float inputValue, float[] samplingBuffer) { | |
mCurrentDataNumber++; | |
int range = samplingBuffer.length; | |
if (mCurrentDataNumber < range) { | |
range = mCurrentDataNumber; | |
} | |
for (int i = (range - 1) - 1; i >= 0; i--) { | |
// copy next element to prev element | |
samplingBuffer[i + 1] = samplingBuffer[i]; | |
} | |
// Add current value to last | |
samplingBuffer[0] = inputValue; | |
float numerator = 0; | |
float denominator = 0; | |
for (int n = 0; n < range; n++) { | |
// weight linearly | |
numerator += (range - n) * samplingBuffer[n]; | |
denominator += (range - n); | |
} | |
final float currentOutputVal = numerator / denominator; | |
return currentOutputVal; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment