--- In , "Andrew Leech" <andrew_leech@y...> wrote: > I want to be able to 'filter' the value returned from an on-board ADC. > When I say 'filter' I think that perhaps 'average' would be a better > description. > I want to be able to set an amount of filtering (say 20%) and none of > the descriptions of digital filters I've found seem to allow this. > They all seem to be designed to filter a specific frequency range that > must be stated in the calculations used to design the filter. Hi Andrew, I am not sure if I read you right but it seems you are trying to compensate for some sort of noise in the incoming signal. I have found it is best to consider analogue signals in their entirety, the bane of the computer programmers I.e. the origin of the signals is important because it determines the frequ ency range of interest. Say a small motor with a varying load needs a faster response time then a w eigh-cell where accuracy is of more importance and the final measurement may take a second or so. So it is best to start a project with the input considerations first, lower the required frequency response as far as possible first of. Say for the small motor it could mean a flywheel or other means of mass inc rease, for a weigher it could mean a dashpot. The next step is to minimise the frequency response from amplifiers and oth er electronics to what the measurement requires, and of course to what the c omputer can handle. Since this is a relatively cheap step, don't spare the caps, it will preven t all sorts of aliassing problems in your digital filters if the signal vari ations (or mains noise) don't get that far or are minimised before they get to the converters. I can't over stress that, you will find it takes an amazingly long time and an even more amazing sample rate to get some form of accuracy in a signal e mbedded in a sine-wave, to say nothing about reduced headroom in the signal paths etc. Keep it out of a device that has to work in a sample/hold fashion and only has a limited A/D accuracy as far as possible. The last step is the one you are interested in, the measurement and/or cont rol loop in the computer. You will find there are as many schemes as there are Engineers, but in the end they all try to do the same thing, provide a digital low pass filter in some form or another, the cut of frequencies, poles etc determined by how ke en the designer is and how fast the computer. There are some rather involved tomes that will give you all the good stuff, but it is very rare to have to go that far, simple solutions usually suffic e in most situations. I.e. a few stages of RC low pass filtering limiting the signals presented t o the lowest frequency needed, followed by something similar in the computer is usually enough to serve the purpose. The trade of in computer filters is of course easily seen, the more involve d the filter, the slower your sample rate (or the faster the computer has to be), often negating the gains by introducing aliassing problems. I.e. sampling at a rate that coincides with some harmonic or other of the i ncoming signal will cause all sorts of grief. If you have made sure to keep higher frequencies out of the signal path in the first place, a simple exponential scheme along the lines of adding the n ew value to a multiple of the previous value and weighing it back usually su ffices. If you find you have to lower the cut of frequency beyond the time availabl e, re-evaluate the input filtering rather then trying to compensate in the C PU with all sorts of interesting algorithms. Hope this helps, Theo |
Software Filtering of an ADC input
Started by ●March 7, 2004
Reply by ●March 7, 20042004-03-07
Reply by ●March 7, 20042004-03-07
Hi, You can always use IIR or a more computative FIR software filter with taylored coefficients. However, on the KISS solution. Lets say the problem is lack of data memory. You would like a adjustable software filter - field tunable. So how about a cascaded running average filter. In stead of one long array, you first average the first 8 samples into a running average, and stuff the result of the first average into a second array doing a running average of the results of the first running average output in another 8 sample array and so on and so on .... till you run out of memory. Its obvious to see that this is a easely tunable filter algorithm. Now stuff that into your excel spreadsheet !!! I adjustable software filter can take the for of I ----- Original Message ----- From: "theobee00" <> To: <> Sent: Sunday, March 07, 2004 12:48 PM Subject: [68HC12] Re: Software Filtering of an ADC input --- In , "Andrew Leech" <andrew_leech@y...> wrote: > I want to be able to 'filter' the value returned from an on-board ADC. > When I say 'filter' I think that perhaps 'average' would be a better > description. > I want to be able to set an amount of filtering (say 20%) and none of > the descriptions of digital filters I've found seem to allow this. > They all seem to be designed to filter a specific frequency range that > must be stated in the calculations used to design the filter. Hi Andrew, I am not sure if I read you right but it seems you are trying to compensate= for some sort of noise in the incoming signal. I have found it is best to consider analogue signals in their entirety, the= bane of the computer programmers. I.e. the origin of the signals is important because it determines the frequ= ency range of interest. Say a small motor with a varying load needs a faster response time then a w= eigh-cell where accuracy is of more importance and the final measurement may= take a second or so. So it is best to start a project with the input considerations first, lower= the required frequency response as far as possible first of. Say for the small motor it could mean a flywheel or other means of mass inc= rease, for a weigher it could mean a dashpot. The next step is to minimise the frequency response from amplifiers and oth= er electronics to what the measurement requires, and of course to what the c= omputer can handle. Since this is a relatively cheap step, don't spare the caps, it will preven= t all sorts of aliassing problems in your digital filters if the signal vari= ations (or mains noise) don't get that far or are minimised before they get = to the converters. I can't over stress that, you will find it takes an amazingly long time and= an even more amazing sample rate to get some form of accuracy in a signal e= mbedded in a sine-wave, to say nothing about reduced headroom in the signal = paths etc. Keep it out of a device that has to work in a sample/hold fashion and only = has a limited A/D accuracy as far as possible. The last step is the one you are interested in, the measurement and/or cont= rol loop in the computer. You will find there are as many schemes as there are Engineers, but in the = end they all try to do the same thing, provide a digital low pass filter in = some form or another, the cut of frequencies, poles etc determined by how ke= en the designer is and how fast the computer. There are some rather involved tomes that will give you all the good stuff,= but it is very rare to have to go that far, simple solutions usually suffic= e in most situations. I.e. a few stages of RC low pass filtering limiting the signals presented t= o the lowest frequency needed, followed by something similar in the computer= is usually enough to serve the purpose. The trade of in computer filters is of course easily seen, the more involve= d the filter, the slower your sample rate (or the faster the computer has to= be), often negating the gains by introducing aliassing problems. I.e. sampling at a rate that coincides with some harmonic or other of the i= ncoming signal will cause all sorts of grief. If you have made sure to keep higher frequencies out of the signal path in = the first place, a simple exponential scheme along the lines of adding the n= ew value to a multiple of the previous value and weighing it back usually su= ffices. If you find you have to lower the cut of frequency beyond the time availabl= e, re-evaluate the input filtering rather then trying to compensate in the C= PU with all sorts of interesting algorithms. Hope this helps, Theo --------------------To learn more about Motorola Microcontrollers, please visit http://www.motorola.com/mcu o learn more about Motorola Microcontrollers, please visit http://www.motorola.com/mcu Yahoo! Groups Links |
|
Reply by ●March 7, 20042004-03-07
Andy, I can't resist summarizing what I understand from the previous correspondence on this topic and Jack Crenshaw's columns in Embedded Systems Programming. See this mailing list archive for a thread starting on Jun 28, 2003 8:43 pm with subject: OT: digital filtering. Filter in hardware before sampling to reduce the noise in the digital values due to high frequency noise in the input signal. The high frequency noise WILL be there, so just do this, don't worry about it. A simple RC filter will do if you have bandwidth to spare, which is likely. The ideal is to have a "brick wall" filter that has zero attenuation at half the sampling frequency, and very large attenuation at the sampling frequency and beyond. A simple RC filter is fairly far from this ideal, but setting its cutoff frequency as low as you can afford will help. If the highest frequency of interest is well below a reasonable sampling rate, then use some sort of digital low pass filter, such as averaging, is needed. The equivalent of an RC filer, which several have suggested, is the simple filter which takes a fraction of the input and a fraction of the current average and adds them. I believe that the simplest statement of this in C is: Avg +=( Avg - CurSample ) >> n. Where Avg and CurSample are 16 bit fractions. This takes a 16-bit subtract, a shift, and a 16-bit add. No array is needed. There is a problem when n is 6 or greater because then you start throwing away significant bits from CurSample. This can be gotten around by clever code that makes Avg in effect a 24 or 32 bit fraction. The corner frequency of this filter is set by n. You should choose it to be above the highest frequency of interest, but probably not as much as 8 times has high. The software filter is much cheaper that an analog filter for low frequencies. Hope this helps. Steve Russell Nohau Emulators At 04:04 AM 3/7/2004, you wrote: >I guess this is a bit of a general question, rather than a specific >HCS12 question, but I want to implement it on an HCS12..... > >I want to be able to 'filter' the value returned from an on-board ADC. >When I say 'filter' I think that perhaps 'average' would be a better >description. > >I want to be able to set an amount of filtering (say 20%) and none of >the descriptions of digital filters I've found seem to allow this. >They all seem to be designed to filter a specific frequency range that >must be stated in the calculations used to design the filter. > >I thought about having a ring buffer to which you place the ADC >results at a constant time interval, then summing the buffer, and >dividing by the number of samples taken. But then how do you allow for >the amount of filtering you want to apply ?? > >I'm going round in circles with this !! > >Any thoughts or ideas would be very much appreciated. > >I also need the routine to be fairly quick and not take up too much >RAM as I'm getting a bit short on it. > >Andy ************************************************************************* Steve Russell mailto: Senior Software Design Engineer http://www.nohau.com Nohau Corporation phone: (408)866-1820 ext. 1873 51 East Campbell Avenue fax: (408)378-7869 Campbell, CA 95008 ************************************************************************* |
Reply by ●March 7, 20042004-03-07
Andy, From a frequency viewpoint, an average is a low-pass filter. For the number of "taps" (values stored) it is not the sharpest filter. The computation is simple, however, as shown in previous posts, and it has a sharper cutoff than the simple RC digital filter. The book, Digital Filters, R.W. Hamming, 1977, Prentice-Hall, has a section on the frequency response of averages. The book (quite readable) is out of print, but probably can be located through a university library system. In case your interested, the passband equ: H(w) = (w * sin(m+1/2) ) / ( (2*m+1)*sin(w/2) ) where w = freq (radians), m = number of taps Here is a chapter of a (very readable) DSP book you can download (free) that also talks about averages. He points out that the average is a filter that responds the quickest to a step input, so for some situations it is optimal. http://www.dspguide.com/ch15.htm You mention "20%" for the filtering. That sounds like you are thinking in terms an IIR (infinite impluse response) filter, such as the simple digital implementation of a RC filter, also in the previous posts. If you implement that form for filter make sure the storage for the accumulated value will accommodate the largest value. As that "%" gets smaller, which has the effect of making the cut-off freq lower, the accumulated value gets very large (but, unlike the circular buffer/average filter, only one value has to be stored, so it doesn't take a lot of memory). The characteristics of the signal you are measuring determines the which type of filter is best (assuming it fits in memory!). The IIR/RC type of filter "remembers" a big glitch, or noise spike, "forever," i.e. a noise spike takes a long time to "fade away," whereas the averaging filtering drops a big number after one cycle around the buffer. Regards, Donald E Haselwood At 07:04 AM 3/7/04, you wrote: >I guess this is a bit of a general question, rather than a specific >HCS12 question, but I want to implement it on an HCS12..... > >I want to be able to 'filter' the value returned from an on-board ADC. >When I say 'filter' I think that perhaps 'average' would be a better >description. > >I want to be able to set an amount of filtering (say 20%) and none of >the descriptions of digital filters I've found seem to allow this. >They all seem to be designed to filter a specific frequency range that >must be stated in the calculations used to design the filter. > >I thought about having a ring buffer to which you place the ADC >results at a constant time interval, then summing the buffer, and >dividing by the number of samples taken. But then how do you allow for >the amount of filtering you want to apply ?? > >I'm going round in circles with this !! > >Any thoughts or ideas would be very much appreciated. > >I also need the routine to be fairly quick and not take up too much >RAM as I'm getting a bit short on it. > >Andy > >--------------------To learn more >about Motorola Microcontrollers, please visit ><http://www.motorola.com/mcu" target="_blank" rel="nofollow">http://www.motorola.com/mcu>http://www.motorola.com/mcu >o learn more about Motorola Microcontrollers, please visit ><http://www.motorola.com/mcu" target="_blank" rel="nofollow">http://www.motorola.com/mcu>http://www.motorola.com/mcu > > >---------- >>Yahoo! Terms of Service. |
Reply by ●March 8, 20042004-03-08
--- In , linktek <linktek@s...> wrote: > You can always use IIR or a more computative FIR software filter with > taylored coefficients. Of course, the problem is the more 'interesting' the filter, the more chance you have of introducing stability problems in control loops due to the phase shifts. Anyway, for those who like to play, http://www-users.cs.york.ac.uk/~fisher/mkfilter/ It lets you specify the type of filter you want to use, corner frequencies, poles etc, it shows you the graphs of freq, phase, impulse etc, and to top it of, writes the C code for you with the parameters neatly filled in, piece of cake. > average filter. In stead of one long array, you first average > the first 8 samples into a running average, and stuff the result of the > first average into a second array doing a running average of the results of > the first running average output in another 8 sample array and so on and so > on .... till you run out of memory. Its obvious to see that this is a easely > tunable filter algorithm. If I read that right it's a simple to compute averaging filter over 8/64/128/etc samples, or did I miss something? > Now stuff that into your excel spreadsheet !!! Don't think excel has a clock. Cheers, Theo |
Reply by ●March 8, 20042004-03-08
Since you're working in assembler, here's some code that does
the recursive smoothing described below... ****************************************************************************** * AV_256TH_UNS - long tc averaging - unsigned * * - JSR AV_256TH_UNS ;D:pass sample,X:pntr running av. * ****************************************************************************** AV_256TH_UNS PSHY LDY #$0100 ;*1/256 EMUL PSHY LDY #$FF00 ;*255/256 LDD 0,X ;unsigned EMUL BCC _AV_256_2 INY _AV_256_2 PULD LEAY D,Y ;unsigned addition TFR Y,D STD 0,X PULY RTS ****************************************************************************** bruce --- In , "Robert Smith" <bobsmith5@e...> wrote: > Andrew -- > > I have used the following method with good results on several projects: > > 1. Allocate an accumulator in RAM to hold the running average. > > 2. For every A/D sample update the accumulator as follows: > > Accum = (Prior Accum * 7/8) + 1/8 * New Sample. > > Working in assembler this is almost trival to implement. Use shifts and > accumulates. > > Shift 'Prior Accum' left 3 to obtain 1/8 of 'Prior Accum' > > Subtract 1/8 Prior Accum from 'Prior Accum' to obtain 7/8 'Prior Accum' > > Now compute 1/8 of 'New Sample' by shifting right 3 and add this to '7/8 > Prior Accum' to > obtain the Updated accumulator and smoothed value. > > The smoothing factor of 1/8 was arbitrairly chosen but is representative of > factors that have worked well in the past. > > You can adjust the "degree of filtering" by using different smoothing > factors such as 1/4 or 1/16. > This only requires that you adjust the shift counts. > > Note: If you do this all in fixed point, you can expect to see binary > truncation effects in the result. In effect the output will develop a dead > zone such that new samples very near the running average will not affect the > filter output. The output will jump from one smoothed value to another as > the new samples change by a few counts. This creates a sort of dead zone > effect. This is an inevitble consequence of binary truncation and will show > up in any filtering technique that you devise. > > It is much better to eliminate the noise at the source by careful attention > to hardware design and to use little or no software filtering. > > Warning: Be very careful to consider the dynamic range of the intermediate > results of this calculation. Binary overfow at any step of this process > will produce absolute rubbish for output!!! > > You can examine the results of this filtering process by clever use of > Excel. > > Good Luck, Bob Smith > > |
Reply by ●March 8, 20042004-03-08
See below. Emmett Redd Ph.D. mailto: Associate Professor (417)836-5221 Department of Physics, Astronomy, and Materials Science Southwest Missouri State University Fax (417)836-6226 901 SOUTH NATIONAL Dept (417)836-5131 SPRINGFIELD, MO 65804 USA > -----Original Message----- > From: theobee00 [mailto:] > Sent: Monday, March 08, 2004 1:10 AM > To: > Subject: [68HC12] Re: Software Filtering of an ADC input > > --- In , linktek <linktek@s...> wrote: > > Now stuff that into your excel spreadsheet !!! > > Don't think excel has a clock. As a model, Excel works because each column (or row) can represent the calculation performed at each successive, uniformly spaced, time step. > > > Cheers, > > Theo |
|
Reply by ●March 8, 20042004-03-08
You can get an excellent tool for characterizing various filters from Nuhertz Technologies (www.nuhertz.com). I used their Filter Free tool years ago while researching digital filters for an embedded project. The tool I used then (version 2.0) generated frequency response graphs, electrical schematic equivalents, and transfer functions, which made code generation much easier. One aspect of this filtering discussion that needs mentioning is sample rate. You can have an excellent filter algorithm set up, and lose all benefits from it by sampling at the wrong rate. One product I worked on was originally set up with an IIR filter tailored for 60Hz rejection, but the software guy was executing it every 100 ms (10 Hz sample rate). Checks with the Nuhertz tool showed that this resulted in 0 db filtering. By using the same IIR filter, but sampling at an 11 Hz rate, 60Hz noise rejection was better than -45 db. This improvement was confirmed in later lab tests. Karl linktek <linktek@shaw. To: ca> cc: Subject: Re: [68HC12] Re: Software Filtering of an ADC input 03/07/2004 01:58 PM Please respond to 68HC12 Hi, You can always use IIR or a more computative FIR software filter with taylored coefficients. However, on the KISS solution. Lets say the problem is lack of data memory. You would like a adjustable software filter - field tunable. So how about a cascaded running average filter. In stead of one long array, you first average the first 8 samples into a running average, and stuff the result of the first average into a second array doing a running average of the results of the first running average output in another 8 sample array and so on and so on .... till you run out of memory. Its obvious to see that this is a easely tunable filter algorithm. Now stuff that into your excel spreadsheet !!! (deletia...) ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________ ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________ |
|
Reply by ●March 8, 20042004-03-08
You could try this: Average = 80%(OldSample) + 20%(NewSample). Store Average at OldSample. The percentages can be changed to whatever you want. This does not require a big buffer or serious calculations. It does have some disadvantages, however. Another method I use for simple filtering is: OldSample + NewSample ----------------------------------- 2 This gives rudimentary filtering, but you can sprinkle it around in your code after calculations to make things smoother. I also use a 16 value ring filter, where the new value is added to a sum, and the oldest value is subtracted out. For 16 bit values, this requires 32 bytes of buffer, a 2 byte sum, and a 2 byte pointer, as well as your original and filtered values. This is a total of 40 bytes, which is a pretty good chunk of RAM. > I thought about doing it like this, but it would need to have a > large array to average the samples, and I can't afford the RAM > space that would use. > > I'm sure there must be some clever way of doing this without > using an array - just can't see it yet. Regards, Darrell Norquay Datalog Technology Inc. Calgary, Alberta, Canada Voice: (403) 243-2220 Fax: (403) 243-2872 Email: Web: www.datalog.ab.ca |
Reply by ●March 8, 20042004-03-08
--- In , "Redd, Emmett R" <EmmettRedd@s...> > > > Now stuff that into your excel spreadsheet !!! > > > > Don't think excel has a clock. > > As a model, Excel works because each column (or row) can represent the > calculation performed at each successive, uniformly spaced, time step. > In the context of the discussion clock meant, take ADC sample, put in cell, do average, shift result to next group of cells etc. For that you need a clock synchronised to the sample rate. But hey, knowing M$ and their tendency to built in a few thousand extra features a week, it probably is capable of that as well:-) Cheers, Theo |