Page 1 of 1

AVERAGE FLOATING POINT

Posted: Wed Aug 25, 2021 3:42 pm
by maxtisc
Hi Team
I wanted to ask you if anyone has ever done a FIFO floating point average, I would need to do this type of average on a sample of 50 results after they have been converted by a sensor with SPI interface, looking in the forum it does not seem like anyone has already done so, what is the correct approach for you?
Thank you

Re: AVERAGE FLOATING POINT

Posted: Wed Aug 25, 2021 4:41 pm
by medelec35
Hi.
I'm using a BME280 Temperature/Humidity/Pressure Sensor.
So I could calculate altitude, It was vital the pressure float results were ultra-stable.
To do that I used
Very stable readings.png
Very stable readings.png (145.45 KiB) Viewed 2887 times
The altitude was so stable it could measure the height of me lol.
Note: I used ground to keep floats rounded up to 2dp.

Re: AVERAGE FLOATING POINT

Posted: Wed Aug 25, 2021 9:32 pm
by maxtisc
Hello Medelec35
thanks for your submission but I think it is not good for my work, I had already done such a thing, if I understand correctly yours is a mathematical average made on 150 samples, and having a loop of 1 mS it can be said that the result that you could theoretically see on a display would be every 150mS, unfortunately my times are much longer as I receive new data only every 200mS so with an average set to 50 I have a new value to send to the display every 10Sec, I would need it a FIFO type average, that when I filled the buffer, I update the oldest data with the new one and recalculate the result, and I would have an update every 200mS
I hope I have understood your example, otherwise correct me
thanks for your help

Re: AVERAGE FLOATING POINT

Posted: Wed Aug 25, 2021 9:58 pm
by mnf
Here's an example using a FIFO list of data (just random data here...)
fifo.fcfx
(12.69 KiB) Downloaded 197 times
The macro AddValue does all the work - main just pumps in data and displays debug to UART (with a lomg delay that could be reduced for simulation...)

Martin

Re: AVERAGE FLOATING POINT

Posted: Wed Aug 25, 2021 10:25 pm
by chipfryer27
Thanks Martin

A new string to my bow.

Regards

Re: AVERAGE FLOATING POINT

Posted: Wed Aug 25, 2021 11:59 pm
by mnf
No problem, hope it is useful

If speed is important then make the sum global and subtract the existing value (buffer[pos]) before adding the new value... Then you can remove the loop too...

Math

Re: AVERAGE FLOATING POINT

Posted: Thu Aug 26, 2021 11:06 am
by maxtisc
Great Martin, looks like what I needed, in simulation works well, soon I try to do it in my program
thank you very much