I want to calculate y(n)=32677Sin(45/1024•n)
, where y
is an integer and n
ranges from 0 to 2048. How can I make this process quicker and more precisely?
Now I want to show you a reference answer:
Since Sin(a+b)=Sin(a)Cos(b)+Cos(a)Sin(b)
And Cos(a+b)=Cos(a)Cos(b)-Sin(a)Cos(b)
.
So I can store Sin(45/1024•1)
and Cos(45/1024•1)
only.Then use this formula:
Sin(45/1024•2)=Sin(45/1024•1+45/1024•1)
,
Cos(45/1024•2)=Cos(45/1024•1+45/1024•1)
,
Sin(45/1024•n)=Sin(45/1024•(n-1)+45/1024•1)
,
Cos(45/1024•n)=Cos(45/1024•(n-1)+45/1024•1)
,
This way maybe quicker without storing large array.
7
If n
ranges from 0 to 2048, you can pre-calculate the values, store then in an array. y(n)
would become values[n]
.
10
Compute the table at compile time instead of run time.
You’re doing a 2048-element table of 16-bit scaled integer values.
Write a cheap Matlab script, with a print that gives a data line suitable for your final programming language. Cut-and-paste the result into your source code, as a constant data table, and do a table lookup at run time. This pushes the initial computation time off into the build cycle, instead of the program startup time.
2
Given the form of the function, the natural answer is the CORDIC algorithm. It’s a much cleaner approach than the breakdown in the question. On the other hand, the table it needs is far, far smaller than the table others have suggested.