Sound Open Firmware
Loading...
Searching...
No Matches
trig.h File Reference
Include dependency graph for trig.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  cordic_cmpx

Macros

#define PI_Q4_28   843314857 /* int32(pi * 2^28) */
#define PI_MUL2_Q4_28   1686629713 /* int32(2 * pi * 2^28) */
#define PI_DIV2_Q3_29   843314857 /* int32(pi / 2 * 2^29) */
#define PI_Q3_29   1686629713 /* int32(pi * 2^29) */
#define CORDIC_31B_TABLE_SIZE   31
#define CORDIC_15B_TABLE_SIZE   15
#define CORDIC_30B_ITABLE_SIZE   30
#define CORDIC_16B_ITABLE_SIZE   16
#define CORDIC_31B_ITERATIONS   (CORDIC_31B_TABLE_SIZE - 1)
#define CORDIC_16B_ITERATIONS   (CORDIC_16B_ITABLE_SIZE - 1)

Enumerations

enum  cordic_cfg {
  EN_32B_CORDIC_SINE , EN_32B_CORDIC_COSINE , EN_32B_CORDIC_CEXP , EN_16B_CORDIC_SINE ,
  EN_16B_CORDIC_COSINE , EN_16B_CORDIC_CEXP
}

Functions

void cordic_approx (int32_t th_rad_fxp, int32_t a_idx, int32_t *sign, int32_t *b_yn, int32_t *xn, int32_t *th_cdc_fxp)
 cordic_approx() - CORDIC-based approximation of sine and cosine
int32_t is_scalar_cordic_acos (int32_t realvalue, int numiters_minus_one)
 is_scalar_cordic_acos() - CORDIC-based approximation for inverse cosine
int32_t is_scalar_cordic_asin (int32_t realvalue, int numiters_minus_one)
 is_scalar_cordic_asin() - CORDIC-based approximation for inverse sine
void cmpx_cexp (int32_t sign, int32_t b_yn, int32_t xn, cordic_cfg type, struct cordic_cmpx *cexp)
 cmpx_cexp() - CORDIC-based approximation of complex exponential e^(j*THETA)
static int32_t sin_fixed_32b (int32_t th_rad_fxp)
 sin_fixed_32b() - Sine function using CORDIC algorithm
static int32_t cos_fixed_32b (int32_t th_rad_fxp)
 cos_fixed_32b() - Cosine function using CORDIC algorithm
static int16_t sin_fixed_16b (int32_t th_rad_fxp)
 sin_fixed_16b() - Sine function using CORDIC algorithm
static int16_t cos_fixed_16b (int32_t th_rad_fxp)
 cos_fixed_16b() - Cosine function using CORDIC algorithm
static void cmpx_exp_32b (int32_t th_rad_fxp, struct cordic_cmpx *cexp)
 cmpx_exp_32b() - CORDIC-based approximation of complex exponential e^(j*THETA).
static void cmpx_exp_16b (int32_t th_rad_fxp, struct cordic_cmpx *cexp)
 cmpx_exp_16b() - CORDIC-based approximation of complex exponential e^(j*THETA).
static int32_t asin_fixed_32b (int32_t cdc_asin_th)
 asin_fixed_32b() - CORDIC-based approximation of inverse sine
static int32_t acos_fixed_32b (int32_t cdc_acos_th)
 acos_fixed_32b() - CORDIC-based approximation of inverse cosine
static int16_t asin_fixed_16b (int32_t cdc_asin_th)
 asin_fixed_16b() - CORDIC-based approximation of inverse sine
static int16_t acos_fixed_16b (int32_t cdc_acos_th)
 acos_fixed_16b() - CORDIC-based approximation of inverse cosine
int32_t sofm_atan2_32b (int32_t y, int32_t x)
 sofm_atan2_32b() - Four-quadrant arctangent using degree-9 Remez minimax polynomial

Macro Definition Documentation

◆ CORDIC_15B_TABLE_SIZE

#define CORDIC_15B_TABLE_SIZE   15

◆ CORDIC_16B_ITABLE_SIZE

#define CORDIC_16B_ITABLE_SIZE   16

◆ CORDIC_16B_ITERATIONS

#define CORDIC_16B_ITERATIONS   (CORDIC_16B_ITABLE_SIZE - 1)

◆ CORDIC_30B_ITABLE_SIZE

#define CORDIC_30B_ITABLE_SIZE   30

◆ CORDIC_31B_ITERATIONS

#define CORDIC_31B_ITERATIONS   (CORDIC_31B_TABLE_SIZE - 1)

◆ CORDIC_31B_TABLE_SIZE

#define CORDIC_31B_TABLE_SIZE   31

◆ PI_DIV2_Q3_29

