“What is the best way to solve the problem?” This is of course a very context dependent question. The best depends on what you need out of your solution. Often in the embedded context best means fast, low memory and “accurate enough.” This is where regression comes into play.
Regressions ~= Approximations
I have been doing a lot of coordinate transformation problems over the last couple of months: sine, cosine heavy operations, neither of which are efficient on my target hardware. My first thought was to use a Taylor’s series approximation (cos(x) = 1 – x^2 / 2! + x^4/4! – x^6/6!) however even with precompuation of the factorials (and leveraging the earlier powers to get the higher powers) the performance of this was too intensive.
My solution was to use a piece-wise quadratic regression. While I paid a cost in determining which region I was in, the total execution time was 1/5 the direct cosine call and 1/2 the Taylor series.
As this last simple example shows, you don’t need the highest resolution to represent your target. Regressions need to be “good enough”. But how do you know if it is good enough?
Ask the following questions…
- What range do I need to cover: The larger the range of the regression the more terms are required to keep the error level down. Understand the range of your inputs when designing the regression.
- What resolution do I need: To hit 99.999% accuracy requires a more complex regression then requiring 98% accuracy.
- Are there spikes: Validate there are no spikes (i.e. large errors) in the range under consideration.
- Are errors cumulative: One trick to speed up regressions is to use last pass data, 99.999% of the time this does not introduce errors, but look out for the 0.001%.
- How expensive is my regression: Finally, make sure the regression you come up with is less computationally expensive than the equations you had.
Connecting the dots: the power of Physics
When performing a regression for control systems, more often then not the basic underlying equations are understood. Because of this the correct form for the regression can be determined up front. For more information on performing regression I would recommend this link from MathWorks.