DSPRelated.com
Forums
Online · Live + on demand
February 9-11, 2027
Price goes up Jan 1: register now to lock in today's rate
Register Now →

low pass digital filter on a fixed point microcontroller

Started by Johnny Chang ●August 5, 2008
On Wed, 06 Aug 2008 20:00:31 GMT, I wrote:

>On Wed, 6 Aug 2008 09:29:09 -0700 (PDT), Johnny Chang ><johnnyz86@gmail.com> wrote: > >>It seems like the accuracy if I don't use floats will >>just decrease and decrease, > >No. In fact, floating point will often strip you of important bits of >accuracy if you aren't very careful and thoughtful about what you are >doing with it. In comparison, keeping things fixed point retains all >the bits with usually fair ease. So the opposite is the case than >what you think. The problem with fixed point is dynamic range, not >accuracy.
I was using the term 'accuracy' in the same sloppy way as the OP probably was. For those with a penchant here, the better term would have been precision. (Accuracy itself is beyond the scope of the discussion so far.) Jon
Jonathan Kirwan wrote:

> (You can test a lot of this off-line > on your PC with simple software you cobble up and some captured or > simulated data you prepare.)
You don't even need to write software, you can use Excel (or another free spreadsheet program, like Open Office) for testing. Very nice for adjusting coefficients and testing formulas, because you can display a diagram for the calculated values and you can see immediately how the diagram changes when you change the parameters. Another nice program is GNU Plot. For a project I've written a C program, which tests filters and creates the frequency response curve data for GNU Plot. Then a little script executes the C program and then starts GNU Plot. Nearly as simple to adjust the formulas as with Excel, but GNU Plot has a lot more features. -- Frank Buss, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
On Aug 6, 9:29 am, Johnny Chang <johnny...@gmail.com> wrote:
> On Aug 6, 4:54 am, Jonathan Kirwan <jkir...@easystreet.com> wrote: > > > > > On Wed, 6 Aug 2008 08:33:13 +0200, Frank Buss <f...@frank-buss.de> > > wrote: > > > >Johnny Chang wrote: > > > >> What is the easiest way to do this? > > > >A simple lowpass filter could look like this in C: > > > >float d = 8.0f; > > > >float lowpassFilter(float sample) { > > > static float filtered = 0.0f; > > > filtered += (sample - filtered) / d; > > > return filtered; > > >} > > > >where "d" (=1, 2, 3...) defines the cut-off frequency: the higher d, the > > >lower is the cut-off frequency. "sample" is the next incoming sample and > > >"filtered" is the output of the filter. You call lowpassFilter for each > > >incoming sample and it returns the filtered value. > > > >If you use d=2^n, with n=0, 1, 2..., it is easy to implement it very fast > > >and for fixed point numbers. > > > I'm guessing from the op's comments, but it may be similar to a > > beam-forming application. The op didn't say about wide or narrow > > band, but I'd guess relative phase of in-band signals would be > > important to preserve. On the other hand, the op seemed to be > > cavalier about the suggestion regarding an external low-pass so maybe > > I'm choosing the wrong comments to focus on and a simple, one-pole IIR > > is fine. > > > I think a lot more information about what is going on is important to > > know to provide much useful help. > > > Jon > > I could've sworn I responded to these posts but my message isn't > showing up. It is kinda similar to beam forming, basically I am doing > quick correlations with these signals and the high frequencies are > messing up my algorithm. I can't change the filter on the end because > that signal is being passed elsewhere where quality is important, and > I can't add another filter because the board is already built. I just > want to filter the signal before it is processed as cycle-efficient as > possible. Is it possible using only fixed point arithmetic and not > using floats? It seems like the accuracy if I don't use floats will > just decrease and decrease, unless I premultiply each one by the > maximum value of the sample each time or something and do other > compensations. > > Johnny
Hi If the frequencies that you are trying to get rid of are more than half your sample frequency, once sampled, they are not separable from any other signal in band. This would require proper filtering on the hardware before the A/D. If you have multiple simulaneous A/Ds sampling at different phases or frequencies, it is possible to make it look like a single A/D of a higher sample rate. Otherwise, you will be limited in what you can do. Also, realize that random noise has a wide spectrum. Attemps to filter it will leave artifacts across the spectrum. This is a limiting factor in any windowing or filtering method. Dwight
On Wed, 6 Aug 2008 22:27:59 +0200, Frank Buss <fb@frank-buss.de>
wrote:

