Dsp.h — DSP Utilities

The DSP module contains basic functions for working with buffers of samples.

Sample Type Conversion

Floating Point to Integer Conversion

Error_t FloatBufferToInt16(signed short *dest, const float *src, unsigned length)

Convert an array of float samples to 16-bit signed.

Converts array of floating point samples in [-1, 1] to signed 16-bit samples

Return
Error code.
Parameters
  • dest -

    Signed samples array

  • src -

    Floating point samples to convert

  • length -

    Number of samples.

Error_t DoubleBufferToInt16(signed short *dest, const double *src, unsigned length)

Convert an array of double samples to 16-bit signed.

Converts array of double-precision samples in [-1, 1] to signed 16- bit samples

Return
Error code.
Parameters
  • dest -

    Signed samples array

  • src -

    double-precision samples to convert

  • length -

    Number of samples.

Integer to Floating Point Conversion

Error_t Int16BufferToFloat(float *dest, const signed short *src, unsigned length)

Convert an array of 16-bit signed samples to floats.

Converts array of 16-bit integer samples to float samples in [-1,1]

Return
Error code.
Parameters
  • dest -

    floating point samples array

  • src -

    integer samples to convert

  • length -

    Number of samples.

Error_t Int16BufferToDouble(double *dest, const signed short *src, unsigned length)

Convert an array of 16-bit signed samples to double.

Converts array of 16-bit integer samples to double samples in [-1,1]

Return
Error code.
Parameters
  • dest -

    double-precisionsamples array

  • src -

    integer samples to convert

  • length -

    Number of samples.

Single- to Double-Precision Conversion

Error_t DoubleToFloat(float *dest, const double *src, unsigned length)

Convert double-precision samples to floats.

Convert array of double-precision samples to float samples in [-1,1]

Return
Error code.
Parameters
  • dest -

    single-precision sample destination array

  • src -

    double-precision samples to convert

  • length -

    Number of samples.

Error_t FloatToDouble(double *dest, const float *src, unsigned length)

Convert single-precision samples to doubles.

Convert array of single-precision samples to doubles in [-1,1]

Return
Error code.
Parameters
  • dest -

    double-precision sample destination array

  • src -

    single-precision samples to convert

  • length -

    Number of samples.

Basic Buffer Operations

Error_t FillBuffer(float *dest, unsigned length, float value)

Fill an array with a given value.

Fill the passed array with the value passed in as value. Uses a vectorized implementation if available.

Return
Error code.
Parameters
  • dest -

    Array to fill

  • length -

    Size of array in samples

  • value -

    Value to use.

Error_t ClearBuffer(float *dest, unsigned length)

Set array to zero.

Fill the passed array with zeros.

Return
Error code.
Parameters
  • dest -

    Array to fill

  • length -

    Size of array in samples

Error_t CopyBuffer(float *dest, const float *src, unsigned length)

Copy an array.

copy an array from src to dest

Return
Error code.
Parameters
  • dest -

    Array to fill

  • src -

    source buffer

  • length -

    Size of array in samples

Error_t CopyBufferStride(float *dest, unsigned dest_stride, const float *src, unsigned src_stride, unsigned length)

Copy an array with given source and destination strides.

copy an array from src to dest

Return
Error code.
Parameters
  • dest -

    Array to fill

  • dest_stride -

    Destination stride

  • src -

    source buffer

  • src_stride -

    Sounrce stride

  • length -

    Size of array in samples

Vector Min/Max

Min

float VectorMin(const float *src, unsigned length)

Find the Minimum value in a vector.

Return
Miminum value in vector
Parameters
  • src -

    Vector to search

  • length -

    Vector length in samples

Error_t VectorMinVI(float *value, unsigned *index, const float *src, unsigned length)

Find the index and value of the Maximum value in a vector.

Parameters
  • value -

    Minimum value in vector

  • index -

    Index of minimum value

  • src -

    Vector to search

  • length -

    Vector length in samples

Max

float VectorMax(const float *src, unsigned length)

Find the Maximum value in a vector.

Return
Maximum value in vector
Parameters
  • src -

    Vector to search

  • length -

    Vector length in samples

Error_t VectorMaxVI(float *value, unsigned *index, const float *src, unsigned length)

Find the index and value of the Maximum value in a vector.

Parameters
  • value -

    Maximum value in vector

  • index -

    Index of maximum value

  • src -

    Vector to search

  • length -

    Vector length in samples

Vector Absolute Value

Error_t VectorAbs(float *dest, const float *in, unsigned length)

Vector Absolute Value.

