Offline
Full Member
Karma: 0
Posts: 180
|
 |
« on: April 29, 2012, 03:02:01 am » |
Googling arund I found this smoothing algorithm. Seems simple, but I follow how it would work. // declared globally float smoothADC;
// inside loop() or other repeating function smoothADC = 0.9 * smoothADC + 0.1 * analogRead(0); Make sense to anyone?
|
|
|
|
|
Logged
|
|
|
|
|
Smithfield, Rhode Island
Offline
God Member
Karma: 2
Posts: 828
|
 |
« Reply #1 on: April 29, 2012, 03:07:18 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.
|
|
|
|
|
Logged
|
|
|
|
|
Smithfield, Rhode Island
Offline
God Member
Karma: 2
Posts: 828
|
 |
« Reply #2 on: April 29, 2012, 03:08:35 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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 129
Posts: 10379
|
 |
« Reply #3 on: April 29, 2012, 03:11:17 am » |
Googling arund I found this smoothing algorithm. It's an "exponentially weighted moving average" or EMWA (missing the proper initialization as skyjumper mentioned).
|
|
|
|
|
Logged
|
|
|
|
|
'round the world...
Offline
Edison Member
Karma: 21
Posts: 2385
|
 |
« Reply #4 on: April 29, 2012, 03:13:29 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: 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.
|
|
|
|
|
Logged
|
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o. Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum). Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.
Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).
|
|
|
|
Smithfield, Rhode Island
Offline
God Member
Karma: 2
Posts: 828
|
 |
« Reply #5 on: April 29, 2012, 04:13:36 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)...
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 299
Posts: 26025
Solder is electric glue
|
 |
« Reply #6 on: April 29, 2012, 04:27:33 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 129
Posts: 10379
|
 |
« Reply #7 on: April 29, 2012, 01:09:04 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). 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?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 180
|
 |
« Reply #8 on: April 29, 2012, 03:04:27 pm » |
This seems to work, but is very slow to respond. How can I speed up the response? float smooth ;
void loop() { SSerial.print(analogRead(5)); //show raw output of pot value SSerial.print(" "); // add a space for readibility
smooth = 0.9 * smooth + 0.1 * analogRead(5); SSerial_println(smooth); //show filtered output of pot value
delay (100); }
|
|
|
|
« Last Edit: April 29, 2012, 03:06:56 pm by db2db »
|
Logged
|
|
|
|
|
Gosport, UK
Offline
Faraday Member
Karma: 19
Posts: 3117
|
 |
« Reply #9 on: April 29, 2012, 03:08:36 pm » |
Take the delay() out?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 129
Posts: 10379
|
 |
« Reply #10 on: April 29, 2012, 03:28:26 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... That code uses an uninitialized variable missing the proper initialization 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?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19368
I don't think you connected the grounds, Dave.
|
 |
« Reply #11 on: April 29, 2012, 03:34:08 pm » |
smooth is initialised to zero by the compiler, so it is going to take a while to ramp up.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Smithfield, Rhode Island
Offline
God Member
Karma: 2
Posts: 828
|
 |
« Reply #12 on: April 29, 2012, 03:41:02 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?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19368
I don't think you connected the grounds, Dave.
|
 |
« Reply #13 on: April 29, 2012, 03:43:21 pm » |
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.
|
|
|
|
« Last Edit: April 29, 2012, 03:47:38 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 299
Posts: 26025
Solder is electric glue
|
 |
« Reply #14 on: April 29, 2012, 03:45:03 pm » |
My question is, what is a reasonable value to initialize with? A reading from your pot. Do it in setup().
|
|
|
|
|
Logged
|
|
|
|
|
|