1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_sin_cos_f32.c
10 * Description: Sine and Cosine calculation for floating-point values.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated.
28 * -------------------------------------------------------------------- */
33 * @ingroup groupController
37 * @defgroup SinCos Sine Cosine
39 * Computes the trigonometric sine and cosine values using a combination of table lookup
40 * and linear interpolation.
41 * There are separate functions for Q31 and floating-point data types.
42 * The input to the floating-point version is in degrees while the
43 * fixed-point Q31 have a scaled input with the range
44 * [-1 1) mapping to [-180 180) degrees.
46 * The implementation is based on table lookup using 360 values together with linear interpolation.
48 * -# Calculation of the nearest integer table index.
49 * -# Compute the fractional portion (fract) of the input.
50 * -# Fetch the value corresponding to \c index from sine table to \c y0 and also value from \c index+1 to \c y1.
51 * -# Sine value is computed as <code> *psinVal = y0 + (fract * (y1 - y0))</code>.
52 * -# Fetch the value corresponding to \c index from cosine table to \c y0 and also value from \c index+1 to \c y1.
53 * -# Cosine value is computed as <code> *pcosVal = y0 + (fract * (y1 - y0))</code>.
64 * Cosine Table is generated from following loop
65 * <pre>for(i = 0; i < 360; i++)
67 * cosTable[i]= cos((i-180) * PI/180.0);
71 static const float32_t cosTable[360] = {
72 -0.999847695156391270f, -0.999390827019095760f, -0.998629534754573830f,
73 -0.997564050259824200f, -0.996194698091745550f, -0.994521895368273290f,
74 -0.992546151641321980f, -0.990268068741570250f,
75 -0.987688340595137660f, -0.984807753012208020f, -0.981627183447663980f,
76 -0.978147600733805690f, -0.974370064785235250f, -0.970295726275996470f,
77 -0.965925826289068200f, -0.961261695938318670f,
78 -0.956304755963035440f, -0.951056516295153530f, -0.945518575599316740f,
79 -0.939692620785908320f, -0.933580426497201740f, -0.927183854566787310f,
80 -0.920504853452440150f, -0.913545457642600760f,
81 -0.906307787036649940f, -0.898794046299167040f, -0.891006524188367790f,
82 -0.882947592858926770f, -0.874619707139395740f, -0.866025403784438710f,
83 -0.857167300702112220f, -0.848048096156425960f,
84 -0.838670567945424160f, -0.829037572555041620f, -0.819152044288991580f,
85 -0.809016994374947340f, -0.798635510047292940f, -0.788010753606721900f,
86 -0.777145961456970680f, -0.766044443118977900f,
87 -0.754709580222772010f, -0.743144825477394130f, -0.731353701619170460f,
88 -0.719339800338651300f, -0.707106781186547460f, -0.694658370458997030f,
89 -0.681998360062498370f, -0.669130606358858240f,
90 -0.656059028990507500f, -0.642787609686539360f, -0.629320391049837280f,
91 -0.615661475325658290f, -0.601815023152048380f, -0.587785252292473030f,
92 -0.573576436351045830f, -0.559192903470746680f,
93 -0.544639035015027080f, -0.529919264233204790f, -0.515038074910054270f,
94 -0.499999999999999780f, -0.484809620246337000f, -0.469471562785890530f,
95 -0.453990499739546750f, -0.438371146789077510f,
96 -0.422618261740699330f, -0.406736643075800100f, -0.390731128489273600f,
97 -0.374606593415912070f, -0.358367949545300270f, -0.342020143325668710f,
98 -0.325568154457156420f, -0.309016994374947340f,
99 -0.292371704722736660f, -0.275637355816999050f, -0.258819045102520850f,
100 -0.241921895599667790f, -0.224951054343864810f, -0.207911690817759120f,
101 -0.190808995376544800f, -0.173648177666930300f,
102 -0.156434465040231040f, -0.139173100960065350f, -0.121869343405147370f,
103 -0.104528463267653330f, -0.087155742747658235f, -0.069756473744125330f,
104 -0.052335956242943620f, -0.034899496702500733f,
105 -0.017452406437283477f, 0.000000000000000061f, 0.017452406437283376f,
106 0.034899496702501080f, 0.052335956242943966f, 0.069756473744125455f,
107 0.087155742747658138f, 0.104528463267653460f,
108 0.121869343405147490f, 0.139173100960065690f, 0.156434465040230920f,
109 0.173648177666930410f, 0.190808995376544920f, 0.207911690817759450f,
110 0.224951054343864920f, 0.241921895599667900f,
111 0.258819045102520740f, 0.275637355816999160f, 0.292371704722736770f,
112 0.309016994374947450f, 0.325568154457156760f, 0.342020143325668820f,
113 0.358367949545300380f, 0.374606593415911960f,
114 0.390731128489273940f, 0.406736643075800210f, 0.422618261740699440f,
115 0.438371146789077460f, 0.453990499739546860f, 0.469471562785890860f,
116 0.484809620246337110f, 0.500000000000000110f,
117 0.515038074910054380f, 0.529919264233204900f, 0.544639035015027200f,
118 0.559192903470746790f, 0.573576436351046050f, 0.587785252292473140f,
119 0.601815023152048270f, 0.615661475325658290f,
120 0.629320391049837500f, 0.642787609686539360f, 0.656059028990507280f,
121 0.669130606358858240f, 0.681998360062498480f, 0.694658370458997370f,
122 0.707106781186547570f, 0.719339800338651190f,
123 0.731353701619170570f, 0.743144825477394240f, 0.754709580222772010f,
124 0.766044443118978010f, 0.777145961456970900f, 0.788010753606722010f,
125 0.798635510047292830f, 0.809016994374947450f,
126 0.819152044288991800f, 0.829037572555041620f, 0.838670567945424050f,
127 0.848048096156425960f, 0.857167300702112330f, 0.866025403784438710f,
128 0.874619707139395740f, 0.882947592858926990f,
129 0.891006524188367900f, 0.898794046299167040f, 0.906307787036649940f,
130 0.913545457642600870f, 0.920504853452440370f, 0.927183854566787420f,
131 0.933580426497201740f, 0.939692620785908430f,
132 0.945518575599316850f, 0.951056516295153530f, 0.956304755963035440f,
133 0.961261695938318890f, 0.965925826289068310f, 0.970295726275996470f,
134 0.974370064785235250f, 0.978147600733805690f,
135 0.981627183447663980f, 0.984807753012208020f, 0.987688340595137770f,
136 0.990268068741570360f, 0.992546151641321980f, 0.994521895368273290f,
137 0.996194698091745550f, 0.997564050259824200f,
138 0.998629534754573830f, 0.999390827019095760f, 0.999847695156391270f,
139 1.000000000000000000f, 0.999847695156391270f, 0.999390827019095760f,
140 0.998629534754573830f, 0.997564050259824200f,
141 0.996194698091745550f, 0.994521895368273290f, 0.992546151641321980f,
142 0.990268068741570360f, 0.987688340595137770f, 0.984807753012208020f,
143 0.981627183447663980f, 0.978147600733805690f,
144 0.974370064785235250f, 0.970295726275996470f, 0.965925826289068310f,
145 0.961261695938318890f, 0.956304755963035440f, 0.951056516295153530f,
146 0.945518575599316850f, 0.939692620785908430f,
147 0.933580426497201740f, 0.927183854566787420f, 0.920504853452440370f,
148 0.913545457642600870f, 0.906307787036649940f, 0.898794046299167040f,
149 0.891006524188367900f, 0.882947592858926990f,
150 0.874619707139395740f, 0.866025403784438710f, 0.857167300702112330f,
151 0.848048096156425960f, 0.838670567945424050f, 0.829037572555041620f,
152 0.819152044288991800f, 0.809016994374947450f,
153 0.798635510047292830f, 0.788010753606722010f, 0.777145961456970900f,
154 0.766044443118978010f, 0.754709580222772010f, 0.743144825477394240f,
155 0.731353701619170570f, 0.719339800338651190f,
156 0.707106781186547570f, 0.694658370458997370f, 0.681998360062498480f,
157 0.669130606358858240f, 0.656059028990507280f, 0.642787609686539360f,
158 0.629320391049837500f, 0.615661475325658290f,
159 0.601815023152048270f, 0.587785252292473140f, 0.573576436351046050f,
160 0.559192903470746790f, 0.544639035015027200f, 0.529919264233204900f,
161 0.515038074910054380f, 0.500000000000000110f,
162 0.484809620246337110f, 0.469471562785890860f, 0.453990499739546860f,
163 0.438371146789077460f, 0.422618261740699440f, 0.406736643075800210f,
164 0.390731128489273940f, 0.374606593415911960f,
165 0.358367949545300380f, 0.342020143325668820f, 0.325568154457156760f,
166 0.309016994374947450f, 0.292371704722736770f, 0.275637355816999160f,
167 0.258819045102520740f, 0.241921895599667900f,
168 0.224951054343864920f, 0.207911690817759450f, 0.190808995376544920f,
169 0.173648177666930410f, 0.156434465040230920f, 0.139173100960065690f,
170 0.121869343405147490f, 0.104528463267653460f,
171 0.087155742747658138f, 0.069756473744125455f, 0.052335956242943966f,
172 0.034899496702501080f, 0.017452406437283376f, 0.000000000000000061f,
173 -0.017452406437283477f, -0.034899496702500733f,
174 -0.052335956242943620f, -0.069756473744125330f, -0.087155742747658235f,
175 -0.104528463267653330f, -0.121869343405147370f, -0.139173100960065350f,
176 -0.156434465040231040f, -0.173648177666930300f,
177 -0.190808995376544800f, -0.207911690817759120f, -0.224951054343864810f,
178 -0.241921895599667790f, -0.258819045102520850f, -0.275637355816999050f,
179 -0.292371704722736660f, -0.309016994374947340f,
180 -0.325568154457156420f, -0.342020143325668710f, -0.358367949545300270f,
181 -0.374606593415912070f, -0.390731128489273600f, -0.406736643075800100f,
182 -0.422618261740699330f, -0.438371146789077510f,
183 -0.453990499739546750f, -0.469471562785890530f, -0.484809620246337000f,
184 -0.499999999999999780f, -0.515038074910054270f, -0.529919264233204790f,
185 -0.544639035015027080f, -0.559192903470746680f,
186 -0.573576436351045830f, -0.587785252292473030f, -0.601815023152048380f,
187 -0.615661475325658290f, -0.629320391049837280f, -0.642787609686539360f,
188 -0.656059028990507500f, -0.669130606358858240f,
189 -0.681998360062498370f, -0.694658370458997030f, -0.707106781186547460f,
190 -0.719339800338651300f, -0.731353701619170460f, -0.743144825477394130f,
191 -0.754709580222772010f, -0.766044443118977900f,
192 -0.777145961456970680f, -0.788010753606721900f, -0.798635510047292940f,
193 -0.809016994374947340f, -0.819152044288991580f, -0.829037572555041620f,
194 -0.838670567945424160f, -0.848048096156425960f,
195 -0.857167300702112220f, -0.866025403784438710f, -0.874619707139395740f,
196 -0.882947592858926770f, -0.891006524188367790f, -0.898794046299167040f,
197 -0.906307787036649940f, -0.913545457642600760f,
198 -0.920504853452440150f, -0.927183854566787310f, -0.933580426497201740f,
199 -0.939692620785908320f, -0.945518575599316740f, -0.951056516295153530f,
200 -0.956304755963035440f, -0.961261695938318670f,
201 -0.965925826289068200f, -0.970295726275996470f, -0.974370064785235250f,
202 -0.978147600733805690f, -0.981627183447663980f, -0.984807753012208020f,
203 -0.987688340595137660f, -0.990268068741570250f,
204 -0.992546151641321980f, -0.994521895368273290f, -0.996194698091745550f,
205 -0.997564050259824200f, -0.998629534754573830f, -0.999390827019095760f,
206 -0.999847695156391270f, -1.000000000000000000f
211 * Sine Table is generated from following loop
212 * <pre>for(i = 0; i < 360; i++)
214 * sinTable[i]= sin((i-180) * PI/180.0);
219 static const float32_t sinTable[360] = {
220 -0.017452406437283439f, -0.034899496702500699f, -0.052335956242943807f,
221 -0.069756473744125524f, -0.087155742747658638f, -0.104528463267653730f,
222 -0.121869343405147550f, -0.139173100960065740f,
223 -0.156434465040230980f, -0.173648177666930280f, -0.190808995376544970f,
224 -0.207911690817759310f, -0.224951054343864780f, -0.241921895599667730f,
225 -0.258819045102521020f, -0.275637355816999660f,
226 -0.292371704722737050f, -0.309016994374947510f, -0.325568154457156980f,
227 -0.342020143325668880f, -0.358367949545300210f, -0.374606593415912240f,
228 -0.390731128489274160f, -0.406736643075800430f,
229 -0.422618261740699500f, -0.438371146789077290f, -0.453990499739546860f,
230 -0.469471562785891080f, -0.484809620246337170f, -0.499999999999999940f,
231 -0.515038074910054380f, -0.529919264233204900f,
232 -0.544639035015026860f, -0.559192903470746900f, -0.573576436351046380f,
233 -0.587785252292473250f, -0.601815023152048160f, -0.615661475325658400f,
234 -0.629320391049837720f, -0.642787609686539470f,
235 -0.656059028990507280f, -0.669130606358858350f, -0.681998360062498590f,
236 -0.694658370458997140f, -0.707106781186547570f, -0.719339800338651410f,
237 -0.731353701619170570f, -0.743144825477394240f,
238 -0.754709580222771790f, -0.766044443118978010f, -0.777145961456971010f,
239 -0.788010753606722010f, -0.798635510047292720f, -0.809016994374947450f,
240 -0.819152044288992020f, -0.829037572555041740f,
241 -0.838670567945424050f, -0.848048096156426070f, -0.857167300702112330f,
242 -0.866025403784438710f, -0.874619707139395850f, -0.882947592858927100f,
243 -0.891006524188367900f, -0.898794046299166930f,
244 -0.906307787036650050f, -0.913545457642600980f, -0.920504853452440370f,
245 -0.927183854566787420f, -0.933580426497201740f, -0.939692620785908430f,
246 -0.945518575599316850f, -0.951056516295153640f,
247 -0.956304755963035550f, -0.961261695938318890f, -0.965925826289068310f,
248 -0.970295726275996470f, -0.974370064785235250f, -0.978147600733805690f,
249 -0.981627183447663980f, -0.984807753012208020f,
250 -0.987688340595137660f, -0.990268068741570360f, -0.992546151641322090f,
251 -0.994521895368273400f, -0.996194698091745550f, -0.997564050259824200f,
252 -0.998629534754573830f, -0.999390827019095760f,
253 -0.999847695156391270f, -1.000000000000000000f, -0.999847695156391270f,
254 -0.999390827019095760f, -0.998629534754573830f, -0.997564050259824200f,
255 -0.996194698091745550f, -0.994521895368273290f,
256 -0.992546151641321980f, -0.990268068741570250f, -0.987688340595137770f,
257 -0.984807753012208020f, -0.981627183447663980f, -0.978147600733805580f,
258 -0.974370064785235250f, -0.970295726275996470f,
259 -0.965925826289068310f, -0.961261695938318890f, -0.956304755963035440f,
260 -0.951056516295153530f, -0.945518575599316740f, -0.939692620785908320f,
261 -0.933580426497201740f, -0.927183854566787420f,
262 -0.920504853452440260f, -0.913545457642600870f, -0.906307787036649940f,
263 -0.898794046299167040f, -0.891006524188367790f, -0.882947592858926880f,
264 -0.874619707139395740f, -0.866025403784438600f,
265 -0.857167300702112220f, -0.848048096156426070f, -0.838670567945423940f,
266 -0.829037572555041740f, -0.819152044288991800f, -0.809016994374947450f,
267 -0.798635510047292830f, -0.788010753606722010f,
268 -0.777145961456970790f, -0.766044443118978010f, -0.754709580222772010f,
269 -0.743144825477394240f, -0.731353701619170460f, -0.719339800338651080f,
270 -0.707106781186547460f, -0.694658370458997250f,
271 -0.681998360062498480f, -0.669130606358858240f, -0.656059028990507160f,
272 -0.642787609686539250f, -0.629320391049837390f, -0.615661475325658180f,
273 -0.601815023152048270f, -0.587785252292473140f,
274 -0.573576436351046050f, -0.559192903470746900f, -0.544639035015027080f,
275 -0.529919264233204900f, -0.515038074910054160f, -0.499999999999999940f,
276 -0.484809620246337060f, -0.469471562785890810f,
277 -0.453990499739546750f, -0.438371146789077400f, -0.422618261740699440f,
278 -0.406736643075800150f, -0.390731128489273720f, -0.374606593415912010f,
279 -0.358367949545300270f, -0.342020143325668710f,
280 -0.325568154457156640f, -0.309016994374947400f, -0.292371704722736770f,
281 -0.275637355816999160f, -0.258819045102520740f, -0.241921895599667730f,
282 -0.224951054343865000f, -0.207911690817759310f,
283 -0.190808995376544800f, -0.173648177666930330f, -0.156434465040230870f,
284 -0.139173100960065440f, -0.121869343405147480f, -0.104528463267653460f,
285 -0.087155742747658166f, -0.069756473744125302f,
286 -0.052335956242943828f, -0.034899496702500969f, -0.017452406437283512f,
287 0.000000000000000000f, 0.017452406437283512f, 0.034899496702500969f,
288 0.052335956242943828f, 0.069756473744125302f,
289 0.087155742747658166f, 0.104528463267653460f, 0.121869343405147480f,
290 0.139173100960065440f, 0.156434465040230870f, 0.173648177666930330f,
291 0.190808995376544800f, 0.207911690817759310f,
292 0.224951054343865000f, 0.241921895599667730f, 0.258819045102520740f,
293 0.275637355816999160f, 0.292371704722736770f, 0.309016994374947400f,
294 0.325568154457156640f, 0.342020143325668710f,
295 0.358367949545300270f, 0.374606593415912010f, 0.390731128489273720f,
296 0.406736643075800150f, 0.422618261740699440f, 0.438371146789077400f,
297 0.453990499739546750f, 0.469471562785890810f,
298 0.484809620246337060f, 0.499999999999999940f, 0.515038074910054160f,
299 0.529919264233204900f, 0.544639035015027080f, 0.559192903470746900f,
300 0.573576436351046050f, 0.587785252292473140f,
301 0.601815023152048270f, 0.615661475325658180f, 0.629320391049837390f,
302 0.642787609686539250f, 0.656059028990507160f, 0.669130606358858240f,
303 0.681998360062498480f, 0.694658370458997250f,
304 0.707106781186547460f, 0.719339800338651080f, 0.731353701619170460f,
305 0.743144825477394240f, 0.754709580222772010f, 0.766044443118978010f,
306 0.777145961456970790f, 0.788010753606722010f,
307 0.798635510047292830f, 0.809016994374947450f, 0.819152044288991800f,
308 0.829037572555041740f, 0.838670567945423940f, 0.848048096156426070f,
309 0.857167300702112220f, 0.866025403784438600f,
310 0.874619707139395740f, 0.882947592858926880f, 0.891006524188367790f,
311 0.898794046299167040f, 0.906307787036649940f, 0.913545457642600870f,
312 0.920504853452440260f, 0.927183854566787420f,
313 0.933580426497201740f, 0.939692620785908320f, 0.945518575599316740f,
314 0.951056516295153530f, 0.956304755963035440f, 0.961261695938318890f,
315 0.965925826289068310f, 0.970295726275996470f,
316 0.974370064785235250f, 0.978147600733805580f, 0.981627183447663980f,
317 0.984807753012208020f, 0.987688340595137770f, 0.990268068741570250f,
318 0.992546151641321980f, 0.994521895368273290f,
319 0.996194698091745550f, 0.997564050259824200f, 0.998629534754573830f,
320 0.999390827019095760f, 0.999847695156391270f, 1.000000000000000000f,
321 0.999847695156391270f, 0.999390827019095760f,
322 0.998629534754573830f, 0.997564050259824200f, 0.996194698091745550f,
323 0.994521895368273400f, 0.992546151641322090f, 0.990268068741570360f,
324 0.987688340595137660f, 0.984807753012208020f,
325 0.981627183447663980f, 0.978147600733805690f, 0.974370064785235250f,
326 0.970295726275996470f, 0.965925826289068310f, 0.961261695938318890f,
327 0.956304755963035550f, 0.951056516295153640f,
328 0.945518575599316850f, 0.939692620785908430f, 0.933580426497201740f,
329 0.927183854566787420f, 0.920504853452440370f, 0.913545457642600980f,
330 0.906307787036650050f, 0.898794046299166930f,
331 0.891006524188367900f, 0.882947592858927100f, 0.874619707139395850f,
332 0.866025403784438710f, 0.857167300702112330f, 0.848048096156426070f,
333 0.838670567945424050f, 0.829037572555041740f,
334 0.819152044288992020f, 0.809016994374947450f, 0.798635510047292720f,
335 0.788010753606722010f, 0.777145961456971010f, 0.766044443118978010f,
336 0.754709580222771790f, 0.743144825477394240f,
337 0.731353701619170570f, 0.719339800338651410f, 0.707106781186547570f,
338 0.694658370458997140f, 0.681998360062498590f, 0.669130606358858350f,
339 0.656059028990507280f, 0.642787609686539470f,
340 0.629320391049837720f, 0.615661475325658400f, 0.601815023152048160f,
341 0.587785252292473250f, 0.573576436351046380f, 0.559192903470746900f,
342 0.544639035015026860f, 0.529919264233204900f,
343 0.515038074910054380f, 0.499999999999999940f, 0.484809620246337170f,
344 0.469471562785891080f, 0.453990499739546860f, 0.438371146789077290f,
345 0.422618261740699500f, 0.406736643075800430f,
346 0.390731128489274160f, 0.374606593415912240f, 0.358367949545300210f,
347 0.342020143325668880f, 0.325568154457156980f, 0.309016994374947510f,
348 0.292371704722737050f, 0.275637355816999660f,
349 0.258819045102521020f, 0.241921895599667730f, 0.224951054343864780f,
350 0.207911690817759310f, 0.190808995376544970f, 0.173648177666930280f,
351 0.156434465040230980f, 0.139173100960065740f,
352 0.121869343405147550f, 0.104528463267653730f, 0.087155742747658638f,
353 0.069756473744125524f, 0.052335956242943807f, 0.034899496702500699f,
354 0.017452406437283439f, 0.000000000000000122f
359 * @brief Floating-point sin_cos function.
360 * @param[in] theta input value in degrees
361 * @param[out] *pSinVal points to the processed sine output.
362 * @param[out] *pCosVal points to the processed cos output.
367 void arm_sin_cos_f32(
372 uint32_t i; /* Index for reading nearwst output values */
373 float32_t x1 = -179.0f; /* Initial input value */
374 float32_t y0, y1; /* nearest output values */
375 float32_t fract; /* fractional part of input */
377 /* Calculation of fractional part */
380 fract = theta - (float32_t) ((int32_t) theta);
384 fract = (theta - (float32_t) ((int32_t) theta)) + 1.0f;
387 /* index calculation for reading nearest output values */
388 i = (uint32_t) (theta - x1);
390 /* reading nearest sine output values */
392 y1 = sinTable[i + 1u];
394 /* Calculation of sine value */
395 *pSinVal = y0 + (fract * (y1 - y0));
397 /* reading nearest cosine output values */
399 y1 = cosTable[i + 1u];
401 /* Calculation of cosine value */
402 *pCosVal = y0 + (fract * (y1 - y0));
407 * @} end of SinCos group