Go Down

### Topic: smoothing pot input (Read 7178 times)previous topic - next topic

#### db2db

##### Apr 29, 2012, 10:02 am

Googling arund I found this smoothing algorithm. Seems simple, but I follow how it would work.
Code: [Select]
` // declared globallyfloat smoothADC;// inside loop() or other repeating functionsmoothADC = 0.9 * smoothADC + 0.1 * analogRead(0); `

Make sense to anyone?

#### skyjumper

#1
##### Apr 29, 2012, 10:07 am
No, that makes no sense as written. Are you sure nothing is omitted? That code uses an uninitialized variable, which can never ever work.

#### skyjumper

#2
##### Apr 29, 2012, 10:08 am
What you could do instead is save the last n values and average them. Depending upon the nature of the data that is an easy way to do it that can work well.

#3
##### Apr 29, 2012, 10:11 am
Quote
Googling arund I found this smoothing algorithm.

It's an "exponentially weighted moving average" or EMWA (missing the proper initialization as skyjumper mentioned).

#### bubulindo

#4
##### Apr 29, 2012, 10:13 am

No, that makes no sense as written. Are you sure nothing is omitted? That code uses an uninitialized variable, which can never ever work.

I think he asked if the algorithm is correct, not if the code was properly written. And it is only the initialization of the variable that is missing (which most likely is cleared and set in more than one place in the code).

That formula you have is a sort of averaging filter. Weighed average I think may be a better name for it.

Basically you have this:

Code: [Select]
`smooth = ((smooth * 9) + analogRead(0))/10; `

So every new sample has an effect of 10% in your signal. A little Excel sheet can do wonders to show the result of this and different weights applied to this formula.

This... is a hobby.

#### skyjumper

#5
##### Apr 29, 2012, 11:13 am
Can a filter like this be used to smooth the last n samples, so that old data is at some point essentially discarded? Currently I am storing the last 20 samples and averaging them, but I have been looking for a way to achieve this without consuming 80 bytes (20 floats)...

#### Grumpy_Mike

#6
##### Apr 29, 2012, 11:27 am
Quote
Can a filter like this be used to smooth the last n samples, so that old data is at some point essentially discarded?

I think you are missing the point after many iterations the old data is so diluted that it has no effect, this is not homeopathic computing.

The alternative is to use an array and apply a different weighting to each sample. In this way any required filter response can be created providing you have enough terms. However, the increase in processing time reduces the top frequency of the filter. This sort of programming is known as digital signal processing.

#7
##### Apr 29, 2012, 08:09 pm
Can a filter like this be used to smooth the last n samples, so that old data is at some point essentially discarded?

A follow-up to @Grumpy_Mike's answer: Assuming your samples are integers (e.g. from the analog-to-digital converter), using db2db's ? = 0.9, the contribution for the seventh previous value becomes so small that it is not present in a float.  In other words, for practical purposes it's zero.  ? = 0.9 gives a history of six samples (the contribution of the six is 1 / 1E-06; not much).

Quote
Currently I am storing the last 20 samples and averaging them, but I have been looking for a way to achieve this without consuming 80 bytes (20 floats)...

What are you trying to achieve?  Is the goal to filter out noise when reading a stable signal (in which case the "windowed average" you're doing now is probably the right choice)?  Do you need the average to "respond quickly" to signal changes?

#### db2db

#8
##### Apr 29, 2012, 10:04 pmLast Edit: Apr 29, 2012, 10:06 pm by db2db Reason: 1
This seems to work, but is very slow to respond. How can I speed up the response?

Code: [Select]
`float smooth ;void loop() {SSerial.print(analogRead(5));  //show raw output of pot valueSSerial.print(" ");                  // add a space for readibilitysmooth = 0.9 * smooth + 0.1 * analogRead(5);  SSerial_println(smooth);  //show filtered output of pot valuedelay (100);}`

#### dxw00d

#9
##### Apr 29, 2012, 10:08 pm
Take the delay() out?

#10
##### Apr 29, 2012, 10:28 pm
This seems to work, but is very slow to respond. How can I speed up the response?

First, let's review snippets from some of the replies...

Quote
That code uses an uninitialized variable

Quote
missing the proper initialization

Quote
And it is only the initialization of the variable that is missing

I can't help but notice that the code you posted does not initialize smooth.  Do you think that might be the problem?

#### AWOL

#11
##### Apr 29, 2012, 10:34 pm
smooth is initialised to zero by the compiler, so it is going to take a while to ramp up.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### skyjumper

#12
##### Apr 29, 2012, 10:41 pm

smooth is initialised to zero by the compiler, so it is going to take a while to ramp up.

No, its not. Only static variables are initialized by the compiler. Non-statics will have a random value.

My question is, what is a reasonable value to initialize with?

#### AWOL

#13
##### Apr 29, 2012, 10:43 pmLast Edit: Apr 29, 2012, 10:47 pm by AWOL Reason: 1
Quote
No, its not. Only static variables are initialized by the compiler. Non-statics will have a random value.
It's a global; it is initialised to zero.
Happily, the float representation of zero is the same as int or long.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### Grumpy_Mike

#14
##### Apr 29, 2012, 10:45 pm
Quote
My question is, what is a reasonable value to initialize with?