AFBR-S50 API Reference Manual v1.5.6
AFBR-S50 Time-of-Flight Sensor SDK for Embedded Software
Loading...
Searching...
No Matches
Fixed Point Math

A basic math library for fixed point number in the Qx.y fomat. More...

Collaboration diagram for Fixed Point Math:

Macros

#define UQ6_2_MAX   ((uq6_2_t)UINT8_MAX)
 
#define UQ6_2_ONE   ((uq6_2_t)(1U<<2U))
 
#define UQ4_4_MAX   ((uq4_4_t)UINT8_MAX)
 
#define UQ4_4_ONE   ((uq4_4_t)(1U<<4U))
 
#define UQ2_6_MAX   ((uq2_6_t)UINT8_MAX)
 
#define UQ2_6_ONE   ((uq2_6_t)(1U<<6U))
 
#define UQ1_7_MAX   ((uq1_7_t)UINT8_MAX)
 
#define UQ1_7_ONE   ((uq1_7_t)(1U<<7U))
 
#define UQ0_8_MAX   ((uq0_8_t)UINT8_MAX)
 
#define Q3_4_MIN   ((q3_4_t)INT8_MIN)
 
#define Q3_4_MAX   ((q3_4_t)INT8_MAX)
 
#define UQ3_4_ONE   ((q3_4_t)(1 << 4))
 
#define Q1_6_MIN   ((q1_6_t)INT8_MIN)
 
#define Q1_6_MAX   ((q1_6_t)INT8_MAX)
 
#define UQ1_6_ONE   ((q1_6_t)(1 << 4))
 
#define UQ12_4_MAX   ((uq12_4_t)UINT16_MAX)
 
#define UQ12_4_ONE   ((uq12_4_t)(1U<<4U))
 
#define UQ10_6_MAX   ((uq10_6_t)UINT16_MAX)
 
#define UQ10_6_ONE   ((uq10_6_t)(1U << 6U))
 
#define UQ1_15_MAX   ((uq1_15_t)UINT16_MAX)
 
#define UQ1_15_ONE   ((uq1_15_t)(1U << 15U))
 
#define UQ0_16_MAX   ((uq0_16_t)UINT16_MAX)
 
#define Q11_4_MIN   ((q11_4_t)INT16_MIN)
 
#define Q11_4_MAX   ((q11_4_t)INT16_MAX)
 
#define Q11_4_ONE   ((q11_4_t)(1 << 4))
 
#define Q7_8_MIN   ((q7_8_t)INT16_MIN)
 
#define Q7_8_MAX   ((q7_8_t)INT16_MAX)
 
#define Q7_8_ONE   ((q7_8_t)(1 << 8))
 
#define Q3_12_MIN   ((q3_12_t)INT16_MIN)
 
#define Q3_12_MAX   ((q3_12_t)INT16_MAX)
 
#define Q3_12_ONE   ((q3_12_t)(1 << 12))
 
#define Q0_15_MIN   ((q0_15_t)INT16_MIN)
 
#define Q0_15_MAX   ((q0_15_t)INT16_MAX)
 
#define UQ28_4_MAX   ((uq28_4_t)UINT32_MAX)
 
#define UQ28_4_ONE   ((uq28_4_t)(1U<<4U))
 
#define UQ16_16_MAX   ((uq16_16_t)UINT32_MAX)
 
#define UQ16_16_ONE   ((uq16_16_t)(1U << 16U))
 
#define UQ16_16_E   (0x2B7E1U)
 
#define UQ10_22_MAX   ((uq10_22_t)UINT32_MAX)
 
#define UQ10_22_ONE   ((uq10_22_t)(1U << 22U))
 
#define Q27_4_MIN   ((q27_4_t)INT32_MIN)
 
#define Q27_4_MAX   ((q27_4_t)INT32_MAX)
 
#define UQ27_4_ONE   ((q27_4_t)(1 << 4))
 
#define Q16_15_MIN   ((q16_15_t)INT32_MIN)
 
#define Q16_15_MAX   ((q16_15_t)INT32_MAX)
 
#define Q15_16_MIN   ((q15_16_t)INT32_MIN)
 
#define Q15_16_MAX   ((q15_16_t)INT32_MAX)
 
#define Q15_16_ONE   ((q15_16_t)(1 << 16))
 
#define Q9_22_MIN   ((q9_22_t)INT32_MIN)
 
#define Q9_22_MAX   ((q9_22_t)INT32_MAX)
 
#define Q9_22_ONE   ((q9_22_t)(1 << 22))
 
