This example demonstrates how to use the msp_fir_iq31 API and circular buffer feature to filter 32-bit input data. The input signal is composed of two generated sinusoidal signals added together, one sinusoid with a frequency that will pass though the filter and one with a frequency that will be filtered out. The inputs are copied into a circular buffer with twice the length of the filter and allows input history from previous filter operations to be reused without allocating and copying additional samples to the start of the input data. The generated input and result can be compared to see the effect of the filter.
#include "msp430.h"
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "DSPLib.h"
#define FS 8192
#define SIGNAL_LENGTH 256
#define SIGNAL_FREQUENCY1 200
#define SIGNAL_AMPLITUDE1 0.6
#define SIGNAL_FREQUENCY2 2100
#define SIGNAL_AMPLITUDE2 0.15
#define PI 3.1415926536
#ifndef FILTER_COEFFS_EX2
const _iq31 FILTER_COEFFS_EX2[42] = {
};
#endif
#define FIR_LENGTH 64
#define COEFF_LENTH sizeof(FILTER_COEFFS_EX2)/sizeof(FILTER_COEFFS_EX2[0])
_iq31 circularBuffer[2*FIR_LENGTH];
_iq31 filterCoeffs[COEFF_LENTH];
_iq31 input[SIGNAL_LENGTH];
_q15 temp[SIGNAL_LENGTH];
_iq31 result[SIGNAL_LENGTH];
volatile uint32_t cycleCount;
extern void initSignal(void);
void main(void)
{
uint16_t samples;
uint16_t copyindex;
uint16_t filterIndex;
WDTCTL = WDTPW + WDTHOLD;
#ifdef __MSP430_HAS_PMM__
PM5CTL0 &= ~LOCKLPM5;
#endif
initSignal();
memcpy(filterCoeffs, FILTER_COEFFS_EX2, sizeof(filterCoeffs));
fillParams.
length = FIR_LENGTH*2;
copyParams.
length = FIR_LENGTH;
firParams.
length = FIR_LENGTH;
firParams.
coeffs = filterCoeffs;
samples = 0;
copyindex = 0;
filterIndex = 2*FIR_LENGTH - COEFF_LENTH;
while (samples < SIGNAL_LENGTH) {
status =
msp_copy_iq31(©Params, &input[samples], &circularBuffer[copyindex]);
status =
msp_fir_iq31(&firParams, &circularBuffer[filterIndex], &result[samples]);
copyindex ^= FIR_LENGTH;
filterIndex ^= FIR_LENGTH;
samples += FIR_LENGTH;
}
__no_operation();
}
void initSignal(void)
{
sinParams.
length = SIGNAL_LENGTH;
sinParams.
length = SIGNAL_LENGTH;
addParams.
length = SIGNAL_LENGTH;
convertParams.
length = SIGNAL_LENGTH;
}