1 // Copyright 2007 "Gilles Degottex"
3 // This file is part of "Music"
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.
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.
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
21 // #include <iostream>
23 #include <CppAddons/CAMath.h>
27 // b = fir1(32,[0.00001 0.23]);
28 vector<double> Music::fir1_lowpass(int n, double cutoff)
34 for(size_t i=0; i<b.size(); i++)
35 b[i] = cutoff*sinc(cutoff*(i-d));
40 vector<double> Music::fir1_highpass(int n, double cutoff)
47 for(size_t i=0; i<b.size(); i++)
49 b[i] = cutoff*sinc(cutoff*(i-d));
53 for(size_t i=0; i<b.size(); i++)
60 vector<double> Music::fir1_bandpass(int n, double low_cutoff, double high_cutoff)
62 vector<double> b(n, 0.0);
64 if(low_cutoff>high_cutoff)
67 vector<double> lowf = fir1_lowpass(n, low_cutoff);
68 vector<double> highf = fir1_highpass(n, high_cutoff);
70 return conv(lowf, highf);
73 Music::FIRRTFilter::FIRRTFilter(std::vector<double>& imp_res)
75 assert(imp_res.size()>0);
78 // m_to_filter.reserve(imp_res.size());
81 double Music::FIRRTFilter::operator()(double v)
85 m_to_filter.push_front(v);
87 if(m_to_filter.size()>=m_imp_res.size())
90 for(size_t i=0; i<m_imp_res.size(); i++)
91 value += m_imp_res[i]*m_to_filter[i];
94 // m_queue.push_front(value);
97 m_to_filter.pop_back();
103 Music::RectangularHighPassRTFilter::RectangularHighPassRTFilter(int N)
107 void Music::RectangularHighPassRTFilter::reset(int N)
113 m_summed_values.clear();
115 double Music::RectangularHighPassRTFilter::operator()(double v)
117 m_summed_values.push_front(v);
119 while(int(m_summed_values.size())>m_N)
121 m_sum -= m_summed_values.back();
123 m_summed_values.pop_back();
126 return m_summed_values[m_summed_values.size()/2] - m_sum/m_summed_values.size();
129 double Music::RectangularLowPassRTFilter::operator()(double v)
134 double Music::RectangularBandPassRTFilter::operator()(double v)
141 Type : biquad, tweaked butterworthReferences : Posted by Patrice TarrabiaCode : www.musicdsp.org
142 r = rez amount, from sqrt(2) to ~ 0.1
144 (from ~0 Hz to SampleRate/2 - though many
145 synths seem to filter only up to SampleRate/4)
148 out(n) = a1 * in + a2 * in(n-1) + a3 * in(n-2) - b1*out(n-1) - b2*out(n-2)
151 c = 1.0 / tan(pi * f / sample_rate);
153 a1 = 1.0 / ( 1.0 + r * c + c * c);
156 b1 = 2.0 * ( 1.0 - c*c) * a1;
157 b2 = ( 1.0 - r * c + c * c) * a1;
160 c = tan(pi * f / sample_rate);
162 a1 = 1.0 / ( 1.0 + r * c + c * c);
165 b1 = 2.0 * ( c*c - 1.0) * a1;
166 b2 = ( 1.0 - r * c + c * c) * a1;