Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 94 additions & 36 deletions src/include/sof/math/trig.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@

#include <stdint.h>

#define PI_DIV2_Q4_28 421657428
#define PI_DIV2_Q3_29 843314856
#define PI_Q4_28 843314857
#define PI_MUL2_Q4_28 1686629713
#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 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)

typedef enum {
EN_32B_CORDIC_SINE,
Expand All @@ -36,13 +39,49 @@ struct cordic_cmpx {
int32_t im;
};

/**
* cordic_approx() - CORDIC-based approximation of sine and cosine
* @param th_rad_fxp Input angle in radian Q4.28 format.
* @param a_idx Used LUT size.
* @param sign Output pointer to sine/cosine sign.
* @param b_yn Output pointer to sine value in Q2.30 format.
* @param xn Output pointer to cosine value in Q2.30 format.
* @param th_cdc_fxp Output pointer to the residual angle in Q2.30 format.
*/
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);
int32_t is_scalar_cordic_acos(int32_t realvalue, int16_t numiters);
int32_t is_scalar_cordic_asin(int32_t realvalue, int16_t numiters);

/**
* is_scalar_cordic_acos() - CORDIC-based approximation for inverse cosine
* @param realvalue Input cosine value in Q2.30 format.
* @param numiters_minus_one Number of iterations minus one.
* @return Inverse cosine angle in Q3.29 format.
*/
int32_t is_scalar_cordic_acos(int32_t realvalue, int numiters_minus_one);

/**
* is_scalar_cordic_asin() - CORDIC-based approximation for inverse sine
* @param realvalue Input sine value in Q2.30 format.
* @param numiters_minus_one Number of iterations minus one.
* @return Inverse sine angle in Q2.30 format.
*/
int32_t is_scalar_cordic_asin(int32_t realvalue, int numiters_minus_one);

/**
* cmpx_cexp() - CORDIC-based approximation of complex exponential e^(j*THETA)
* @param sign Sine sign
* @param b_yn Sine value in Q2.30 format
* @param xn Cosine value in Q2.30 format
* @param type CORDIC type
* @param cexp Output pointer to complex result in struct cordic_cmpx
*/
void cmpx_cexp(int32_t sign, int32_t b_yn, int32_t xn, cordic_cfg type, struct cordic_cmpx *cexp);
/* Input is Q4.28, output is Q1.31 */

/**
* sin_fixed_32b() - Sine function using CORDIC algorithm
* @param th_rad_fxp Input angle in radian Q4.28 format.
* @return 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
Expand Down Expand Up @@ -71,6 +110,10 @@ static inline int32_t sin_fixed_32b(int32_t th_rad_fxp)
}

/**
* cos_fixed_32b() - Cosine function using CORDIC algorithm
* @param th_rad_fxp Input angle in radian Q4.28 format.
* @return 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
Expand Down Expand Up @@ -98,8 +141,11 @@ static inline int32_t cos_fixed_32b(int32_t th_rad_fxp)
return sat_int32(Q_SHIFT_LEFT((int64_t)th_cdc_fxp, 30, 31));
}

/* Input is Q4.28, output is Q1.15 */
/**
* sin_fixed_16b() - Sine function using CORDIC algorithm
* @param th_rad_fxp Input angle in radian Q4.28 format.
* @return 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
Expand Down Expand Up @@ -129,6 +175,10 @@ static inline int16_t sin_fixed_16b(int32_t th_rad_fxp)
}

/**
* cos_fixed_16b() - Cosine function using CORDIC algorithm
* @param th_rad_fxp Input angle in radian Q4.28 format.
* @return 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
Expand Down Expand Up @@ -158,7 +208,10 @@ static inline int16_t cos_fixed_16b(int32_t th_rad_fxp)
}

/**
* CORDIC-based approximation of complex exponential e^(j*THETA).
* cmpx_exp_32b() - CORDIC-based approximation of complex exponential e^(j*THETA).
* @param th_rad_fxp Input angle in radian Q4.28 format.
* @param cexp Output 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
Expand Down Expand Up @@ -190,7 +243,10 @@ static inline void cmpx_exp_32b(int32_t th_rad_fxp, struct cordic_cmpx *cexp)
}

/**
* CORDIC-based approximation of complex exponential e^(j*THETA).
* cmpx_exp_16b() - CORDIC-based approximation of complex exponential e^(j*THETA).
* @param th_rad_fxp Input angle in radian Q4.28 format.
* @param cexp Output 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
Expand Down Expand Up @@ -223,7 +279,10 @@ static inline void cmpx_exp_16b(int32_t th_rad_fxp, struct cordic_cmpx *cexp)
}

/**
* CORDIC-based approximation of inverse sine
* asin_fixed_32b() - CORDIC-based approximation of inverse sine
* @param cdc_asin_th Input value in Q2.30 format.
* @return 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.
Expand All @@ -238,17 +297,18 @@ static inline int32_t asin_fixed_32b(int32_t cdc_asin_th)
int32_t th_asin_fxp;

if (cdc_asin_th >= 0)
th_asin_fxp = is_scalar_cordic_asin(cdc_asin_th,
CORDIC_31B_TABLE_SIZE);
th_asin_fxp = is_scalar_cordic_asin(cdc_asin_th, CORDIC_31B_ITERATIONS);
else
th_asin_fxp = -is_scalar_cordic_asin(-cdc_asin_th,
CORDIC_31B_TABLE_SIZE);
th_asin_fxp = -is_scalar_cordic_asin(-cdc_asin_th, CORDIC_31B_ITERATIONS);

return th_asin_fxp; /* Q2.30 */
}

