/** * @file tiny_resample.h * @author SHUAIWEN CUI (SHUAIWEN001@e.ntu.edu.sg) * @brief tiny_resample | code | header * @version 1.0 * @date 2025-04-29 * @copyright Copyright (c) 2025 * */#pragma once/* DEPENDENCIES */// tiny_dsp configuration file#include"tiny_dsp_config.h"// ESP32 DSP Library for Acceleration#if MCU_PLATFORM_SELECTED == MCU_PLATFORM_ESP32 // ESP32 DSP library#endif#ifdef __cplusplusextern"C"{#endif/** * @name tiny_downsample_skip_f32 * @brief Downsample a signal by a given factor using skipping * * @param input pointer to the input signal array * @param input_len length of the input signal array * @param output pointer to the output signal array * @param output_len pointer to the length of the output signal array * @param keep number of samples to keep * @param skip number of samples to skip * * @return tiny_error_t */tiny_error_ttiny_downsample_skip_f32(constfloat*input,intinput_len,float*output,int*output_len,intkeep,intskip);/** * @name tiny_upsample_zero_f32 * @brief Upsample a signal using zero-insertion between samples * * @param input pointer to the input signal array * @param input_len length of the input signal array * @param output pointer to the output signal array * @param target_len target length for the output signal array * @return tiny_error_t */tiny_error_ttiny_upsample_zero_f32(constfloat*input,intinput_len,float*output,inttarget_len);/** * @name: tiny_resample_f32 * @brief Resample a signal to a target length * * @param input pointer to the input signal array * @param input_len length of the input signal array * @param output pointer to the output signal array * @param target_len target length for the output signal array * @return tiny_error_t */tiny_error_ttiny_resample_f32(constfloat*input,intinput_len,float*output,inttarget_len);#ifdef __cplusplus}#endif
/** * @file tiny_resample.c * @author SHUAIWEN CUI (SHUAIWEN001@e.ntu.edu.sg) * @brief tiny_resample | code | source * @version 1.0 * @date 2025-04-29 * @copyright Copyright (c) 2025 * *//* DEPENDENCIES */#include"tiny_resample.h" // tiny_resample header/** * @name tiny_downsample_skip_f32 * @brief Downsample a signal by a given factor using skipping * * @param input pointer to the input signal array * @param input_len length of the input signal array * @param output pointer to the output signal array * @param output_len pointer to the length of the output signal array * @param keep number of samples to keep * @param skip number of samples to skip * * @return tiny_error_t */tiny_error_ttiny_downsample_skip_f32(constfloat*input,intinput_len,float*output,int*output_len,intkeep,intskip){if(!input||!output||!output_len)returnTINY_ERR_DSP_NULL_POINTER;if(input_len<=0||keep<=0||skip<=0)returnTINY_ERR_DSP_INVALID_PARAM;intout_len=input_len/skip;*output_len=out_len;for(inti=0;i<out_len;i++){output[i]=input[i*skip];}returnTINY_OK;}/** * @name tiny_upsample_zero_f32 * @brief Upsample a signal using zero-insertion between samples * * @param input pointer to the input signal array * @param input_len length of the input signal array * @param output pointer to the output signal array * @param target_len target length for the output signal array * @return tiny_error_t */tiny_error_ttiny_upsample_zero_f32(constfloat*input,intinput_len,float*output,inttarget_len){if(!input||!output)returnTINY_ERR_DSP_NULL_POINTER;if(input_len<=0||target_len<=0)returnTINY_ERR_DSP_INVALID_PARAM;intfactor=target_len/input_len;if(factor<=0)returnTINY_ERR_DSP_INVALID_PARAM;for(inti=0;i<target_len;i++){output[i]=(i%factor==0)?input[i/factor]:0.0f;}returnTINY_OK;}/** * @name: tiny_resample_f32 * @brief Resample a signal to a target length * * @param input pointer to the input signal array * @param input_len length of the input signal array * @param output pointer to the output signal array * @param target_len target length for the output signal array * @return tiny_error_t */tiny_error_ttiny_resample_f32(constfloat*input,intinput_len,float*output,inttarget_len){if(!input||!output)returnTINY_ERR_DSP_NULL_POINTER;if(input_len<=0||target_len<=0)returnTINY_ERR_DSP_INVALID_PARAM;floatratio=(float)(target_len)/(float)(input_len);for(inti=0;i<target_len;i++){floatpos=i/ratio;intindex=(int)floorf(pos);floatfrac=pos-index;if(index>=input_len-1)output[i]=input[input_len-1];// Clamp at endelseoutput[i]=input[index]*(1.0f-frac)+input[index+1]*frac;}returnTINY_OK;}