Logo
MSP DSP Library
Usage

Fixed-Point Types

Overview

DSP library for MSP devices uses the same fixed-point types as the IQmathLibry, Q15 and IQ31. The fixed-point Q15 type is defined as _q15 and stored as a 16-bit signed integer. Similarly, the IQ31 type is defined as _iq31 and stored as a 32-bit signed integer. The following equations show the fractional fixed-point representation and can be used to convert between the integer value stored in memory and the corresponding Q15 or IQ31 value.

     $f_{q15}(x) = x * 2^{-15}$

     $f_{iq31}(x) = x * 2^{-31}$

The Q15 and IQ31 formats can both be used to represent fractional decimal numbers, the difference is the resolution of the data. The tables below show the properties of both formats as well as several standard data types.

Type Integer bits Fractional bits Minimum Maximum Resolution
int16_t 16 0 -32768 32767 1
int32_t 32 0 -2147483648 2147483648 1
_q15 1 15 -1.0 0.9999695 0.00003051758
_iq31 1 31 -1.0 0.9999999995 0.00000000046

Macros

To help work with fixed-point number the DSP library defines C macros to quickly convert from decimal to fixed point, an example is shown below. For more details on available macro functions see the support api documentation section.

// Constant definition
#define MY_CONST        (0.0125)
#define MY_CONST_Q15    (_Q15(MY_CONST))
#define MY_CONST_IQ31   (_IQ31(MY_CONST))

// Constant coefficients
const _q15 coeffs[4] = {
    _Q15(-0.5), _Q15(-0.25), _Q15(0.0), _Q15(0.25)
};

// Sample function
void myFunction(_iq31 *cmplxData)
{
    cmplxData[0] = _IQ31(0.0);
    cmplxData[1] = MY_CONST_IQ31;

    return;
}

Complex Numbers

Complex numbers are represented as pairs of consecutive data where the even indices are the real inputs and the odd indices are the imaginary inputs. The length passed to complex DSPLib functions is the number of complex data pairs, meaning the functions assume twice the length has been allocated for complex data storage. An example of how complex data arrays can be allocated and referenced using some of the available macros is below. Refer to the the complex code examples for a more detailed look at working with complex numbers.

// Complex vector length
#define CMPLX_VECTOR_LENGTH     64

// Complex data arrays
_q15 src[CMPLX_VECTOR_LENGTH*2];
_q15 dst[CMPLX_VECTOR_LENGTH*2];

void myFunction(void)
{
    _q15 *srcPtr;
    uint16_t length;
    msp_status status;
    msp_cmplx_add_q15_params params;

    // Initialize source
    srcPtr = src;
    length = CMPLX_VECTOR_LENGTH;
    while (length--) {
            CMPLX_REAL(srcPtr) = 0;
            CMPLX_IMAG(srcPtr) = 0;
            srcPtr += CMPLX_INCREMENT;
    }

    // Complex add: dst = src + src;
    params.length = CMPLX_VECTOR_LENGTH;
    status = msp_cmplx_add_q15(&params, src, src, dst);

    return;
}

LEASC Hardware Acceleration

Many of the DSPLib functions support the Low Energy Accelerator for Signal Conditioning (LEASC) peripheral available on some MSP430 FRAM devices. LEASC enables best in class ultra-low power signal processing and embedded math calculations. Much of the peripheral usage is abstracted as part of the library but there are a few key considerations for using LEASC with the DSPLib functions.
  • Data and coefficient arrays must reside in a shared RAM section that both the CPU and LEASC can access.
    • CCS linker section: ".leaRAM"
    • IAR linker section: "LEARAM"
  • Data placed into the shared RAM section must be aligned as specified by the DSPLib functions used.
    • The majority of functions only require 4-byte alignment.
    • Transform (FFT) functions require more specific alignment, refer to the transform api documentation for exact requirements.
    • By default alignment is checked before invoking LEASC commands, if criteria are not met the function will return MSP_LEASC_INVALID_ADDRESS.
  • Functions using LEASC will enter LPM0 with interrupts enabled while LEASC is running.
    • The LEASC interrupt routine is provided by DSPLib and will wake the device upon completion of the command.
    • The application is free to leave other interrupts enabled and service them as needed.
The library provides preprocessor macros to help place DSPLib data structures with the correct alignment. A small example of the usage is below, see the code examples for complete usage example.

// Place real FFT data array into DSPLib data with correct alignment. 
DSPLIB_DATA(fftData,MSP_ALIGN_FFT_Q15(256))
_q15 fftData[256];

// Place data vector into DSPLib data with default alignment
DSPLIB_DATA(vectorData,4)
_q15 vectorData[16];