Alejandro Acuña
2024-08-12 1876e65234c20209001178705cfa50d8f9ded67a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// FIRFilter.h: interface for the FIRFilter class.
//
//////////////////////////////////////////////////////////////////////
 
#ifndef AFX_FIRFILTER_H__2606F3F5_42AA_4DB8_84F6_771D94AE518C__INCLUDED_
#define AFX_FIRFILTER_H__2606F3F5_42AA_4DB8_84F6_771D94AE518C__INCLUDED_
 
#include <vector>
#include <math.h>
 
template <class T>
class FIRFilter  
{
public:
    FIRFilter(double *coeffs, int numCoeffs)
    {
        if (numCoeffs > 0)
        {
            for (int i=0; i<numCoeffs; i++)
            {
                coeff.push_back(coeffs[i]);
                history.push_back(0);
            }
        }
        this->numCoeffs = numCoeffs;
        int bits = sizeof(T) * 8;
        maxValue =   pow(2, bits-1) - 1;
        minValue = - pow(2, bits-1);
    }
 
    virtual ~FIRFilter()
    {
    }
 
    int DoFIR(void *in, void *out, int numSamples)
    {
        T *input = (T*)in;
        T *output = (T*)out;
        for (int i=0; i<numSamples; i++)
        {
            history.insert(history.begin(), (long)(input[i]));
            history.pop_back();
            double result = 0;
            for (int j=0; j<numCoeffs; j++)
            {
                result += history[j] * coeff[j];
            }
            result = result > maxValue ? maxValue : result;
            result = result < minValue ? minValue : result;
            output[i] = (T)result;
        }
        return 0;
    }
 
private:
    std::vector<long> history;
    std::vector<double> coeff;
    int numCoeffs;
    double minValue, maxValue;
};
 
#endif // !defined(AFX_FIRFILTER_H__2606F3F5_42AA_4DB8_84F6_771D94AE518C__INCLUDED_)