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 (y == 0.0f)
53  {
54  if (x < 0.0f)
55  return 0x80000000;
56  return 0x00000000;
57  }
58  if (x == 0.0f)
59  {
60  if (y < 0.0f)
61  return 0xc0000000;
62  return 0x40000000;
63  }
64 
65  abs_y = fabsf(y);
66 
67  /* If we are in quadrant II or III, flip things around */
68  if (x < 0.0f)
69  angle = 3.0f - (x + abs_y)/(abs_y - x);
70  else
71  angle = 1.0f - (x - abs_y)/(abs_y + x);
72  angle *= 536870912.0f;
73 
74  /* If we are in quadrant III or IV, negate to return an
75  answer in the range +-pi */
76  if (y < 0.0f)
77  angle = -angle;
78  return (int32_t) angle;
79 }
80 /*- End of function --------------------------------------------------------*/
81 
82 #if 0
83 /* This returns its answer in radians, in the range +-pi. */
84 static __inline__ float arctan2f(float y, float x)
85 {
86  float angle;
87  float fx;
88  float fy;
89 
90  if (y == 0.0f)
91  {
92  if (x < 0.0f)
93  return 3.1415926f;
94  return 0.0f;
95  }
96  if (x == 0.0f)
97  {
98  if (y < 0.0f)
99  return 3.1415926f*1.5f;
100  return 3.1415926f*0.5f;
101  }
102  fx = fabsf(x);
103  fy = fabsf(y);
104  /* Deal with the octants */
105  /* N.B. 0.28125 == (1/4 + 1/32) */
106  if (fy > fx)
107  angle = 3.1415926f/2.0f - fx*fy/(y*y + 0.28125f*x*x);
108  else
109  angle = fy*fx/(x*x + 0.28125f*y*y);
110 
111  /* Deal with the quadrants, to bring the final answer to the range +-pi */
112  if (x < 0.0f)
113  angle = 3.1415926f - angle;
114  if (y < 0.0f)
115  angle = -angle;
116  return angle;
117 }
118 /*- End of function --------------------------------------------------------*/
119 #endif
120 
121 #if defined(__cplusplus)
122 }
123 #endif
124 
125 #endif
126 /*- End of file ------------------------------------------------------------*/
CLIP_DTMF_HASH_CALLER_NUMBER
#define CLIP_DTMF_HASH_CALLER_NUMBER
Definition: adsi.h:363
adsi_standard_to_str
const char * adsi_standard_to_str(int standard)
Return a short name for an ADSI standard.
Definition: adsi.c:1103
MCLASS_CALLER_NAME
@ MCLASS_CALLER_NAME
Definition: adsi.h:239
CLASS_SDMF_CALLERID
@ CLASS_SDMF_CALLERID
Definition: adsi.h:214
CLIP_DTMF_HASH_UNSPECIFIED
#define CLIP_DTMF_HASH_UNSPECIFIED
Definition: adsi.h:367
CLIP_ABSENCE2
@ CLIP_ABSENCE2
Definition: adsi.h:277
CLIP_DATETIME
@ CLIP_DATETIME
Definition: adsi.h:267
adsi_rx_init
adsi_rx_state_t * adsi_rx_init(adsi_rx_state_t *s, int standard, put_msg_func_t put_msg, void *user_data)
Initialise an ADSI receive context.
Definition: adsi.c:427
adsi_tx
int adsi_tx(adsi_tx_state_t *s, int16_t amp[], int max_len)
Generate a block of ADSI audio samples.
Definition: adsi.c:478
CLIP_MDMF_CALLERID
@ CLIP_MDMF_CALLERID
Definition: adsi.h:254
JCLIP_DIALED_NUM_DES
@ JCLIP_DIALED_NUM_DES
Definition: adsi.h:350
CLIP_SERVICE_INFO
@ CLIP_SERVICE_INFO
Definition: adsi.h:303
CLASS_SDMF_MSG_WAITING
@ CLASS_SDMF_MSG_WAITING
Definition: adsi.h:218
JCLIP_ABSENCE
@ JCLIP_ABSENCE
Definition: adsi.h:352
CLIP_ADD_CHARGE
@ CLIP_ADD_CHARGE
Definition: adsi.h:299
CLIP_DISPLAY_INFO
@ CLIP_DISPLAY_INFO
Definition: adsi.h:301
CLIP_DTMF_C_CALLER_NUMBER
#define CLIP_DTMF_C_CALLER_NUMBER
Definition: adsi.h:370
ACLIP_CALLER_NUMBER
@ ACLIP_CALLER_NUMBER
Definition: adsi.h:321
CLIP_DTMF_C_TERMINATED
#define CLIP_DTMF_C_TERMINATED
Definition: adsi.h:360
ACLIP_MDMF_CALLERID
@ ACLIP_MDMF_CALLERID
Definition: adsi.h:312
JCLIP_CALLER_NUMBER
@ JCLIP_CALLER_NUMBER
Definition: adsi.h:344
CLIP_DTMF_HASH_TERMINATED
#define CLIP_DTMF_HASH_TERMINATED
Definition: adsi.h:358
ACLIP_REDIRECT
@ ACLIP_REDIRECT
Definition: adsi.h:327
adsi_rx_free
int adsi_rx_free(adsi_rx_state_t *s)
Free the resources of an ADSI receive context.
Definition: adsi.c:471
CLIP_CALLER_NAME
@ CLIP_CALLER_NAME
Definition: adsi.h:275
ACLIP_CALLER_NAME
@ ACLIP_CALLER_NAME
Definition: adsi.h:331
CLASS_MDMF_CALLERID
@ CLASS_MDMF_CALLERID
Definition: adsi.h:216
adsi_next_field
int adsi_next_field(adsi_rx_state_t *s, const uint8_t *msg, int msg_len, int pos, uint8_t *field_type, uint8_t const **field_body, int *field_len)
Get a field from an ADSI message.
Definition: adsi.c:881
CLIP_MESSAGE_ID
@ CLIP_MESSAGE_ID
Definition: adsi.h:281
CLIP_DIALED_NUMBER
@ CLIP_DIALED_NUMBER
Definition: adsi.h:271
adsi_tx_init
adsi_tx_state_t * adsi_tx_init(adsi_tx_state_t *s, int standard)
Initialise an ADSI transmit context.
Definition: adsi.c:660
CLIP_MDMF_MSG_WAITING
@ CLIP_MDMF_MSG_WAITING
Definition: adsi.h:256
ACLIP_NUMBER_ABSENCE
@ ACLIP_NUMBER_ABSENCE
Definition: adsi.h:325
MCLASS_DIALED_NUMBER
@ MCLASS_DIALED_NUMBER
Definition: adsi.h:231
MCLASS_DATETIME
@ MCLASS_DATETIME
Definition: adsi.h:227
adsi_tx_free
int adsi_tx_free(adsi_tx_state_t *s)
Free the resources of an ADSI transmit context.
Definition: adsi.c:692
adsi_rx_state_s
Definition: private/adsi.h:83
JCLIP_MDMF_CALLERID
#define JCLIP_MDMF_CALLERID
Definition: adsi.h:338
CLIP_ABSENCE1
@ CLIP_ABSENCE1
Definition: adsi.h:273
ACLIP_SDMF_CALLERID
@ ACLIP_SDMF_CALLERID
Definition: adsi.h:310
adsi_rx
int adsi_rx(adsi_rx_state_t *s, const int16_t amp[], int len)
Receive a chunk of ADSI audio.
Definition: adsi.c:402
MCLASS_ABSENCE2
@ MCLASS_ABSENCE2
Definition: adsi.h:241
CLIP_DTMF_C_ABSENCE
#define CLIP_DTMF_C_ABSENCE
Definition: adsi.h:374
adsi_tx_state_s
Definition: private/adsi.h:35
CLIP_CHARGE
@ CLIP_CHARGE
Definition: adsi.h:295
CLIP_REDIR_NUMBER
@ CLIP_REDIR_NUMBER
Definition: adsi.h:293
ACLIP_DATETIME
@ ACLIP_DATETIME
Definition: adsi.h:319
spandsp-sim.h
MCLASS_ABSENCE1
@ MCLASS_ABSENCE1
Definition: adsi.h:233
CLIP_CALLER_NUMBER
@ CLIP_CALLER_NUMBER
Definition: adsi.h:269
CLIP_DTMF_C_REDIRECT_NUMBER
#define CLIP_DTMF_C_REDIRECT_NUMBER
Definition: adsi.h:372
adsi_tx_set_preamble
void adsi_tx_set_preamble(adsi_tx_state_t *s, int preamble_len, int preamble_ones_len, int postamble_ones_len, int stop_bits)
Adjust the preamble associated with an ADSI transmit context.
Definition: adsi.c:512
ACLIP_DIALED_NUMBER
@ ACLIP_DIALED_NUMBER
Definition: adsi.h:323
CLIP_CALLTYPE
@ CLIP_CALLTYPE
Definition: adsi.h:285
CLIP_MDMF_CHARGE_INFO
@ CLIP_MDMF_CHARGE_INFO
Definition: adsi.h:258
ACLIP_NAME_ABSENCE
@ ACLIP_NAME_ABSENCE
Definition: adsi.h:333
MCLASS_CALLER_NUMBER
@ MCLASS_CALLER_NUMBER
Definition: adsi.h:229
MCLASS_REDIRECT
@ MCLASS_REDIRECT
Definition: adsi.h:235
MCLASS_QUALIFIER
@ MCLASS_QUALIFIER
Definition: adsi.h:237
MCLASS_VISUAL_INDICATOR
#define MCLASS_VISUAL_INDICATOR
Definition: adsi.h:248
CLASS_MDMF_MSG_WAITING
@ CLASS_MDMF_MSG_WAITING
Definition: adsi.h:220
JCLIP_CALLER_NUM_DES
@ JCLIP_CALLER_NUM_DES
Definition: adsi.h:346
JCLIP_DIALED_NUMBER
@ JCLIP_DIALED_NUMBER
Definition: adsi.h:348
ACLIP_QUALIFIER
@ ACLIP_QUALIFIER
Definition: adsi.h:329
adsi_add_field
int adsi_add_field(adsi_tx_state_t *s, uint8_t *msg, int len, uint8_t field_type, uint8_t const *field_body, int field_len)
Insert the header or a field into an ADSI message.
Definition: adsi.c:1001
CLIP_NUM_MSG
@ CLIP_NUM_MSG
Definition: adsi.h:287
CLIP_DTMF_HASH_ABSENCE
#define CLIP_DTMF_HASH_ABSENCE
Definition: adsi.h:365
CLIP_MDMF_SMS
@ CLIP_MDMF_SMS
Definition: adsi.h:260
CLIP_VISUAL_INDICATOR
@ CLIP_VISUAL_INDICATOR
Definition: adsi.h:279
adsi_tx_put_message
int adsi_tx_put_message(adsi_tx_state_t *s, const uint8_t *msg, int len)
Put a message into the input buffer of an ADSI transmit context.
Definition: adsi.c:565
CLIP_DURATION
@ CLIP_DURATION
Definition: adsi.h:297