This commit is contained in:
Steven Dan
2025-12-11 09:43:42 +08:00
commit d8b2974133
1822 changed files with 280037 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
cmake_minimum_required(VERSION 3.21)
include($ENV{XMOS_CMAKE_PATH}/xcommon.cmake)
project(graphic_eq_test)
set(APP_HW_TARGET XCORE-AI-EXPLORER)
set(APP_COMPILER_FLAGS
-O3
-g
-report
-Wall
-Werror
-fxscope)
set(APP_DEPENDENT_MODULES lib_audio_dsp)
set(APP_C_SRCS src/main.c)
set(XMOS_SANDBOX_DIR ${CMAKE_SOURCE_DIR}/../../..)
XMOS_REGISTER_APP()
project(graphic_eq_coeff_test)
set(APP_HW_TARGET XCORE-AI-EXPLORER)
set(APP_COMPILER_FLAGS
-O3
-g
-report
-Wall
-Werror
-fxscope
-fcmdline-buffer-bytes=1024)
set(APP_DEPENDENT_MODULES lib_audio_dsp)
set(APP_C_SRCS src/main_coeffs.c)
set(XMOS_SANDBOX_DIR ${CMAKE_SOURCE_DIR}/../../..)
XMOS_REGISTER_APP()

View File

@@ -0,0 +1,56 @@
// Copyright 2024-2025 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "dsp/adsp.h"
FILE * _fopen(char * fname, char* mode) {
FILE * fp = fopen(fname, mode);
if (fp == NULL)
{
printf("Error opening a file\n");
exit(1);
}
return fp;
}
int main()
{
q2_30 coeffs_buf[50] = {0};
int32_t DWORD_ALIGNED state[160] = {0};
int32_t gains_buf[10] = {0};
FILE * in = _fopen("../sig_48k.bin", "rb");
FILE * out = _fopen("sig_out.bin", "wb");
FILE * gains = _fopen("gains.bin", "rb");
FILE * coeffs = _fopen("coeffs.bin", "rb");
fseek(in, 0, SEEK_END);
int in_len = ftell(in) / sizeof(int32_t);
fseek(in, 0, SEEK_SET);
fread(&gains_buf, sizeof(int32_t), 10, gains);
fclose(gains);
fread(&coeffs_buf, sizeof(int32_t), 50, coeffs);
fclose(coeffs);
// q2_30 * coeffs = adsp_graphic_eq_10b_init(48000.0f);
for (unsigned i = 0; i < in_len; i++)
{
int32_t samp = 0, samp_out = 0;
fread(&samp, sizeof(int32_t), 1, in);
//printf("%ld ", samp);
samp_out = adsp_graphic_eq_10b(samp, gains_buf, coeffs_buf, state);
//printf("%ld ", samp_out);
fwrite(&samp_out, sizeof(int32_t), 1, out);
}
fclose(in);
fclose(out);
return 0;
}

View File

@@ -0,0 +1,35 @@
// Copyright 2024-2025 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "control/helpers.h"
FILE * _fopen(char * fname, char* mode) {
FILE * fp = fopen(fname, mode);
if (fp == NULL)
{
printf("Error opening a file\n");
exit(1);
}
return fp;
}
int main(int argc, char* argv[])
{
float fs = atof(argv[1]);
FILE * out = _fopen("coeffs_out.bin", "wb");
printf("%f\n", fs);
q2_30 * coeffs_buf = adsp_graphic_eq_10b_init(fs);
for (int i=0; i < 50; i++){
printf("%d: %ld\n", i, coeffs_buf[i]);
}
fwrite(&coeffs_buf[0], sizeof(int32_t), 50, out);
fclose(out);
return 0;
}

View File

