|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright(c) 2026 Intel Corporation. |
| 4 | + |
| 5 | +#include <math.h> |
| 6 | +#include <sof/common.h> |
| 7 | +#include <sof/math/icomplex32.h> |
| 8 | +#include <zephyr/logging/log.h> |
| 9 | +#include <zephyr/ztest.h> |
| 10 | + |
| 11 | +/* Test data tables from Octave generated reference */ |
| 12 | +#include "test_complex_polar_tables.h" |
| 13 | + |
| 14 | +LOG_MODULE_REGISTER(test_complex_polar, LOG_LEVEL_INF); |
| 15 | + |
| 16 | +#define COMPLEX_ABS_TOL 1.2e-8 |
| 17 | +#define MAGNITUDE_ABS_TOL 7.1e-8 |
| 18 | +#define ANGLE_ABS_TOL 4.4e-5 |
| 19 | + |
| 20 | +/** |
| 21 | + * @brief Test complex to polar conversion function |
| 22 | + * |
| 23 | + * This test validates the icomplex32_to_polar(() function against |
| 24 | + * Octave-generated reference values. The test includes 1000 |
| 25 | + * |
| 26 | + * Complex number values are Q1.31 -1.0 to +1.0 |
| 27 | + * Polar magnitude values are Q2.30 0 to +2.0 |
| 28 | + * Polar angle values are Q3.29 from -pi to +pi |
| 29 | + */ |
| 30 | +ZTEST(math_complex, test_icomplex32_to_polar) |
| 31 | +{ |
| 32 | + struct icomplex32 complex, complex_mag_max, complex_ang_max; |
| 33 | + struct ipolar32 polar; |
| 34 | + double ref_magnitude, ref_angle; |
| 35 | + double magnitude, angle; |
| 36 | + double delta_mag, delta_ang; |
| 37 | + double magnitude_scale_q30 = 1.0 / 1073741824.0; |
| 38 | + double angle_scale_q29 = 1.0 / 536870912.0; |
| 39 | + double delta_mag_max = 0; |
| 40 | + double delta_ang_max = 0; |
| 41 | + int i; |
| 42 | + |
| 43 | + for (i = 0; i < TEST_COMPLEX_POLAR_NUM_POINTS; i++) { |
| 44 | + complex.real = test_real_values[i]; |
| 45 | + complex.imag = test_imag_values[i]; |
| 46 | + ref_magnitude = magnitude_scale_q30 * test_magnitude_values[i]; |
| 47 | + ref_angle = angle_scale_q29 * test_angle_values[i]; |
| 48 | + sofm_icomplex32_to_polar(&complex, &polar); |
| 49 | + |
| 50 | + magnitude = magnitude_scale_q30 * polar.magnitude; |
| 51 | + delta_mag = fabs(ref_magnitude - magnitude); |
| 52 | + if (delta_mag > delta_mag_max) { |
| 53 | + delta_mag_max = delta_mag; |
| 54 | + complex_mag_max = complex; |
| 55 | + } |
| 56 | + |
| 57 | + angle = angle_scale_q29 * polar.angle; |
| 58 | + delta_ang = fabs(ref_angle - angle); |
| 59 | + if (delta_ang > delta_ang_max) { |
| 60 | + delta_ang_max = delta_ang; |
| 61 | + complex_ang_max = complex; |
| 62 | + } |
| 63 | + |
| 64 | + zassert_true(delta_mag <= MAGNITUDE_ABS_TOL, "Magnitude calc error at (%d, %d)", |
| 65 | + complex.real, complex.imag); |
| 66 | + zassert_true(delta_ang <= ANGLE_ABS_TOL, "Angle calc error at (%d, %d)", |
| 67 | + complex.real, complex.imag); |
| 68 | + } |
| 69 | + |
| 70 | + /* Re-run worst cases to print info */ |
| 71 | + sofm_icomplex32_to_polar(&complex_mag_max, &polar); |
| 72 | + printf("delta_mag_max = %g at (%d, %d) -> (%d, %d)\n", delta_mag_max, complex_mag_max.real, |
| 73 | + complex_mag_max.imag, polar.magnitude, polar.angle); |
| 74 | + |
| 75 | + sofm_icomplex32_to_polar(&complex_ang_max, &polar); |
| 76 | + printf("delta_ang_max = %g at (%d, %d) -> (%d, %d)\n", delta_ang_max, complex_ang_max.real, |
| 77 | + complex_ang_max.imag, polar.magnitude, polar.angle); |
| 78 | +} |
| 79 | + |
| 80 | +ZTEST(math_complex, test_ipolar32_to_complex) |
| 81 | +{ |
| 82 | + struct icomplex32 complex; |
| 83 | + struct ipolar32 polar, polar_real_max, polar_imag_max; |
| 84 | + double ref_real, ref_imag; |
| 85 | + double real, imag; |
| 86 | + double delta_real, delta_imag; |
| 87 | + double scale_q31 = 1.0 / 2147483648.0; |
| 88 | + double delta_real_max = 0; |
| 89 | + double delta_imag_max = 0; |
| 90 | + int i; |
| 91 | + |
| 92 | + for (i = 0; i < TEST_COMPLEX_POLAR_NUM_POINTS; i++) { |
| 93 | + polar.magnitude = test_magnitude_values[i]; |
| 94 | + polar.angle = test_angle_values[i]; |
| 95 | + ref_real = scale_q31 * test_real_values[i]; |
| 96 | + ref_imag = scale_q31 * test_imag_values[i]; |
| 97 | + sofm_ipolar32_to_complex(&polar, &complex); |
| 98 | + |
| 99 | + real = scale_q31 * complex.real; |
| 100 | + delta_real = fabs(ref_real - real); |
| 101 | + if (delta_real > delta_real_max) { |
| 102 | + delta_real_max = delta_real; |
| 103 | + polar_real_max = polar; |
| 104 | + } |
| 105 | + |
| 106 | + imag = scale_q31 * complex.imag; |
| 107 | + delta_imag = fabs(ref_imag - imag); |
| 108 | + if (delta_imag > delta_imag_max) { |
| 109 | + delta_imag_max = delta_imag; |
| 110 | + polar_imag_max = polar; |
| 111 | + } |
| 112 | + |
| 113 | + zassert_true(delta_real <= COMPLEX_ABS_TOL, "Real calc error at (%d, %d)", |
| 114 | + polar.magnitude, polar.angle); |
| 115 | + zassert_true(delta_imag <= COMPLEX_ABS_TOL, "Imag calc error at (%d, %d)", |
| 116 | + polar.magnitude, polar.angle); |
| 117 | + } |
| 118 | + |
| 119 | + /* Re-run worst cases to print info */ |
| 120 | + sofm_ipolar32_to_complex(&polar_real_max, &complex); |
| 121 | + printf("delta_real_max = %g at (%d, %d) -> (%d, %d)\n", delta_real_max, |
| 122 | + polar_real_max.magnitude, polar_real_max.angle, complex.real, complex.imag); |
| 123 | + |
| 124 | + sofm_ipolar32_to_complex(&polar_imag_max, &complex); |
| 125 | + printf("delta_iamg_max = %g at (%d, %d) -> (%d, %d)\n", delta_imag_max, |
| 126 | + polar_imag_max.magnitude, polar_imag_max.angle, complex.real, complex.imag); |
| 127 | +} |
| 128 | + |
| 129 | +ZTEST_SUITE(math_complex, NULL, NULL, NULL, NULL, NULL); |
0 commit comments