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.

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)...

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.

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?