#define USE_HW_DIV   0
 
#define USE_64BIT_MUL   0
 

Typedefs

typedef uint8_t uq6_2_t
 Unsigned fixed point number: UQ6.2.
 
typedef uint8_t uq4_4_t
 Unsigned fixed point number: UQ4.4.
 
typedef uint8_t uq2_6_t
 Unsigned fixed point number: UQ2.6.
 
typedef uint8_t uq1_7_t
 Unsigned fixed point number: UQ1.7.
 
typedef uint8_t uq0_8_t
 Unsigned fixed point number: UQ0.8.
 
typedef int8_t q3_4_t
 Signed fixed point number: Q3.4.
 
typedef int8_t q1_6_t
 Signed fixed point number: Q1.6.
 
typedef uint16_t uq12_4_t
 Unsigned fixed point number: UQ12.4.
 
typedef uint16_t uq10_6_t
 Unsigned fixed point number: UQ10.6.
 
typedef uint16_t uq1_15_t
 Unsigned fixed point number: UQ1.15.
 
typedef uint16_t uq0_16_t
 Unsigned fixed point number: UQ0.16.
 
typedef int16_t q11_4_t
 Signed fixed point number: Q11.4.
 
typedef int16_t q7_8_t
 Signed fixed point number: Q7.8.
 
typedef int16_t q3_12_t
 Signed fixed point number: Q3.12.
 
typedef int16_t q0_15_t
 Signed fixed point number: Q0.15.
 
typedef uint32_t uq28_4_t
 Unsigned fixed point number: UQ28.4.
 
typedef uint32_t uq16_16_t
 Unsigned fixed point number: UQ16.16.
 
typedef uint32_t uq10_22_t
 Unsigned fixed point number: UQ10.22.
 
typedef int32_t q27_4_t
 Signed fixed point number: Q27.4.
 
typedef int32_t q16_15_t
 Signed fixed point number: Q16.15.
 
typedef int32_t q15_16_t
 Signed fixed point number: Q15.16.
 
typedef int32_t q9_22_t
 Signed fixed point number: Q9.22.
 

Functions

int32_t fp_div16 (int32_t a, q15_16_t b)
 32-bit implementation of an Q15.16 division.
 
uq1_15_t fp_ema15c (uq1_15_t mean, uq1_15_t x, uq0_8_t weight)
 Circular exponentially weighted moving average using UQ1.15 format.
 
q11_4_t fp_ema4 (q11_4_t mean, q11_4_t x, uq0_8_t weight)
 Exponentially weighted moving average using the Q11.4 format.
 
q7_8_t fp_ema8 (q7_8_t mean, q7_8_t x, uq0_8_t weight)
 Exponentially weighted moving average using the Q7.8 format.
 
uint32_t uint_ema32 (uint32_t mean, uint32_t x, uq0_8_t weight)
 Exponentially weighted moving average using the Q15.16 format.
 
int32_t int_ema32 (int32_t mean, int32_t x, uq0_8_t weight)
 Exponentially weighted moving average using the Q15.16 format.
 
q15_16_t fp_ema16 (q15_16_t mean, q15_16_t x, uq0_8_t weight)
 Exponentially weighted moving average using the Q15.16 format.
 
uq16_16_t fp_exp16 (q15_16_t x)
 Calculates the exponential of an fixed point number Q15.16 format.
 
q15_16_t fp_log16 (uq16_16_t x)
 Calculates the natural logarithm (base e) of an fixed point number.
 
void muldwu (uint32_t w[], uint32_t u, uint32_t v)
 Long multiplication of two unsigned 32-bit into an 64-bit value on 32-bit architecture.
 
uint32_t fp_mulu (uint32_t u, uint32_t v, uint_fast8_t shift)
 64-bit implementation of an unsigned multiplication with fixed point format.
 
int32_t fp_muls (int32_t u, int32_t v, uint_fast8_t shift)
 64-bit implementation of a signed multiplication with fixed point format.
 
uint32_t fp_mul_u32_u16 (uint32_t u, uint16_t v, uint_fast8_t shift)
 48-bit implementation of a unsigned multiplication with fixed point format.
 
int32_t fp_mul_s32_u16 (int32_t u, uint16_t v, uint_fast8_t shift)
 48-bit implementation of an unsigned/signed multiplication with fixed point format.
 
uint32_t fp_rndu (uint32_t Q, uint_fast8_t n)
 Converting with rounding from UQx.n1 to UQx.n2.
 
int32_t fp_rnds (int32_t Q, uint_fast8_t n)
 Converting with rounding from Qx.n1 to Qx.n2.
 
uint32_t fp_truncu (uint32_t Q, uint_fast8_t n)
 Converting with truncation from UQx.n1 to UQx.n2.
 
int32_t fp_truncs (int32_t Q, uint_fast8_t n)
 Converting with truncation from Qx.n1 to Qx.n2.
 

Detailed Description

A basic math library for fixed point number in the Qx.y fomat.


This module contains common fixed point type definitions as well as some basic math algorithms. All types are based on integer types. The number are defined with the Q number format.

Warning
This definitions are not portable and work only with little-endian systems!

Macro Definition Documentation

◆ Q0_15_MAX

#define Q0_15_MAX   ((q0_15_t)INT16_MAX)

Maximum value of Q0.15 number format.

◆ Q0_15_MIN

#define Q0_15_MIN   ((q0_15_t)INT16_MIN)

Minimum value of Q0.15 number format.

◆ Q11_4_MAX

#define Q11_4_MAX   ((q11_4_t)INT16_MAX)

Maximum value of Q11.4 number format.

◆ Q11_4_MIN

#define Q11_4_MIN   ((q11_4_t)INT16_MIN)

Minimum value of Q11.4 number format.

◆ Q11_4_ONE

#define Q11_4_ONE   ((q11_4_t)(1 << 4))

The 1/one/unity in Q11.4 number format.

◆ Q15_16_MAX

#define Q15_16_MAX   ((q15_16_t)INT32_MAX)

Maximum value of Q15.16 number format.

◆ Q15_16_MIN

#define Q15_16_MIN   ((q15_16_t)INT32_MIN)

Minimum value of Q15.16 number format.

◆ Q15_16_ONE

#define Q15_16_ONE   ((q15_16_t)(1 << 16))

The 1/one/unity in Q15.16 number format.

◆ Q16_15_MAX

#define Q16_15_MAX   ((q16_15_t)INT32_MAX)

Maximum value of Q16.15 number format.

◆ Q16_15_MIN

#define Q16_15_MIN   ((q16_15_t)INT32_MIN)

Minimum value of Q16.15 number format.

◆ Q1_6_MAX

#define Q1_6_MAX   ((q1_6_t)INT8_MAX)

Maximum value of Q1.6 number format.

◆ Q1_6_MIN

#define Q1_6_MIN   ((q1_6_t)INT8_MIN)

Minimum value of Q1_6 number format.

◆ Q27_4_MAX

#define Q27_4_MAX   ((q27_4_t)INT32_MAX)

Maximum value of Q27.4 number format.

◆ Q27_4_MIN

#define Q27_4_MIN   ((q27_4_t)INT32_MIN)

Minimum value of Q27.4 number format.

◆ Q3_12_MAX

#define Q3_12_MAX   ((q3_12_t)INT16_MAX)

Maximum value of Q3.12 number format.

◆ Q3_12_MIN

#define Q3_12_MIN   ((q3_12_t)INT16_MIN)

Minimum value of Q3.12 number format.

◆ Q3_12_ONE

#define Q3_12_ONE   ((q3_12_t)(1 << 12))

The 1/one/unity in Q3.12 number format.

◆ Q3_4_MAX

#define Q3_4_MAX   ((q3_4_t)INT8_MAX)

Maximum value of Q3.4 number format.

◆ Q3_4_MIN

#define Q3_4_MIN   ((q3_4_t)INT8_MIN)

Minimum value of Q3.4 number format.

◆ Q7_8_MAX

#define Q7_8_MAX   ((q7_8_t)INT16_MAX)

Maximum value of Q7.8 number format.

◆ Q7_8_MIN

#define Q7_8_MIN   ((q7_8_t)INT16_MIN)

Minimum value of Q7.8 number format.

◆ Q7_8_ONE

#define Q7_8_ONE   ((q7_8_t)(1 << 8))

The 1/one/unity in Q7.8 number format.

◆ Q9_22_MAX

#define Q9_22_MAX   ((q9_22_t)INT32_MAX)

Maximum value of Q9.22 number format.

◆ Q9_22_MIN

#define Q9_22_MIN   ((q9_22_t)INT32_MIN)

Minimum value of Q9.22 number format.

◆ Q9_22_ONE

◆ UQ0_16_MAX

#define UQ0_16_MAX   ((uq0_16_t)UINT16_MAX)

Maximum value of UQ0.16 number format.

◆ UQ0_8_MAX

