DSPRelated.com
Forums

Noob Question on Filter Coefficient

Started by fole...@gmail.com October 18, 2006
Hello,
I have a task to update some C code that does some FIR Filtering. I was
thinking I would just use FDATool in Matlab, put in new parameters and
stick it back into the C code.

I am kind of puzzled by a few things concerning FIR filter
coefficients.

1) when using the filter command in Matlab, I see that from searching
the web, the denominator part is set to 1 most of the time. However,
when you receive a filter design from Matlab, you get both the
denominator and numerator part. Do you always set the denominator part
to 1, if all you have are some plain ol coefficients? i.e. scopeFIR
produces only one set of coefficients.

2) Looking at some filter coefficients (scopeFIR, FDATool), they are
very small numbers with lots of decimal places (I.e.
0.006096622300200799). However, in the C code I am examining, they are
large numbers (i.e. 8122). What gives? I read that somethings
coeficients are normalized?

Any tips/hints greatly appreciated.

Foley

foleyma@gmail.com wrote:
> Hello, > I have a task to update some C code that does some FIR Filtering. I was > thinking I would just use FDATool in Matlab, put in new parameters and > stick it back into the C code. > > I am kind of puzzled by a few things concerning FIR filter > coefficients. > > 1) when using the filter command in Matlab, I see that from searching > the web, the denominator part is set to 1 most of the time. However, > when you receive a filter design from Matlab, you get both the > denominator and numerator part. Do you always set the denominator part > to 1, if all you have are some plain ol coefficients? i.e. scopeFIR > produces only one set of coefficients. > > 2) Looking at some filter coefficients (scopeFIR, FDATool), they are > very small numbers with lots of decimal places (I.e. > 0.006096622300200799). However, in the C code I am examining, they are > large numbers (i.e. 8122). What gives? I read that somethings > coeficients are normalized? > > Any tips/hints greatly appreciated. > > Foley >
a. Just making coefficients in a tool and pouring them into code that you don't understand is a recipe for disaster. b. Matlab is giving you the 'whole' filter -- if you want to be complete about it an N-tap FIR filter has a transfer function with z^-(N-1) in it. c. Your C code is probably using fixed-point math. Since the only fixed-point numbers that C understands are integers, the whole filter has been multiplied up to give sufficient precision. Somewhere in the filter code it'll be divided down again (probably by shifting). -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Posting from Google? See http://cfaj.freeshell.org/google/ "Applied Control Theory for Embedded Systems" came out in April. See details at http://www.wescottdesign.com/actfes/actfes.html
Thank you for your response Tim,

I've always had your point a) in mind. I did see bit shifting in the
code, but I thought it had to do with bit growth - after reading it, it
may be for both... I found a paper titled "Practical Considerations in
Fixed-POint FIR Filter Implementations", I will read through it and any
other articles I find.

As for your response about Matlab giving the whole filter, I understand
that they give the coefficients to the filter as a form of the transfer
function
Y(Z) = [b(1) + B(2)z^-1...]/[a(1) + a(2)z^-1...]

If I understand you correctly, other progams (ScopeFIR, or whenever I
see 1 array of coefficients) give only the B portion and it would be
assumed the a()'s are 1? If it's possible to represent an equivalent
these two ways, what advantage does Matlab have in giving both the
numerator and denominator - since it requires more memory, and probably
more computation.

Thanks,
Foley

