Using Curve Fitting to Calculate f(x)

 
by DSPStore Staff

Any function f(x) can be approximated graphically by a series of values along with intermediate slopes and intercepts (Figure 1) valid over specific ranges of the input variable x. Thus, to generate the value of f(x) for a given value of x, the algorithm must firstly determine the range occupied by the input value x and then calculate the output value y from the formula for a straight line:

     y = mx + c

where m and c are the predetermined slope and intercept point for the straight line fit over the selected input range. For example, assume an input value of 0.4 for which we require the logarithm (base 10).


From Figure 1, we note that, for the input range from 0.25 to 0.5, the straight line approximation {2} has a slope of 12.0 and an intercept on the y axis of -9.0. Making use of the above equation, we can calculate that the logarithm of 0.4 is -4.2 dB. (Actual value is -3.98 dB.)

In this example, the ranges into which the input is divided are based on powers of two, i.e. 1 to 0.5, 0.5 to 0.25, 0.25 to 0.125, etc. Some functions, however, are best approximated using a linear division of ranges, i.e. 1 to 0.9, 0.9 to 0.8, 0.8 to 0.7, etc., particularly those with a small dynamic range.

In some cases, it may be found that the slope and intercept values for all of the ranges of a curve fit can be represented by a small number of distinct values plus an appropriate left or right shift. This property allows a considerable saving in memory for storing the curve fit constants, and may also lead to simplification of the curve fit routine.

As an example, consider the function y = v (x). The equations for calculating the slope m and intercept values for each range are

       Slope = (y2 - y1)/(x2 - x1)

where (x1, y1) are the starting co ordinates of the range being considered, and (x2, y2) the end co ordinates (start of next range).

       Intercept = y1 - mx1

The two equations above yield the slope and intercept values shown in Table 1. Note that there are only two distinct values, (v2 - 1) and (2 - v2), plus appropriate shifts, to represent the slopes and intercepts for all four ranges.

The curve fitting technique can be applied to a vast range of arithmetic functions and is relatively efficient both in terms of flexibility and algorithm execution time. To emphasise this point, consider the function

       y = a/vb

This can be performed in two ways. The first method is to use a curve fitting routine to find vb and then perform the division a/vb. The second method is to approximate the function f(b) = 1/vb and then multiply the result by a.

In terms of program execution time, the second method is considerably more efficient. The curve fitting routine will take approximately the same time to evaluate both vb and 1/vb, whereas the division algorithm is substantially more complex than simple multiplication. This technique can be used in a wide variety of DSP applications.