/**
* CORDIC-based approximation of inverse cosine
* acos_fixed_32b() - CORDIC-based approximation of inverse cosine
* @param cdc_acos_th Input value in Q2.30 format.
* @return 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.
Expand All @@ -262,18 +322,18 @@ static inline int32_t acos_fixed_32b(int32_t cdc_acos_th)
int32_t th_acos_fxp;

if (cdc_acos_th >= 0)
th_acos_fxp = is_scalar_cordic_acos(cdc_acos_th,
CORDIC_31B_TABLE_SIZE);
th_acos_fxp = is_scalar_cordic_acos(cdc_acos_th, CORDIC_31B_ITERATIONS);
else
th_acos_fxp =
PI_MUL2_Q4_28 - is_scalar_cordic_acos(-cdc_acos_th,
CORDIC_31B_TABLE_SIZE);
th_acos_fxp = PI_Q3_29 - is_scalar_cordic_acos(-cdc_acos_th, CORDIC_31B_ITERATIONS);

return th_acos_fxp; /* Q3.29 */
}

/**
* CORDIC-based approximation of inverse sine
* asin_fixed_16b() - CORDIC-based approximation of inverse sine
* @param cdc_asin_th Input value in Q2.30 format.
* @return 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.
Expand All @@ -289,17 +349,18 @@ static inline int16_t asin_fixed_16b(int32_t cdc_asin_th)
int32_t th_asin_fxp;

if (cdc_asin_th >= 0)
th_asin_fxp = is_scalar_cordic_asin(cdc_asin_th,
CORDIC_16B_ITABLE_SIZE);
th_asin_fxp = is_scalar_cordic_asin(cdc_asin_th, CORDIC_16B_ITERATIONS);
else
th_asin_fxp = -is_scalar_cordic_asin(-cdc_asin_th,
CORDIC_16B_ITABLE_SIZE);
th_asin_fxp = -is_scalar_cordic_asin(-cdc_asin_th, CORDIC_16B_ITERATIONS);
/*convert Q2.30 to Q2.14 format*/
return sat_int16(Q_SHIFT_RND(th_asin_fxp, 30, 14));
}

/**
* CORDIC-based approximation of inverse cosine
* acos_fixed_16b() - CORDIC-based approximation of inverse cosine
* @param cdc_acos_th Input value in Q2.30 format.
* @return 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.
Expand All @@ -314,12 +375,9 @@ static inline int16_t acos_fixed_16b(int32_t cdc_acos_th)
int32_t th_acos_fxp;

if (cdc_acos_th >= 0)
th_acos_fxp = is_scalar_cordic_acos(cdc_acos_th,
CORDIC_16B_ITABLE_SIZE);
th_acos_fxp = is_scalar_cordic_acos(cdc_acos_th, CORDIC_16B_ITERATIONS);
else
th_acos_fxp =
PI_MUL2_Q4_28 - is_scalar_cordic_acos(-cdc_acos_th,
CORDIC_16B_ITABLE_SIZE);
th_acos_fxp = PI_Q3_29 - is_scalar_cordic_acos(-cdc_acos_th, CORDIC_16B_ITERATIONS);

/*convert Q3.29 to Q3.13 format*/
return sat_int16(Q_SHIFT_RND(th_acos_fxp, 29, 13));
Expand Down
Loading