Tim Wescott wrote:
> foleyma@gmail.com wrote: > > Hello, > > I have a task to update some C code that does some FIR Filtering. I was > > thinking I would just use FDATool in Matlab, put in new parameters and > > stick it back into the C code. > > > > I am kind of puzzled by a few things concerning FIR filter > > coefficients. > > > > 1) when using the filter command in Matlab, I see that from searching > > the web, the denominator part is set to 1 most of the time. However, > > when you receive a filter design from Matlab, you get both the > > denominator and numerator part. Do you always set the denominator part > > to 1, if all you have are some plain ol coefficients? i.e. scopeFIR > > produces only one set of coefficients. > > > > 2) Looking at some filter coefficients (scopeFIR, FDATool), they are > > very small numbers with lots of decimal places (I.e. > > 0.006096622300200799). However, in the C code I am examining, they are > > large numbers (i.e. 8122). What gives? I read that somethings > > coeficients are normalized? > > > > Any tips/hints greatly appreciated. > > > > Foley > > > a. Just making coefficients in a tool and pouring them into code > that you don't understand is a recipe for disaster. > > b. Matlab is giving you the 'whole' filter -- if you want to be > complete about it an N-tap FIR filter has a transfer function > with z^-(N-1) in it. > > c. Your C code is probably using fixed-point math. Since the only > fixed-point numbers that C understands are integers, the whole > filter has been multiplied up to give sufficient precision. > Somewhere in the filter code it'll be divided down again (probably > by shifting). > > -- > > Tim Wescott > Wescott Design Services > http://www.wescottdesign.com > > Posting from Google? See http://cfaj.freeshell.org/google/ > > "Applied Control Theory for Embedded Systems" came out in April. > See details at http://www.wescottdesign.com/actfes/actfes.html
"foleyma@gmail.com" <foleyma@gmail.com> writes:

> Thank you for your response Tim, > > I've always had your point a) in mind. I did see bit shifting in the > code, but I thought it had to do with bit growth - after reading it, it > may be for both... I found a paper titled "Practical Considerations in > Fixed-POint FIR Filter Implementations", I will read through it and any > other articles I find. > > As for your response about Matlab giving the whole filter, I understand > that they give the coefficients to the filter as a form of the transfer > function > Y(Z) = [b(1) + B(2)z^-1...]/[a(1) + a(2)z^-1...] > > If I understand you correctly, other progams (ScopeFIR, or whenever I > see 1 array of coefficients) give only the B portion and it would be > assumed the a()'s are 1? If it's possible to represent an equivalent > these two ways, what advantage does Matlab have in giving both the > numerator and denominator - since it requires more memory, and probably > more computation. > > Thanks, > Foley
Hi Foley, Recall that a transfer function H(z) of a system is defined as the output of the system, Y(z), over the input to the system, X(z): H(z) = Y(z) / X(z). Therefore if H(z) is a rational functtion, i.e., H(z) = B(z) / A(z), where B(z) and A(z) are polynomials in z, then you have the following situation: Y(z) / X(z) = B(z) / A(z) Y(z) * A(z) = X(z) * B(z). If A(z) = 1, then when you translate this from the z-domain to the time domain, you will find that the output at time n, y[n], is only a function of the input samples x[0], x[1], .... Such a filter is called an FIR filter (Finite Impulse Response) since any finite polynomial has a finite number of terms and the coefficients of the polynomial B(z) wind up being the impulse response of the filter. If A(z) is not just "1" but a function of z, then the output is a function of both the input samples x and the output y. Such a filter is called a *recursive filter* and is commonly referred to as an IIR (Infinite Impulse Response) filter. IIR filters allow much greater flexibility and capability than FIR filters. They are also significantly more difficult to design and implement than FIRs, particularly in fixed-point arithmetic, since stability and round-off issues become prominent. --Randy
> > Tim Wescott wrote: >> foleyma@gmail.com wrote: >> > Hello, >> > I have a task to update some C code that does some FIR Filtering. I was >> > thinking I would just use FDATool in Matlab, put in new parameters and >> > stick it back into the C code. >> > >> > I am kind of puzzled by a few things concerning FIR filter >> > coefficients. >> > >> > 1) when using the filter command in Matlab, I see that from searching >> > the web, the denominator part is set to 1 most of the time. However, >> > when you receive a filter design from Matlab, you get both the >> > denominator and numerator part. Do you always set the denominator part >> > to 1, if all you have are some plain ol coefficients? i.e. scopeFIR >> > produces only one set of coefficients. >> > >> > 2) Looking at some filter coefficients (scopeFIR, FDATool), they are >> > very small numbers with lots of decimal places (I.e. >> > 0.006096622300200799). However, in the C code I am examining, they are >> > large numbers (i.e. 8122). What gives? I read that somethings >> > coeficients are normalized? >> > >> > Any tips/hints greatly appreciated. >> > >> > Foley >> > >> a. Just making coefficients in a tool and pouring them into code >> that you don't understand is a recipe for disaster. >> >> b. Matlab is giving you the 'whole' filter -- if you want to be >> complete about it an N-tap FIR filter has a transfer function >> with z^-(N-1) in it. >> >> c. Your C code is probably using fixed-point math. Since the only >> fixed-point numbers that C understands are integers, the whole >> filter has been multiplied up to give sufficient precision. >> Somewhere in the filter code it'll be divided down again (probably >> by shifting). >> >> -- >> >> Tim Wescott >> Wescott Design Services >> http://www.wescottdesign.com >> >> Posting from Google? See http://cfaj.freeshell.org/google/ >> >> "Applied Control Theory for Embedded Systems" came out in April. >> See details at http://www.wescottdesign.com/actfes/actfes.html >
-- % Randy Yates % "She's sweet on Wagner-I think she'd die for Beethoven. %% Fuquay-Varina, NC % She love the way Puccini lays down a tune, and %%% 919-577-9882 % Verdi's always creepin' from her room." %%%% <yates@ieee.org> % "Rockaria", *A New World Record*, ELO http://home.earthlink.net/~yatescr
Randy Yates <yates@ieee.org> writes:
> [...] > time domain, you will find that the output at time n, y[n], is only a > function of the input samples x[0], x[1], ....
I should have written is only a function of the input samples ..., x[-1], x[0], x[1], .... -- % Randy Yates % "So now it's getting late, %% Fuquay-Varina, NC % and those who hesitate %%% 919-577-9882 % got no one..." %%%% <yates@ieee.org> % 'Waterfall', *Face The Music*, ELO http://home.earthlink.net/~yatescr
foleyma@gmail.com wrote:
(top posting fixed)
> Tim Wescott wrote: > >>foleyma@gmail.com wrote: >> >>>Hello, >>>I have a task to update some C code that does some FIR Filtering. I was >>>thinking I would just use FDATool in Matlab, put in new parameters and >>>stick it back into the C code. >>> >>>I am kind of puzzled by a few things concerning FIR filter >>>coefficients. >>> >>>1) when using the filter command in Matlab, I see that from searching >>>the web, the denominator part is set to 1 most of the time. However, >>>when you receive a filter design from Matlab, you get both the >>>denominator and numerator part. Do you always set the denominator part >>>to 1, if all you have are some plain ol coefficients? i.e. scopeFIR >>>produces only one set of coefficients. >>> >>>2) Looking at some filter coefficients (scopeFIR, FDATool), they are >>>very small numbers with lots of decimal places (I.e. >>>0.006096622300200799). However, in the C code I am examining, they are >>>large numbers (i.e. 8122). What gives? I read that somethings >>>coeficients are normalized? >>> >>>Any tips/hints greatly appreciated. >>> >>>Foley >>> >> >>a. Just making coefficients in a tool and pouring them into code >> that you don't understand is a recipe for disaster. >> >>b. Matlab is giving you the 'whole' filter -- if you want to be >> complete about it an N-tap FIR filter has a transfer function >> with z^-(N-1) in it. >> >>c. Your C code is probably using fixed-point math. Since the only >> fixed-point numbers that C understands are integers, the whole >> filter has been multiplied up to give sufficient precision. >> Somewhere in the filter code it'll be divided down again (probably >> by shifting).
>
> Thank you for your response Tim, > > I've always had your point a) in mind. I did see bit shifting in the > code, but I thought it had to do with bit growth - after reading it, it > may be for both... I found a paper titled "Practical Considerations in > Fixed-POint FIR Filter Implementations", I will read through it and any > other articles I find. > > As for your response about Matlab giving the whole filter, I understand > that they give the coefficients to the filter as a form of the transfer > function > Y(Z) = [b(1) + B(2)z^-1...]/[a(1) + a(2)z^-1...] > > If I understand you correctly, other progams (ScopeFIR, or whenever I > see 1 array of coefficients) give only the B portion and it would be > assumed the a()'s are 1? If it's possible to represent an equivalent > these two ways, what advantage does Matlab have in giving both the > numerator and denominator - since it requires more memory, and probably > more computation. >
Matlab probably wants to give you something in a form that you can stick right into their "filter" function and get answers, without having to concern yourself with whether it's an IIR or FIR filter. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Posting from Google? See http://cfaj.freeshell.org/google/ "Applied Control Theory for Embedded Systems" came out in April. See details at http://www.wescottdesign.com/actfes/actfes.html