>Jonathan Kirwan wrote: > >> (You can test a lot of this off-line >> on your PC with simple software you cobble up and some captured or >> simulated data you prepare.) > >You don't even need to write software, you can use Excel (or another free >spreadsheet program, like Open Office) for testing. Very nice for adjusting >coefficients and testing formulas, because you can display a diagram for >the calculated values and you can see immediately how the diagram changes >when you change the parameters.
Yes. I figured that part almost goes without saying, though. But yes.
>Another nice program is GNU Plot. For a project I've written a C program, >which tests filters and creates the frequency response curve data for GNU >Plot. Then a little script executes the C program and then starts GNU Plot. >Nearly as simple to adjust the formulas as with Excel, but GNU Plot has a >lot more features.
I have it installed and I use it here and there. Good addition to point out. Jon
On Aug 6, 4:00&#4294967295;pm, Jonathan Kirwan <jkir...@easystreet.com> wrote:
> On Wed, 6 Aug 2008 09:29:09 -0700 (PDT), Johnny Chang > > > > <johnny...@gmail.com> wrote: > >On Aug 6, 4:54&#4294967295;am, Jonathan Kirwan <jkir...@easystreet.com> wrote: > >> On Wed, 6 Aug 2008 08:33:13 +0200, Frank Buss <f...@frank-buss.de> > >> wrote: > > >> >Johnny Chang wrote: > > >> >> What is the easiest way to do this? > > >> >A simple lowpass filter could look like this in C: > > >> >float d = 8.0f; > > >> >float lowpassFilter(float sample) { > >> > &#4294967295; &#4294967295;static float filtered = 0.0f; > >> > &#4294967295; &#4294967295;filtered += (sample - filtered) / d; > >> > &#4294967295; &#4294967295;return filtered; > >> >} > > >> >where "d" (=1, 2, 3...) defines the cut-off frequency: the higher d, the > >> >lower is the cut-off frequency. "sample" is the next incoming sample and > >> >"filtered" is the output of the filter. You call lowpassFilter for each > >> >incoming sample and it returns the filtered value. > > >> >If you use d=2^n, with n=0, 1, 2..., it is easy to implement it very fast > >> >and for fixed point numbers. > > >> I'm guessing from the op's comments, but it may be similar to a > >> beam-forming application. &#4294967295;The op didn't say about wide or narrow > >> band, but I'd guess relative phase of in-band signals would be > >> important to preserve. &#4294967295;On the other hand, the op seemed to be > >> cavalier about the suggestion regarding an external low-pass so maybe > >> I'm choosing the wrong comments to focus on and a simple, one-pole IIR > >> is fine. > > >> I think a lot more information about what is going on is important to > >> know to provide much useful help. > > >> Jon > > >I could've sworn I responded to these posts but my message isn't > >showing up. &#4294967295;It is kinda similar to beam forming, basically I am doing > >quick correlations with these signals and the high frequencies are > >messing up my algorithm. > > Correlations against a known signal or? &#4294967295;I'm wondering if you are > looking to extract phase (delay) against something you expect to see > or... >
Against similar signals like beamforming.
> >I can't change the filter on the end because > >that signal is being passed elsewhere where quality is important, and > >I can't add another filter because the board is already built. > > That much, I'd gathered already. &#4294967295;What worries me is the question of > group delay, among others, and the impact on what you are attempting.
Very true, I know a bit about it and it would probably devastate my algorithm, how can I easily know phase delays of filters? If it is near linear phase, do I have to do anything or apply some kind of allpass filter with opposite group delay?
> > >I just > >want to filter the signal before it is processed as cycle-efficient as > >possible. > > Of course. &#4294967295;We all want that. &#4294967295;;) > > >Is it possible using only fixed point arithmetic and not > >using floats? > > Probably possible either way. &#4294967295;I tend to force myself to completely > eliminate any use of floats on fixed-point processors (which I believe > you said you are using.) &#4294967295;Data comes in through the ADC in fixed > format and data goes out, usually, in fixed format to DACs or > whatever. &#4294967295;So why introduce issues attending floating point in the > middle, unless you have some clear reason. &#4294967295;Even in the case of a very > wide dynamic range, which you haven't said you are dealing with, it is > still possible to avoid most of the problems with generic floating > point routines by tailoring your routines in precise locations to your > needs, instead. > > Of course, you need to know how. &#4294967295;Most folks don't and, like the case > of a "to a man with a chainsaw, everything looks like a tree," use > floating point rather carelessly and as the "one tool that fits all > problems." >
well, this particular signal I am only using for analysis, the signals that are going back to the DAC will have parted from these bits by now. I will be dealing with wide dynamic range in some areas, how do suggest tailoring my routines? In some very large areas the way we do it now is using a large 32bit integer and bit shifting it, losing precision. This project has given me a greater appreciation of the power of fixed point.
> >It seems like the accuracy if I don't use floats will > >just decrease and decrease, > > No. &#4294967295;In fact, floating point will often strip you of important bits of > accuracy if you aren't very careful and thoughtful about what you are > doing with it. &#4294967295;In comparison, keeping things fixed point retains all > the bits with usually fair ease. &#4294967295;So the opposite is the case than > what you think. &#4294967295;The problem with fixed point is dynamic range, not > accuracy. > > >unless I premultiply each one by the > >maximum value of the sample each time or something and do other > >compensations. > > I can't say. &#4294967295;All this takes "sit down time" and some careful thinking > about exactly what you are doing to find the better path. &#4294967295;And that > isn't likely here. &#4294967295;But suffice it that it may be worth some of your > effort carefully considering what you are doing in the context of > various approaches. &#4294967295;It sounds like you aren't sufficiently trained in > numerical methods, though. &#4294967295;So perhaps that won't help and you'll just > have to take what you easily find and hope it gets you there. > > Back to the issue at hand... can your algorithm (forget about floats > or fixed point for a moment) cope well with varying phase delays as > different frequencies pass through the filter? &#4294967295;Do you need a FIR? &#4294967295;Or > will a simple IIR get you there? &#4294967295;(You can test a lot of this off-line > on your PC with simple software you cobble up and some captured or > simulated data you prepare.)
I was under the impression that allpasses could be used to fix these delays but essentiallyl I haven't tested the algorithm with different phase delays, but it would probably not perform very well. I can implement some stuff in matlab to test, though. So for linear phase I thought FIR. My understanding with IIR I have to make another buffer for output samples which would be more annoying to implement? thank you for all your input.
On Aug 6, 12:57&#4294967295;pm, Frank Buss <f...@frank-buss.de> wrote:
> Johnny Chang wrote: > > Is it possible using only fixed point arithmetic and not > > using floats? It seems like the accuracy if I don't use floats will > > just decrease and decrease, unless I premultiply each one by the > > maximum value of the sample each time or something and do other > > compensations. > > Can you show some code? This is working code from one of my projects: > > int value = sample << 8; > filtered += (value - filtered) >> 4; > > This assumes x.8 fixed point numbers (x depending on the size of "int"). > Instead of ">> 4" you can use lower values, for higer cut-off frequency. > The "filtered" value is in x.8 fixed point, too. "sample" is the raw sample > input. > > If you have problems with accuracy, try x.16 fixed point. Instead of 2^n > filter coeeficients, you can implement other number, too, if you combine > multiplications and shifting with large enough interger or simulated big > integer types. But you wrote a simple filter should work, so maybe you > won't need it. > > If you want a better filter, search for IIR filter of higher orders, Google > has many answers. Implementing it with fixed point numbers is more > complicated, but possible. > > -- > Frank Buss, f...@frank-buss.dehttp://www.frank-buss.de,http://www.it4-systems.de
this looks great, I will try it out, thank you. Actually first I will test the affects of filters like this in matlab with my simulation..
On Wed, 6 Aug 2008 14:25:22 -0700, dkelvey@hotmail.com wrote
(in article 
<838644c2-e573-48d6-9f44-f86719777b03@27g2000hsf.googlegroups.com>):

