Sunday, 13 November 2016

java - How to determine ALPHA, smoothing constant, of a LPF?


I am implementing a simple LPF in Java.


But I have problems in choosing the value of ALPHA.



public class LowPassFilter {

/*
* Time smoothing constant for low-pass filter 0 ≤ α ≤ 1 ; a smaller value
* basically means more smoothing See:
* http://en.wikipedia.org/wiki/Low-pass_filter#Discrete-time_realization
*/
private static final float ALPHA = 0.2f;

private LowPassFilter() {

}

/**
* Filter the given input against the previous values and return a low-pass
* filtered result.
*
* @param input
* float array to smooth.
* @param prev
* float array representing the previous values.

* @return float array smoothed with a low-pass filter.
*/
public static float[] filter(float[] input, float[] prev) {

if (input == null || prev == null)
throw new NullPointerException("input and prev float arrays must be non-NULL");
if (input.length != prev.length)
throw new IllegalArgumentException("input and prev must be the same length");

for (int i = 0; i < input.length; i++) {

prev[i] = prev[i] + ALPHA * (input[i] - prev[i]);
}

return prev;
}
}

ALPHA is defined to be dT/(dT+RC), where dT is the event delivering rate and RC is the time-constant of LPF.


To find the value of ALPHA, I need to know the values of dT and RC.


My LPF should have a cut-off frequency of 4Hz and the sampling rate is 350Hz. Anybody can help determine the proper ALPHA value?



Thanks in advance!



Answer



All formulas I'm using come from the wikipedia page you cite in your code.


First, calculate RC. We have,


fc = 1/(2*pi*RC)

so


RC = 1/(2*pi*fc)

Now, ALPHA is simply:



ALPHA = dt / ( dt + RC )

where dt is 1/samplerate.


For your values (samplerate=350, fc=4) I got


ALPHA = 0.0670

Caveat: I tried this on a few other values and the shape of the filter was what I expected, but the exact 3dB cutoff frequency was never quite where it should have been. Not sure if this indicates something wrong, or that's just the nature of the beast.


No comments:

Post a Comment

readings - Appending 内 to a company name is read ない or うち?

For example, if I say マイクロソフト内のパートナーシップは強いです, is the 内 here read as うち or ない? Answer 「内」 in the form: 「Proper Noun + 内」 is always read 「ない...