Repository navigation
test: math: port dcblock_math_test CMocka test to ztest #11468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lgirdwood
merged 1 commit into
thesofproject:main
from
tmleman:topic/upstream/pr/ut/ztest/math/dcblock
Nov 20, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # 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_dcblock) | ||
|
|
||
| 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 | ||
| ${SOF_ROOT}/src/audio/dcblock | ||
| ${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_dcblock_ztest.c | ||
| ${SOF_ROOT}/src/audio/dcblock/dcblock_generic.c | ||
| ${SOF_ROOT}/src/math/numbers.c | ||
| ) | ||
|
|
||
| target_link_libraries(app PRIVATE m) | ||
|
|
||
| sof_append_relative_path_definitions(app) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
| # | ||
| # Copyright(c) 2026 Intel Corporation. | ||
|
|
||
| CONFIG_ZTEST=y | ||
| CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n | ||
| # printk()/zassert messages in this suite print doubles (%g); without this, | ||
| # printk silently emits the format string literally instead of the value. | ||
| CONFIG_CBPRINTF_FP_SUPPORT=y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,321 @@ | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // | ||
| // Copyright(c) 2026 Intel Corporation. | ||
| // | ||
| // Converted from CMock to Ztest | ||
| // Original tests from test/cmocka/src/audio/dcblock/dcblock_math_test.c: | ||
| // Author: Piotr Hoppe <piotr.hoppe@intel.com> | ||
|
|
||
| #include <zephyr/ztest.h> | ||
|
|
||
| #include <sof/common.h> | ||
| #include <sof/audio/format.h> | ||
| #include <sof/audio/audio_stream.h> | ||
|
|
||
| #include "dcblock.h" | ||
|
|
||
| #include <math.h> | ||
| #include <stdint.h> | ||
| #include <string.h> | ||
|
|
||
| /* math.h's M_PI is only exposed under glibc's GNU/POSIX feature test macros, | ||
| * which Zephyr's strict -std=c17 build does not define. | ||
| */ | ||
| #ifndef M_PI | ||
| #define M_PI 3.14159265358979323846 | ||
| #endif | ||
|
|
||
| #define TEST_CHANNELS 2 | ||
| #define TEST_FRAMES 256 | ||
|
|
||
| /* | ||
| * The dcblock processing functions operate on struct cir_buf_source / | ||
| * struct cir_buf_sink circular buffer views. The tests use static, non | ||
| * wrapping sample buffers, so the views simply span the whole array. | ||
| */ | ||
| static void cir_buf_src_setup(struct cir_buf_source *s, const void *data, size_t bytes) | ||
| { | ||
| s->buf_start = data; | ||
| s->buf_end = (const char *)data + bytes; | ||
| s->ptr = data; | ||
| } | ||
|
|
||
| static void cir_buf_snk_setup(struct cir_buf_sink *s, void *data, size_t bytes) | ||
| { | ||
| s->buf_start = data; | ||
| s->buf_end = (char *)data + bytes; | ||
| s->ptr = data; | ||
| } | ||
|
|
||
| /* Q2.30 coefficient close to 1.0 used for the DC removal test cases. */ | ||
| #define R_COEF_NEAR_ONE 1063004406 /* ~0.99 in Q2.30 */ | ||
|
|
||
| /* | ||
| * Reference DC blocking filter implemented in floating point. Mirrors the | ||
| * fixed point recurrence y[n] = x[n] - x[n-1] + R * y[n-1] where R is the | ||
| * Q2.30 coefficient converted to a real number. | ||
| */ | ||
| struct ref_state { | ||
| double x_prev; | ||
| double y_prev; | ||
| }; | ||
|
|
||
| static double dcblock_ref(struct ref_state *s, double r, double x) | ||
| { | ||
| double y = x - s->x_prev + r * s->y_prev; | ||
|
|
||
| s->x_prev = x; | ||
| s->y_prev = y; | ||
| return y; | ||
| } | ||
|
|
||
| /* Fill the source buffer with a per-channel sinusoid plus a DC offset. */ | ||
| static void gen_input(double *ref_in, int channels, int frames, double dc) | ||
| { | ||
| int ch, i; | ||
|
|
||
| for (i = 0; i < frames; i++) { | ||
| for (ch = 0; ch < channels; ch++) { | ||
| double phase = 2.0 * M_PI * (i + 1) * (ch + 1) / 64.0; | ||
|
|
||
| ref_in[i * channels + ch] = dc + 0.3 * sin(phase); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Runs the S32 processing function and compares it to the floating point | ||
| * reference. Returns the mean absolute value of the last quarter of the | ||
| * output which is used to check DC convergence. | ||
| */ | ||
| static double run_s32_case(int32_t r_coeff, double dc, double tol_rel) | ||
| { | ||
| struct comp_data cd; | ||
| struct cir_buf_source csrc; | ||
| struct cir_buf_sink csnk; | ||
| int32_t src[TEST_FRAMES * TEST_CHANNELS]; | ||
| int32_t dst[TEST_FRAMES * TEST_CHANNELS]; | ||
| double ref_in[TEST_FRAMES * TEST_CHANNELS]; | ||
| struct ref_state rstate[TEST_CHANNELS]; | ||
| double r = (double)r_coeff / (double)ONE_Q2_30; | ||
| double tail_abs_sum = 0.0; | ||
| int tail_count = 0; | ||
| dcblock_func func; | ||
| int ch, i; | ||
|
|
||
| memset(&cd, 0, sizeof(cd)); | ||
| cd.channels = TEST_CHANNELS; | ||
| for (ch = 0; ch < TEST_CHANNELS; ch++) | ||
| cd.R_coeffs[ch] = r_coeff; | ||
|
|
||
| memset(rstate, 0, sizeof(rstate)); | ||
| gen_input(ref_in, TEST_CHANNELS, TEST_FRAMES, dc); | ||
|
|
||
| for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++) | ||
| src[i] = (int32_t)round(ref_in[i] * 2147483647.0); | ||
|
|
||
| cir_buf_src_setup(&csrc, src, sizeof(src)); | ||
| cir_buf_snk_setup(&csnk, dst, sizeof(dst)); | ||
|
|
||
| func = dcblock_find_func(SOF_IPC_FRAME_S32_LE); | ||
| zassert_not_null(func, "No S32 dcblock processing function found"); | ||
| zassert_equal(func(&cd, &csrc, &csnk, TEST_FRAMES), 0, "dcblock S32 processing failed"); | ||
|
|
||
| for (i = 0; i < TEST_FRAMES; i++) { | ||
| for (ch = 0; ch < TEST_CHANNELS; ch++) { | ||
| int idx = i * TEST_CHANNELS + ch; | ||
| double refy = dcblock_ref(&rstate[ch], r, | ||
| (double)src[idx] / 2147483648.0); | ||
| double outy = (double)dst[idx] / 2147483648.0; | ||
| double delta = fabs(refy - outy); | ||
|
|
||
| zassert_true(delta <= tol_rel, | ||
| "s32 mismatch idx %d ref %g out %g delta %g exceeds %g", | ||
| idx, refy, outy, delta, tol_rel); | ||
|
|
||
| if (i >= TEST_FRAMES * 3 / 4) { | ||
| tail_abs_sum += fabs(outy); | ||
| tail_count++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return tail_count ? tail_abs_sum / tail_count : 0.0; | ||
| } | ||
|
|
||
| /* Passthrough: R = 1.0 gives y[n] = x[n] - x[n-1], a pure differentiator. */ | ||
| ZTEST(math_dcblock_suite, test_dcblock_passthrough) | ||
| { | ||
| /* With no DC and R=1.0 the reference matches bit-close. */ | ||
| run_s32_case(ONE_Q2_30, 0.0, 1.0e-6); | ||
| } | ||
|
|
||
| /* A strong DC offset must be attenuated towards zero in steady state. */ | ||
| ZTEST(math_dcblock_suite, test_dcblock_dc_removal) | ||
| { | ||
| double tail = run_s32_case(R_COEF_NEAR_ONE, 0.5, 1.0e-6); | ||
|
|
||
| /* The residual DC plus small sinusoid must be well below the input DC. */ | ||
| printk("dc_removal tail mean abs = %g\n", tail); | ||
| zassert_true(tail < 0.25, "tail mean abs %g not below 0.25", tail); | ||
| } | ||
|
|
||
| /* Full scale input must not overflow (saturation path in dcblock_generic). */ | ||
| ZTEST(math_dcblock_suite, test_dcblock_saturation) | ||
| { | ||
| struct comp_data cd; | ||
| struct cir_buf_source csrc; | ||
| struct cir_buf_sink csnk; | ||
| int32_t src[TEST_FRAMES * TEST_CHANNELS]; | ||
| int32_t dst[TEST_FRAMES * TEST_CHANNELS]; | ||
| dcblock_func func; | ||
| int i; | ||
|
|
||
| memset(&cd, 0, sizeof(cd)); | ||
| cd.channels = TEST_CHANNELS; | ||
| for (i = 0; i < TEST_CHANNELS; i++) | ||
| cd.R_coeffs[i] = R_COEF_NEAR_ONE; | ||
|
|
||
| /* | ||
| * Alternate per frame so that every channel sees the full-scale | ||
| * transitions and exercises the saturation path. | ||
| */ | ||
| for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++) | ||
| src[i] = ((i / TEST_CHANNELS) & 1) ? INT32_MAX : INT32_MIN; | ||
|
|
||
| cir_buf_src_setup(&csrc, src, sizeof(src)); | ||
| cir_buf_snk_setup(&csnk, dst, sizeof(dst)); | ||
|
|
||
| func = dcblock_find_func(SOF_IPC_FRAME_S32_LE); | ||
| zassert_not_null(func, "No S32 dcblock processing function found"); | ||
| zassert_equal(func(&cd, &csrc, &csnk, TEST_FRAMES), 0, "dcblock S32 processing failed"); | ||
|
|
||
| for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++) { | ||
| int32_t expected = ((i / TEST_CHANNELS) & 1) ? INT32_MAX : INT32_MIN; | ||
|
|
||
| zassert_equal(dst[i], expected, "saturation mismatch at idx %d", i); | ||
| } | ||
| } | ||
|
|
||
| /* Bit-exactness check against the floating point reference for S32. */ | ||
| ZTEST(math_dcblock_suite, test_dcblock_bitexact_s32) | ||
| { | ||
| run_s32_case(R_COEF_NEAR_ONE, 0.1, 1.0e-6); | ||
| } | ||
|
|
||
| /* | ||
| * Runs the S16 processing function and compares it to the floating point | ||
| * reference. The component internally works in Q1.31, so the tolerance must | ||
| * account for the 16-bit output quantization step (2 LSB of S16). | ||
| */ | ||
| static void run_s16_case(int32_t r_coeff, double dc) | ||
| { | ||
| struct comp_data cd; | ||
| struct cir_buf_source csrc; | ||
| struct cir_buf_sink csnk; | ||
| int16_t src[TEST_FRAMES * TEST_CHANNELS]; | ||
| int16_t dst[TEST_FRAMES * TEST_CHANNELS]; | ||
| double ref_in[TEST_FRAMES * TEST_CHANNELS]; | ||
| struct ref_state rstate[TEST_CHANNELS]; | ||
| double r = (double)r_coeff / (double)ONE_Q2_30; | ||
| double tol = 2.0 / 32768.0; | ||
| dcblock_func func; | ||
| int ch, i; | ||
|
|
||
| memset(&cd, 0, sizeof(cd)); | ||
| cd.channels = TEST_CHANNELS; | ||
| for (ch = 0; ch < TEST_CHANNELS; ch++) | ||
| cd.R_coeffs[ch] = r_coeff; | ||
|
|
||
| memset(rstate, 0, sizeof(rstate)); | ||
| gen_input(ref_in, TEST_CHANNELS, TEST_FRAMES, dc); | ||
|
|
||
| for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++) | ||
| src[i] = (int16_t)round(ref_in[i] * 32767.0); | ||
|
|
||
| cir_buf_src_setup(&csrc, src, sizeof(src)); | ||
| cir_buf_snk_setup(&csnk, dst, sizeof(dst)); | ||
|
|
||
| func = dcblock_find_func(SOF_IPC_FRAME_S16_LE); | ||
| zassert_not_null(func, "No S16 dcblock processing function found"); | ||
| zassert_equal(func(&cd, &csrc, &csnk, TEST_FRAMES), 0, "dcblock S16 processing failed"); | ||
|
|
||
| for (i = 0; i < TEST_FRAMES; i++) { | ||
| for (ch = 0; ch < TEST_CHANNELS; ch++) { | ||
| int idx = i * TEST_CHANNELS + ch; | ||
| double refy = dcblock_ref(&rstate[ch], r, | ||
| (double)src[idx] / 32768.0); | ||
| double outy = (double)dst[idx] / 32768.0; | ||
| double delta = fabs(refy - outy); | ||
|
|
||
| zassert_true(delta <= tol, | ||
| "s16 mismatch idx %d ref %g out %g delta %g exceeds %g", | ||
| idx, refy, outy, delta, tol); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* Bit-exactness check against the floating point reference for S16. */ | ||
| ZTEST(math_dcblock_suite, test_dcblock_bitexact_s16) | ||
| { | ||
| run_s16_case(R_COEF_NEAR_ONE, 0.1); | ||
| } | ||
|
|
||
| /* | ||
| * Runs the S24 (in 32-bit container) processing function and compares it to | ||
| * the floating point reference with a tolerance of 2 LSB of S24. | ||
| */ | ||
| static void run_s24_case(int32_t r_coeff, double dc) | ||
| { | ||
| struct comp_data cd; | ||
| struct cir_buf_source csrc; | ||
| struct cir_buf_sink csnk; | ||
| int32_t src[TEST_FRAMES * TEST_CHANNELS]; | ||
| int32_t dst[TEST_FRAMES * TEST_CHANNELS]; | ||
| double ref_in[TEST_FRAMES * TEST_CHANNELS]; | ||
| struct ref_state rstate[TEST_CHANNELS]; | ||
| double r = (double)r_coeff / (double)ONE_Q2_30; | ||
| double tol = 2.0 / 8388608.0; | ||
| dcblock_func func; | ||
| int ch, i; | ||
|
|
||
| memset(&cd, 0, sizeof(cd)); | ||
| cd.channels = TEST_CHANNELS; | ||
| for (ch = 0; ch < TEST_CHANNELS; ch++) | ||
| cd.R_coeffs[ch] = r_coeff; | ||
|
|
||
| memset(rstate, 0, sizeof(rstate)); | ||
| gen_input(ref_in, TEST_CHANNELS, TEST_FRAMES, dc); | ||
|
|
||
| for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++) | ||
| src[i] = (int32_t)round(ref_in[i] * 8388607.0); | ||
|
|
||
| cir_buf_src_setup(&csrc, src, sizeof(src)); | ||
| cir_buf_snk_setup(&csnk, dst, sizeof(dst)); | ||
|
|
||
| func = dcblock_find_func(SOF_IPC_FRAME_S24_4LE); | ||
| zassert_not_null(func, "No S24 dcblock processing function found"); | ||
| zassert_equal(func(&cd, &csrc, &csnk, TEST_FRAMES), 0, "dcblock S24 processing failed"); | ||
|
|
||
| for (i = 0; i < TEST_FRAMES; i++) { | ||
| for (ch = 0; ch < TEST_CHANNELS; ch++) { | ||
| int idx = i * TEST_CHANNELS + ch; | ||
| double refy = dcblock_ref(&rstate[ch], r, | ||
| (double)src[idx] / 8388608.0); | ||
| double outy = (double)dst[idx] / 8388608.0; | ||
| double delta = fabs(refy - outy); | ||
|
|
||
| zassert_true(delta <= tol, | ||
| "s24 mismatch idx %d ref %g out %g delta %g exceeds %g", | ||
| idx, refy, outy, delta, tol); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* Bit-exactness check against the floating point reference for S24. */ | ||
| ZTEST(math_dcblock_suite, test_dcblock_bitexact_s24) | ||
| { | ||
| run_s24_case(R_COEF_NEAR_ONE, 0.1); | ||
| } | ||
|
|
||
| ZTEST_SUITE(math_dcblock_suite, NULL, NULL, NULL, NULL, NULL); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
| # | ||
| # Copyright(c) 2026 Intel Corporation. | ||
| # | ||
| # DC blocking filter math unit tests 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 | ||
| - dcblock | ||
| integration_platforms: | ||
| - native_sim | ||
| arch_exclude: xtensa | ||
|
|
||
| tests: | ||
| sof.unit.math.dcblock: | ||
| platform_allow: | ||
| - native_sim | ||
| build_only: false |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.