@@ -0,0 +1,132 @@
# Copyright 2024-2025 XMOS LIMITED.
# This Software is subject to the terms of the XMOS Public Licence: Version 1.
import numpy as np
import soundfile as sf
from pathlib import Path
import shutil
import subprocess
import audio_dsp.dsp.graphic_eq as geq
from audio_dsp.dsp.generic import Q_SIG
import audio_dsp.dsp.signal_gen as gen
import pytest
from test.test_utils import xdist_safe_bin_write, float_to_qxx, qxx_to_float, q_convert_flt
bin_dir = Path(__file__).parent / "bin"
gen_dir = Path(__file__).parent / "autogen"
fs = 48000
def get_sig(len=0.05):
sig_fl = gen.log_chirp(fs, len, 0.5)
sig_fl = q_convert_flt(sig_fl, 23, Q_SIG)
sig_int = float_to_qxx(sig_fl)
name = "sig_48k"
sig_path = bin_dir / str(name + ".bin")
xdist_safe_bin_write(sig_int, sig_path)
# wav file does not need to be locked as it is only used for debugging outside pytest
wav_path = gen_dir / str(name + ".wav")
sf.write(wav_path, sig_fl, int(fs), "PCM_24")
return sig_fl
def get_c_wav(dir_name, sim = True):
app = "xsim" if sim else "xrun --io"
run_cmd = app + " " + str(bin_dir / "graphic_eq_test.xe")
stdout = subprocess.check_output(run_cmd, cwd = dir_name, shell = True)
print("run msg:\n", stdout.decode())
sig_bin = dir_name / "sig_out.bin"
assert sig_bin.is_file(), f"Could not find output bin {sig_bin}"
sig_int = np.fromfile(sig_bin, dtype=np.int32)
sig_fl = qxx_to_float(sig_int)
sf.write(gen_dir / "sig_c.wav", sig_fl, fs, "PCM_24")
return sig_fl
def run_py(filt: geq.graphic_eq_10_band, sig_fl):
out_int = np.zeros(sig_fl.size)
out_fl = np.zeros(sig_fl.size)
for n in range(sig_fl.size):
out_int[n] = filt.process_xcore(sig_fl[n])
# sf.write(gen_dir / "sig_py_int.wav", out_int, fs, "PCM_24")
return out_int
def single_test(filt, tname, sig_fl):
test_dir = bin_dir / tname
test_dir.mkdir(exist_ok = True, parents = True)
all_filt_info = np.empty(0, dtype=np.int32)
for biquad in filt.biquads:
all_filt_info = np.append(all_filt_info, np.array(biquad.int_coeffs, dtype=np.int32))
all_filt_info.tofile(test_dir / "coeffs.bin")
all_filt_info = np.empty(0, dtype=np.int32)
all_filt_info = np.append(all_filt_info, np.array(filt.gains_int, dtype=np.int32))
all_filt_info.tofile(test_dir / "gains.bin")
out_py_int = run_py(filt, sig_fl)
out_c = get_c_wav(test_dir)
shutil.rmtree(test_dir)
np.testing.assert_allclose(out_c, out_py_int, rtol=0, atol=0)
@pytest.fixture(scope="module")
def in_signal():
bin_dir.mkdir(exist_ok=True, parents=True)
gen_dir.mkdir(exist_ok=True, parents=True)
return get_sig()
@pytest.mark.parametrize("gains", [[-6, 0, -5, 1, -4, 2, -3, 3, -2, 4]])
def test_geq_c(in_signal, gains):
peq = geq.graphic_eq_10_band (fs, 1, gains)
filter_name = f"geq_{abs(gains[0])}"
single_test(peq, filter_name, in_signal)
@pytest.mark.parametrize("fs", [16000, 32000, 44100, 48000, 88200, 96000, 192000])
def test_geq_coeffs_c(fs):
gains = np.zeros(10)
peq = geq.graphic_eq_10_band(fs, 1, gains)
verbose = True
tname = f"geq_coeffs_{fs}"
test_dir = bin_dir / tname
test_dir.mkdir(exist_ok = True, parents = True)
run_cmd = "xsim --args " + str(bin_dir / "graphic_eq_coeff_test.xe") + f" {fs}"
stdout = subprocess.check_output(run_cmd, cwd = test_dir, shell = True)
if verbose: print("run msg:\n", stdout.decode())
sig_bin = test_dir / "coeffs_out.bin"
assert sig_bin.is_file(), f"Could not find output bin {sig_bin}"
c_coeffs = np.fromfile(sig_bin, dtype=np.int32)
py_coeffs = np.array(peq._get_coeffs())
if fs == 16000:
np.testing.assert_allclose(py_coeffs, c_coeffs, rtol=2**-19.9, atol=1)
else:
np.testing.assert_allclose(py_coeffs, c_coeffs, rtol=2**-21, atol=1)
if __name__ =="__main__":
bin_dir.mkdir(exist_ok=True, parents=True)
gen_dir.mkdir(exist_ok=True, parents=True)
sig_fl = get_sig()
# test_geq_c(sig_fl, [0, -2000,-2000,-2000,-2000,-2000,-2000,-2000,-2000,-2000,])
test_geq_c(sig_fl, [-6, 0, -5, 1, -4, 2, -3, 3, -2, 4])

