If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

• Resolved

# Filter Library for MSP430?

Hi,

I’m trying to measure reactive energy with MSP430F47187 and its SD16_A module. To calculate reactive energy I need a 90 degree phase shifter known as hilbert filter. If I use FIR filter, matlab says It requires 106 tabs(Transition Width 35 Hz, Passband Ripple 0.02dB/0.1% and Sampling Frequency 1024). I don’t know MSP430 can do this job at that frequency by using Q1.15 style fixed point arithmetic for each phase which is totally three.

Here are my questions:

1.    In SLAA409A software code there are some coefficients for 90 degree phase shift. But they are for 4096 Hz sampling frequency and totally 128 tabs. In that C file (emeter-fir-table.c), it is written “This file was generated automatically by generate-fir-table”. Where can I find this “generate-fir-table” software? (I think it is software¿)

2.    Can I do same job by using IIR filter instead of FIR which requires too much processing power?

3.    Does TI have a filtering library for MSP430 series?

4.    In same application note, there is a dc filter function which I didn’t understand how it works.

// A routine to filter away the DC content from an AC mains waveform

// signal. It does this my using a heavily damped integrator to estimate

// the DC level. The current DC level is then subtracted from the signal.

int16_t dc_filter(register int32_t *p, register int16_t x)

{

*p += ((((int32_t) x << 16) - *p) >> 14);

x -= (*p >> 16);

}

I think it was designed for 4Khz sampling frequency, because for me it becomes stabile after 40 seconds. I need something faster, is it possible?

Regards,

BP.

• IIR Allpass / constant relative phase shift filters can work well for phase shifting. I recall using those in the past, though I'll have to see if I can find a reference to their calculation and use. It was not with the MSP430 that I've used those, so you'd have to adapt some of the fast multiplication (fixed point or CSD) code from MSP430 code bases to apply to other filtering algorithms as needed. SLAA329 Efficient Multiplication and Division Using MSP430 SLAA331 Wave Digital Filtering Using the MSP430 SLAA357 Efficient MSP430 Code Synthesis for an FIR Filter SLAP108 Optimized Digital Filtering for the MSP430 SLAA228 Digital FIR Filter Design Using the MSP430F16x There are some other filtering or fast signal processing related TI application notes. Here's stuff on the IIR and allpass filters: http://www.dsprelated.com/showmessage/21946/1.php http://en.wikipedia.org/wiki/Allpass_filter#Digital_Implementation I'm sure I could dig up lots of references some new and others I've used in the past, but I think you get the idea from those starting points, so feel free to ask if you need more information and I'll see what I can find.
• In reply to C. Hayes:

PS I'm not sure if you're just trying to dissect that specific integrator code or if you're just trying to understand how to structure that block in your application in general regardless of the implementation. An integrator as described is basically similar to a low pass filter, the particular cutoff frequency can be arbitrarily low, and if you're looking to block AC response for any frequency (however low -- 50Hz, 10Hz, 5Hz, 1Hz, 0.01 Hz, ...) but pass true DC then of course it will never be perfectly possible since an arbitrarily low frequency (possibly infinitely low) can look just like DC over any given finite interval of time. So if the network above takes 40 seconds to rise then that isn't really passing just DC but also any frequencies less than say 1/160TH Hz which is certainly still 'AC' if you're studying something like tides or geological waves or whatever. The only point is where do you draw the line as to how fast you want to generate an estimate of what the DC level offset truly is versus how much you can tolerate error in that due to AC fluctuations causing errors by passing through the DC estimator low pass filter. So the sampling rate may not be too much of an importance so much as the cutoff frequency and 'DC' level tracking performance dynamics you want. Over the long term balanced AC signals will sum to zero average whereas unipolar / unbalanced signals plus a DC offset will have mean values of some average signal level between your DC level and the peak AC level since in that case the mean signal will always be positive and nonzero. So whether you use just something like an integrator or other lowpass filter or some mean tracking filter with some particular response time constant just depends on the nature of your expected input DC signal + AC spectrum.
• In reply to C. Hayes:

Hi Hayes,

Thanks for useful information. For the DC Filter I found this trick:

http://www.dspguru.com/dsp/tricks/fixed-point-dc-blocking-filter-with-noise-shaping

