C code for implementation of low pass filter to time series signal

Started by helen September 7, 2006
I am doing C programming for low pass filtering time series signal and then
downsampling the signal.

Is there any available C source code for dummies? 
For instance, given filter coefficients, h[n], and signal
x[numberofSamples], how shall I get the filtered signal ??

Thanks 



helen wrote:
> I am doing C programming for low pass filtering time series signal and then > downsampling the signal. > > Is there any available C source code for dummies? > For instance, given filter coefficients, h[n], and signal > x[numberofSamples], how shall I get the filtered signal ?? > > Thanks >
Is this a finite impulse response filter (FIR), or an infinite impulse response filter (IIR)? If it's a FIR then you just take the last N samples and multiply them sample-for-sample by the filter coefficients, so for sample n you'd use: out[n] = sum from {k = max(0, n - N)} to n {x[n - k] * h[k]} This article _may_ clear things up. Although it's written more for control system design it does go into the implementation of IIR filters: http://www.wescottdesign.com/articles/zTransform/z-transforms.html. -- 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
helen wrote:
> I am doing C programming for low pass filtering time series signal and then > downsampling the signal. > > Is there any available C source code for dummies? > For instance, given filter coefficients, h[n], and signal > x[numberofSamples], how shall I get the filtered signal ?? >
You might find this useful: http://www.dspguru.com/info/faqs/firfaq.htm Good luck. -- Jim Thomas Principal Applications Engineer Bittware, Inc jthomas@bittware.com http://www.bittware.com (603) 226-0404 x536 There's a fine line between clever and stupid
I have the C++ code for convolution : 

iCoefficients can be the FIR / IIR Coefficients, iIndex is the Index
number of the vector that is being filtered.
vec is the typedef for std::vector<double> 

For an IIR filter your code would look like:

filteredData[i] =
Convolution(i,input,BCoefficients)-Convolution(i,filteredData,ACoefficients);


double Convolution(int iIndex, vec& iData, vec& iCoefficients) 
/**< answer = conv(signal, filtercoefficients)*/
{
     double answer = 0;
     for(int l=0; l<iCoefficients.size();l++) 
     {
         if((iIndex-l-1)>=0)
         {     
             answer+= iData(iIndex-l-1)*iCoefficients(l); 
         }
         else
         {
             answer+=0.0;             
         }
     }
     return answer;
}






 As for calculating the coefficients of a low pass filter, try using the
billinear transform of if you need fast results, calculate the
coefficients in MATLAB and save them in your code.
I have the C++ code for convolution : 

iCoefficients can be the FIR / IIR Coefficients, iIndex is the Index
number of the vector that is being filtered.
vec is the typedef for std::vector<double> 

For an IIR filter your code would look like:

filteredData[i] =
Convolution(i,input,BCoefficients)-Convolution(i,filteredData,ACoefficients);


double Convolution(int iIndex, vec& iData, vec& iCoefficients) 
/**< answer = conv(signal, filtercoefficients)*/
{
     double answer = 0;
     for(int l=0; l<iCoefficients.size();l++) 
     {
         if((iIndex-l-1)>=0)
         {     
             answer+= iData(iIndex-l-1)*iCoefficients(l); 
         }
         else
         {
             answer+=0.0;             
         }
     }
     return answer;
}






 As for calculating the coefficients of a low pass filter, try using the
billinear transform of if you need fast results, calculate the
coefficients in MATLAB and save them in your code.
helen wrote:
> I am doing C programming for low pass filtering time series signal and then > downsampling the signal. > > Is there any available C source code for dummies? > For instance, given filter coefficients, h[n], and signal > x[numberofSamples], how shall I get the filtered signal ?? >
You might find this useful: http://www.dspguru.com/info/faqs/firfaq.htm Good luck. -- Jim Thomas Principal Applications Engineer Bittware, Inc jthomas@bittware.com http://www.bittware.com (603) 226-0404 x536 There's a fine line between clever and stupid
helen wrote:
> I am doing C programming for low pass filtering time series signal and then > downsampling the signal. > > Is there any available C source code for dummies? > For instance, given filter coefficients, h[n], and signal > x[numberofSamples], how shall I get the filtered signal ?? > > Thanks >
Is this a finite impulse response filter (FIR), or an infinite impulse response filter (IIR)? If it's a FIR then you just take the last N samples and multiply them sample-for-sample by the filter coefficients, so for sample n you'd use: out[n] = sum from {k = max(0, n - N)} to n {x[n - k] * h[k]} This article _may_ clear things up. Although it's written more for control system design it does go into the implementation of IIR filters: http://www.wescottdesign.com/articles/zTransform/z-transforms.html. -- 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
I am doing C programming for low pass filtering time series signal and then
downsampling the signal.

Is there any available C source code for dummies? 
For instance, given filter coefficients, h[n], and signal
x[numberofSamples], how shall I get the filtered signal ??

Thanks