00001 /* 00002 * SpanDSP - a series of DSP components for telephony 00003 * 00004 * arctan2.h - A quick rough approximate arc tan 00005 * 00006 * Written by Steve Underwood <steveu@coppice.org> 00007 * 00008 * Copyright (C) 2003 Steve Underwood 00009 * 00010 * All rights reserved. 00011 * 00012 * This program is free software; you can redistribute it and/or modify 00013 * it under the terms of the GNU General Public License as published by 00014 * the Free Software Foundation; either version 2 of the License, or 00015 * (at your option) any later version. 00016 * 00017 * This program is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU General Public License for more details. 00021 * 00022 * You should have received a copy of the GNU General Public License 00023 * along with this program; if not, write to the Free Software 00024 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00025 * 00026 * $Id: arctan2.h,v 1.5 2005/11/28 13:43:34 steveu Exp $ 00027 */ 00028 00029 /*! \file */ 00030 00031 #if !defined(_ARCTAN2_H_) 00032 #define _ARCTAN2_H_ 00033 00034 /*! \page arctan2_page Fast approximate four quadrant arc-tangent 00035 \section arctan2_page_sec_1 What does it do? 00036 This module provides a fast approximate 4-quadrant arc tangent function, 00037 based on something at dspguru.com. The worst case error is about 4.07 degrees. 00038 This is fine for many "where am I" type evaluations in comms. work. 00039 00040 \section arctan2_page_sec_2 How does it work? 00041 ???. 00042 */ 00043 00044 #ifdef __cplusplus 00045 extern "C" { 00046 #endif 00047 00048 /* This returns its answer as a signed 32 bit integer phase value. */ 00049 static __inline__ int32_t arctan2(float y, float x) 00050 { 00051 float abs_y; 00052 float angle; 00053 00054 if (x == 0.0 || y == 0.0) 00055 return 0; 00056 00057 abs_y = fabsf(y); 00058 00059 /* If we are in quadrant II or III, flip things around */ 00060 if (x < 0.0) 00061 angle = 3.0 - (x + abs_y)/(abs_y - x); 00062 else 00063 angle = 1.0 - (x - abs_y)/(abs_y + x); 00064 angle *= 536870912.0; 00065 00066 /* If we are in quadrant III or IV, negate to return an 00067 answer in the range +-pi */ 00068 if (y < 0.0) 00069 angle = -angle; 00070 return (int32_t) angle; 00071 } 00072 /*- End of function --------------------------------------------------------*/ 00073 00074 #ifdef __cplusplus 00075 } 00076 #endif 00077 00078 #endif 00079 /*- End of file ------------------------------------------------------------*/