> If the frequencies that you are trying to get rid of are more than > half your sample frequency, once sampled, they are not separable from > any other signal in band.
Just a common nit. That is greater than or equal to half the sampling rate. People (and DSP book authors among them) tend to say you have to sample at least twice as fast as your greatest frequency then call it the Nyquist frequency. They should say you need to sample at greater than twice the highest frequency. (It is the Nyquist criterion but you have to sample > Nyquist). And it better be quite a bit greater if you don't want to be forced to use very long sample sets. I don't want to sound like I think anyone here is not up to speed, its just that these things become rules of thumb. Imagine sampling at exactly twice the highest freq. You will get a DC component instead, sampling at the same value every time, unless the amplitude is changing very rapidly. I used to teach this stuff and practically every book and article repeated this same error. I suppose they still do. Oversampling, as it is called, is a very good thing. -- Charlie Springer
On Fri, 17 Oct 2008 23:05:42 -0700, Charlie Springer wrote:

> On Wed, 6 Aug 2008 14:25:22 -0700, dkelvey@hotmail.com wrote (in article > <838644c2-e573-48d6-9f44-f86719777b03@27g2000hsf.googlegroups.com>): > >> If the frequencies that you are trying to get rid of are more than >> half your sample frequency, once sampled, they are not separable from >> any other signal in band. > > Just a common nit. That is greater than or equal to half the sampling > rate. People (and DSP book authors among them) tend to say you have to > sample at least twice as fast as your greatest frequency then call it > the Nyquist frequency. They should say you need to sample at greater > than twice the highest frequency. (It is the Nyquist criterion but you > have to sample > Nyquist). And it better be quite a bit greater if you > don't want to be forced to use very long sample sets. > > I don't want to sound like I think anyone here is not up to speed, its > just that these things become rules of thumb. Imagine sampling at > exactly twice the highest freq. You will get a DC component instead, > sampling at the same value every time, unless the amplitude is changing > very rapidly. I used to teach this stuff and practically every book and > article repeated this same error. I suppose they still do. Oversampling, > as it is called, is a very good thing. > > -- Charlie Springer
In theory it needs to be at least twice the highest frequency component; in reality it needs to be significantly greater than twice the highest _significant_ frequency component, with some tolerance for the _actual_ highest frequency component as long as there isn't much of it. Here's some more detail: http://www.wescottdesign.com/articles/Sampling/ sampling.html -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" gives you just what it says. See details at http://www.wescottdesign.com/actfes/actfes.html
Charlie Springer wrote:
> On Wed, 6 Aug 2008 14:25:22 -0700, dkelvey@hotmail.com wrote > (in article > <838644c2-e573-48d6-9f44-f86719777b03@27g2000hsf.googlegroups.com>): > >> If the frequencies that you are trying to get rid of are more than >> half your sample frequency, once sampled, they are not separable from >> any other signal in band. > > Just a common nit. That is greater than or equal to half the sampling rate. > People (and DSP book authors among them) tend to say you have to sample at > least twice as fast as your greatest frequency then call it the Nyquist > frequency. They should say you need to sample at greater than twice the > highest frequency. (It is the Nyquist criterion but you have to sample > > Nyquist). And it better be quite a bit greater if you don't want to be forced > to use very long sample sets. > > I don't want to sound like I think anyone here is not up to speed, its just > that these things become rules of thumb. Imagine sampling at exactly twice > the highest freq. You will get a DC component instead, sampling at the same > value every time, unless the amplitude is changing very rapidly. I used to > teach this stuff and practically every book and article repeated this same > error. I suppose they still do. Oversampling, as it is called, is a very good > thing.
Charlie, There will always be some violation of the >2x requirement because filters aren't perfect. The need is to keep the violation small enough so that aliases are tolerable. Just as it takes a long time to resolve frequencies near DC, it takes just as long to resolve frequencies just below fs/2. Just think about detecting a signal at 999.999 Hz when sampling at 2 KHz. How long before a full cycle is acquired? Exactly as long as for a .001 Hz signal. so another good reason for moderate oversampling is the accurate capture of frequencies a bit below Myquist's limit. Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
On Sat, 18 Oct 2008 12:50:57 -0700, Tim Wescott wrote
(in article <85ydnecbMfa8o2fVnZ2dnUVZ_oLinZ2d@web-ster.com>):

>> Just a common nit. That is greater than or equal to half the sampling >> rate. People (and DSP book authors among them) tend to say you have to >> sample at least twice as fast as your greatest frequency then call it >> the Nyquist frequency. They should say you need to sample at greater >> than twice the highest frequency. (It is the Nyquist criterion but you >> have to sample > Nyquist). And it better be quite a bit greater if you >> don't want to be forced to use very long sample sets. >> >> I don't want to sound like I think anyone here is not up to speed, its >> just that these things become rules of thumb. Imagine sampling at >> exactly twice the highest freq. You will get a DC component instead, >> sampling at the same value every time, unless the amplitude is changing >> very rapidly. I used to teach this stuff and practically every book and >> article repeated this same error. I suppose they still do. Oversampling, >> as it is called, is a very good thing. >> >> -- Charlie Springer > > In theory it needs to be at least twice the highest frequency component; > in reality it needs to be significantly greater than twice the highest > _significant_ frequency component, with some tolerance for the _actual_ > highest frequency component as long as there isn't much of it. > > Here's some more detail: http://www.wescottdesign.com/articles/Sampling/ > sampling.html
You state in your own paper that sampling at the Nyquist rate is meaningless without phase (and how can you have phase if you sample at the Nyquist rate?). So I respectfully submit that your premise is incorrect. In theory, you need to sample at least greater than twice the highest frequency component. Just change your into from "The Nyquist theorem states that if you have a signal that is perfectly band limited to a bandwidth of f0 then you can collect all the information there is in that signal by sampling, as long as your sample rate is 2f0 or more." to ---sample rate > 2f0 or more. The form of the theorem you are using is for analytical functions of infinite extent. Thus the misconception. If you can not sample for an infinite length of time (and who has the time for that?), you must exclude the equality. Your intro should say function instead of signal and remove the other references to bandwidth and signals, then relate it to the Fourier Transform to define bandlimiting. Note the FT is also from -infinity to infinity. Or you can just say >2f0. -- Charlie Springer