#define UQ0_8_MAX   ((uq0_8_t)UINT8_MAX)

Maximum value of UQ0.8 number format.

◆ UQ10_22_MAX

#define UQ10_22_MAX   ((uq10_22_t)UINT32_MAX)

Maximum value of UQ10.22 number format.

◆ UQ10_22_ONE

#define UQ10_22_ONE   ((uq10_22_t)(1U << 22U))

The 1/one/unity in UQ10.22 number format.

◆ UQ10_6_MAX

#define UQ10_6_MAX   ((uq10_6_t)UINT16_MAX)

Maximum value of UQ10.6 number format.

◆ UQ10_6_ONE

#define UQ10_6_ONE   ((uq10_6_t)(1U << 6U))

The 1/one/unity in UQ10.6 number format.

◆ UQ12_4_MAX

#define UQ12_4_MAX   ((uq12_4_t)UINT16_MAX)

Maximum value of UQ12.4 number format.

◆ UQ12_4_ONE

#define UQ12_4_ONE   ((uq12_4_t)(1U<<4U))

◆ UQ16_16_E

#define UQ16_16_E   (0x2B7E1U)

Euler's number, e, in UQ16.16 format.

◆ UQ16_16_MAX

#define UQ16_16_MAX   ((uq16_16_t)UINT32_MAX)

Maximum value of UQ16.16 number format.

◆ UQ16_16_ONE

#define UQ16_16_ONE   ((uq16_16_t)(1U << 16U))

The 1/one/unity in UQ16.16 number format.

◆ UQ1_15_MAX

#define UQ1_15_MAX   ((uq1_15_t)UINT16_MAX)

Maximum value of UQ1.15 number format.

◆ UQ1_15_ONE

#define UQ1_15_ONE   ((uq1_15_t)(1U << 15U))

The 1/one/unity in UQ1.15 number format.

◆ UQ1_6_ONE

#define UQ1_6_ONE   ((q1_6_t)(1 << 4))

The 1/one/unity in UQ1.6 number format.

◆ UQ1_7_MAX

#define UQ1_7_MAX   ((uq1_7_t)UINT8_MAX)

Maximum value of UQ1.7 number format.

◆ UQ1_7_ONE

#define UQ1_7_ONE   ((uq1_7_t)(1U<<7U))

The 1/one/unity in UQ1.7 number format.

◆ UQ27_4_ONE

#define UQ27_4_ONE   ((q27_4_t)(1 << 4))

The 1/one/unity in UQ27.4 number format.

◆ UQ28_4_MAX

#define UQ28_4_MAX   ((uq28_4_t)UINT32_MAX)

Maximum value of UQ28.4 number format.

◆ UQ28_4_ONE

#define UQ28_4_ONE   ((uq28_4_t)(1U<<4U))

The 1/one/unity in UQ28.4 number format.

◆ UQ2_6_MAX

#define UQ2_6_MAX   ((uq2_6_t)UINT8_MAX)

Maximum value of UQ2.6 number format.

◆ UQ2_6_ONE

#define UQ2_6_ONE   ((uq2_6_t)(1U<<6U))

The 1/one/unity in UQ2.6 number format.

◆ UQ3_4_ONE

#define UQ3_4_ONE   ((q3_4_t)(1 << 4))

The 1/one/unity in UQ3.4 number format.

◆ UQ4_4_MAX

#define UQ4_4_MAX   ((uq4_4_t)UINT8_MAX)

Maximum value of UQ4.4 number format.

◆ UQ4_4_ONE

#define UQ4_4_ONE   ((uq4_4_t)(1U<<4U))

The 1/one/unity in UQ4.4 number format.

◆ UQ6_2_MAX

#define UQ6_2_MAX   ((uq6_2_t)UINT8_MAX)

Maximum value of UQ6.2 number format.

◆ UQ6_2_ONE

#define UQ6_2_ONE   ((uq6_2_t)(1U<<2U))

The 1/one/unity in UQ6.2 number format.

◆ USE_64BIT_MUL

#define USE_64BIT_MUL   0

Set to use hardware division (Cortex-M3/4) over software division (Cortex-M0/1).

◆ USE_HW_DIV

#define USE_HW_DIV   0

Set to use hardware division (Cortex-M3/4) over software division (Cortex-M0/1).

Typedef Documentation

◆ q0_15_t

typedef int16_t q0_15_t

Signed fixed point number: Q0.15.


A signed fixed point number format based on the 16-bit integer type with 0 integer and 15 fractional bits.

  • Range: -1 .. 0.999969482
  • Granularity: 0.000030518

