low pass digital filter on a fixed point microcontroller

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

Reply to
Johnny Chang
Loading thread data ...

I would do it analogue as part of the anti-alias filter before the ADC.

F.

Reply to
falderals

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 to
Jerry Avins

d

I would like to do this but the board has already been designed and fabricated 100 times unfortunately

Reply to
Johnny Chang

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 to
Frank Buss

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 to
Jonathan Kirwan

he

fast

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

Reply to
Johnny Chang

Can you show some code? This is working code from one of my projects:

int value = sample > 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 to
Frank Buss

Correlations against a known signal or? I'm wondering if you are looking to extract phase (delay) against something you expect to see or...

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.

Of course. We all want that. ;)

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."

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 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

Reply to
Jonathan Kirwan

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

Reply to
Jonathan Kirwan

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
Reply to
Frank Buss

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

Reply to
dkelvey

Yes. I figured that part almost goes without saying, though. But yes.

I have it installed and I use it here and there. Good addition to point out.

Jon

Reply to
Jonathan Kirwan

, the

and

ch

ery fast

Against similar signals like beamforming.

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?

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.

r

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.

Reply to
Johnny Chang

le

le

tems.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..

Reply to
Johnny Chang

Hi Johnny,

Grest, It's an impressive simple filter!

But how can I choose d value given the sampling frequency Fs and expected cut-off frequency Fc?

I guess there must be some formula like

d= Func( Fs, Fc) ?

Best Regards, Austin

fast

Reply to
Austin

On Wed, 6 Aug 2008 14:25:22 -0700, snipped-for-privacy@hotmail.com wrote (in article ):

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

Reply to
Charlie Springer

I don't know, I would just simulate it:

formatting link

(screenshot, if you don't have Java:

formatting link
)

The displayed dB value is the gain of the power of the signal. The calculation is very brute force: it just runs the filter with 8 cycles of a sine for each sine frequency and calculates the power of the signal for each tested frequency.

--
Frank Buss, fb@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Reply to
Frank Buss

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:

formatting link
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
Reply to
Tim Wescott

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.
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Reply to
Jerry Avins

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.