FIR filter in 8051 micro

Started by rome...@gmail.com October 29, 2007
Hi all,
           I have a set of samples for which I want to apply FIR
filtering (assuming that filtering will be good). I know that digital
filtering will give near precise filtering but I have little knowledge
in FIR/IIR and I do not know where to apply which filter(yes, I
googled ) . I need a small code segment that will do FIR filtering for
my samples . I am using an 8051 microcontroller .
Thanks in advance.

romeshkulasekhara@gmail.com wrote:
> Hi all, > I have a set of samples for which I want to apply FIR > filtering (assuming that filtering will be good). I know that digital > filtering will give near precise filtering but I have little knowledge > in FIR/IIR and I do not know where to apply which filter(yes, I > googled ) . I need a small code segment that will do FIR filtering for > my samples . I am using an 8051 microcontroller . > Thanks in advance.
You need to learn how to do triple or quadruple precision math in the 8051. You can read the book at http://www.dspguide.com/ and the online courses at http://www.bores.com/courses/intro/index.htm do begin addressing your needs. The architecture of the 8051 makes processing filters on it very slow. There are some modern variants that have facilities that can speed it up a bit, but it would not be my processor of choice. Even if some of the variants have a multiplication op code, it will be single precision and that's not enough for most filters. Jerry -- Engineering is the art of making what you want from things you can get. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
>I need a small code segment that will do FIR filtering for
>my samples . I am using an 8051 microcontroller .
Hello, the link below is an online filter calculator that produces C code for the designed filter. I doubt that you want to compile it for the 8051, but it produces some working code to start with. Cheers Markus http://www-users.cs.york.ac.uk/~fisher/mkfilter/
mnentwig wrote:
>> I need a small code segment that will do FIR filtering for >> my samples . I am using an 8051 microcontroller . > > Hello, > > the link below is an online filter calculator that produces C code for the > designed filter. I doubt that you want to compile it for the 8051, but it > produces some working code to start with. > > Cheers > > Markus > > http://www-users.cs.york.ac.uk/~fisher/mkfilter/ >
He said he wants an FIR. The only FIRs that page can generate are Hilbert transforms. Steve
Well, you're right, and raised cosine.
There's probably plenty of FIR example code out there anyway.

-Markus

mnentwig wrote:
> Well, you're right, and raised cosine. > There's probably plenty of FIR example code out there anyway.
I told him it probably won't fit, either in the time he has (which wasn't stated) or in the available space. The crippled machine has one data pointer. There's an increment instruction for it, but not a decrement. The Harvard architecture makes it difficult to fetch coefficients from the program ROM, so one way or another coefficients need to be stored in the RAM segment. Circular buffers can be kludged up. I remember building a pair for interrupt-driven serial communication. (There's a bug in the UART timing, so if 9 bits are needed, you have to do the timing "by hand". For 8 bits, you tell it you're sending 9.) The only reason to use that machine is price. If you're too cheap to spring for the difference between it and, say, a 68HC11 or -12, you get what you deserve. If I needed to do any low-octane DSP, I'd at least use a 68HC16. Nowadays, there's better. Jerry -- Engineering is the art of making what you want from things you can get. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

romeshkulasekhara@gmail.com wrote:

> I have a set of samples for which I want to apply FIR > filtering (assuming that filtering will be good). I know that digital > filtering will give near precise filtering but I have little knowledge > in FIR/IIR and I do not know where to apply which filter(yes, I > googled ) . I need a small code segment that will do FIR filtering for > my samples . I am using an 8051 microcontroller .
It is hard to imagine more unsuitable architecture for the FIR filter then x51. I doubt if there is any application for which this filter could be useful. Perhaps, you should look at CIC or moving average filters. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com

Jerry Avins wrote:

> I told him it probably won't fit, either in the time he has (which > wasn't stated) or in the available space. The crippled machine has one > data pointer. There's an increment instruction for it, but not a > decrement. The Harvard architecture makes it difficult to fetch > coefficients from the program ROM, so one way or another coefficients > need to be stored in the RAM segment. Circular buffers can be kludged > up.
Oh, come on :) movc A, @A + DPTR mov B, A mov A, @R0 mul AB .... inc DPTR inc R0 mov A, R0 anl A, #buffer mov R0, A ....
> I remember building a pair for interrupt-driven serial > communication. (There's a bug in the UART timing, so if 9 bits are > needed, you have to do the timing "by hand". For 8 bits, you tell it > you're sending 9.) > > The only reason to use that machine is price. If you're too cheap to > spring for the difference between it and, say, a 68HC11 or -12, you get > what you deserve. If I needed to do any low-octane DSP, I'd at least use > a 68HC16. Nowadays, there's better. > > Jerry
First, filtering using an embedded 8-bit micro is possible. Ignore people
who are talking double precision, etc, etc....they aren't embedded people
with experience in this area. I have invented an algorithm that works well
on a PIC (8-bit also). I call it the "tri-band filter". I had a situation
where I had to generate a measure of the power spectrum in 3 bands in an
audio spectrum of a piano (about 4.5KHz). So, I took 32 samples at a time
(I was limted by memory) and ran a FIR filter on the data. I basically
chose a FIR filter of the form ax0+bx1+cx2 and centered it at the midpoint
of my frequency range. My sample rate was twice the highest frequency (by
Nyquist).
Now, I used some online filter coefficient generator. What I noticed is
the LPF coefficients were the same as the HPF coefficients just swapping
signs here and there, so as long as I calculated ax0, bx1 and cx2, I could
use the same calculations for both filters. The second trick I used was to
round any floating point values to the nearest powers of two. For example,
one term was 0.3664, and I rounded it to 0.375 (3/8*x), which can be
calculated as ((x << 2) + x) >> 3, which involves no multiplies (my PIC
didn't have a multiply) and only involves integer calculations. Note that
the frequency response of the filter (if you choose your coefficients
carefully) was almost identical to the non-altered one and my results were
very accurate (much better than my older state variable filter that I used
to use). If you combine the touching of coords to be combinations of power
of two so you can do your calculations using shifts and adds and reuse
coefficients to give you both High and Low pass results simultaneously. The
band pass was obtained by subtracting the LPF and HPF values from the
original data. You will need to use a 16 bit accumulator (assuming you are
using 8-bit sampled data) and remember to shift left and add BEFORE
shifting right as you'll get more accurate results that way.
This algorithm works very well for 8-bit processors.
Enjoy...



webmasterpdx wrote:
> First, filtering using an embedded 8-bit micro is possible. Ignore people > who are talking double precision, etc, etc....they aren't embedded people > with experience in this area. I have invented an algorithm that works well > on a PIC (8-bit also). I call it the "tri-band filter". I had a situation > where I had to generate a measure of the power spectrum in 3 bands in an > audio spectrum of a piano (about 4.5KHz). So, I took 32 samples at a time > (I was limted by memory) and ran a FIR filter on the data. I basically > chose a FIR filter of the form ax0+bx1+cx2 and centered it at the midpoint > of my frequency range. My sample rate was twice the highest frequency (by > Nyquist). > Now, I used some online filter coefficient generator. What I noticed is > the LPF coefficients were the same as the HPF coefficients just swapping > signs here and there, so as long as I calculated ax0, bx1 and cx2, I could > use the same calculations for both filters. The second trick I used was to > round any floating point values to the nearest powers of two. For example, > one term was 0.3664, and I rounded it to 0.375 (3/8*x), which can be > calculated as ((x << 2) + x) >> 3, which involves no multiplies (my PIC > didn't have a multiply) and only involves integer calculations. Note that > the frequency response of the filter (if you choose your coefficients > carefully) was almost identical to the non-altered one and my results were > very accurate (much better than my older state variable filter that I used > to use). If you combine the touching of coords to be combinations of power > of two so you can do your calculations using shifts and adds and reuse > coefficients to give you both High and Low pass results simultaneously. The > band pass was obtained by subtracting the LPF and HPF values from the > original data. You will need to use a 16 bit accumulator (assuming you are > using 8-bit sampled data) and remember to shift left and add BEFORE > shifting right as you'll get more accurate results that way. > This algorithm works very well for 8-bit processors. > Enjoy...
You do realize that this is a year-old thread? Whatever, you seem like the kind of pragmatic programmer I admire. Jerry -- Engineering is the art of making what you want from things you can get. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Dnia 21-10-2009 o 19:08:05 Jerry Avins <jya@ieee.org> napisa³(a):

(...)
> Nevertheless, the approach is rare enough to be admired anywhere in the > world.Jerry
Yes, I agree. I like when people can use their abilities of thinking. -- Mikolaj
Mikolaj wrote:
> On 21-10-2009 at 11:19:22 webmasterpdx <webmasterpdx@gmail.com> wrote: > > What you are describing > is in my country called engineering > and so called "inventions" > are just called in my country > basic knowledge and thinking. > And what you did with coefficients is called scalling > (more or less). > > There is absolutely nothing exciting > and nothing unusual > in such everyday work. > It takes 2-3 yrs to become such inventor :).
Nevertheless, the approach is rare enough to be admired anywhere in the world. I come from an industry where redesigning a circuit to make it a penny cheaper without reducing performance -- often performance was actually improved -- would bring a substantial bonus at year's end. Jerry -- Engineering is the art of making what you want from things you can get. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
On 21-10-2009 at 11:19:22 webmasterpdx <webmasterpdx@gmail.com> wrote:

What you are describing
is in my country called engineering
and so called "inventions"
are just called in my country
basic knowledge and thinking.
And what you did with coefficients is called scalling
(more or less).

There is absolutely nothing exciting
and nothing unusual
in such everyday work.
It takes 2-3 yrs to become such inventor :).

-- 
Mikolaj
Yeah, I know it's an old post, but I want to help anyone who might
come across that to know that this kind of stuff is possible. It does
take a pragmatic approach. One area I try to evangelize is that I see
a lot of newer engineers coming out of university with experience
using XSCALE processors and PCs with unlimited resources and often
these people never lived in the age of the slow mainframes, slow CPU
modules or even slow CPUs. Mostly it's about prices and time to
market. As a result, we often have to get very creative with what we
have, and if we can do something with a $1 uP instead of a $10 DSP or
$30 XSCALE, then we should go for the $1 uP.

Of course when you play with new algorithms you invent, by all means
simulate it. I simulated the filter before using it both on my TI-86
and then later in C (where I could simulate 8-bit calculations) and
gnuplot to plot the results. I created input signals that were
combinations of sine waves at different frequencies added up, and the
results were pretty darn accurate. Not the perfect simulation, but
good enough for me. Then in the prototype product, it worked just
fine.

Coming up with clever ways to measure and condition our signals forms
the core of innovation in small embedded projects as they usually
involve measuring some quantity to give results that can be used
elsewhere.

e.g. If using a uP with a crystal clock, the most accurate measurement
you can make is that of time (width of a pulse), so if you want to
measure capacitance or inductance, tying that to a circuit that
measures rise time will give you much better results than trying to
measure the quantity itself.
Another example, if not unusual.....I was tasked with measuring
velocity of rollerblades. The problem was that using light was a
problem due to mud, etc, being thrown up. So, instead we measured
sound and autocorrellation. The idea being that the front blades will
go over the same bumps as the back blades, so an autocorrelation
should show a bump for each wheel, so if you know the distance between
the wheels you can determine the velocity. It wasn't quite that simple
and there are faster ways of doing autocorrelations than a multiply
accumulate (average minimum differences) that only involves subtracts
and adds. Bottom line, the technique worked.

I guess what I'm saying is that you can often (more often than people
realize) do it with a PIC or 8051, just as well as with an XSCALE or
full DSP, and I encourage people to do that when possible. Thats all.
Of course, there are occasions when you do need double precisions (ill
conditioned equations, etc, etc), but these, for me, are last
resorts...