I can attenuate signals under 20Hz which we can say DC for this application.

MSP430F471xx series has 32 bit hardware multiplier module with fixed point support which is good news for me. But SLAA409A uses only Q15 style multiplication, while the module support Q31 style multiplication and accumulation (MACS32). May be it is highly enough for this application.

http://www.mathworks.com/access/helpdesk/help/toolbox/signal/sosfilt.html

Another question, when I design Hilbert by using FIR (linear-phase, odd symmetry, type 3), it also produces a fixed delay. Let say it has 128+1 tabs, 90 degree shifted signal comes after 64th sample. I already have a buffer for inputs. Because of that, I need another circular buffer for also output. Isn’t there a way to prevent this lag?

Thank you.

Hi,

That DC blocking algorithm looks good.  Thanks for mentioning it.

BasePointer

http://www.mathworks.com/access/helpdesk/help/toolbox/signal/sosfilt.html

`You are correct, that is the type of biquadratic IIR filter section he meant in the message you read.  You can cascade a combination of 1st, 2nd, other order filter stages to produce a composite filter which is of more stringent performance specification in the composite effect of all the individual stages.  So a 4th order could be a couple of 2nd order sections acting in conjunction, et. al. depending on the particular design of your filter and its sections. I believe the 'ellip' function in MATLAB can be used to design an elliptical type filter according to a specified performance characteristic and produce the pole/zero locations that of that filter as a result.  These could be modulated or rotated to produce an IIR or FIR filter which works similarly to a Hilbert transformer as various thread posts and articles suggest may be done. Some alternatively cited references suggest 'firpm' or the older 'remez' functions to design an optimized FIR filter via the Remez Exchange / Parks-McClellan algorithms.  There are other approaches to both FIR and IIR hilbert transform like impelemntations also listed in various of the below links.`

BasePointer
MSP430F471xx series has 32 bit hardware multiplier module with fixed point support which is good news for me. But SLAA409A uses only Q15 style multiplication, while the module support Q31 style multiplication and accumulation (MACS32). May be it is highly enough for this application.

`That is a good resource to have.  You might well be able to use it for a 12, 14, 15, or 16 bit precision implementation of the filtering coefficients.  You just have to watch out for possible overflow or underflow or rounding errors since these can be problems when operating with even 16 or 32 bit precision depending on the calculations you are conducting.The most commonly recommended implementation for digital filtering on the more common MSP430 models (without such a hardware multiplier) is to use the CSD scheme of filter arithmetic and encoding and that seems to work well.  Especially commended by TI's application notes and in other sources is the lattice wave digital filter LWDF / WDF / lattice filter type of realization of IIR filters as the combination of simple 1st and 2nd order sections since they can be calculated with less problems due to arithmetic precision on a fixed point processor especially one without floating point or fixed point hardware multiplication capability.  I suspect if you did an IIR implementation it might be OK to just use the CSD reference code for a LWDF type filter since it shouldn't be too many dozen operations long in many cases.  Whereas a FIR implementation would likely benefit very much from using the HW multiplier since you'll be doing many MACs over a long filter, though still you may end up with performance / resource concerns if you have 100+ FIR taps.It will be easier to get a more linear phase and less ripple magnitude response with the FIR implementation, though to a given precision you can approximate those with an IIR (LWDF type or not) implementation if that seems beneficial.`

BasePointer

Another question, when I design Hilbert by using FIR (linear-phase, odd symmetry, type 3), it also produces a fixed delay. Let say it has 128+1 tabs, 90 degree shifted signal comes after 64th sample. I already have a buffer for inputs. Because of that, I need another circular buffer for also output. Isn’t there a way to prevent this lag?

`I don't think you can get around filtering either the voltage (or the current) signal to produce a phase shift of 90 degrees to correlate with the other signal based on the architecture you seem to be following.  If the resulting filtered output of one channel has not only the desired phase shift but also a significant delay then you must buffer the other signal with a matched delay such that you can multiply the equally delayed signals with one channel quadrature phase shifted and the other not.  I'm not sure if these considerations are what you're referring to as an 'output' delay -- really it would seem like it is a matching delay one branch for the V channel and another one for the I channel but I would think at that point those outputs are all you would need and there would be no significant additional delays needed.`

