DSPRelated.com
Forums

newbie: fir filter design

Started by Unknown June 28, 2005
Hi again,

I need to design a FIR filter with an even number of coefficients. I
was wondering if the following formula is correct for calculating the
coefficients for a lowpass filter with hamming window -

for(n=0;n<L;n++)
  b[n] = (1/R)sinc(n/R){0.54+0.46*sin(2*pi*n/L)

where R = Nyquist f / cutoff f
L = filter length

Thanks for your help once more,

Ann.

aine_canby@yahoo.com wrote:
> Hi again, > > I need to design a FIR filter with an even number of coefficients. I > was wondering if the following formula is correct for calculating the > coefficients for a lowpass filter with hamming window - > > for(n=0;n<L;n++) > b[n] = (1/R)sinc(n/R){0.54+0.46*sin(2*pi*n/L) > > where R = Nyquist f / cutoff f > L = filter length > > Thanks for your help once more, > > Ann.
http://www.cg.tuwien.ac.at/studentwork/CESCG/CESCG99/TTheussl/node9.html Please learn to exploit Google. Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Thanks, I was trying to be too smart with that sin function.

OK, heres what I have now -

for(i=0;i<L;i++)
h[i] = 1/R*sinc(i-(L-1)/2)*(0.54+0.46*cos(2*PI* (i-(L-1)/2) / L))

Is this correct for an odd number of coefficients?

Now, what about an even number? I could do it for 255 and leave one of
the taps a zero. Is there a way of using all the taps?

Thanks,

Ann.

Thanks, I was trying to be too smart with that sin function.

OK, heres what I have now -

for(int i=0;i<L;i++)
		b[i] =
1/double(R)*sinc((i-((L-1)/2.0))/double(R))*(0.54+0.46*cos(2*PI*
(i-((L-1)/2.0))/L));

Is this correct for an odd number of coefficients?

Now, what about an even number? I could do it for 255 and leave one of
the taps a zero. Is there a way of using all the taps?

Thanks,

Ann.

aine_canby@yahoo.com wrote:
> Thanks, I was trying to be too smart with that sin function. > > OK, heres what I have now - > > for(int i=0;i<L;i++) > b[i] = > 1/double(R)*sinc((i-((L-1)/2.0))/double(R))*(0.54+0.46*cos(2*PI* > (i-((L-1)/2.0))/L));
sinc? Why? from where? The window is simply a cycle of cosine raised on a pedestal so that the lowest point is slightly positive and the highest is unity. The reference I gave you shows a closely related window. Once you understand what they are, you can derive your own equation. Note sin^2(x) = [1-cos(x/2)]/2 and related identities.
> Is this correct for an odd number of coefficients? > > Now, what about an even number? I could do it for 255 and leave one of > the taps a zero. Is there a way of using all the taps?
That's an exercise for the student. Note that when using a window whose end coefficients are zero, you keep a little more significance by computing the window to cover two extra points, then discarding 0*0. Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Well this is how we learned to generate symmetrical filter coefficients
in class for a lowpass filter with 33 taps -

h[n] = (1/R)sinc(n/R) n =-16 to 16

where R = Nyquist frequency / Cutoff Frequency

I'm just trying to create a more practical version of this so I can
generate the tap values using C. I also added the window.

Does my code make sense?

I plotted the frequency response of the tap values my code generated
using freqz, and I got what I expected. I set R = 5 and L = 1023 and I
got a stopband attenuation of -60dBs or so.

aine_canby@yahoo.com wrote:
> Well this is how we learned to generate symmetrical filter coefficients > in class for a lowpass filter with 33 taps - > > h[n] = (1/R)sinc(n/R) n =-16 to 16 > > where R = Nyquist frequency / Cutoff Frequency > > I'm just trying to create a more practical version of this so I can > generate the tap values using C. I also added the window. > > Does my code make sense? > > I plotted the frequency response of the tap values my code generated > using freqz, and I got what I expected. I set R = 5 and L = 1023 and I > got a stopband attenuation of -60dBs or so.
I'm confused. Are you generating filter coefficients, or the coefficients of the Hamming window? I may well have given you a bum steer. Yes indeed: sinc for low-pass (and high-pass too by subtracting one from the middle coefficient) and a window to reduce the leakage. Is there a reason for not using one of the many software optimizers to do the whole job? Practice? I too like and use old tools, sometimes because because that's what I have, sometimes because they work better. One of my better tools: http://users.rcn.com/jyavins/Jointer.jpg (I also have an electric one.) Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Thanks Jerry.

As part of my class project, I have to produce the coefs myself.

I'm happy enough now with what I have, just wondering though about the
implementation of a 256 tap filter. Should I simply calculate for 255
taps and then leave the last tap empty, or is there a version the
following equation for even numbered taps -

h[n] = (1/R)sinc(n/R) n =-16 to 16 

Ann.

Hi Ann!

Your FIR filter should be designed to be linear-phase. A FIR filter is 
linear-phase if (and only if) its coefficients are symmetrical around the 
center coefficient, that is, the first coefficient is the same as the last; 
the second is the same as the next-to-last, etc. (A linear-phase FIR filter 
having an odd number of coefficients will have a single coefficient in the 
center which has no mate.)   b[n] = b[ L-n-1]! So it's not a good idea to 
just throw one coefficient away to get an even numbered filter! If your two 
functions fullfill the abovementioned criterium, you are on a good way:

So you may write for your window function: 0.54 + 0.46 * sin( 2*PI*n/ 
(L-1) ) ( since n = 0.... L-1 )

or in C++ pseudo code: w[n] =  0.54 + 0.46 * sin (2. * PI * (float) n / 
float( L - 1) ;

And your low pass filter should be: (1/R) * sinc (  (n - ( L-1)/2)   /R )

or in pseudo C++ code:

float m = ( float ) n - float( L-1) /2.f;

 h[n] = (1/R) * sin ( m/R ) / (m/R) ;

Whether the filter length L is odd or even does not matter for those 
formulas ( if I didn't make errors -  Please remember, that I am no filter 
expert ). Just set L = 255 or L = 256 !

Yours Karin



<aine_canby@yahoo.com> schrieb im Newsbeitrag 
news:1119965921.445942.226820@g43g2000cwa.googlegroups.com...

> Hi again, > > I need to design a FIR filter with an even number of coefficients. I > was wondering if the following formula is correct for calculating the > coefficients for a lowpass filter with hamming window - > > for(n=0;n<L;n++) > b[n] = (1/R)sinc(n/R){0.54+0.46*sin(2*pi*n/L) > > where R = Nyquist f / cutoff f > L = filter length > > Thanks for your help once more, > > Ann. >
Karin wrote:

> A FIR filter is linear-phase if (and only if) its coefficients are symmetrical > around the center coefficient, that is, the first coefficient is the same as > the last; the second is the same as the next-to-last, etc.
Hello Karin, I'm not sure about that second "if". I think hermitian symmetric FIRs are also linear-phase ... Regards, Andor