comparison spandsp-0.0.6pre17/src/spandsp/arctan2.h @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents
children
comparison
equal deleted inserted replaced
3:c6c5a16ce2f2 4:26cd8f1ef0b1
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 * $Id: arctan2.h,v 1.13 2008/05/29 13:04:19 steveu Exp $
26 */
27
28 /*! \file */
29
30 #if !defined(_SPANDSP_ARCTAN2_H_)
31 #define _SPANDSP_ARCTAN2_H_
32
33 /*! \page arctan2_page Fast approximate four quadrant arc-tangent
34 \section arctan2_page_sec_1 What does it do?
35 This module provides a fast approximate 4-quadrant arc tangent function,
36 based on something at dspguru.com. The worst case error is about 4.07 degrees.
37 This is fine for many "where am I" type evaluations in comms. work.
38
39 \section arctan2_page_sec_2 How does it work?
40 ???.
41 */
42
43 #if defined(__cplusplus)
44 extern "C"
45 {
46 #endif
47
48 /* This returns its answer as a signed 32 bit integer phase value. */
49 static __inline__ int32_t arctan2(float y, float x)
50 {
51 float abs_y;
52 float angle;
53
54 if (x == 0.0f || y == 0.0f)
55 return 0;
56
57 abs_y = fabsf(y);
58
59 /* If we are in quadrant II or III, flip things around */
60 if (x < 0.0f)
61 angle = 3.0f - (x + abs_y)/(abs_y - x);
62 else
63 angle = 1.0f - (x - abs_y)/(abs_y + x);
64 angle *= 536870912.0f;
65
66 /* If we are in quadrant III or IV, negate to return an
67 answer in the range +-pi */
68 if (y < 0.0f)
69 angle = -angle;
70 return (int32_t) angle;
71 }
72 /*- End of function --------------------------------------------------------*/
73
74 #if 0
75 /* This returns its answer in radians, in the range +-pi. */
76 static __inline__ float arctan2f(float y, float x)
77 {
78 float angle;
79 float fx;
80 float fy;
81
82 if (x == 0.0f || y == 0.0f)
83 return 0;
84 fx = fabsf(x);
85 fy = fabsf(y);
86 /* Deal with the octants */
87 /* N.B. 0.28125 == (1/4 + 1/32) */
88 if (fy > fx)
89 angle = 3.1415926f/2.0f - fx*fy/(y*y + 0.28125f*x*x);
90 else
91 angle = fy*fx/(x*x + 0.28125f*y*y);
92
93 /* Deal with the quadrants, to bring the final answer to the range +-pi */
94 if (x < 0.0f)
95 angle = 3.1415926f - angle;
96 if (y < 0.0f)
97 angle = -angle;
98 return angle;
99 }
100 /*- End of function --------------------------------------------------------*/
101 #endif
102
103 #if defined(__cplusplus)
104 }
105 #endif
106
107 #endif
108 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.