#define PI_DIV2_Q3_29   843314857 /* int32(pi / 2 * 2^29) */

◆ PI_MUL2_Q4_28

#define PI_MUL2_Q4_28   1686629713 /* int32(2 * pi * 2^28) */

◆ PI_Q3_29

#define PI_Q3_29   1686629713 /* int32(pi * 2^29) */

◆ PI_Q4_28

#define PI_Q4_28   843314857 /* int32(pi * 2^28) */

Enumeration Type Documentation

◆ cordic_cfg

enum cordic_cfg
Enumerator
EN_32B_CORDIC_SINE 
EN_32B_CORDIC_COSINE 
EN_32B_CORDIC_CEXP 
EN_16B_CORDIC_SINE 
EN_16B_CORDIC_COSINE 
EN_16B_CORDIC_CEXP 

Function Documentation

◆ acos_fixed_16b()

int16_t acos_fixed_16b ( int32_t cdc_acos_th)
inlinestatic

acos_fixed_16b() - CORDIC-based approximation of inverse cosine

Parameters
cdc_acos_thInput value in Q2.30 format.
Returns
Inverse cosine angle in Q3.13 format.

inverse cosine of cdc_acos_theta based on a CORDIC approximation acos(cdc_acos_th) inverse cosine angle values in radian produces using the DCORDIC (Double CORDIC) algorithm. Q2.30 cdc_acos_th , input value range [-1 to 1] Q3.13 th_acos_fxp, output value range [3.14159265346825 to 0] LUT size set type 31 number of iteration 15 Error (max = 0.000059799232976), THD+N = -89.824298401466635 (dBc)

◆ acos_fixed_32b()

int32_t acos_fixed_32b ( int32_t cdc_acos_th)
inlinestatic

acos_fixed_32b() - CORDIC-based approximation of inverse cosine

Parameters
cdc_acos_thInput value in Q2.30 format.
Returns
Inverse cosine angle in Q3.29 format.

inverse cosine of cdc_acos_theta based on a CORDIC approximation acos(cdc_acos_th) inverse cosine angle values in radian produces using the DCORDIC (Double CORDIC) algorithm. Q2.30 cdc_acos_th , input value range [-1 to 1] Q3.29 th_acos_fxp, output value range [3.14159265346825 to 0] LUT size set type 31 Error (max = 0.000000026077032), THD+N = -157.948952635422842 (dBc)

◆ asin_fixed_16b()

int16_t asin_fixed_16b ( int32_t cdc_asin_th)
inlinestatic

asin_fixed_16b() - CORDIC-based approximation of inverse sine

Parameters
cdc_asin_thInput value in Q2.30 format.
Returns
Inverse sine angle in Q2.14 format.

inverse sine of cdc_asin_theta based on a CORDIC approximation. asin(cdc_asin_th) inverse sine angle values in radian produces using the DCORDIC (Double CORDIC) algorithm. Inverse sine angle values in rad Q2.30 cdc_asin_th, value in between range of [-1 to 1] Q2.14 th_asin_fxp, output value range [-1.5707963258028 to 1.5707963258028] LUT size set type 31 number of iteration 15 Error (max = 0.000059800222516), THD+N = -89.824282520774048 (dBc)

◆ asin_fixed_32b()

int32_t asin_fixed_32b ( int32_t cdc_asin_th)
inlinestatic

asin_fixed_32b() - CORDIC-based approximation of inverse sine

Parameters
cdc_asin_thInput value in Q2.30 format.
Returns
Inverse sine angle in Q2.30 format.

inverse sine of cdc_asin_theta based on a CORDIC approximation. asin(cdc_asin_th) inverse sine angle values in radian produces using the DCORDIC (Double CORDIC) algorithm. Inverse sine angle values in rad Q2.30 cdc_asin_th, value in between range of [-1 to 1] Q2.30 th_asin_fxp, output value range [-1.5707963258028 to 1.5707963258028] LUT size set type 15 Error (max = 0.000000027939677), THD+N = -157.454534077921551 (dBc)

◆ cmpx_cexp()

void cmpx_cexp ( int32_t sign,
int32_t b_yn,
int32_t xn,
cordic_cfg type,
struct cordic_cmpx * cexp )

cmpx_cexp() - CORDIC-based approximation of complex exponential e^(j*THETA)

Parameters
signSine sign
b_ynSine value in Q2.30 format
xnCosine value in Q2.30 format
typeCORDIC type
cexpOutput pointer to complex result in struct cordic_cmpx

◆ cmpx_exp_16b()

void cmpx_exp_16b ( int32_t th_rad_fxp,
struct cordic_cmpx * cexp )
inlinestatic

cmpx_exp_16b() - CORDIC-based approximation of complex exponential e^(j*THETA).

