fixed point taylor sine/cosine approximation model

Can anybody share sine/cosine taylor approx model which is compatible with hdl coder?

2 件のコメント

Walter Roberson
Walter Roberson 2022 年 6 月 19 日
is there a reason why you are not using https://www.mathworks.com/help/fixedpoint/ref/cordicsin.html
Gary
Gary 2022 年 6 月 21 日
I do not wish to use the inbuilt model of simulink but to build one.

サインインしてコメントする。

回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022 年 6 月 19 日

0 投票

WHy not to use matlab's built-in taylor() expansion fcn: https://www.mathworks.com/help/symbolic/sym.taylor.html?s_tid=doc_ta
E.g.:
syms x
taylor(sin(x), x, pi)
ans = 
taylor(cos(x), x, pi/2)
ans = 

20 件のコメント

Gary
Gary 2022 年 6 月 20 日
does this work on all four quadrants?
Walter Roberson
Walter Roberson 2022 年 6 月 20 日
In practice, finite Taylor series get wildly inaccurate outside of the quadrant they were optimized for. If you must use taylor then do argument reduction to move into one quadrant that you have expanded.
Gary
Gary 2022 年 6 月 20 日
being novice, I did not quite understand what you mean. Could you elaborate with example.
Gary
Gary 2022 年 6 月 21 日
My requirements is that I have theta input and I need to produce sin(theta) and cosine(theta). Theta varies from -pi to +pi. The standard expansion of taylor (sin(x)) works for me. Now I want to change the range from 0 to 2pi. What is the method? and I did not find any nonlinearity for the range specified(-pi to +pi). As I could plot a perfect sine/cosine wave.
Walter Roberson
Walter Roberson 2022 年 6 月 21 日
Example of how taylor series can get very inaccurate:
syms x
f = sin(x);
t1 = taylor(f, x, 0, 'order', 10)
t1 = 
t2 = taylor(f, x, 0, 'order', 12)
t2 = 
fplot([t1, t2, f], [0 2*pi])
Gary
Gary 2022 年 6 月 21 日
Strange but please see the simulation for taylor model in simulink. It looks perfect.
Walter Roberson
Walter Roberson 2022 年 6 月 21 日
When I run that model, the sine output is all zero, and the cosine output is a copy of the triangle wave at 50 Hz.
Gary
Gary 2022 年 6 月 21 日
When I fix the simulation time to be .02 seconds. The output is perfect. If I fix sim time to be 1 second, I got the similar response that you have mentioned. This is strange
Walter Roberson
Walter Roberson 2022 年 6 月 21 日
You have the limitation that you let the sample time of the sawtooth be "automatic". Simulink does not analyze the taylor series blocks to figure out that the function being modeled is periodic and work out the period and adjust the frequency to sample several times per sawtooth cycle. When your simulation time is large enough, Simulink happens to sample at a multiple of 100 Hz, getting the high and low of the sawtooth, corresponding to sampling at integer multiples of 0 and π, giving you all zero for sin and ±1 for cos
Gary
Gary 2022 年 6 月 22 日
Now can we conclude that taylor series is accurate, since this is a problem with simulink (btw how to resolve this problem in simulink)
Walter Roberson
Walter Roberson 2022 年 6 月 22 日
Wrong conclusion! When you set the sample time smaller you will see more error in Simulink.
Gary
Gary 2022 年 6 月 22 日
So what is the workaround. Is it a problem with simulink or taylor series in general?
Walter Roberson
Walter Roberson 2022 年 6 月 22 日
You need to set an accuracy goal, and figure out how many taylor terms are needed to achieve the accuracy.
Hmmm... and I just realized that you need to add 1 to the output of the sawtooth in order to move to the 0 2π range as it is currently -π π
Gary
Gary 2022 年 6 月 22 日
編集済み: Gary 2022 年 6 月 22 日
With the given domain range -pi to pi, taylor series approx with 7th order is just perfect for sine. No nonlinearity observed in the output. I think the simulation setting in simulink have to be tweaked to get the right results.
Walter Roberson
Walter Roberson 2022 年 6 月 22 日
Reminder that you want to change the range upwards by π so you need to add 1 to sawtooth wave, and you re-test
Walter Roberson
Walter Roberson 2022 年 6 月 22 日
This shows that taylor up to x^7 term is off by 30 by the time you get to 2*pi
syms x
f = sin(x);
t1 = taylor(f, x, 0, 'order', 8)
t1 = 
fplot([t1, f], [0 2*pi])
fplot(t1-f, [0 2*pi])
Walter Roberson
Walter Roberson 2022 年 6 月 22 日
Your simulink model uses up to x^9, and is off by a bit more than 10 by the time you get to 2*pi .
syms x
f = sin(x);
t1 = taylor(f, x, 0, 'order', 10)
t1 = 
fplot([t1, f], [0 2*pi])
fplot(t1-f, [0 2*pi])
Walter Roberson
Walter Roberson 2022 年 6 月 22 日
You need order 22 (x^21) to have an error of less than 1/1000
syms x
f = sin(x);
target = 1/1000;
for order = 2:50
t = taylor(f, x, 0, 'order', order);
val_at_end = subs(t, x, 2*pi);
if abs(val_at_end) < target; break; end
end
order
order = 22
t
t = 
fplot([t, f], [0 2*pi])
fplot(t-f, [0 2*pi])
Gary
Gary 2022 年 6 月 23 日
Thank you . It was excellent analysis. I am clear now.

サインインしてコメントする。

Kiran Kintali
Kiran Kintali 2022 年 7 月 4 日

0 投票

HDL Coder supports code generation for single precision trigonometric functions.
Getting Started with HDL Coder Native Floating-Point Support
Taylor series approximation using HDL Coder
If you want to build Taylor series approximation by youself you could build using basic Math operations and sufficient amount of fixed-point conversion.
syms x
f = sin(x);
T2sin = taylor(f, x, 'Order', 2); % T2sin = x
T4sin = taylor(f, x, 'Order', 4); % T4sin = -x^3/6 + x
T6sin = taylor(f, x, 'Order', 6); % T6sin = x^5/120 - x^3/6 + x
On you build such a model you can further use optimizations such as multiplier partitioning, resource sharing and pipelining options to optimize the model for area/performance/latency/power.

2 件のコメント

Walter Roberson
Walter Roberson 2022 年 7 月 4 日
They were already using a model with basic math blocks to calculate Taylor series of sine and cosine. I showed, however, that in their target range 0 to 2π that the error for their model was unacceptable, and that to bring the error to 1/1000 you need taylor order 21.
Gary
Gary 2022 年 7 月 17 日
I managed to get 3 digits accuracy sine/cosine using chebhyshev polynomials(order 3). Thank you for sharing all the resources

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCode Generation についてさらに検索

製品

リリース

R2014a

タグ

質問済み:

2022 年 6 月 19 日

コメント済み:

2022 年 7 月 17 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by