Go Down

Topic: Question on Filters (Read 3350 times) previous topic - next topic

orly_andico

I just discovered this useful page:
http://www.schwietering.com/jayduino/filtuino/

Now I am reading encoder position and would like to smooth out spikes. But more than that, I want to get rid of long-term slow trends.

e.g. I am sampling at 1Hz rate.  I only want to see signals from 1/500Hz to 1/20 Hz.

So using the above site I built a 4th-order Butterworth bandpass filter and.... it doesn't work.

The filtered value slowly goes to infinity.  By this I assume the filters produced in the above site are IIR filters.

Questions:

1) I notice that these filters only keep a history of 10-12 past values.  How can they filter out something like 1/500 Hz?  Is that even possible?  (I'm thinking if there's a Nyquist frequency for high frequency cutoff, is there also something for low-frequency cutoff)

2) There's code floating around here for an FIR filter.. but where can I get the filter coefficients?

kanurys

Maybe you could use a moving average, which acts like a tune-able low pass filter and then subtract it out. Google moving average.

orly_andico

Yes I've tried that. Thing is I need to remove all frequencies below 1/500 Hz but keep the ones above. It is non-obvious how to specify the transition frequency of a moving average, which is simply a low-pass filter (convolving with a top hat function).

Magician

Quote
1) I notice that these filters only keep a history of 10-12 past values. 
Not correct, with IIR - infinite recursion
How can they filter out something like 1/500 Hz?  Is that even possible?  (I'm thinking if there's a Nyquist frequency for high frequency cutoff, is there also something for low-frequency cutoff)
Good question. There is limits on lower end, due float / uCPU resolution. It was noted, that arduino is capable to calculate down to 7/8 digits after comma, and when you driving as low as 1/500 Hz, you need better than that. 32-bits or even 64-bits math, which is quite complicated itself, and you 'd not wish to mix with already complex filter design.
The best practice, is keep a "window" at the upper end, decreasing sampling rate. In your case, better to reduce sampling down to 1/10 sec, so upper limit 1/20 in agreement with Shennon-Kotel'nikov theorem, than lower end 1/ 50 to sampling would much easier to calculate with lower required resolution math


2) There's code floating around here for an FIR filter.. but where can I get the filter coefficients?

http://www.exstrom.com/journal/sigproc/
http://arduino.cc/forum/index.php/topic,115139.0.html
http://www.musicdsp.org/archive.php?classid=3#195
http://www-users.cs.york.ac.uk/~fisher/mkfilter/
http://forums.parallax.com/showthread.php/133173-FIR2PASM-Automatic-FIR-Filter-Code-Generator
http://www.dsprelated.com/resources.php
http://www.dspguru.com/

IMHO, there is nothing better than FFT for band pass filtering, explained here:
http://coolarduino.wordpress.com/2012/06/28/stereo-audio-vu-meter-on-arduino/

MarkT

Standard band-pass filters assume the band is narrower than the frequency of interest, I think
that's why the site isn't giving you want you need.

If you want to cut both high frequencies and low frequencies, cascade a low-pass and a high-pass
filter...
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

MarkT

Quote
Not correct, with IIR - infinite recursion

IIR stands for infinite impulse response, in case anyone was confused by that statement.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

orly_andico

Hmmm.

I wrote up the same functions in Processing and got better results (must be the higher precision of a PC compared to an Arduino).

I did realize that if I lower my sampling rate, I will need a less aggressive alpha to filter out the very low frequency that I want.

The challenge is that if I sample at 1/10 Hz, I won't be able to catch any errors that occur at a higher frequency than once in 10 seconds.

I guess I could decimate my data for the very-low-frequency filter...

Magician

Quote
I guess I could decimate my data for the very-low-frequency filter...

No, you can't

Go Up