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. # 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. 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. 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. 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). Every M samples are kept from the input signal and all the others are removed. # 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. Frequency response from 32KHz to 44.1KHz 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_resampling The re-sampling context structure associated. output A 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. input A 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. channel The channel number to compute (starting from 0 to nb_channels - 1 referred in dsp16_resampling_setup).
 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_resampling The re-sampling context structure to be freed. free_fct A pointer on the free function to be used.
 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_resampling The re-sampling context structure associated.

References dsp_resampling_t::current_buffer_size.

 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_resampling The re-sampling context structure associated.
 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_output The re-sampling context which will be updated according to the dsp_resampling_input context. dsp_resampling_input The input re-sampling context.
Returns
true if the process succeed, false otherwise. A process can fail only if the preliminary conditions are not respected.
 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_rate The sample rate of the input signal. output_sample_rate The sample rate of the output signal. buffer_size The size of the input buffers. order The order of the re-sampling module. This defines indirectly the order of the filter to be used. Its order will be * ="">. malloc_fct A pointer on a memory allocation function. nb_channels The number of channels to compute. options Add 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.