◆ q11_4_t

typedef int16_t q11_4_t

Signed fixed point number: Q11.4.


A signed fixed point number format based on the 16-bit signed integer type with 11 integer and 4 fractional bits.

  • Range: -2048 ... 2047.9375
  • Granularity: 0.0625

◆ q15_16_t

typedef int32_t q15_16_t

Signed fixed point number: Q15.16.


A signed fixed point number format based on the 32-bit integer type with 15 integer and 16 fractional bits.

  • Range: -32768 .. 32767.99998
  • Granularity: 1.52588E-05

◆ q16_15_t

typedef int32_t q16_15_t

Signed fixed point number: Q16.15.


A signed fixed point number format based on the 32-bit integer type with 16 integer and 15 fractional bits.

  • Range: -65536 .. 65536.999969482
  • Granularity: 0.000030518

◆ q1_6_t

typedef int8_t q1_6_t

Signed fixed point number: Q1.6.


A signed fixed point number format based on the 8-bit signed integer type with 1 integer and 6 fractional bits.

  • Range: -2 ... 1.984375
  • Granularity: 0.015625

◆ q27_4_t

typedef int32_t q27_4_t

Signed fixed point number: Q27.4.


A signed fixed point number format based on the 32-bit signed integer type with 27 integer and 4 fractional bits.

  • Range: -134217728 ... 134217727.9375
  • Granularity: 0.0625

◆ q3_12_t

typedef int16_t q3_12_t

Signed fixed point number: Q3.12.


A signed fixed point number format based on the 16-bit integer type with 3 integer and 12 fractional bits.

  • Range: -8 .. 7.99975586
  • Granularity: 0.00024414

◆ q3_4_t

typedef int8_t q3_4_t

Signed fixed point number: Q3.4.


A signed fixed point number format based on the 8-bit signed integer type with 3 integer and 4 fractional bits.

  • Range: -8 ... 7.9375
  • Granularity: 0.0625

◆ q7_8_t

typedef int16_t q7_8_t

Signed fixed point number: Q7.8.


A signed fixed point number format based on the 16-bit signed integer type with 7 integer and 8 fractional bits.

  • Range: -128 .. 127.99609375
  • Granularity: 0.00390625

◆ q9_22_t

typedef int32_t q9_22_t

Signed fixed point number: Q9.22.


A signed fixed point number format based on the 32-bit integer type with 9 integer and 22 fractional bits.

  • Range: -512 ... 511.9999998
  • Granularity: 2.38418579101562E-07

◆ uq0_16_t

typedef uint16_t uq0_16_t

Unsigned fixed point number: UQ0.16.


An unsigned fixed point number format based on the 16-bit unsigned integer type with 0 integer and 16 fractional bits.

  • Range: 0 .. 0.9999847412109375
  • Granularity: 1.52587890625e-5

◆ uq0_8_t

typedef uint8_t uq0_8_t

Unsigned fixed point number: UQ0.8.


An unsigned fixed point number format based on the 8-bit unsigned integer type with 1 integer and 7 fractional bits.

  • Range: 0 .. 0.99609375
  • Granularity: 0.00390625

◆ uq10_22_t

typedef uint32_t uq10_22_t

Unsigned fixed point number: UQ10.22.


An unsigned fixed point number format based on the 32-bit unsigned integer type with 10 integer and 22 fractional bits.

  • Range: 0 ... 1023.99999976158
  • Granularity: 2.38418579101562E-07

◆ uq10_6_t

typedef uint16_t uq10_6_t

Unsigned fixed point number: UQ10.6.


An unsigned fixed point number format based on the 16-bit unsigned integer type with 10 integer and 6 fractional bits.

  • Range: 0 ... 1023.984375
  • Granularity: 0.015625

◆ uq12_4_t

typedef uint16_t uq12_4_t

Unsigned fixed point number: UQ12.4.


An unsigned fixed point number format based on the 16-bit unsigned integer type with 12 integer and 4 fractional bits.

  • Range: 0 ... 4095.9375
  • Granularity: 0.0625

◆ uq16_16_t

typedef uint32_t uq16_16_t

Unsigned fixed point number: UQ16.16.


An unsigned fixed point number format based on the 32-bit unsigned integer type with 16 integer and 16 fractional bits.

  • Range: 0 ... 65535.999984741
  • Granularity: 0.000015259

◆ uq1_15_t

typedef uint16_t uq1_15_t

Unsigned fixed point number: UQ1.15.


