Import upstream version 0.99.2
[fmit.git] / libs / Music / Filter.h
1 // Copyright 2007 "Gilles Degottex"
2
3 // This file is part of "Music"
4
5 // "Music" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
9 //
10 // "Music" is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
20 #ifndef _Filter_h_
21 #define _Filter_h_
22
23 #include <assert.h>
24 #include <vector>
25 #include <deque>
26
27 namespace Music
28 {
29     /**************************************************************************
30     * Parks-McClellan algorithm for FIR filter design (C version)
31     *-------------------------------------------------
32     *  Copyright (c) 1995,1998  Jake Janovetz (janovetz@uiuc.edu)
33     *
34     *  This library is free software; you can redistribute it and/or
35     *  modify it under the terms of the GNU Library General Public
36     *  License as published by the Free Software Foundation; either
37     *  version 2 of the License, or (at your option) any later version.
38     *
39     *  This library is distributed in the hope that it will be useful,
40     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
41     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
42     *  Library General Public License for more details.
43
44     *  You should have received a copy of the GNU Library General Public
45     *  License along with this library; if not, write to the Free
46     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
47     *
48     *************************************************************************/
49
50     #define BANDPASS       1
51     #define DIFFERENTIATOR 2
52     #define HILBERT        3
53
54     #define NEGATIVE       0
55     #define POSITIVE       1
56
57 //     #define Pi             3.1415926535897932
58 //     #define Pi2            6.2831853071795865
59
60     #define GRIDDENSITY    16
61     #define MAXITERATIONS  40
62
63     /* Function prototype for remez() - the only function that should need be
64     * called from external code
65     */
66     void remez(double h[], int numtaps,
67             int numband, double bands[], double des[], double weight[],
68             int type);
69
70         std::vector<double> fir1_lowpass(int n, double cutoff);
71         std::vector<double> fir1_highpass(int n, double cutoff);
72         std::vector<double> fir1_bandpass(int n, double low_cutoff, double high_cutoff);
73
74         /* Real-Time Filter
75         */
76         class RTFilter
77         {
78           public:
79                 virtual double operator()(double v)=0;
80
81                 virtual int getLength()=0;
82
83                 virtual ~RTFilter(){}
84         };
85
86         class FIRRTFilter : public RTFilter
87         {
88                 std::vector<double> m_imp_res;
89                 std::deque<double> m_to_filter;
90
91           public:
92                 FIRRTFilter(){m_imp_res=std::vector<double>(1, 1.0);}
93                 FIRRTFilter(std::vector<double>& imp_res);
94                 void setImpulseResponse(std::vector<double> imp_res)    {m_imp_res=imp_res;}
95
96                 virtual int getLength()                                                                 {return m_imp_res.size();}
97
98                 virtual double operator()(double v);
99         };
100
101         class RectangularHighPassRTFilter : public RTFilter
102         {
103                 int m_N;
104                 std::deque<double> m_summed_values;
105                 double m_sum;
106
107           public:
108                 RectangularHighPassRTFilter(int N=0);
109
110                 void reset(int N);
111                 virtual int getLength()                                                                 {return m_N;}
112
113                 virtual double operator()(double v);
114         };
115         class RectangularLowPassRTFilter : public RTFilter
116         {
117           public:
118                 virtual double operator()(double v);
119         };
120         class RectangularBandPassRTFilter : public RTFilter
121         {
122           public:
123                 virtual double operator()(double v);
124         };
125
126         class DummyRTFilter : public RTFilter
127         {
128           public:
129                 DummyRTFilter() {}
130
131                 virtual int getLength()                                                                 {return 1;}
132
133                 virtual double operator()(double v)     {return v;}
134         };
135 }
136
137 #endif // _Filter_h_