Skip to content

Commit 1f66f7d

Browse files
singalsulgirdwood
authored andcommitted
Math: Complex: Calculate complex phase angle with sofm_atan2()
This patch updates the phase angle calculation in function sofm_icomple32_to_polar() with more efficient sofm_atan2() function. Calculate of angle with arctan function avoids the 64 bit divide in the previous arccos function based calculation. In 48 kHz stereo STFT processing with 1024 size FFT and hop of 256 this change saves about 14 MCPS in MTL platform (204 MCPS to 190 MCPS). Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
1 parent 7cc974c commit 1f66f7d

3 files changed

Lines changed: 7 additions & 12 deletions

File tree

src/math/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ config MATH_DECIBELS
108108
config MATH_COMPLEX
109109
bool "Operations for complex numbers"
110110
default n
111+
select CORDIC_FIXED
112+
select MATH_ATAN2
113+
select SQRT_FIXED
111114
help
112115
Select this to enable functions for complex numbers
113116
arithmetic such as conversions to/from polar format.

src/math/complex.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <sof/audio/format.h>
1010
#include <sof/math/icomplex32.h>
1111
#include <sof/math/sqrt.h>
12+
#include <sof/math/trig.h>
1213
#include <stdint.h>
1314

1415
/* sofm_icomplex32_to_polar() - Convert (re, im) complex number to polar. */
@@ -17,8 +18,6 @@ void sofm_icomplex32_to_polar(struct icomplex32 *complex, struct ipolar32 *polar
1718
struct icomplex32 c = *complex;
1819
int64_t squares_sum;
1920
int32_t sqrt_arg;
20-
int32_t acos_arg;
21-
int32_t acos_val;
2221

2322
/* Calculate square of magnitudes Q1.31, result is Q2.62 */
2423
squares_sum = (int64_t)c.real * c.real + (int64_t)c.imag * c.imag;
@@ -27,16 +26,8 @@ void sofm_icomplex32_to_polar(struct icomplex32 *complex, struct ipolar32 *polar
2726
sqrt_arg = Q_SHIFT_RND(squares_sum, 62, 30);
2827
polar->magnitude = sofm_sqrt_int32(sqrt_arg); /* Q2.30 */
2928

30-
/* Avoid divide by zero and ambiguous angle for a zero vector. */
31-
if (polar->magnitude == 0) {
32-
polar->angle = 0;
33-
return;
34-
}
35-
36-
/* Calculate phase angle with acos( complex->real / polar->magnitude) */
37-
acos_arg = sat_int32((((int64_t)c.real) << 29) / polar->magnitude); /* Q2.30 */
38-
acos_val = acos_fixed_32b(acos_arg); /* Q3.29 */
39-
polar->angle = (c.imag < 0) ? -acos_val : acos_val;
29+
/* Angle */
30+
polar->angle = sofm_atan2_32b(c.imag, c.real); /* Q3.29 */
4031
}
4132
EXPORT_SYMBOL(sofm_icomplex32_to_polar);
4233

test/ztest/unit/math/basic/complex/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ target_sources(app PRIVATE
2222
${SOF_ROOT}/src/math/complex.c
2323
${SOF_ROOT}/src/math/sqrt_int32.c
2424
${SOF_ROOT}/src/math/trig.c
25+
${SOF_ROOT}/src/math/atan2.c
2526
)
2627

2728
# Link math library for standard math functions

0 commit comments

Comments
 (0)