comparison spandsp-0.0.3/spandsp-0.0.3/src/spandsp/arctan2.h @ 5:f762bf195c4b

import spandsp-0.0.3
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 16:00:21 +0200
parents
children
comparison
equal deleted inserted replaced
4:26cd8f1ef0b1 5:f762bf195c4b
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 General Public License version 2, as
14 * 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 General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * 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.7 2006/10/24 13:22:01 steveu Exp $
26 */
27
28 /*! \file */
29
30 #if !defined(_ARCTAN2_H_)
31 #define _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 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 /* This returns its answer as a signed 32 bit integer phase value. */
48 static __inline__ int32_t arctan2(float y, float x)
49 {
50 float abs_y;
51 float angle;
52
53 if (x == 0.0 || y == 0.0)
54 return 0;
55
56 abs_y = fabsf(y);
57
58 /* If we are in quadrant II or III, flip things around */
59 if (x < 0.0)
60 angle = 3.0f - (x + abs_y)/(abs_y - x);
61 else
62 angle = 1.0f - (x - abs_y)/(abs_y + x);
63 angle *= 536870912.0;
64
65 /* If we are in quadrant III or IV, negate to return an
66 answer in the range +-pi */
67 if (y < 0.0)
68 angle = -angle;
69 return (int32_t) angle;
70 }
71 /*- End of function --------------------------------------------------------*/
72
73 #ifdef __cplusplus
74 }
75 #endif
76
77 #endif
78 /*- End of file ------------------------------------------------------------*/

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