Parameters
th_rad_fxpInput angle in radian Q4.28 format.
cexpOutput pointer to complex result in struct cordic_cmpx in Q1.15 format.

computes COS(THETA) + j*SIN(THETA) using CORDIC algorithm approximation and returns the complex result. THETA values must be in the range [-2*pi, 2*pi). The cordic exponential algorithm converges, when the angle is in the range [-pi/2, pi/2).If an angle is outside of this range, then a multiple of pi/2 is added or subtracted from the angle until it is within the range [-pi/2,pi/2).Start with the angle in the range [-2*pi, 2*pi) and output has range in [-1.0 to 1.0] Error (max = 0.000060862861574), THD+N = -89.049303454077403 +---------------—+--------------—+-----—+---------—+ | thRadFxp |cdccexpth |thRadFxp| cdccexpth| +-—+--—+----—+-—+-—+----—+-----—+---------—+ |WLen| FLen|Signbit|WLen|FLen|Signbit| Qformat| Qformat | +-—+--—+----—+-—+-—+----—+-----—+---------—+ | 32 | 28 | 1 | 32 | 15 | 1 | 4.28 | 1.15 | +---------------—+--------------—+-----—+---------—+

◆ cmpx_exp_32b()

void cmpx_exp_32b ( int32_t th_rad_fxp,
struct cordic_cmpx * cexp )
inlinestatic

cmpx_exp_32b() - CORDIC-based approximation of complex exponential e^(j*THETA).

Parameters
th_rad_fxpInput angle in radian Q4.28 format.
cexpOutput pointer to complex result in struct cordic_cmpx in Q2.30 format.

computes COS(THETA) + j*SIN(THETA) using CORDIC algorithm approximation and returns the complex result. THETA values must be in the range [-2*pi, 2*pi). The cordic exponential algorithm converges, when the angle is in the range [-pi/2, pi/2).If an angle is outside of this range, then a multiple of pi/2 is added or subtracted from the angle until it is within the range [-pi/2,pi/2).Start with the angle in the range [-2*pi, 2*pi) and output has range in [-1.0 to 1.0] Error (max = 0.000000015832484), THD+N = -167.082852232808847 +---------------—+--------------—+-----—+---------—+ | thRadFxp |cdccexpth |thRadFxp| cdccexpth| +-—+--—+----—+-—+-—+----—+-----—+---------—+ |WLen| FLen|Signbit|WLen|FLen|Signbit| Qformat| Qformat | +-—+--—+----—+-—+-—+----—+-----—+---------—+ | 32 | 28 | 1 | 32 | 15 | 1 | 4.28 | 2.30 | +---------------—+--------------—+-----—+---------—+

◆ cordic_approx()

void cordic_approx ( int32_t th_rad_fxp,
int32_t a_idx,
int32_t * sign,
int32_t * b_yn,
int32_t * xn,
int32_t * th_cdc_fxp )

cordic_approx() - CORDIC-based approximation of sine and cosine

Parameters
th_rad_fxpInput angle in radian Q4.28 format.
a_idxUsed LUT size.
signOutput pointer to sine/cosine sign.
b_ynOutput pointer to sine value in Q2.30 format.
xnOutput pointer to cosine value in Q2.30 format.
th_cdc_fxpOutput pointer to the residual angle in Q2.30 format.

◆ cos_fixed_16b()

int16_t cos_fixed_16b ( int32_t th_rad_fxp)
inlinestatic

cos_fixed_16b() - Cosine function using CORDIC algorithm

Parameters
th_rad_fxpInput angle in radian Q4.28 format.
Returns
Cosine value in Q1.15 format.

Compute fixed point cordic cosine with table lookup and interpolation The cordic cos algorithm converges, when the angle is in the range [-pi/2, pi/2).If an angle is outside of this range, then a multiple of pi/2 is added or subtracted from the angle until it is within the range [-pi/2,pi/2).Start with the angle in the range [-2*pi, 2*pi) and output has range in [-1.0 to 1.0] +---------------—+--------------—+-----—+---------—+ | thRadFxp | cdccosth |thRadFxp| cdccosth| +-—+--—+----—+-—+-—+----—+-----—+---------—+ |WLen| FLen|Signbit|WLen|FLen|Signbit| Qformat| Qformat | +-—+--—+----—+-—+-—+----—+-----—+---------—+ | 32 | 28 | 1 | 32 | 15 | 1 | 4.28 | 1.15 | +---------------—+--------------—+-----—+---------—+

◆ cos_fixed_32b()

int32_t cos_fixed_32b ( int32_t th_rad_fxp)
inlinestatic

cos_fixed_32b() - Cosine function using CORDIC algorithm