An unsigned fixed point number format based on the 16-bit unsigned integer type with 1 integer and 15 fractional bits.

  • Range: 0 .. 1.999969
  • Granularity: 0.000031

◆ uq1_7_t

typedef uint8_t uq1_7_t

Unsigned fixed point number: UQ1.7.


An unsigned fixed point number format based on the 8-bit unsigned integer type with 1 integer and 7 fractional bits.

  • Range: 0 .. 1.9921875
  • Granularity: 0.0078125

◆ uq28_4_t

typedef uint32_t uq28_4_t

Unsigned fixed point number: UQ28.4.


An unsigned fixed point number format based on the 32-bit unsigned integer type with 28 integer and 4 fractional bits.

  • Range: 0 ... 268435455.9375
  • Granularity: 0.0625

◆ uq2_6_t

typedef uint8_t uq2_6_t

Unsigned fixed point number: UQ2.6.


An unsigned fixed point number format based on the 8-bit unsigned integer type with 2 integer and 6 fractional bits.

  • Range: 0 .. 3.984375
  • Granularity: 0.015625

◆ uq4_4_t

typedef uint8_t uq4_4_t

Unsigned fixed point number: UQ4.4.


An unsigned fixed point number format based on the 8-bit unsigned integer type with 4 integer and 4 fractional bits.

  • Range: 0 .. 15.9375
  • Granularity: 0.0625

◆ uq6_2_t

typedef uint8_t uq6_2_t

Unsigned fixed point number: UQ6.2.


An unsigned fixed point number format based on the 8-bit unsigned integer type with 6 integer and 2 fractional bits.

  • Range: 0 .. 63.75
  • Granularity: 0.25

Function Documentation

◆ fp_div16()

int32_t fp_div16 ( int32_t  a,
q15_16_t  b 
)
inline

32-bit implementation of an Q15.16 division.


Algorithm to evaluate a/b, where b is in Q15.16 format, on a 32-bit architecture with maximum precision. The result is correctly rounded and given as the input format. Division by 0 yields max. values determined by signs of numerator. Too high/low results are truncated to max/min values.

Depending on the architecture, the division is implemented with a 64-bit division and shifting (Cortex-M3/4) or as a fast software algorithm (Cortex-M0/1) wich runs fast on processors without hardware division.

See also
https://code.google.com/archive/p/libfixmath
Parameters
aNumerator in any Qx.y format
bDenominator in Q15.16 format
Returns
Result = a/b in the same Qx.y format as the input parameter a.

◆ fp_ema15c()

uq1_15_t fp_ema15c ( uq1_15_t  mean,
uq1_15_t  x,
uq0_8_t  weight 
)
inline

Circular exponentially weighted moving average using UQ1.15 format.


Evaluates the moving average (exponentially weighted) for circular data in UQ1.15 format. Circular data is that MAX_VALUE + 1 == MIN_VALUE. For example the usual phase information.

Problem: Due to circularity of phase values, i.e. 0+x and 2PI+x are the same, the usual EMA has issues with the wrap around effect. Especially for vectors with phase around 0 (or 2PI), two values like 0 + x and PI - y are averaged to something around PI instead of 0 which would be more correct.

Solution: Assume that phase jumps of more than PI are not allowed or possible. If a deviation of the new value to the smoothed signal occurs, it is clear that this stems from the wrap around effect and can be caught and correctly handled by the smoothing algorithm.

Caution: If a target comes immediately into the field of view, phase jumps of > PI are indeed possible and volitional. However, the averaging break there anyway since the smoothed signal approaches only with delay to the correct values. The error made here is, that the smoothed signal approaches from the opposite direction. However, is approaches even faster since it always takes the shortest direction.

Parameters
meanThe previous mean value in UQ1.15 format.
xThe current value to be added to the average UQ1.15 format.
weightThe EMA weight in UQ0.7 format.
Returns
The new mean value in UQ1.15 format.

◆ fp_ema16()

q15_16_t fp_ema16 ( q15_16_t  mean,
q15_16_t  x,
uq0_8_t  weight 
)
inline

Exponentially weighted moving average using the Q15.16 format.


Evaluates the moving average (exponentially weighted) for data in Q15.16 format.

Parameters
meanThe previous mean value in Q15.16 format.
xThe current value to be added to the average Q15.16 format.
weightThe EMA weight in UQ0.7 format.
Returns
The new mean value in Q15.16 format.

◆ fp_ema4()

