Transcendental Functions in Low Level C
To produce the sine function this system works by simplifying the sine wave using a Taylor Series approximation.
With the first three terms it is possible to produce error rates below 0.23% at the fringes and up to 15 decimal places of precision at 0. However the function needs further approximation before it can be utilized on the Xilinx board.
We will use the method of Finite Difference, sometimes called Newton’s method of divide difference. Apparently all polynomials can be simplified using the method of finite difference. We have to apply a Taylor Series approxamtion first beacuse the Finite Difference method will only work on polynomials. The Taylor Series used here is a 5th degree polynomial, and this method will apply to it, even as it is scale (to between 0 and 4095), and shifted (to all values above 0V).
The method of finite difference says that every polynomial can be simplified by taking the difference of two subsequent terms.
Diff_1=f(x)-f(x-1)
Diff_2=Diff_1n – Diff_1n-1
Diff_2 is a constant because f(x) in the example is a 2nd degree polynomial.
This system can be used in reverse to predict the next term in a series. The constant of this series (2), plus Diff_1 and the current f(x) produces the next value:
2+7=9
16+9=25
Getting the initial seed values require calculation of the original function, but after that’s done (offline), the rest can be computed using only the addition operations. For a polynomial of degree 5, five seed values are required.
The system must represent a maximum value of 4095 and a minimum of .00006(Diff_5 the smallest seed value rounded up). Any numbers larger than this will overflow the 32 bit registers used by
MicroBlaze.
note: There are a few magic numbers in my solutions, because I've applied this mehtod to solve a very speicfic problem, perhaps later I'll produce a more general solution.
The Code(Aside from some html copy paste wonkyness ist's all here):
void Update_sin(){
Set_Sin_wave(freq);
}