View File

@@ -0,0 +1,72 @@
# Copyright 2024-2025 XMOS LIMITED.
# This Software is subject to the terms of the XMOS Public Licence: Version 1.
import pytest
import numpy as np
import audio_dsp.dsp.graphic_eq as geq
import audio_dsp.dsp.signal_gen as gen
import audio_dsp.dsp.utils as utils
def chirp_filter_test(filter, fs):
length = 0.05
signal = gen.log_chirp(fs, length, 0.5)
output_flt = np.zeros(len(signal))
output_xcore = np.zeros(len(signal))
for n in np.arange(len(signal)):
output_flt[n] = filter.process(signal[n])
for n in np.arange(len(signal)):
output_xcore[n] = filter.process_xcore(signal[n])
# small signals are always going to be ropey due to quantizing, so just check average error of top half
top_half = utils.db(output_flt) > -50
if np.any(top_half):
error_vpu = np.abs(utils.db(output_flt[top_half])-utils.db(output_xcore[top_half]))
mean_error_vpu = utils.db(np.nanmean(utils.db2gain(error_vpu)))
assert mean_error_vpu < 0.05
@pytest.mark.parametrize("fs", [48000])
@pytest.mark.parametrize("n_chans", [1, 2])
@pytest.mark.parametrize("gains", [[0, -12, 0, 12, 0, -12, 0, 12, 0, -12],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
def test_geq(gains, fs, n_chans):
filter = geq.graphic_eq_10_band(fs, n_chans, gains)
chirp_filter_test(filter, fs)
@pytest.mark.parametrize("fs", [48000, 96000, 192000])
@pytest.mark.parametrize("n_chans", [1, 2, 4])
@pytest.mark.parametrize("gains", [[0, -12, 0, 12, 0, -12, 0, 12, 0, -12],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
@pytest.mark.parametrize("q_format", [27, 31])
def test_geq_frames(gains, fs, n_chans, q_format):
filter = geq.graphic_eq_10_band(fs, n_chans, gains, Q_sig=q_format)
length = 0.05
signal = gen.log_chirp(fs, length, 0.5)
signal = np.tile(signal, [n_chans, 1])
signal_frames = utils.frame_signal(signal, 1, 1)
output_flt = np.zeros_like(signal)
output_xcore = np.zeros_like(signal)
frame_size = 1
for n in range(len(signal_frames)):
output_flt[:, n*frame_size:(n+1)*frame_size] = filter.process_frame(signal_frames[n])
assert np.all(output_flt[0, :] == output_flt)
for n in range(len(signal_frames)):
output_xcore[:, n*frame_size:(n+1)*frame_size] = filter.process_frame_xcore(signal_frames[n])
assert np.all(output_xcore[0, :] == output_xcore)
if __name__ == "__main__":
# test_geq([0, -12, 0, 12, 0, -12, 0, 12, 0, -12], 48000, 1)
test_geq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ], 48000, 1)