q11_4_t fp_ema4 ( q11_4_t  mean,
q11_4_t  x,
uq0_8_t  weight 
)
inline

Exponentially weighted moving average using the Q11.4 format.


Evaluates the moving average (exponentially weighted) for data in Q11.4 format.

Parameters
meanThe previous mean value in Q11.4 format.
xThe current value to be added to the average Q11.4 format.
weightThe EMA weight in UQ0.7 format.
Returns
The new mean value in Q11.4 format.

◆ fp_ema8()

q7_8_t fp_ema8 ( q7_8_t  mean,
q7_8_t  x,
uq0_8_t  weight 
)
inline

Exponentially weighted moving average using the Q7.8 format.


Evaluates the moving average (exponentially weighted) for data in Q7.8 format.

Parameters
meanThe previous mean value in Q7.8 format.
xThe current value to be added to the average Q7.8 format.
weightThe EMA weight in UQ0.7 format.
Returns
The new mean value in Q7.8 format.

◆ fp_exp16()

uq16_16_t fp_exp16 ( q15_16_t  x)

Calculates the exponential of an fixed point number Q15.16 format.


Calculates y = exp(x) in fixed point representation.

     Note that the result might not be 100 % accurate and might contain
     a small error!

     @see https://www.quinapalus.com/efunc.html
Parameters
xThe input parameter in unsigned fixed point format Q15.16.
Returns
Result y = exp(x) in the UQ16.16 format.

◆ fp_log16()

q15_16_t fp_log16 ( uq16_16_t  x)

Calculates the natural logarithm (base e) of an fixed point number.


Calculates y = ln(x) = log_e(x) in fixed point representation.

     Note that the result might not be 100 % accurate and might contain
     a small error!

     @see https://www.quinapalus.com/efunc.html
Parameters
xThe input parameter in unsigned fixed point format Q15.16.
Returns
Result y = ln(x) in the UQ16.16 format.

◆ fp_mul_s32_u16()

int32_t fp_mul_s32_u16 ( int32_t  u,
uint16_t  v,
uint_fast8_t  shift 
)
inline

48-bit implementation of an unsigned/signed multiplication with fixed point format.


Algorithm to evaluate a*b, where a and b are arbitrary fixed point numbers with 32-bit signed and 16-bit unsigned format respectively. The multiplication is done in two 16x16-bit operations and the result is shifted down by the passed shift parameter in order to return a 32-bit value. The shift is executed with correct rounding.

Note that the result must fit into the 32-bit value. An assertion error occurs otherwise (or undefined behavior of no assert available).

Parameters
uThe left parameter in Qx1.y1 format
vThe right parameter in Qx2.y2 format
shiftThe final right shift (rounding) value.
Returns
Result = (a*b)>>shift in Qx.(y1+y2-shift) format.

◆ fp_mul_u32_u16()

uint32_t fp_mul_u32_u16 ( uint32_t  u,
uint16_t  v,
uint_fast8_t  shift 
)
inline

48-bit implementation of a unsigned multiplication with fixed point format.


Algorithm to evaluate a*b, where a and b are arbitrary fixed point numbers with 32-bit unsigned and 16-bit unsigned format respectively. The multiplication is done in two 16x16-bit operations and the result is shifted down by the passed shift parameter in order to return a 32-bit value.

Note that the result must fit into the 32-bit value. An assertion error occurs otherwise (or undefined behavior of no assert available).

Parameters
uThe left parameter in Qx1.y1 format
vThe right parameter in Qx2.y2 format
shiftThe final right shift (rounding) value.
Returns
Result = (a*b)>>shift in Qx.(y1+y2-shift) format.

◆ fp_muls()

int32_t fp_muls ( int32_t  u,
int32_t  v,
uint_fast8_t  shift 
)
inline

64-bit implementation of a signed multiplication with fixed point format.


Algorithm to evaluate a*b, where a and b are arbitrary fixed point number of 32-bit width. The multiplication is done in 64-bit and the result is shifted down by the passed shift parameter in order to return a 32-bit value. The shift is executed with correct rounding.

Note that the result must fit into the 32-bit value. An assertion error occurs otherwise (or undefined behavior of no assert available).

Parameters
uThe left parameter in Qx1.y1 format
vThe right parameter in Qx2.y2 format
shiftThe final right shift (rounding) value.
Returns
Result = (a*b)>>shift in Qx.(y1+y2-shift) format.

◆ fp_mulu()

uint32_t fp_mulu ( uint32_t  u,
uint32_t  v,
uint_fast8_t  shift 
)
inline