At some length it is theoretically more efficient to calculate FIR filters via a forward and inverse FFT, but I think that is not so practical on the MSP430 if it has very limited RAM and also the fixed point precision limitations it has et. al. and you're only filtering with an impulse response of 130 or so length, and, anyway it doesn't reduce the delay intrinsic to the z^-1 storage needed for the causal filter, it just reduces the arithmetic complexity of the implementation of the filtering, so it doesn't solve the problem entirely.

If you used a 'complex' filter type implementation where you'd filter both the V channel and the I channel into a pair of filters which generated relative outputs in quadrature then you'd process each channel in a corresponding fashion and then you'd directly be able to multiply the outputs of the V and I filters without (as far as I know) adding additional large delays to either branch.

It is possible that using a smaller state / order IIR approximation of a Hilbert transformer would reduce your arithmetic processing and your tap / state memory storage needs substantially and alleviate the problems in those areas with long high delay high MAC count FIR implementations.

If there's some other aspect of 'output' delay that you're concerned about, I don't quite understand how it arised based on a limited understanding of your application's architecture.  SLAA409 page 12's diagram doesn't seem to show any additional large delays other than that which is needed to shift the phase of one channel by 90 degrees.

Here's more references which may or may not be useful...

http://www.student.oulu.fi/~oniemita/dsp/hilbert/

http://www.dsprelated.com/showmessage/21946/1.php
"Hilbert IIR filter implementation"

http://www.dsprelated.com/showmessage/24752/1.php
"Designing IIR hilbert transform pair"

http://www.dsprelated.com/showmessage/88412/1.php
"High Speed IIR"

http://www.dsprelated.com/showmessage/22749/1.php
"Hilbert transform & analytic signals"

http://newsgroups.derkeiler.com/Archive/Comp/comp.dsp/2009-03/msg01457.html

http://www.ti.com/sc/docs/products/micro/msp430/wave.pdf
"Wave digital filtering for TI's sensor signal processor MSP430"

http://focus.ti.com/lit/ml/slap108/slap108.pdf
"Optimized Digital Filtering for the MSP430 "

http://focus.ti.com/mcu/docs/litabsmultiplefilelist.tsp?sectionId=96&tabId=1502&literatureNumber=slaa331&docCategoryId=1&familyId=342
"Wave Digital Filtering using the MSP430"

http://focus.ti.com/general/docs/litabsmultiplefilelist.tsp?literatureNumber=slaa357
"Efficient MSP430 Code Synthesis for an FIR filter"

http://www.bdti.com/faq/2.htm#210
"How do I calculate the coefficients for a Hilbert transformer?"

"Synthesis of very sharp Hilbert transformer using the frequency-response masking technique"
Y.C. Lim, Y.J.Yu

"Wave digital filters with minimum multiplier for discrete Hilbert transformer realization"
S.A. Samad, A. Hussain, D. Isa

"Analytic signal generation---tips and traps", Andrew Reilly and Gordon Frazer and Boualem Boashash, IEEE Transactions on Signal Processing, no. 11, vol. 42, Nov. 1994, pp. 3241-3245.

"Optimal FIR and IIR Hilbert transformer design via LS and minimax fitting" - Instrumentation and Measurement, IEEE Transactions on; Istvan Kollar, Rik Pintelon, Johan Schoukens.

"Design of multiplierless elliptic IIR halfband filters and hilbert transformers", Miroslav D. Lutovac, Ljiljana D. Milic

"A Uniformly Convergent Approximation for Ideal Complex Half-Band Filters", Gagan Mirchandani, Mohamed Elfataui

"Approximate linear phase Hilbert transformer"
Ljiljana D. Milic, Miroslav D. Lutovac

"Design of Hilbert transformer for solid-state energy meter"
Bojan Anđelković, Milunka Damnjanović

"Elliptic half-band IIR filters"
Miroslav D. Lutovac, Ljiljana D. Milic

http://metalab.uniten.edu.my/~farrukh/DSP/DSP%205%20-%20IIR%20Filter%20Design.ppt

http://metalab.uniten.edu.my/~farrukh/DSP/DSP%206%20-%20FIR%20Filter%20Design.ppt

https://ccrma.stanford.edu/~jos/sasp/Matlab_Support_Least_Squares_FIR.html