Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
DSPLIB - Signal re-sampling

All the signal re-sampling functions implemented in the DSP advanced library.

Following is a brief description of the frequency re-sampling algorithm used in this module. It is aimed for anybody so no specifics digital signal processing knowledges are required to understand this presentation.

Summary

The principle is simple, it consists in 2 main stages, up-sampling the signal frequency by an integer value (L), this action is also called interpolation, and then down-sampling it by another integer value (M), also known as decimation.

L and M calculation

L and M are 2 integers that are calculated by getting the GCD (Greatest Common Divisor) of the input (Fsin) and the output (Fsout) frequencies.
The number resulting will divide Fsin and Fsout to respectively give M and L.

resampling_l_and_m.gif

Interpolation (frequency up-sampling)

This process up samples the frequency of the input signal by an integer factor. The factor used at this stage of the process by the re-sampling algorithm will be the pre-calculated "interpolation factor" L. Which means, if we consider this process as a black box with 1 input (u) and 1 output (v), the output signal sampling frequency (Fs(v)) will be equals to the input signal sampling frequency (Fs(u)) multiplied by L.

resampling_interpolation.gif

The following describes the algorithm used to implement the interpolation.
The method consists in extending the signal by filling "blank spaces" with zeros, in order to obtain a signal with the desired sampling rate.

resampling_interpolation2.gif

Then this signal goes through a filter in order to remove the highest frequencies, to give it back its original shape. The cut off frequency is calculated according to the input frequency of the signal.
The filter used in this algorithm, is most likely a lowpass FIR filter, which is used as a poly-phase filter. This optimizes greatly the performances of this process because poly-phase filters are simply, classical filters cut into pieces. And in this case, the aim is to have one piece with the original samples and the other with the zeros used to up sample the signal.
Then, by re-ordering the coefficients in a certain way, this process is equivalent to apply a filter only on the original sample parts since the result of filtering a null signal is a null signal.

resampling_interpolation3.gif

Now, the signal is interpolated, it needs to be down sampled.

Decimation (frequency down-sampling)

This process is much simpler than the interpolation.
It just consists in removing samples in order to keep the same signal wave form but with a lower sampling rate.
Therefore, to obtain the desired output sampling frequency, the signal has to be down sampled by M (decimation factor).

resampling_decimation.gif

Every M samples are kept from the input signal and all the others are removed.

resampling_decimation2.gif

Conclusion

By processing these 2 main stages, the signal is re-sampled by a factor equals to L/M. Therefore, the smaller the 2 frequencies have their GCD (Greatest Common Divisor), the more memory it will need (to store the FIR filter coefficients).
This method is one of the most used in digital signal processing systems. It will generate a clean signal and evaluate at best the waveform of the output signal.

Frequency response

The signal is attenuated on high frequencies. Following are traces showing the frequency response of the re-sampling algorithm over different sampling rate conversions.

freq_resp_from_32kHz_to_44.1KHz.jpg
Frequency response from 32KHz to 44.1KHz
freq_resp_from_48kHz_to_48.51KHz.jpg
Frequency response from 48KHz to 48.51KHz

Data Structures

struct  dsp16_resampling_options_t
 Options to attribute to the resampling algorithm. More...
 
struct  dsp_resampling_context_t
 This structure is used to store the context of streaming information during resampling process. More...
 
struct  dsp_resampling_t
 This structure is used to store the context of a resampling process. More...
 

Functions

void dsp16_resampling_compute (dsp_resampling_t *dsp_resampling, dsp16_t *output, dsp16_t *input, int channel)
 The re-sampling computation function. More...
 
int dsp16_resampling_get_output_current_buffer_size (dsp_resampling_t *dsp_resampling)
 Returns the current length in sample of the output signal. More...
 
int dsp16_resampling_get_output_max_buffer_size (dsp_resampling_t *dsp_resampling)
 Returns the maximal length in sample of the output signal. More...
 
bool dsp16_resampling_link (dsp_resampling_t *dsp_resampling_output, dsp_resampling_t *dsp_resampling_input)
 Link a stream previously re-sampled using the dsp_resampling_input resampling structure with the new re-sampling structure dsp_resampling_output. This is used to keep the continuity with two pieces of a stream re-sampled using two different re-sampling parameters. More...
 

Resampling setup function

typedef void *(* malloc_fct_t )(int)
 A pointer on a memory allocation function. More...
 
typedef void(* free_fct_t )(void *)
 A pointer on a memory free function. More...
 
dsp_resampling_tdsp16_resampling_setup (int input_sample_rate, int output_sample_rate, int buffer_size, int order, int nb_channels, malloc_fct_t malloc_fct, dsp16_resampling_options_t *options)
 This function is the 16-bit signal resampling setup function. It has to be called only once at the initialization of the resampling process. More...
 
void dsp16_resampling_free (dsp_resampling_t *dsp_resampling, free_fct_t free_fct)
 Function used to free the previously allocated structure issued by the dsp16_resampling_setup function. More...
 

typedef void(* free_fct_t)(void *)

A pointer on a memory free function.

typedef void*(* malloc_fct_t)(int)

A pointer on a memory allocation function.

void dsp16_resampling_compute ( dsp_resampling_t dsp_resampling,
dsp16_t output,
dsp16_t input,
int  channel 
)

The re-sampling computation function.

