Hi All,
I tried to post this via DSPRelated, but it has not been moderated
yet, so I am trying another route. I'm a DSP noob, and have a shaky
grasp of the theory, so please go slow on me :)
Anyway, I have an embedded system with a 14 bit digital sensor (and a
PIC32 microcontroller), from which I get samples every 1mS. I need to
filter out a signal that is between 1Hz - 2.5Hz(approx), and I also
want to remove a x<1Hz slow component. So I designed a 2nd order
butterworth BPF using Matlab (butter + filter functions), with cut-off
at .8Hz and 2.5 Hz. I tried this filter on my sampled data, and the
results were fine.
Next I tried to implement this filter using C code and float data
types for the coefficients on the PIC32 micro, but all I get is junk.
To check my C code, I tried out the same code on Visual Studio on my
PC, and found that the filter works only if I use "double" data types
on VStudio. If I use "float" VStudio filter also does not work.
Basically the data is correct initially and then after about a 100
points, it goes out of range and crazy..
I guess that this is a wordlength / precision issue (because the
coefficients are very small?), but I am not able to viaualize exactly
why it is happening, because intutively I think that even if there are
precision issues, it should result in worse performance (maybe more
noisy data) but not completely crazy values? What's going on? And how
do I go about solving this? Should I try out other filter
implementation topoligies or add more stages or what? Or is some
scaling enough?
Any guidance / pointers would be appreciated!
The matlab coefficients are:
double[] B= new double[5] { 4.3122003325465159E-5, 0.0,
-8.6244006650930318E-5, 0.0, 4.3122003325465159E-5 };
double[] A= new double[5] { 1.0, -3.9812618362095562,
5.9440373174333914, -3.9442883933007971, 0.98151291362106841 };
Merry christmas and a happy new year to all of you!
Govind
Low frequency IIR-BPF filter -- Help!!
Started by ●December 29, 2011
Reply by ●December 30, 20112011-12-30
On 12/29/2011 10:13 PM, govind wrote:> Hi All, > > I tried to post this via DSPRelated, but it has not been moderated > yet, so I am trying another route. I'm a DSP noob, and have a shaky > grasp of the theory, so please go slow on me :) > > Anyway, I have an embedded system with a 14 bit digital sensor (and a > PIC32 microcontroller), from which I get samples every 1mS. I need to > filter out a signal that is between 1Hz - 2.5Hz(approx), and I also > want to remove a x<1Hz slow component. So I designed a 2nd order > butterworth BPF using Matlab (butter + filter functions), with cut-off > at .8Hz and 2.5 Hz. I tried this filter on my sampled data, and the > results were fine. > > Next I tried to implement this filter using C code and float data > types for the coefficients on the PIC32 micro, but all I get is junk. > To check my C code, I tried out the same code on Visual Studio on my > PC, and found that the filter works only if I use "double" data types > on VStudio. If I use "float" VStudio filter also does not work. > Basically the data is correct initially and then after about a 100 > points, it goes out of range and crazy.. > > I guess that this is a wordlength / precision issue (because the > coefficients are very small?), but I am not able to viaualize exactly > why it is happening, because intutively I think that even if there are > precision issues, it should result in worse performance (maybe more > noisy data) but not completely crazy values? What's going on? And how > do I go about solving this? Should I try out other filter > implementation topoligies or add more stages or what? Or is some > scaling enough? > > Any guidance / pointers would be appreciated!the sample rate is much too high for the frequencies that interest you. 20 Hz would be quite sufficient, but you can't simply keep every 50th sample because of aliasing considerations. If you can easily lower the cut-off of the analog anti-alias filter that and the sample rate, that would be fine, but I suspect you can't. Filter your signal in two steps, then. The first filter should cut off above 50 Hz or so, and you then can keep only every tenth sample. There's a saving to be had: most of the calculations for the samples you will discard can be eliminated.> The matlab coefficients are: > > double[] B= new double[5] { 4.3122003325465159E-5, 0.0, > -8.6244006650930318E-5, 0.0, 4.3122003325465159E-5 }; > > double[] A= new double[5] { 1.0, -3.9812618362095562, > 5.9440373174333914, -3.9442883933007971, 0.98151291362106841 }; > > Merry christmas and a happy new year to all of you!Numbers that precise spin my head. They represent less than a molecule in the thickness of a board. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Reply by ●December 30, 20112011-12-30
Hi Jerry, Thanks a lot for your response.>the sample rate is much too high for the frequencies that interest you. >20 Hz would be quite sufficient, but you can't simply keep every 50th >sample because of aliasing considerations. If you can easily lower the >cut-off of the analog anti-alias filter that and the sample rate, that >would be fine, but I suspect you can't.Well I can lower the samplnig rate BUT I do not have an anti-aliasing filter. This air-pressure sensor that I am evaluating gives me a direct digital output and there is some high-frequency noise in the output because there is an air-pump pumping air into the system, so I was afraid that if the sampling rate was low then the noise would get ailased on to the signal that interests me (?) And I also thought that the better the sampling rate, the better the signal and everything else :) I guess that was ill founded..> Filter your signal in two steps, >then. The first filter should cut off above 50 Hz or so, and you then >can keep only every tenth sample. There's a saving to be had: most of >the calculations for the samples you will discard can be eliminated.I shall try this out, thanks! Please ignore the duplicate message in the maillist.. Govind
Reply by ●December 30, 20112011-12-30
On Fri, 30 Dec 2011 14:05:56 +0800, govind wrote:> Hi Jerry, > > Thanks a lot for your response. > >>the sample rate is much too high for the frequencies that interest you. >>20 Hz would be quite sufficient, but you can't simply keep every 50th >>sample because of aliasing considerations. If you can easily lower the >>cut-off of the analog anti-alias filter that and the sample rate, that >>would be fine, but I suspect you can't. > > Well I can lower the samplnig rate BUT I do not have an anti-aliasing > filter. This air-pressure sensor that I am evaluating gives me a direct > digital output and there is some high-frequency noise in the output > because there is an air-pump pumping air into the system, so I was > afraid that if the sampling rate was low then the noise would get > ailased on to the signal that interests me (?)What Jerry was suggesting (or should have been) was to filter and decimate in the digital domain. This is actually one of those times when a CIC filter would make sense, as much because of simplicity as anything else.> And I also thought that the better the sampling rate, the better the > signal and everything else :) I guess that was ill founded..Well, one out of two: the better the sampling rate, the better the signal. The "and everything else" depends on the problem at hand. In general, though, your ADC will contribute noise. This extra noise is freely offered to you by the ADC manufacturer, at no extra charge for the part. To the extent that the ADC quantization noise is broken up by thermal noise and variations in the actual signal, sampling fast and filtering will give you more resolution (if not more precision).>> snip <<-- My liberal friends think I'm a conservative kook. My conservative friends think I'm a liberal kook. Why am I not happy that they have found common ground? Tim Wescott, Communications, Control, Circuits & Software http://www.wescottdesign.com
Reply by ●December 30, 20112011-12-30
On Thu, 29 Dec 2011 23:31:58 -0500, Jerry Avins wrote:> On 12/29/2011 10:13 PM, govind wrote: >> Hi All, >> >> I tried to post this via DSPRelated, but it has not been moderated yet, >> so I am trying another route. I'm a DSP noob, and have a shaky grasp of >> the theory, so please go slow on me :) >> >> Anyway, I have an embedded system with a 14 bit digital sensor (and a >> PIC32 microcontroller), from which I get samples every 1mS. I need to >> filter out a signal that is between 1Hz - 2.5Hz(approx), and I also >> want to remove a x<1Hz slow component. So I designed a 2nd order >> butterworth BPF using Matlab (butter + filter functions), with cut-off >> at .8Hz and 2.5 Hz. I tried this filter on my sampled data, and the >> results were fine. >> >> Next I tried to implement this filter using C code and float data types >> for the coefficients on the PIC32 micro, but all I get is junk. To >> check my C code, I tried out the same code on Visual Studio on my PC, >> and found that the filter works only if I use "double" data types on >> VStudio. If I use "float" VStudio filter also does not work. Basically >> the data is correct initially and then after about a 100 points, it >> goes out of range and crazy.. >> >> I guess that this is a wordlength / precision issue (because the >> coefficients are very small?), but I am not able to viaualize exactly >> why it is happening, because intutively I think that even if there are >> precision issues, it should result in worse performance (maybe more >> noisy data) but not completely crazy values? What's going on? And how >> do I go about solving this? Should I try out other filter >> implementation topoligies or add more stages or what? Or is some >> scaling enough? >> >> Any guidance / pointers would be appreciated! > > the sample rate is much too high for the frequencies that interest you. > 20 Hz would be quite sufficient, but you can't simply keep every 50th > sample because of aliasing considerations. If you can easily lower the > cut-off of the analog anti-alias filter that and the sample rate, that > would be fine, but I suspect you can't. Filter your signal in two steps, > then. The first filter should cut off above 50 Hz or so, and you then > can keep only every tenth sample. There's a saving to be had: most of > the calculations for the samples you will discard can be eliminated. > >> The matlab coefficients are: >> >> double[] B= new double[5] { 4.3122003325465159E-5, 0.0, >> -8.6244006650930318E-5, 0.0, 4.3122003325465159E-5 }; >> >> double[] A= new double[5] { 1.0, -3.9812618362095562, >> 5.9440373174333914, -3.9442883933007971, 0.98151291362106841 }; >> >> Merry christmas and a happy new year to all of you! > > Numbers that precise spin my head. They represent less than a molecule > in the thickness of a board.A 1:1000 ratio of filter cut-off to sample rate is a bit extreme, but I wouldn't say it's completely out of the ball park. It would require more than 32 bits of precision to wring the full available desired signal out of the available signal, so decimating and computing at a lower sampling rate is probably a good idea, however. Trying to implement the whole (4th-order) mess as a single direct-form filter, as I suspect the OP is doing, isn't a good idea under any circumstances. -- My liberal friends think I'm a conservative kook. My conservative friends think I'm a liberal kook. Why am I not happy that they have found common ground? Tim Wescott, Communications, Control, Circuits & Software http://www.wescottdesign.com
Reply by ●December 31, 20112011-12-31
Hi, here's my take on the problem: Average n samples, then decimate by n. http://www.dsprelated.com/showcode/262.php It's hard to say whether or not it's good enough without knowing the actual problem, your sample rate, the noise level in alias bands etc. Clearly, it's not the most advanced algorithm, but it might be the most convenient one (short of dumping samples without -any- filtering...)
Reply by ●December 31, 20112011-12-31
On Sat, 31 Dec 2011 08:05:02 -0600, mnentwig wrote:> Hi, > > here's my take on the problem: > Average n samples, then decimate by n. > > http://www.dsprelated.com/showcode/262.php > > It's hard to say whether or not it's good enough without knowing the > actual problem, your sample rate, the noise level in alias bands etc. > Clearly, it's not the most advanced algorithm, but it might be the most > convenient one (short of dumping samples without -any- filtering...)That's what I usually do for control systems. It makes a system that dodges many of the deficiencies in modern ADCs, and since control systems generally have low bandwidth WRT their sampling rate and since their greatest sensitivity to error is generally at DC, the characteristic of "anything close to an alias gets filtered out" works out to great advantage. I really like your graphic showing the aliases mapping from nulls to DC -- I think I'll steal it :). -- Tim Wescott Control system and signal processing consulting www.wescottdesign.com
Reply by ●January 1, 20122012-01-01
-- I think I'll steal it :). You're welcome... I can't remember exactly, but it seems likely that I "stole" the idea from one of your older posts anyway. Source (vector drawing): www.dsprelated.com/blogimages/MarkusNentwig/sn_movingAverageDecimator/fig1.svg
Reply by ●January 1, 20122012-01-01
On Sun, 01 Jan 2012 03:19:51 -0600, "mnentwig" <markus.nentwig@n_o_s_p_a_m.renesasmobile.com> wrote:>-- I think I'll steal it :). > >You're welcome... I can't remember exactly, but it seems likely that I >"stole" the idea from one of your older posts anyway. > >Source (vector drawing): >www.dsprelated.com/blogimages/MarkusNentwig/sn_movingAverageDecimator/fig1.svg >The idea isn't new, but the graphic does a good job of illustrating it. Stealing useful graphics can be a real time-saver, so I may snag it, too (always with attribution, though)! Eric Jacobsen Anchor Hill Communications www.anchorhill.com
Reply by ●January 2, 20122012-01-02
On Sun, 01 Jan 2012 03:19:51 -0600, mnentwig wrote:> -- I think I'll steal it :). > > You're welcome... I can't remember exactly, but it seems likely that I > "stole" the idea from one of your older posts anyway.You may have -- but I think you did a dynamite job of expressing the idea. A picture may be worth 1000 words, but if those 1000 are sometimes all babble -- not in this case. -- Tim Wescott Control system and signal processing consulting www.wescottdesign.com






