DSPRelated.com
Forums

Any help about FIR filter algorithm

Started by mariya August 15, 2005
Hello

Can anyone tell me how i can get an output array from the following piece
of code. 

nm1=N-1;
yn=0;
for(k=0; k<nm1; ++){		/* shift data to make room for new sample */
	x[nm1-k]=x[nm1-k-1];
	x[0]=xn;
}
for(k=0; k<N; ++k){
	yn=yn+h[k]*x[k];	/* filter data and compute output sample */
}
return(yn);			/* filter output sample */

I do understand the basic formula about the FIR filters but as i am trying
to implement it , i am a bit lost about how to store my outputs in array.
Any net pages about this algo will be helpfull too!
thanks in advance


		
This message was sent using the Comp.DSP web interface on
www.DSPRelated.com
mariya skrev:
> Hello > > Can anyone tell me how i can get an output array from the following piece > of code.
You are using C or C++? The curly braces indicate you use one of them, the lack of data formats indicates you don't.
> nm1=N-1; > yn=0;
Please proviode memory allocations for your buffers. It will be easier to criticise your code if you allocate memory, it shows a bit more clearly how you think.
> for(k=0; k<nm1; ++){ /* shift data to make room for new sample */ > x[nm1-k]=x[nm1-k-1]; > x[0]=xn; > }
You don't have to do it like this, you could use one large buffer and pointers. But then, you seem to have problems with memory handling, so leave this as is for now.
> for(k=0; k<N; ++k){ > yn=yn+h[k]*x[k]; /* filter data and compute output sample */ > }
Here you compute one sample that was initialized above. It is not clear where in memory the accummulated sum will be stored.
> return(yn); /* filter output sample */ > > I do understand the basic formula about the FIR filters but as i am trying > to implement it , i am a bit lost about how to store my outputs in array.
The key here seems to be how to allocate memory for the buffers and arrays. You need to decide on data types first (float or int in C/C++), and you need to learn how to allocate memory ("malloc" in C, "new" in C++). Once you know how to do that, you are well on your way to get your function working.
> Any net pages about this algo will be helpfull too!
I think you are close enough, the problem is not the FIR filter as such, but how to make C/C++ (if you use one of these?) do what you need it to do. Once the data buffers are sorted out, most of the work is done. Rune
Hello

Thanks a lot for your reply, as matter of fact when i was trying to
implement the FIR filter algorithm it became harder than i thought. The
filters coeff. are float and it has been also a problem to get them
converted to integer, do u think there is a better way to convert these
filters coefficients to integer. I will implement this filter using C
language, so if i remember well when we allocate memory using c , we use
malloc is that correct of course i will certainly have a look on those in
my C programming book!
thanks again!


*************************************************************************
>mariya skrev: >> Hello >> >> Can anyone tell me how i can get an output array from the following
piece
>> of code. > >You are using C or C++? The curly braces indicate you use one of them, >the lack of data formats indicates you don't. > >> nm1=N-1; >> yn=0; > >Please proviode memory allocations for your buffers. >It will be easier to criticise your code if you allocate >memory, it shows a bit more clearly how you think. > >> for(k=0; k<nm1; ++){ /* shift data to make room for new sample */ >> x[nm1-k]=x[nm1-k-1]; >> x[0]=xn; >> } > >You don't have to do it like this, you could use one large buffer >and pointers. But then, you seem to have problems with memory >handling, so leave this as is for now. > >> for(k=0; k<N; ++k){ >> yn=yn+h[k]*x[k]; /* filter data and compute output sample */ >> } > >Here you compute one sample that was initialized above. >It is not clear where in memory the accummulated sum will be >stored. > >> return(yn); /* filter output sample */ >> >> I do understand the basic formula about the FIR filters but as i am
trying
>> to implement it , i am a bit lost about how to store my outputs in
array.
> >The key here seems to be how to allocate memory for the buffers >and arrays. You need to decide on data types first (float or int >in C/C++), and you need to learn how to allocate memory ("malloc" >in C, "new" in C++). > >Once you know how to do that, you are well on your way to get your >function working. > >> Any net pages about this algo will be helpfull too! > >I think you are close enough, the problem is not the FIR filter >as such, but how to make C/C++ (if you use one of these?) do what >you need it to do. Once the data buffers are sorted out, most of >the work is done. > >Rune > >
This message was sent using the Comp.DSP web interface on www.DSPRelated.com
mariya wrote:
> Hello > > Thanks a lot for your reply, as matter of fact when i was trying to > implement the FIR filter algorithm it became harder than i thought. The > filters coeff. are float and it has been also a problem to get them > converted to integer, do u think there is a better way to convert these > filters coefficients to integer. I will implement this filter using C > language, so if i remember well when we allocate memory using c , we use > malloc is that correct of course i will certainly have a look on those in > my C programming book! > thanks again!
Integer probably won't do. You almost certainly need to deal with fractions. You will likely have to deal with fixed point, which requires adjusting (shifting) the results of multiplications when using integer multipliers. Maybe you can find a C++ fixed-point math class. That's one more thing to understand, so for now it is probably simpler to work in float if you can afford the time. 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;
>Integer probably won't do. You almost certainly need to deal with >fractions. You will likely have to deal with fixed point, which requires
>adjusting (shifting) the results of multiplications when using integer >multipliers. Maybe you can find a C++ fixed-point math class. That's one
>more thing to understand, so for now it is probably simpler to work in >float if you can afford the time. > >Jerry
************************************************************* Definitely it would be much easier to stick with the float type but my big concern is that this filtering involves a convolution with an ADC input of type unsigned int, i have tried in some ways to convert them and i get the truncation error... I am affraid i cannot use the c++ fixed-point class as i am not familier with this language!! thanx! This message was sent using the Comp.DSP web interface on www.DSPRelated.com
mariya skrev:
> >Integer probably won't do. You almost certainly need to deal with > >fractions. You will likely have to deal with fixed point, which requires > > >adjusting (shifting) the results of multiplications when using integer > >multipliers. Maybe you can find a C++ fixed-point math class. That's one > > >more thing to understand, so for now it is probably simpler to work in > >float if you can afford the time. > > > >Jerry > > ************************************************************* > > Definitely it would be much easier to stick with the float type but my big > concern is that this filtering involves a convolution with an ADC input of > type unsigned int, i have tried in some ways to convert them and i get the > truncation error... I am affraid i cannot use the c++ fixed-point class as > i am not familier with this language!! > thanx!
The numerical formats seem to be additional problems to the basic FIR filter algorithm. I wouyld suggest you start out with an ofgf-line float algorithms, so that you can check that the filter works and you understand the basics. Next, convert to whatever numerical format else than float, and see that it works. Only then, start messing with real-time ADCs and the likes. Divide and concer! Rune
thanx again, i will try to work out all these and see where it leads! By
the way what do u mean by "ofgf-line float algorithms"?