Thx
-D
On Oct 20, 4:10 pm, Jerry Avins <j...@ieee.org> wrote:
> glen herrmannsfeldt wrote: > > webmasterpdx <webmaster...@gmail.com> wrote: > >> First, filtering using an embedded 8-bit micro is possible. Ignore people > >> who are talking double precision, etc, etc....they aren't embedded people > >> with experience in this area. > > > Well, a large fraction of DSP is done in 16 bit fixed point. > > In that case, double precision might be 32 bit fixed point, not so > > hard to do on an 8051.  Floating point in any precision is probably > > not the best choice for DSP on an 8 bit processor. > > As far as I remember, the 8051 doesn't have a carry test. (But there's > always a way ...)
does it have an Add with Carry instruction? maybe the OP's sample rate is real slow. r b-j
glen herrmannsfeldt wrote:
> webmasterpdx <webmasterpdx@gmail.com> wrote: >> First, filtering using an embedded 8-bit micro is possible. Ignore people >> who are talking double precision, etc, etc....they aren't embedded people >> with experience in this area. > > Well, a large fraction of DSP is done in 16 bit fixed point. > In that case, double precision might be 32 bit fixed point, not so > hard to do on an 8051. Floating point in any precision is probably > not the best choice for DSP on an 8 bit processor.
As far as I remember, the 8051 doesn't have a carry test. (But there's always a way ...) Jerry -- Engineering is the art of making what you want from things you can get. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
webmasterpdx <webmasterpdx@gmail.com> wrote:
> First, filtering using an embedded 8-bit micro is possible. Ignore people > who are talking double precision, etc, etc....they aren't embedded people > with experience in this area.
Well, a large fraction of DSP is done in 16 bit fixed point. In that case, double precision might be 32 bit fixed point, not so hard to do on an 8051. Floating point in any precision is probably not the best choice for DSP on an 8 bit processor. -- glen
webmasterpdx wrote:
> First, filtering using an embedded 8-bit micro is possible. Ignore people > who are talking double precision, etc, etc....they aren't embedded people > with experience in this area. I have invented an algorithm that works well > on a PIC (8-bit also). I call it the "tri-band filter". I had a situation > where I had to generate a measure of the power spectrum in 3 bands in an > audio spectrum of a piano (about 4.5KHz). So, I took 32 samples at a time > (I was limted by memory) and ran a FIR filter on the data. I basically > chose a FIR filter of the form ax0+bx1+cx2 and centered it at the midpoint > of my frequency range. My sample rate was twice the highest frequency (by > Nyquist). > Now, I used some online filter coefficient generator. What I noticed is > the LPF coefficients were the same as the HPF coefficients just swapping > signs here and there, so as long as I calculated ax0, bx1 and cx2, I could > use the same calculations for both filters. The second trick I used was to > round any floating point values to the nearest powers of two. For example, > one term was 0.3664, and I rounded it to 0.375 (3/8*x), which can be > calculated as ((x << 2) + x) >> 3, which involves no multiplies (my PIC > didn't have a multiply) and only involves integer calculations. Note that > the frequency response of the filter (if you choose your coefficients > carefully) was almost identical to the non-altered one and my results were > very accurate (much better than my older state variable filter that I used > to use). If you combine the touching of coords to be combinations of power > of two so you can do your calculations using shifts and adds and reuse > coefficients to give you both High and Low pass results simultaneously. The > band pass was obtained by subtracting the LPF and HPF values from the > original data. You will need to use a 16 bit accumulator (assuming you are > using 8-bit sampled data) and remember to shift left and add BEFORE > shifting right as you'll get more accurate results that way. > This algorithm works very well for 8-bit processors. > Enjoy...
You do realize that this is a year-old thread? Whatever, you seem like the kind of pragmatic programmer I admire. Jerry -- Engineering is the art of making what you want from things you can get. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
First, filtering using an embedded 8-bit micro is possible. Ignore people
who are talking double precision, etc, etc....they aren't embedded people
with experience in this area. I have invented an algorithm that works well
on a PIC (8-bit also). I call it the "tri-band filter". I had a situation
where I had to generate a measure of the power spectrum in 3 bands in an
audio spectrum of a piano (about 4.5KHz). So, I took 32 samples at a time
(I was limted by memory) and ran a FIR filter on the data. I basically
chose a FIR filter of the form ax0+bx1+cx2 and centered it at the midpoint
of my frequency range. My sample rate was twice the highest frequency (by
Nyquist).
Now, I used some online filter coefficient generator. What I noticed is
the LPF coefficients were the same as the HPF coefficients just swapping
signs here and there, so as long as I calculated ax0, bx1 and cx2, I could
use the same calculations for both filters. The second trick I used was to
round any floating point values to the nearest powers of two. For example,
one term was 0.3664, and I rounded it to 0.375 (3/8*x), which can be
calculated as ((x << 2) + x) >> 3, which involves no multiplies (my PIC
didn't have a multiply) and only involves integer calculations. Note that
the frequency response of the filter (if you choose your coefficients
carefully) was almost identical to the non-altered one and my results were
very accurate (much better than my older state variable filter that I used
to use). If you combine the touching of coords to be combinations of power
of two so you can do your calculations using shifts and adds and reuse
coefficients to give you both High and Low pass results simultaneously. The
band pass was obtained by subtracting the LPF and HPF values from the
original data. You will need to use a 16 bit accumulator (assuming you are
using 8-bit sampled data) and remember to shift left and add BEFORE
shifting right as you'll get more accurate results that way.
This algorithm works very well for 8-bit processors.
Enjoy...




Jerry Avins wrote:

> I told him it probably won't fit, either in the time he has (which > wasn't stated) or in the available space. The crippled machine has one > data pointer. There's an increment instruction for it, but not a > decrement. The Harvard architecture makes it difficult to fetch > coefficients from the program ROM, so one way or another coefficients > need to be stored in the RAM segment. Circular buffers can be kludged > up.
Oh, come on :) movc A, @A + DPTR mov B, A mov A, @R0 mul AB .... inc DPTR inc R0 mov A, R0 anl A, #buffer mov R0, A ....
> I remember building a pair for interrupt-driven serial > communication. (There's a bug in the UART timing, so if 9 bits are > needed, you have to do the timing "by hand". For 8 bits, you tell it > you're sending 9.) > > The only reason to use that machine is price. If you're too cheap to > spring for the difference between it and, say, a 68HC11 or -12, you get > what you deserve. If I needed to do any low-octane DSP, I'd at least use > a 68HC16. Nowadays, there's better. > > Jerry