2

I'm working with python on raspberry pi. I'm using complementary filter to get better values from gyroscope, but it eats too much raspberry's power - it's about 70%. I thought I could increase performance by reducing floating point precision. Now, results have about 12 decimal places, it's way more than I need. Is there any way to set maximum precision? Just rounding the number doesn't meet my needs, since it's just another calculation. Thanks!

Edit: I have tried to use Decimal module and with precision set to 6 it was nearly 6 times slower than float! Is there any other way to work with fixed-point numbers than Decimal (it looks to be created for higher precision than for performance)

ametis
  • 41
  • 5
  • Do you neet floating point? What's wrong with fixed point arithmetic? – Willem Van Onsem Jul 09 '14 at 08:47
  • @CommuSoft: Oh, I probably misunderstood something. How can I set a variable in python to be fixed-point? If i can, it will be the best solution. – ametis Jul 09 '14 at 08:56
  • So, i tried to use Decimal to set precision... and it even worse than simple float :/ – ametis Jul 09 '14 at 09:16
  • Have you tried gmpy (http://www.aleax.it/gmpy.html)? or `cdecimal`? – Willem Van Onsem Jul 09 '14 at 09:17
  • [reduce calculation precision to speed up execution](https://stackoverflow.com/q/50519298/995714), [Is there a way to speed up python via float precision or limiting float range?](https://stackoverflow.com/q/71317117/995714) – phuclv Mar 19 '22 at 09:35

2 Answers2

3

You can force single precision floating point calculations using numpy.

However, I would be very surprised if using single precision floating point worked out any faster than double precision: the raspberry pi has hardware floating point support so I would expect that all calculations are done at full 80 bit precision and then rounded for 32 bit or 64 bit results when saving to memory. The only possible gain would be slightly less memory bandwidth used when saving the values.

Duncan
  • 92,073
  • 11
  • 122
  • 156
  • 1
    Where does 80 bit precision comes from? – Diego Herranz Jul 10 '14 at 15:47
  • 80 bits is the extended precision used internally on x87 architectures to avoid loss of precision during calculations. I don't know whether the ARM does the same or whether it only uses 64 bits internally. – Duncan Jul 10 '14 at 17:01
  • At least on Raspberry Pi Debian (ARM with GCC & clang), 32 bit OS gives you 64-bit HW _long doubles_, whereas 64-bit OS gives you 128-bit software _long doubles_. On x86 you get full 80-bit _long doubles_ externally. I am constantly amused by the contempt shown for mathematical precision among the machine learning community & others. Half precision indeed ;) – m4r35n357 Aug 16 '23 at 10:46
3

It may be that you have the wrong end of the stick.

The data flow form a gyroscope is rather slow, so you should have ample time to filter it with any reasonable filter. Even a Kalman filter should be usable (though probably unnecessary). How often do you sample the gyroscope and accelerometer data? Reasonable maximum values are few hundred Hertz, not more.

The complementary filter for accelerometer and gyroscope measurement is very lightweight, and it by itself should consume very little processing power. It can be implemented on a slow 8-bit processor, so Raspberry is way too fast for it.

Depending on what you do with the complementary filter, the filter itself needs a few floating point operations. If you calculate arcus tangents or equivalent functions, that'll require hundreds of FLOPs. If you do that at a rate of 1 kHz, you'll consume maybe a few hundred kFLOPS (FLoating-point OPerations per Second). The FP throughput of a RPi is approximately 100 MLFOPS, so there is a lot of margin.

Reducing the FP precision will thus not help significantly, the problem is elsewhere. Maybe if you show a bit more of your code, it could be determined where the problem is!

DrV
  • 22,637
  • 7
  • 60
  • 72
  • 1
    The refresh rate is 10ms (100Hz) so there has to be some bad mistake :/ I will revise my code again. – ametis Jul 09 '14 at 10:30
  • 1
    Well. I had bad condition in while loop, and it took all cpu power... now the usage is about 1-3%. I'm sorry for that. – ametis Jul 09 '14 at 10:40