Parameters
th_rad_fxpInput angle in radian Q4.28 format.
Returns
Cosine value in Q1.31 format.

Compute fixed point cordicsine with table lookup and interpolation The cordic cosine algorithm converges, when the angle is in the range [-pi/2, pi/2).If an angle is outside of this range, then a multiple of pi/2 is added or subtracted from the angle until it is within the range [-pi/2,pi/2).Start with the angle in the range [-2*pi, 2*pi) and output has range in [-1.0 to 1.0] +---------------—+--------------—+-----—+-----—+ | thRadFxp | cdccosth |thRadFxp|cdccosth| +-—+--—+----—+-—+-—+----—+-----—+-----—+ |WLen| FLen|Signbit|WLen|FLen|Signbit| Qformat| Qformat| +-—+--—+----—+-—+-—+----—+-----—+-----—+ | 32 | 28 | 1 | 32 | 31 | 1 | 4.28 | 1.31 | +---------------—+--------------—+-----—+-----—+

◆ is_scalar_cordic_acos()

int32_t is_scalar_cordic_acos ( int32_t realvalue,
int numiters_minus_one )

is_scalar_cordic_acos() - CORDIC-based approximation for inverse cosine

Parameters
realvalueInput cosine value in Q2.30 format.
numiters_minus_oneNumber of iterations minus one.
Returns
Inverse cosine angle in Q3.29 format.

◆ is_scalar_cordic_asin()

int32_t is_scalar_cordic_asin ( int32_t realvalue,
int numiters_minus_one )

is_scalar_cordic_asin() - CORDIC-based approximation for inverse sine

Parameters
realvalueInput sine value in Q2.30 format.
numiters_minus_oneNumber of iterations minus one.
Returns
Inverse sine angle in Q2.30 format.

◆ sin_fixed_16b()

int16_t sin_fixed_16b ( int32_t th_rad_fxp)
inlinestatic

sin_fixed_16b() - Sine function using CORDIC algorithm

Parameters
th_rad_fxpInput angle in radian Q4.28 format.
Returns
Sine value in Q1.15 format

Compute fixed point cordic sine with table lookup and interpolation The cordic sine algorithm converges, when the angle is in the range [-pi/2, pi/2).If an angle is outside of this range, then a multiple of pi/2 is added or subtracted from the angle until it is within the range [-pi/2,pi/2).Start with the angle in the range [-2*pi, 2*pi) and output has range in [-1.0 to 1.0] +---------------—+--------------—+-----—+---------—+ | thRadFxp | cdcsinth |thRadFxp| cdcsinth| +-—+--—+----—+-—+-—+----—+-----—+---------—+ |WLen| FLen|Signbit|WLen|FLen|Signbit| Qformat| Qformat | +-—+--—+----—+-—+-—+----—+-----—+---------—+ | 32 | 28 | 1 | 32 | 15 | 1 | 4.28 | 1.15 | +---------------—+--------------—+-----—+---------—+

◆ sin_fixed_32b()

int32_t sin_fixed_32b ( int32_t th_rad_fxp)
inlinestatic

sin_fixed_32b() - Sine function using CORDIC algorithm

Parameters
th_rad_fxpInput angle in radian Q4.28 format.
Returns
Sine value in Q1.31 format.

Compute fixed point cordicsine with table lookup and interpolation The cordic sine algorithm converges, when the angle is in the range [-pi/2, pi/2).If an angle is outside of this range, then a multiple of pi/2 is added or subtracted from the angle until it is within the range [-pi/2,pi/2).Start with the angle in the range [-2*pi, 2*pi) and output has range in [-1.0 to 1.0] +---------------—+--------------—+-----—+-----—+ | thRadFxp | cdcsinth |thRadFxp|cdcsinth| +-—+--—+----—+-—+-—+----—+-----—+-----—+ |WLen| FLen|Signbit|WLen|FLen|Signbit| Qformat| Qformat| +-—+--—+----—+-—+-—+----—+-----—+-----—+ | 32 | 28 | 1 | 32 | 31 | 1 | 4.28 | 1.31 | +---------------—+--------------—+-----—+-----—+

◆ sofm_atan2_32b()

int32_t sofm_atan2_32b ( int32_t y,
int32_t x )

sofm_atan2_32b() - Four-quadrant arctangent using degree-9 Remez minimax polynomial

Parameters
yImaginary component (sine) in Q1.31 format.
xReal component (cosine) in Q1.31 format.
Returns
Angle in Q3.29 radians, range [-pi, +pi].

Uses the Horner-form polynomial: atan(z) = z * (C0 + z^2 * (C1 + z^2 * (C2 + z^2 * (C3 + z^2 * C4))))

with Remez minimax coefficients on [0, 1]. Maximum error ~0.001 degrees (1.94e-5 radians).