Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: FIR filter coefficients

  1. #1
    Junior Member
    Join Date
    Jan 2015
    Posts
    11

    FIR filter coefficients

    Hi all,

    I'm trying to include a few FIR filters into my program. I used the Tfilter website to design the filters however, it output the code but not the filter coefficients like what the FIR examples in arduino. Did i missed out any steps?

    what i want:
    Code:
    /* .fir file parameters which generated these coefficients
    100 1 2 16 32768 44100
    0 1000 2000 
     1.0000  0.0000 
     1.0000 70.0000 
    */
    	(short)0x0015,
    	(short)0x0011,
    	(short)0x0017,
    	(short)0x001E,
    	(short)0x0025,
    	(short)0x002C,
    	(short)0x0032,
    	(short)0x0037,
    	(short)0x003B,
    	(short)0x003C,
    	(short)0x003B,
    	(short)0x0036,
    	(short)0x002E,
    	(short)0x0021,
    	(short)0x000F,
    	(short)0xFFF9,
    	(short)0xFFDE,
    	(short)0xFFBF,
    	(short)0xFF9C,
    	(short)0xFF76,
    	(short)0xFF4F,
    	(short)0xFF27,
    	(short)0xFF02,
    	(short)0xFEDF,
    	(short)0xFEC2,
    	(short)0xFEAC,
    	(short)0xFEA0,
    	(short)0xFE9F,
    	(short)0xFEAC,
    	(short)0xFEC8,
    	(short)0xFEF4,
    	(short)0xFF31,
    	(short)0xFF7F,
    	(short)0xFFDF,
    	(short)0x0050,
    	(short)0x00D0,
    	(short)0x015E,
    	(short)0x01F8,
    	(short)0x029A,
    	(short)0x0341,
    	(short)0x03EA,
    	(short)0x0491,
    	(short)0x0532,
    	(short)0x05CA,
    	(short)0x0653,
    	(short)0x06CC,
    	(short)0x0731,
    	(short)0x077F,
    	(short)0x07B4,
    	(short)0x07CF,
    	(short)0x07CF,
    	(short)0x07B4,
    	(short)0x077F,
    	(short)0x0731,
    	(short)0x06CC,
    	(short)0x0653,
    	(short)0x05CA,
    	(short)0x0532,
    	(short)0x0491,
    	(short)0x03EA,
    	(short)0x0341,
    	(short)0x029A,
    	(short)0x01F8,
    	(short)0x015E,
    	(short)0x00D0,
    	(short)0x0050,
    	(short)0xFFDF,
    	(short)0xFF7F,
    	(short)0xFF31,
    	(short)0xFEF4,
    	(short)0xFEC8,
    	(short)0xFEAC,
    	(short)0xFE9F,
    	(short)0xFEA0,
    	(short)0xFEAC,
    	(short)0xFEC2,
    	(short)0xFEDF,
    	(short)0xFF02,
    	(short)0xFF27,
    	(short)0xFF4F,
    	(short)0xFF76,
    	(short)0xFF9C,
    	(short)0xFFBF,
    	(short)0xFFDE,
    	(short)0xFFF9,
    	(short)0x000F,
    	(short)0x0021,
    	(short)0x002E,
    	(short)0x0036,
    	(short)0x003B,
    	(short)0x003C,
    	(short)0x003B,
    	(short)0x0037,
    	(short)0x0032,
    	(short)0x002C,
    	(short)0x0025,
    	(short)0x001E,
    	(short)0x0017,
    	(short)0x0011,
    	(short)0x0015,
    and this is what i get from the website:
    Code:
    
    
    Code:
    /*
    	This is a very simple C++ implementation of a floating point FIR filter.
     */
    
    template <typename FloatType, int num_coeffs, const FloatType* coeffs>
    class FirFilter {
    public:
    	
      FirFilter():
          current_index_(0) {
        for(int i = 0; i < num_coeffs; ++i)
          history_[i] = 0.0;
      }
    	
      void put(FloatType value) {
        history_[current_index_++] = value;
        if(current_index_ == num_coeffs)
          current_index_ = 0;
      }
    
      FloatType get() {
        FloatType output = 0.0;
        int index = current_index_;
        for(int i = 0; i < num_coeffs; ++i) {
          if(index != 0) {
            --index;
          } else {
            index = num_coeffs - 1;
          }
          output += history_[index] * coeffs[i];
        }
        return output;
      }	
    
    private:
    	FloatType history_[num_coeffs];
    	int current_index_;
    };
    
    /* This is a test program that shows how to use the FirFilter class. */
    
    #include <iostream>
    #include <assert.h>
    #include <math.h>
    
    #define FILTER1_LENGTH 4
    
    float filter1_coeffs[FILTER1_LENGTH] = {
    	0.1,
    	0.2,
    	0.3,
    	0.4
    };
    
    bool approx_equal(float a, float b) {
    	return fabs(a-b) < 1e-6;
    }
    
    int main() {
    	FirFilter<float, FILTER1_LENGTH, filter1_coeffs> filter1;
    	
    	filter1.put(10.0);
    	assert(approx_equal(filter1.get(), 1));
    	filter1.put(10.0);
    	assert(approx_equal(filter1.get(), 3));
    	filter1.put(10.0);
    	assert(approx_equal(filter1.get(), 6));
    	filter1.put(10.0);
    	assert(approx_equal(filter1.get(), 10));
    	
    	filter1.put(0.0);
    	assert(approx_equal(filter1.get(), 9));
    	filter1.put(0.0);
    	assert(approx_equal(filter1.get(), 7));
    	filter1.put(0.0);
    	assert(approx_equal(filter1.get(), 4));
    	filter1.put(0.0);
    	assert(approx_equal(filter1.get(), 0));
    	
    	std::cout << "Tests OK." << std::endl;
    	
    	return 0;
    }

  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,645
    Quote Originally Posted by Racia View Post
    Hi all,

    I'm trying to include a few FIR filters into my program. I used the Tfilter website to design the filters however, it output the code but not the filter coefficients like what the FIR examples in arduino. Did i missed out any steps?
    Please tell us first what you wanted to do, Low-pass/bandpass , bass-band/ stop-band frequencies, ripple etc, i.e. all the parameters you put into TFilter website. With the information you provided I could not reproduce your data. (you could have presented the complete *.h file generated by TFilter)

    Note also the sample C++ code is NOT a generated C++ code of your filter design, it is ONLY a sample /template of how you could convert the provided C-code into C++. Note also that there are notational differences between C and C++ version (e.g. filter_taps vs filter_coeffs)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •