From 1150554ad5835e614095b83985e0c577f47d791f Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 15 May 2023 18:00:59 -0500 Subject: [PATCH 1/3] ulab.numpy: implement sinc for creating audio filters This is useful for generating FIR filters using code snippets generated at https://fiiir.com/ (at least those with a rectangular window type; other window types need additional functions but we can revisit it later if needed) I think this will come in handy for folks who are using the advanced features of our audio synthesizer module, synthio. e.g., the following block now gives highly similar results on ulab or numpy: ```py try: import numpy as np except: from ulab import numpy as np # Example code, computes the coefficients of a low-pass windowed-sinc filter. # Configuration. fS = 48000 # Sampling rate. fL = 4000 # Cutoff frequency. N = 23 # Filter length, must be odd. # Compute sinc filter. h = np.sinc(2 * fL / fS * (np.arange(N) - (N - 1) / 2)) # Normalize to get unity gain. h /= np.sum(h) # Applying the filter to a signal s can be as simple as writing # s = np.convolve(s, h) --- code/numpy/numpy.c | 3 +++ code/numpy/vector.c | 21 +++++++++++++++++++++ code/numpy/vector.h | 1 + code/ulab.h | 4 ++++ 4 files changed, 29 insertions(+) diff --git a/code/numpy/numpy.c b/code/numpy/numpy.c index a53a32cf..7c35f0e1 100644 --- a/code/numpy/numpy.c +++ b/code/numpy/numpy.c @@ -340,6 +340,9 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = { #if ULAB_NUMPY_HAS_SIN { MP_ROM_QSTR(MP_QSTR_sin), MP_ROM_PTR(&vector_sin_obj) }, #endif + #if ULAB_NUMPY_HAS_SINC + { MP_ROM_QSTR(MP_QSTR_sinc), MP_ROM_PTR(&vector_sinc_obj) }, + #endif #if ULAB_NUMPY_HAS_SINH { MP_ROM_QSTR(MP_QSTR_sinh), MP_ROM_PTR(&vector_sinh_obj) }, #endif diff --git a/code/numpy/vector.c b/code/numpy/vector.c index 01e209ea..914ab014 100644 --- a/code/numpy/vector.c +++ b/code/numpy/vector.c @@ -570,6 +570,27 @@ MATH_FUN_1(sin, sin); MP_DEFINE_CONST_FUN_OBJ_1(vector_sin_obj, vector_sin); #endif +#if ULAB_NUMPY_HAS_SINC +//| def sin(a: _ArrayLike) -> ulab.numpy.ndarray: +//| """Computes the sine function""" +//| ... +//| + +static mp_float_t ulab_sinc1(mp_float_t x) { + if (fpclassify(x) == FP_ZERO) { + return MICROPY_FLOAT_CONST(1.); + } + x *= MP_PI; + return MICROPY_FLOAT_C_FUN(sin)(x) / x; +} + +static mp_obj_t vector_sinc(mp_obj_t x_obj) { + return vector_generic_vector(x_obj, ulab_sinc1); +} + +MP_DEFINE_CONST_FUN_OBJ_1(vector_sinc_obj, vector_sinc); +#endif + #if ULAB_NUMPY_HAS_SINH //| def sinh(a: _ArrayLike) -> ulab.numpy.ndarray: //| """Computes the hyperbolic sine""" diff --git a/code/numpy/vector.h b/code/numpy/vector.h index ea38b0fd..c688e95c 100644 --- a/code/numpy/vector.h +++ b/code/numpy/vector.h @@ -39,6 +39,7 @@ MP_DECLARE_CONST_FUN_OBJ_1(vector_log10_obj); MP_DECLARE_CONST_FUN_OBJ_1(vector_log2_obj); MP_DECLARE_CONST_FUN_OBJ_1(vector_radians_obj); MP_DECLARE_CONST_FUN_OBJ_1(vector_sin_obj); +MP_DECLARE_CONST_FUN_OBJ_1(vector_sinc_obj); MP_DECLARE_CONST_FUN_OBJ_1(vector_sinh_obj); #if ULAB_SUPPORTS_COMPLEX MP_DECLARE_CONST_FUN_OBJ_KW(vector_sqrt_obj); diff --git a/code/ulab.h b/code/ulab.h index eedddc5e..1d493ab3 100644 --- a/code/ulab.h +++ b/code/ulab.h @@ -616,6 +616,10 @@ #define ULAB_NUMPY_HAS_SIN (1) #endif +#ifndef ULAB_NUMPY_HAS_SINC +#define ULAB_NUMPY_HAS_SINC (1) +#endif + #ifndef ULAB_NUMPY_HAS_SINH #define ULAB_NUMPY_HAS_SINH (1) #endif From 6000743c456c165f1a68939fb03d6a1250928116 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 16 May 2023 07:32:25 -0500 Subject: [PATCH 2/3] fix docstring of sinc --- code/numpy/vector.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/numpy/vector.c b/code/numpy/vector.c index 914ab014..cc44904b 100644 --- a/code/numpy/vector.c +++ b/code/numpy/vector.c @@ -571,8 +571,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(vector_sin_obj, vector_sin); #endif #if ULAB_NUMPY_HAS_SINC -//| def sin(a: _ArrayLike) -> ulab.numpy.ndarray: -//| """Computes the sine function""" +//| def sinc(a: _ArrayLike) -> ulab.numpy.ndarray: +//| """Computes the normalized sinc function""" //| ... //| From f3e6e1c6d2308cc75e16e351e36f337bb3c93df9 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 16 May 2023 07:33:20 -0500 Subject: [PATCH 3/3] include new requirement of circuitpython build --- requirements_cp_dev.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/requirements_cp_dev.txt b/requirements_cp_dev.txt index 06f35fbd..8a4cdba3 100644 --- a/requirements_cp_dev.txt +++ b/requirements_cp_dev.txt @@ -15,5 +15,4 @@ myst-parser # For stubs and annotations adafruit-circuitpython-typing - - +build