diff --git a/test/ztest/unit/math/dft3/CMakeLists.txt b/test/ztest/unit/math/dft3/CMakeLists.txt new file mode 100644 index 000000000000..b97d6e33b2a5 --- /dev/null +++ b/test/ztest/unit/math/dft3/CMakeLists.txt @@ -0,0 +1,43 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sof_unit_math_dft3) + +set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../../../") +set(sof_top_dir ${SOF_ROOT}) + +include(${SOF_ROOT}/scripts/cmake/misc.cmake) +include(${SOF_ROOT}/scripts/cmake/uuid-registry.cmake) + +target_include_directories(app PRIVATE + ${SOF_ROOT}/zephyr/include + ${SOF_ROOT}/src/include + ${SOF_ROOT}/src/platform/posix/include + # Reference test vectors are #included in place from the CMocka tree + # rather than copied, so both frameworks share identical data. + ${SOF_ROOT}/test/cmocka/src/math/fft + ${PROJECT_BINARY_DIR}/include/generated +) + +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +target_sources(app PRIVATE + test_dft3_ztest.c + ${SOF_ROOT}/src/math/fft/fft_multi.c + ${SOF_ROOT}/src/math/fft/fft_multi_generic.c + ${SOF_ROOT}/src/math/fft/fft_multi_hifi3.c + ${SOF_ROOT}/src/math/fft/fft_common.c + ${SOF_ROOT}/src/math/fft/fft_32.c +) + +target_link_libraries(app PRIVATE m) + +sof_append_relative_path_definitions(app) diff --git a/test/ztest/unit/math/dft3/prj.conf b/test/ztest/unit/math/dft3/prj.conf new file mode 100644 index 000000000000..848e53d24787 --- /dev/null +++ b/test/ztest/unit/math/dft3/prj.conf @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n diff --git a/test/ztest/unit/math/dft3/test_dft3_ztest.c b/test/ztest/unit/math/dft3/test_dft3_ztest.c new file mode 100644 index 000000000000..fb7743c440ab --- /dev/null +++ b/test/ztest/unit/math/dft3/test_dft3_ztest.c @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/math/fft/dft3.c: +// Author: Seppo Ingalsuo + +#include + +#include +#include +#include + +/* Reference test vectors shared with the legacy CMocka test. They are + * #included in place from the CMocka tree (added to the include path in + * CMakeLists.txt) so the exact same fixed-point data is exercised. + */ +#include "ref_dft3_32.h" + +#include +#include +#include + +#define SOFM_DFT3_MAX_ERROR_ABS 3.1 +#define SOFM_DFT3_MAX_ERROR_RMS 1.1 +#define DFT_SIZE 3 + +BUILD_ASSERT(ARRAY_SIZE(input_data_real_q31) == ARRAY_SIZE(input_data_imag_q31)); +BUILD_ASSERT(ARRAY_SIZE(ref_data_real_q31) == ARRAY_SIZE(ref_data_imag_q31)); +BUILD_ASSERT(ARRAY_SIZE(input_data_real_q31) == ARRAY_SIZE(ref_data_real_q31)); +BUILD_ASSERT(ARRAY_SIZE(input_data_real_q31) == DFT_SIZE * REF_SOFM_DFT3_NUM_TESTS); + +/** + * @brief Run the 3-point DFT against its Q1.31 reference vector. + * + * Feeds each of the REF_SOFM_DFT3_NUM_TESTS blocks of DFT_SIZE complex + * samples through dft3_32() and accumulates the absolute and RMS error + * against the pre-computed reference, preserving the fixed-point semantics + * of the original CMocka test. + */ +ZTEST(math_dft3_suite, test_dft3_32) +{ + struct icomplex32 x[DFT_SIZE]; + struct icomplex32 y[DFT_SIZE]; + double delta; + double error_rms; + double delta_max = 0; + double sum_squares = 0; + const int32_t *p_in_real = input_data_real_q31; + const int32_t *p_in_imag = input_data_imag_q31; + const int32_t *p_ref_real = ref_data_real_q31; + const int32_t *p_ref_imag = ref_data_imag_q31; + int i, j; + + for (i = 0; i < REF_SOFM_DFT3_NUM_TESTS; i++) { + for (j = 0; j < DFT_SIZE; j++) { + x[j].real = *p_in_real++; + x[j].imag = *p_in_imag++; + } + + dft3_32(x, y); + + for (j = 0; j < DFT_SIZE; j++) { + delta = (double)*p_ref_real - (double)y[j].real; + sum_squares += delta * delta; + if (delta > delta_max) + delta_max = delta; + else if (-delta > delta_max) + delta_max = -delta; + + delta = (double)*p_ref_imag - (double)y[j].imag; + sum_squares += delta * delta; + if (delta > delta_max) + delta_max = delta; + else if (-delta > delta_max) + delta_max = -delta; + + p_ref_real++; + p_ref_imag++; + } + } + + error_rms = sqrt(sum_squares / (double)(2 * DFT_SIZE * REF_SOFM_DFT3_NUM_TESTS)); + printk("Max absolute error = %5.2f (limit %5.2f), error RMS = %5.2f (limit %5.2f)\n", + (double)delta_max, (double)SOFM_DFT3_MAX_ERROR_ABS, (double)error_rms, + (double)SOFM_DFT3_MAX_ERROR_RMS); + + zassert_true(error_rms < SOFM_DFT3_MAX_ERROR_RMS, + "error RMS %5.2f exceeds limit %5.2f", error_rms, SOFM_DFT3_MAX_ERROR_RMS); + zassert_true(delta_max < SOFM_DFT3_MAX_ERROR_ABS, + "Max absolute error %5.2f exceeds limit %5.2f", delta_max, + SOFM_DFT3_MAX_ERROR_ABS); +} + +ZTEST_SUITE(math_dft3_suite, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/math/dft3/testcase.yaml b/test/ztest/unit/math/dft3/testcase.yaml new file mode 100644 index 000000000000..6f34ca1f04ba --- /dev/null +++ b/test/ztest/unit/math/dft3/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Math 3-point DFT unit test converted from CMock to Ztest +# +# These contents may have been developed with support from one or more Intel-operated +# generative artificial intelligence solutions. +# + +common: + tags: + - SOF + - unit_test + - math + - fft + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.math.dft3: + platform_allow: + - native_sim + build_only: false