>Divide and concer! // did u mean divide and convert?? > >Rune
**************************************************************
>The numerical formats seem to be additional problems to the >basic FIR filter algorithm. I wouyld suggest you start out >with an ofgf-line float algorithms, so that you can check that >the filter works and you understand the basics. > >Next, convert to whatever numerical format else than float, >and see that it works. Only then, start messing with real-time >ADCs and the likes. > >Divide and concer! > >Rune > >
This message was sent using the Comp.DSP web interface on www.DSPRelated.com
"mariya" <angelique1@jippii.fi> wrote in message
news:zq6dnf2_3b8MdZ3eRVn-sw@giganews.com...
> thanx again, i will try to work out all these and see where it leads! By > the way what do u mean by "ofgf-line float algorithms"?
That was a typo - should've been "off-line float algorithms" and by this he means that you should get your FIR filter working using floating point data that you generate/synthesize instead of using data from a real A/D. This will let you focus on the FIR algorithm instead of the data types. Get it to work using known float data and then you can modify it for limited precision data from an A/D. Cheers Bhaskar
> > >Divide and concer! // did u mean divide and convert?? > > > >Rune > > ************************************************************** > >The numerical formats seem to be additional problems to the > >basic FIR filter algorithm. I wouyld suggest you start out > >with an ofgf-line float algorithms, so that you can check that > >the filter works and you understand the basics. > > > >Next, convert to whatever numerical format else than float, > >and see that it works. Only then, start messing with real-time > >ADCs and the likes. > > > >Divide and concer! > > > >Rune > > > > > > > > This message was sent using the Comp.DSP web interface on > www.DSPRelated.com
mariya wrote:
>>Integer probably won't do. You almost certainly need to deal with >>fractions. You will likely have to deal with fixed point, which requires > > >>adjusting (shifting) the results of multiplications when using integer >>multipliers. Maybe you can find a C++ fixed-point math class. That's one > > >>more thing to understand, so for now it is probably simpler to work in >>float if you can afford the time. >> >>Jerry > > > ************************************************************* > > Definitely it would be much easier to stick with the float type but my big > concern is that this filtering involves a convolution with an ADC input of > type unsigned int, i have tried in some ways to convert them and i get the > truncation error... I am affraid i cannot use the c++ fixed-point class as > i am not familier with this language!!
I don't know C++ either. In C, I would cast the ADC values to float, do all calculations including dither (if that's part of the plan.) By scaling so that rounding to integer makes sizes the output to be suitable for the DAC, the job is done. 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;
mariya skrev:
> thanx again, i will try to work out all these and see where it leads! By > the way what do u mean by "ofgf-line float algorithms"? > > >Divide and concer! // did u mean divide and convert??
Nope, it was supposed to be "divide and conquer" (just hope it got write know... ;) Thanks, Bhaskar, for correcting my blunders! Rune