Calculate absolute value of elements in in1 and write the results to dest:

dest[i] = |in[i]| | i = [0, length)

Return
Error code.
Parameters
  • dest -

    Output array to write

  • in -

    Input buffer

  • length -

    Number of samples to negate

Vector Negation

Error_t VectorNegate(float *dest, const float *in, unsigned length)

Negate a vector.

Negate every element in in and write the results to dest:

dest[i] = -in[i] | i = [0, length)

Return
Error code.
Parameters
  • dest -

    Output array to write

  • in -

    Input buffer

  • length -

    Number of samples to negate

Vector Summation

float VectorSum(const float *src, unsigned length)

Sum all values in an array.

Return
Sum of all values in src
Parameters
  • src -

    Data to sum

  • length -

    Number of samples to sum

Vector Addition

Error_t VectorVectorAdd(float *dest, const float *in1, const float *in2, unsigned length)

Add two buffers.

Add values in in1 to values in in2 element-by-element and write results to dest:

dest[i] = in1[i] + in2[i] | i = [0, length)

Return
Error code.
Parameters
  • dest -

    Output array to write

  • in1 -

    First input buffer

  • in2 -

    Second input buffer

  • length -

    Number of samples to add

Error_t VectorScalarAdd(float *dest, const float *in1, const float scalar, unsigned length)

Add scalar to a vector.

add scalar to every element in in1 and write results to dest:

dest[i] = in1[i] + scalar | i = [0, length)

Return
Error code.
Parameters
  • dest -

    Output array to write.

  • in1 -

    Input buffer.

  • scalar -

    Scalar value.

  • length -

    Number of samples to add.

Vector Multiplication

Error_t VectorVectorMultiply(float *dest, const float *in1, const float *in2, unsigned length)

Multiply two buffers.

Multiply values in in1 by values in in2 element by element and write results to dest:

dest[i] = in1[i] * in2[i] | i = [0, length)

Return
Error code.
Parameters
  • dest -

    Output array to write

  • in1 -

    First input buffer

  • in2 -

    Second input buffer

  • length -

    Number of samples to multiply

Error_t VectorScalarMultiply(float *dest, const float *in1, const float scalar, unsigned length)

Multiply buffer by a scalar.

Multiply values in in1 by scalar and write results to dest:

dest[i] = in1[i] * scalar | i = [0, length)

Return
Error code.
Parameters
  • dest -

    Output array to write.

  • in1 -

    Input buffer.

  • scalar -

    Scalar value.

  • length -

    Number of samples to multiply.

Vector Mixing

Error_t VectorVectorMix(float *dest, const float *in1, const float *scalar1, const float *in2, const float *scalar2, unsigned length)

Multiply two buffers by a scalar and sum.

Multiply inputs by scalars and write sum to dest:

dest[i] = (in1[i] * scalar1) + (in2[i] * scalar2) | i = [0, length)

Return
Error code.
Parameters
  • dest -

    Output array to write.

  • in1 -

    Input buffer.

  • scalar1 -

    Scalar value.

  • in2 -

    Input buffer.

  • scalar2 -

    Scalar value.

  • length -

    Number of samples to multiply.

Error_t VectorVectorSumScale(float *dest, const float *in1, const float *in2, const float *scalar, unsigned length)

Sum two vectors and multiply result by a scalar.

Sum input vectors and multiply by a scalar, leaves results in dest:

dest[i] = (in1[i] + in2[i]) * scalar | i = [0, length)

Return
Error code.
Parameters
  • dest -

    Output array to write.

  • in1 -

    Input buffer.

  • in2 -

    Input buffer.

  • scalar -

    Scalar value.

  • length -

    Number of samples to multiply.

Vector Power

Error_t VectorPower(float *dest, const float *in, float power, unsigned length)

Raise elements of vector to a power.

Raise values in in to power of ‘power’ and write results to dest:

dest[i] = in1[i]^power | i = [0, length)

Return
Error code.
Parameters
  • dest -

    Output array to write.

  • in1 -

    Input buffer.

  • power -

    Power to raise input by.

  • length -

    Number of samples to process.

Vector Convolution

Error_t Convolve(float *in1, unsigned in1_length, float *in2, unsigned in2_length, float *dest)

Perform Convolution *.

convolve in1 with in2 and write results to dest

Return
Error code.
Parameters
  • in1 -

    First input to convolve.

  • in1_length -

    Length [samples] of in1.

  • in2 -

    Second input to convolve.

  • in2_length -

    Length[samples] of second input.

  • dest -

    Output buffer. needs to be of length in1_length + in2_length - 1