Parameters
dsp_resamplingThe re-sampling context structure associated.
outputA pointer on a 16-bit vector used to store output data. The length of this vector is defined by the output of the dsp16_resampling_get_output_current_buffer_size function.
inputA pointer on a 16-bit vector used as an input to the re-sampling process. It has to be of a length defined to the dsp16_resampling_setup function as for its sampling rate.
channelThe channel number to compute (starting from 0 to nb_channels - 1 referred in dsp16_resampling_setup).

References dsp_resampling_t::buffer_size, dsp_resampling_t::context, dsp_resampling_t::current_buffer_size, dsp_resampling_t::decimation_factor, DSP16_RESAMPLING_FUNCTION_NAME, DSP16_RESAMPLING_NO_LOOP_FUNCTION_NAME, dsp16_resampling_polynomial_interpolation(), dsp16_vect_copy(), dsp_resampling_t::filter_order, dsp_resampling_t::fir_coefs, dsp_resampling_context_t::index, dsp_resampling_context_t::input_buffer_pos, dsp_resampling_t::interpolation_factor, dsp_resampling_t::last_samples, dsp_resampling_t::link_required, LOOP_UNROLL, LOOP_UNROLL_PLUS_ONE, MREPEAT, and NB_PTS_TO_INTERPOLATE.

Referenced by main().

void dsp16_resampling_free ( dsp_resampling_t dsp_resampling,
free_fct_t  free_fct 
)

Function used to free the previously allocated structure issued by the dsp16_resampling_setup function.

Parameters
dsp_resamplingThe re-sampling context structure to be freed.
free_fctA pointer on the free function to be used.

Referenced by main().

int dsp16_resampling_get_output_current_buffer_size ( dsp_resampling_t dsp_resampling)

Returns the current length in sample of the output signal.

Parameters
dsp_resamplingThe re-sampling context structure associated.

References dsp_resampling_t::current_buffer_size.

Referenced by main().

int dsp16_resampling_get_output_max_buffer_size ( dsp_resampling_t dsp_resampling)

Returns the maximal length in sample of the output signal.

Parameters
dsp_resamplingThe re-sampling context structure associated.

References dsp_resampling_t::buffer_size, dsp_resampling_t::decimation_factor, dsp_resampling_t::filter_order, and dsp_resampling_t::interpolation_factor.

Referenced by main().

bool dsp16_resampling_link ( dsp_resampling_t dsp_resampling_output,
dsp_resampling_t dsp_resampling_input 
)

Link a stream previously re-sampled using the dsp_resampling_input resampling structure with the new re-sampling structure dsp_resampling_output. This is used to keep the continuity with two pieces of a stream re-sampled using two different re-sampling parameters.

Precondition
Some considerations have to be taken care of before using this function. The two structure MUST have in common: the number of channels, the filter order and the input buffer size.
Parameters
dsp_resampling_outputThe re-sampling context which will be updated according to the dsp_resampling_input context.
dsp_resampling_inputThe input re-sampling context.
Returns
true if the process succeed, false otherwise. A process can fail only if the preliminary conditions are not respected.

References dsp_resampling_t::buffer_size, dsp_resampling_t::context, dsp_resampling_t::decimation_factor, dsp16_vect_copy(), dsp_resampling_t::filter_order, dsp_resampling_context_t::index, dsp_resampling_context_t::input_buffer_pos, dsp_resampling_t::interpolation_factor, dsp_resampling_t::last_samples, dsp_resampling_t::link_required, and dsp_resampling_t::nb_channels.

dsp_resampling_t* dsp16_resampling_setup ( int  input_sample_rate,
int  output_sample_rate,
int  buffer_size,
int  order,
int  nb_channels,
malloc_fct_t  malloc_fct,
dsp16_resampling_options_t options 
)

This function is the 16-bit signal resampling setup function. It has to be called only once at the initialization of the resampling process.

Parameters
input_sample_rateThe sample rate of the input signal.
output_sample_rateThe sample rate of the output signal.
buffer_sizeThe size of the input buffers.
orderThe order of the re-sampling module. This defines indirectly the order of the filter to be used. Its order will be <order> * <interpolation factor>="">.
malloc_fctA pointer on a memory allocation function.
nb_channelsThe number of channels to compute.
optionsAdd specific options to the algorithm.
Returns
A pointer on a structure containing the context that will be used during the re-sampling process.
Note
The output must be freed with the dsp16_resampling_free function once the re-sampling process is completed.

References Align_up, dsp_resampling_t::buffer_size, dsp16_resampling_options_t::coefficients_generation, dsp_resampling_t::context, dsp_resampling_t::decimation_factor, DEFAULT_FILTER_FUNCTION, DEFAULT_WINDOW_FUNCTION, dsp16_filt_interpolation_coefsort(), dsp16_op_abs(), DSP16_QB, dsp16_vect_realdiv(), dsp16_vect_realmul(), dsp16_vect_zeropad(), dsp_op_gcd(), dsp16_resampling_options_t::dynamic, dsp_resampling_t::filter_order, dsp_resampling_t::fir_coefs, dsp16_resampling_options_t::fixed, dsp_resampling_t::fs, dsp_resampling_context_t::index, dsp_resampling_context_t::input_buffer_pos, dsp_resampling_t::interpolation_factor, dsp_resampling_t::last_samples, dsp_resampling_t::link_required, Max, Min, dsp_resampling_t::nb_channels, and NB_PTS_TO_INTERPOLATE.

Referenced by main().