spandsp  0.0.6
arctan2.h
Go to the documentation of this file.
1 /*
2  * SpanDSP - a series of DSP components for telephony
3  *
4  * arctan2.h - A quick rough approximate arc tan
5  *
6  * Written by Steve Underwood <steveu@coppice.org>
7  *
8  * Copyright (C) 2003 Steve Underwood
9  *
10  * All rights reserved.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 2.1,
14  * as published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25 
26 /*! \file */
27 
28 #if !defined(_SPANDSP_ARCTAN2_H_)
29 #define _SPANDSP_ARCTAN2_H_
30 
31 /*! \page arctan2_page Fast approximate four quadrant arc-tangent
32 \section arctan2_page_sec_1 What does it do?
33 This module provides a fast approximate 4-quadrant arc tangent function,
34 based on something at dspguru.com. The worst case error is about 4.07 degrees.
35 This is fine for many "where am I" type evaluations in comms. work.
36 
37 \section arctan2_page_sec_2 How does it work?
38 ???.
39 */
40 
41 #if defined(__cplusplus)
42 extern "C"
43 {
44 #endif
45 
46 /* This returns its answer as a signed 32 bit integer phase value. */
47 static __inline__ int32_t arctan2(float y, float x)
48 {
49  float abs_y;
50  float angle;
51 
52  if (x == 0.0f || y == 0.0f)
53  return 0;
54 
55  abs_y = fabsf(y);
56 
57  /* If we are in quadrant II or III, flip things around */
58  if (x < 0.0f)
59  angle = 3.0f - (x + abs_y)/(abs_y - x);
60  else
61  angle = 1.0f - (x - abs_y)/(abs_y + x);
62  angle *= 536870912.0f;
63 
64  /* If we are in quadrant III or IV, negate to return an
65  answer in the range +-pi */
66  if (y < 0.0f)
67  angle = -angle;
68  return (int32_t) angle;
69 }
70 /*- End of function --------------------------------------------------------*/
71 
72 #if 0
73 /* This returns its answer in radians, in the range +-pi. */
74 static __inline__ float arctan2f(float y, float x)
75 {
76  float angle;
77  float fx;
78  float fy;
79 
80  if (x == 0.0f || y == 0.0f)
81  return 0;
82  fx = fabsf(x);
83  fy = fabsf(y);
84  /* Deal with the octants */
85  /* N.B. 0.28125 == (1/4 + 1/32) */
86  if (fy > fx)
87  angle = 3.1415926f/2.0f - fx*fy/(y*y + 0.28125f*x*x);
88  else
89  angle = fy*fx/(x*x + 0.28125f*y*y);
90 
91  /* Deal with the quadrants, to bring the final answer to the range +-pi */
92  if (x < 0.0f)
93  angle = 3.1415926f - angle;
94  if (y < 0.0f)
95  angle = -angle;
96  return angle;
97 }
98 /*- End of function --------------------------------------------------------*/
99 #endif
100 
101 #if defined(__cplusplus)
102 }
103 #endif
104 
105 #endif
106 /*- End of file ------------------------------------------------------------*/