64-bit implementation of an unsigned multiplication with fixed point format.


Algorithm to evaluate a*b, where a and b are arbitrary fixed point number of 32-bit width. The multiplication is done in 64-bit and the result is shifted down by the passed shift parameter in order to return a 32-bit value. The shift is executed with correct rounding.

Note that the result must fit into the 32-bit value. An assertion error occurs otherwise (or undefined behavior of no assert available).

Parameters
uThe left parameter in UQx1.y1 format
vThe right parameter in UQx2.y2 format
shiftThe final right shift (rounding) value.
Returns
Result = (a*b)>>shift in UQx.(y1+y2-shift) format.

◆ fp_rnds()

int32_t fp_rnds ( int32_t  Q,
uint_fast8_t  n 
)
inline

Converting with rounding from Qx.n1 to Qx.n2.


Equivalent to dividing by 2^n with correct rounding to integer values.

Parameters
QThe number in (U)Qx.n1 fixed point format to be rounded.
nThe number of bits to be rounded, e.g. Q7.8 -> Q11.4 => n = 8 - 4 = 4.
Returns
The rounded value in (U)Qx.n2 format.

◆ fp_rndu()

uint32_t fp_rndu ( uint32_t  Q,
uint_fast8_t  n 
)
inline

Converting with rounding from UQx.n1 to UQx.n2.


Equivalent to dividing by 2^n with correct rounding to unsigned integer values.

Parameters
QThe number in (U)Qx.n1 fixed point format to be rounded.
nThe number of bits to be rounded, e.g. UQ8.8 -> UQ12.4 => n = 8 - 4 = 4.
Returns
The rounded value in (U)Qx.n2 format.

◆ fp_truncs()

int32_t fp_truncs ( int32_t  Q,
uint_fast8_t  n 
)
inline

Converting with truncation from Qx.n1 to Qx.n2.


Equivalent to dividing by 2^n with truncating (throw away) the fractional part, resulting in a signed integer/fixed-point value.

Parameters
QThe number in (U)Qx.n1 fixed point format to be truncated.
nThe number of bits to be truncated, e.g. Q7.8 -> Q11.4 => n = 8 - 4 = 4.
Returns
The truncated value in (U)Qx.n2 format.

◆ fp_truncu()

uint32_t fp_truncu ( uint32_t  Q,
uint_fast8_t  n 
)
inline

Converting with truncation from UQx.n1 to UQx.n2.


Equivalent to dividing by 2^n with truncating (throw away) the fractional part, resulting in an unsigned integer/fixed-point value.

Parameters
QThe number in (U)Qx.n1 fixed point format to be truncated.
nThe number of bits to be truncated, e.g. UQ8.8 -> UQ12.4 => n = 8 - 4 = 4.
Returns
The truncated value in (U)Qx.n2 format.

◆ int_ema32()

int32_t int_ema32 ( int32_t  mean,
int32_t  x,
uq0_8_t  weight 
)
inline

Exponentially weighted moving average using the Q15.16 format.


Evaluates the moving average (exponentially weighted) for data in Q15.16 format.

Parameters
meanThe previous mean value in Q15.16 format.
xThe current value to be added to the average Q15.16 format.
weightThe EMA weight in UQ0.7 format.
Returns
The new mean value in Q15.16 format.

◆ muldwu()

void muldwu ( uint32_t  w[],
uint32_t  u,
uint32_t  v 
)
inline

Long multiplication of two unsigned 32-bit into an 64-bit value on 32-bit architecture.


w (two words) gets the product of u and v (one word each). w[0] is the most significant word of the result, w[1] the least. (The words are in big-endian order). It is Knuth's Algorithm M from [Knu2] section 4.3.1.

  • See also
    http://www.hackersdelight.org/hdcodetxt/muldwu.c.txt
    Parameters
    wThe result (u * v) value given as two unsigned 32-bit numbers: w[0] is the most significant word of the result, w[1] the least. (The words are in big-endian order).
    uLeft hand side of the multiplication.
    vRight hand side of the multiplication.

◆ uint_ema32()

uint32_t uint_ema32 ( uint32_t  mean,
uint32_t  x,
uq0_8_t  weight 
)
inline

Exponentially weighted moving average using the Q15.16 format.


Evaluates the moving average (exponentially weighted) for data in Q15.16 format.

Parameters
meanThe previous mean value in Q15.16 format.
xThe current value to be added to the average Q15.16 format.
weightThe EMA weight in UQ0.7 format.
Returns
The new mean value in Q15.16 format.