Cutoff frequency of smooth filter

Hello !
I am using the smooth filter as low pass filter in my project.I am not sure which parameter in its code determines the cutoff frequency of the filter. Can anyone help me in this context ?
Regards !

Here is the code :

/* smooth v0.2
Paul Badger 2007
Smooth is a simple digital low-pass filter that is useful for smoothing sensor jitter or creating a delayed response to
fast moving data. It uses a buffer variable and limits the amount of new data that reaches the output each time through
the loop. Old data is used to make up the difference so that the response of the filter is slowed down.
Someone with some more math might want to add a little more explanation here. This would be called an integrating function
I believe.

Smoothing can also be used to puposefully slow down (number-based) actions.

Details: Smooth takes three parameters, the value to smoothed, the filter value (filterVal), and smoothedVal.

int sensVal - the sensor variable - raw material to be smoothed

float filterVal - The filter value is a float and must be between 0 and .9999 say. 0 is off (no smoothing) and .9999 is maximum smoothing.
The actual performance of the filter is going to be dependent on fast you are sampling your sensor (the total loop time), so
some trial and error will probably be neccessary to get the desired response.

smoothedVal - Use this for the output of the sensor and also feed it back into the loop. Each sensor needs its own value.
Don't use this variable for any other purpose.

This function can easily be rewritten with all-integer math, if you need more speed or want to avoid floats.

Note that an earlier version of the filter I had posted here had the parameters reversed from current version and also would malfunction if used on more than one sensor.
*/

int sensVal; // for raw sensor values
float filterVal; // this determines smoothness - .0001 is max 1 is off (no smoothing)
float smoothedVal; // this holds the last loop value just use a unique variable for every different sensor that needs smoothing

float smoothedVal2; // this would be the buffer value for another sensor if you needed to smooth two different sensors - not used in this sketch

int i, j; // loop counters or demo

void setup()
{
Serial.begin(9600);
Serial.println("start ");
}

/* the main loop is a test of the smooth function. It generates a simulated square wave and then
switches in four different smoothing values. Watch the numbers scroll by as the filter value
slows down the response. */

void loop()
{
for (i = 0; i < 7; i++){ // substitute some different filter values
filterVal = i * .15;

for (j = 0; j< 60; j++){

if (j < 30){ // generate a simulated square wave
sensVal = 1023;
}
else
{
sensVal = 0;
}

// sensVal = analogRead(0); this is what one would do normally
smoothedVal = smooth(sensVal, filterVal, smoothedVal); // second parameter determines smoothness - 0 is off, .9999 is max smooth

Serial.print(sensVal);
Serial.print(" ");
Serial.print(smoothedVal, DEC);
Serial.print(" ");
Serial.print("filterValue * 100 = "); // print doesn't work with floats
Serial.println(filterVal * 100, DEC);
delay(30);
}
}
}

int smooth(int data, float filterVal, float smoothedVal){

if (filterVal > 1){ // check to make sure param's are within range
filterVal = .99;
}
else if (filterVal <= 0){
filterVal = 0;
}

smoothedVal = (data * (1 - filterVal)) + (smoothedVal * filterVal);

return (int)smoothedVal;
}

which parameter in its code determines the cutoff frequency of the filter.

Hmmm... I have been waiting and hoping that someone would give an explanation that would satisfy users at various levels of experience and mathematical background. No luck so far, so here goes.

Since I don't know how much you know and how much you want to know, I will try a fairly general answer.

If you analyze that algorithm used in the sketch at http://www.arduino.cc/en/Tutorial/Smoothing as a low-pass filter, the cutoff frequency is determined by the value of a. The actual value of cutoff (in Hz) depends on the sample frequency. Since the response (magnitude of output divided by magnitude of sinusoidal input) for zero frequency is 1.0, and the magnitude of the response decreases (monotonically) as frequency increases, the cutoff frequency can be (and usually is) defined as the frequency for which the magnitude of the response falls to 1/sqrt(2).

For analytical treatment of sampled data systems (discrete-time systems) Fourier analysis is usually used. The important thing to remember that the treatment of things like this function requires that the input signal can't have any frequency components higher than one-half the sample frequency.

Now, for this filter it turns out that for low values of a (a little smaller than 0.2), the absolute value of transfer function does not fall to 1/sqrt(2) by the time that the frequency approaches half the sampling frequency. In other words, there is no "cutoff" frequency, since it would be greater than half the sampling frequency.

Anyhow...

I threw together a little description of a method of calculating cutoff frequency for this particular filter and posted it here: ArduinoSmoothCutoff See Footnote.

I used 33.33 kHz sample frequency in the paper.

For a sampling frequency of 1 Hz, here are the results of that approach:


[color=#0000ff]Sample frequency = 1.00000

a = 0.00: lpfResponse(0.50) = 1.00000
a = 0.05: lpfResponse(0.50) = 0.90476
a = 0.10: lpfResponse(0.50) = 0.81818
a = 0.15: lpfResponse(0.50) = 0.73913
a = 0.20: fco =  0.35242
a = 0.25: fco =  0.26995
a = 0.30: fco =  0.22066
a = 0.35: fco =  0.18512
a = 0.40: fco =  0.15731
a = 0.45: fco =  0.13445
a = 0.50: fco =  0.11503
a = 0.55: fco =  0.09812
a = 0.60: fco =  0.08313
a = 0.65: fco =  0.06965
a = 0.70: fco =  0.05738
a = 0.75: fco =  0.04611
a = 0.80: fco =  0.03566
a = 0.85: fco =  0.02592
a = 0.90: fco =  0.01678
a = 0.95: fco =  0.00817[/color]

So, for a = 0.4, the cutoff frequency is about 0.157 Hz.

These results all scale linearly with sampling frequency, so, for example if your sample period is 30 ms (sampling frequency is about 33.33 Hz) the cutoff frequency for a = 0.4 would be something like 0.15731/.03, or about 5.24 Hz, as indicated in the table in the paper.

Regards,

Dave

[edit]Footnote:
The paper in the original link had a couple of typographical errors (in the formulas for H1 and H2). I believe that the other formulas and conclusions were (and are) correct. I have modified the link. If you downloaded original paper, get the corrected one in the updated link above. I regret the error (and confusion).[/edit]

Thanks for the reply Dave. The paper you shared can be helpful

Regards
Waqas

I have edited my previous post to give a link to a corrected version of the paper. If you looked at the paper before the "Last Edit" time shown for the previous post, please go back and get the new version with the updated link.Sorry.

Regards,

Dave