Author Topic: Digital Filter implemented on AVR  (Read 6850 times)

0 Members and 1 Guest are viewing this topic.

Offline DanioIOTopic starter

  • Contributor
  • Posts: 35
Digital Filter implemented on AVR
« on: July 20, 2014, 06:46:22 pm »
Hi,
I am trying to implement a digital notch filter on Atmega8. The whole project is about getting data from ADC and filtering it out and then send via Bluetooth. Im designing filters using MATLAB build in functions like fir1, butter, elliptic etc. I would like to operate using floats on AVR, but using simple cast from double (generated by MATLAB) to float results in a filter that isnt stabled (I test it only in Simulink). Can i cast my filter coefficient to float without losing stability?

Or maybe i should use a DSP, can anyone suggest a very cheap one and in a small package like Atmega8 or smaller. (i need only I2C and UART).
« Last Edit: July 20, 2014, 06:52:42 pm by DanioIO »
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1054
  • Country: fi
Re: Digital Filter implemented on AVR
« Reply #1 on: July 20, 2014, 06:58:03 pm »
Sounds like you are having problems with numerical precision if converting to float makes the filter unstable. Can you tell what is your sampling frequency and specifications of your filter? Are you using direct form filter with filters of order > 2? If so, split the transfer function to several second order sections and try again. High-order direct forms are pretty much doomed to fail, excluding some special cases.

FIR filters are always stable regardless of the numerical precision but response shape might get distorted as the quantization becomes coarser.

Regards,
Janne
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21671
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Digital Filter implemented on AVR
« Reply #2 on: July 20, 2014, 07:04:32 pm »
Also, what kind of speed are you expecting?  Don't expect anything over 10kSa/s for integer data in small to moderate filter lengths; maybe more like 100Sa/s (i.e., 100 times slower) for floats.  Proportionally less for proportionally longer filters (required if you want a deep, sharp notch).

Otherwise, there's these things called op-amps...

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Online Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Digital Filter implemented on AVR
« Reply #3 on: July 20, 2014, 07:57:59 pm »
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 26906
  • Country: nl
    • NCT Developments
Re: Digital Filter implemented on AVR
« Reply #4 on: July 20, 2014, 08:24:57 pm »
The last time I used the filter design tool from Matlab it could export the coefficients to a C header file. The ancient version I used only allowed to use even numbers for quantisation (14:16) for 14 bit coefficients and had several other quirks. Furthermore the input signal needs scaling. This factor is given with the coefficients in the result window; not in the C header file. Fixed point math is the way to go here.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Digital Filter implemented on AVR
« Reply #5 on: July 20, 2014, 11:57:09 pm »
Use fixed point math instead. You can find the code pieces online.

for proven implementation, try arm dsp lib, implemented in the latest cmsis.

it is fairly easy to port it to avr.
================================
https://dannyelectronics.wordpress.com/
 

Offline mrflibble

  • Super Contributor
  • ***
  • Posts: 2051
  • Country: nl
Re: Digital Filter implemented on AVR
« Reply #6 on: July 21, 2014, 01:55:55 pm »
As has been suggested, fixed point math is the way to go. Also, depending on your required data rate you might want to take a look at arm microcontrollers that have single cycle multiply-accumulate instructions.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Digital Filter implemented on AVR
« Reply #7 on: July 22, 2014, 02:28:19 am »
The better way around would be to evaluate the performance of the hardware first (i.e. how many MACs you can acheive) then use that to deciede what sort of filter you can use. It may be that you just don't have enough grunt to acheive what you want.

If your IIR filter is unstable then it might be that you are pushing it too hard - all sharp IIR filters are only really suited to long floating point,with careful analysis of possilble rounding issues. You might be able to filter twice with a less sharp filter.

Depending on your needs, see if you can get away with a moving average LP and HP filter to make your bandpass. It is really cheap and easy to implement.

Mike
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline f5r5e5d

  • Frequent Contributor
  • **
  • Posts: 349
Re: Digital Filter implemented on AVR
« Reply #8 on: July 22, 2014, 02:40:43 am »
iir biquad have often unsuspectedly large accumulator word length requirements - one scales as the square of fs/fc

floating point really doesn't do the job - I needed 48 bit accumulators in a project that output 16 bit data
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Digital Filter implemented on AVR
« Reply #9 on: July 22, 2014, 04:47:32 am »
iir biquad have often unsuspectedly large accumulator word length requirements - one scales as the square of fs/fc

floating point really doesn't do the job - I needed 48 bit accumulators in a project that output 16 bit data

Sounds like the perfect application for the Xilinx FPGA's DSP48 blocks (18-bit x 25 bit with 48 bit accumulator), and can run at over hundreds of MegaMACS - not very well suited to an AVR at all.

Just saying!
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21671
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Digital Filter implemented on AVR
« Reply #10 on: July 22, 2014, 06:24:29 am »
Well, I'd guess if you only needed ~100Hz sample rate, you'd have more than enough from an AVR with that kind of load... but anything a little peppier, well.. :)
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline mrflibble

  • Super Contributor
  • ***
  • Posts: 2051
  • Country: nl
Re: Digital Filter implemented on AVR
« Reply #11 on: July 23, 2014, 01:03:02 am »
Sounds like the perfect application for the Xilinx FPGA's DSP48 blocks (18-bit x 25 bit with 48 bit accumulator), and can run at over hundreds of MegaMACS - not very well suited to an AVR at all.

AVR: could do the job for the OP, if it's not too demanding.
STM32F4: lots more headroom. Single cycle 64-bit multiply-accumulate (as in 32X32 + 64 = 64) at 192 MHz.
Spartan6: no contest whatsoever. Pipelined dsp48 blocks are nice. :)

I recently implemented a 5th order delta sigma modulator on both stm32f4 and spartan6. The stm32 does manage a decent clock rate, but nowhere near that of the fpga. FPGA: 200+ MHz? No problem. And loads of resources to spare. But then again the development effort for fpga implementations (vs mcu) tends to be a bit higher.

Anyways, it would help if the OP could tell us some performance requirements. :)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf