So I have a microphones that takes in sound and transmits them back to me, but I also perform analysis on them to find directionality on an AVR which uses fixed point arithmetic. Due to the spacing, I've found that I need to actually cut off frequencies a lot earlier than I wanted and am thinking about implementing a digital filter after the samples are made. What is the easiest way to do this? My frames are 256 samples long, although I am thinking about shrinking it down to 64. I took a DSP a long time ago but unfortunately don't remember as much as I would like. Filtering does not have to be that great, just get rid of some high frequencies, less phase distortion would probably be ideal. I remember how to find coefficients and all that with matlab and least squares but where do I go from there? I had the idea of finding a time domain realization of some FIR filter, windowing, and convolving, but will that be more processing power than I have? Can an IIR be implemented with simple delays and no convolving? I don't care about the phase of the samples I'm trying to get rid off with the lowpass. Thanks
low pass digital filter on a fixed point microcontroller
Started by ●August 5, 2008
Reply by ●August 5, 20082008-08-05
Here's some simple filter design techniques. Look at Chapters 14-21. http://www.dspguide.com/pdfbook.htm Also, you might want to consider a single-pole lowpass: http://www.dspguide.com/ch19/2.htm or a moving-average variation: http://www.dspguide.com/ch15/4.htm Regards, Steve
Reply by ●August 5, 20082008-08-05
On Aug 6, 5:14 am, Johnny Chang <johnny...@gmail.com> wrote:> So I have a microphones that takes in sound and transmits them back to > me, but I also perform analysis on them to find directionality on an > AVR which uses fixed point arithmetic. Due to the spacing, I've found > that I need to actually cut off frequencies a lot earlier than I > wanted and am thinking about implementing a digital filter after the > samples are made. > > What is the easiest way to do this? My frames are 256 samples long, > although I am thinking about shrinking it down to 64. I took a DSP a > long time ago but unfortunately don't remember as much as I would > like. Filtering does not have to be that great, just get rid of some > high frequencies, less phase distortion would probably be ideal. I > remember how to find coefficients and all that with matlab and least > squares but where do I go from there? I had the idea of finding a time > domain realization of some FIR filter, windowing, and convolving, but > will that be more processing power than I have? Can an IIR be > implemented with simple delays and no convolving? I don't care about > the phase of the samples I'm trying to get rid off with the lowpass. > > ThanksI would do it analogue as part of the anti-alias filter before the ADC. F.
Reply by ●August 5, 20082008-08-05
falderals@yahoo.co.uk wrote:> On Aug 6, 5:14 am, Johnny Chang <johnny...@gmail.com> wrote: >> So I have a microphones that takes in sound and transmits them back to >> me, but I also perform analysis on them to find directionality on an >> AVR which uses fixed point arithmetic. Due to the spacing, I've found >> that I need to actually cut off frequencies a lot earlier than I >> wanted and am thinking about implementing a digital filter after the >> samples are made. >> >> What is the easiest way to do this? My frames are 256 samples long, >> although I am thinking about shrinking it down to 64. I took a DSP a >> long time ago but unfortunately don't remember as much as I would >> like. Filtering does not have to be that great, just get rid of some >> high frequencies, less phase distortion would probably be ideal. I >> remember how to find coefficients and all that with matlab and least >> squares but where do I go from there? I had the idea of finding a time >> domain realization of some FIR filter, windowing, and convolving, but >> will that be more processing power than I have? Can an IIR be >> implemented with simple delays and no convolving? I don't care about >> the phase of the samples I'm trying to get rid off with the lowpass. >> >> Thanks > > I would do it analogue as part of the anti-alias filter before the > ADC.That has the advantage of allowing a lower sample rate. Some microcontrolers need all the advantage they can get. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Reply by ●August 6, 20082008-08-06
On Aug 5, 2:44�pm, falder...@yahoo.co.uk wrote:> On Aug 6, 5:14 am, Johnny Chang <johnny...@gmail.com> wrote: > > > > > So I have a microphones that takes in sound and transmits them back to > > me, but I also perform analysis on them to find directionality on an > > AVR which uses fixed point arithmetic. �Due to the spacing, I've found > > that I need to actually cut off frequencies a lot earlier than I > > wanted and am thinking about implementing a digital filter after the > > samples are made. > > > What is the easiest way to do this? My frames are 256 samples long, > > although I am thinking about shrinking it down to 64. �I took a DSP a > > long time ago but unfortunately don't remember as much as I would > > like. �Filtering does not have to be that great, just get rid of some > > high frequencies, less phase distortion would probably be ideal. �I > > remember how to find coefficients and all that with matlab and least > > squares but where do I go from there? I had the idea of finding a time > > domain realization of some FIR filter, windowing, and convolving, but > > will that be more processing power than I have? �Can an IIR be > > implemented with simple delays and no convolving? I don't care about > > the phase of the samples I'm trying to get rid off with the lowpass. > > > Thanks > > I would do it analogue as part of the anti-alias filter before the > ADC. > > F.I would like to do this but the board has already been designed and fabricated 100 times unfortunately
Reply by ●August 6, 20082008-08-06
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. -- Frank Buss, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
Reply by ●August 6, 20082008-08-06
On Wed, 6 Aug 2008 08:33:13 +0200, Frank Buss <fb@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
Reply by ●August 6, 20082008-08-06
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. > > JonI 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
Reply by ●August 6, 20082008-08-06
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, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
Reply by ●August 6, 20082008-08-06
On Wed, 6 Aug 2008 09:29:09 -0700 (PDT), Johnny Chang <johnnyz86@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.Correlations against a known signal or? I'm wondering if you are looking to extract phase (delay) against something you expect to see or...>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. What worries me is the question of group delay, among others, and the impact on what you are attempting.>I just >want to filter the signal before it is processed as cycle-efficient as >possible.Of course. We all want that. ;)>Is it possible using only fixed point arithmetic and not >using floats?Probably possible either way. I tend to force myself to completely eliminate any use of floats on fixed-point processors (which I believe you said you are using.) Data comes in through the ADC in fixed format and data goes out, usually, in fixed format to DACs or whatever. So why introduce issues attending floating point in the middle, unless you have some clear reason. 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. 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.">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.>unless I premultiply each one by the >maximum value of the sample each time or something and do other >compensations.I can't say. All this takes "sit down time" and some careful thinking about exactly what you are doing to find the better path. And that isn't likely here. But suffice it that it may be worth some of your effort carefully considering what you are doing in the context of various approaches. It sounds like you aren't sufficiently trained in numerical methods, though. 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? Do you need a FIR? Or will a simple IIR get you there? (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.) Jon






