Nsound  0.9.4
FilterBandPassIIR.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterBandPassIIR.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2006 Nick Hilton
6 //
7 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
8 //
9 //-----------------------------------------------------------------------------
10 
11 //-----------------------------------------------------------------------------
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 //
27 //-----------------------------------------------------------------------------
28 
29 #include <Nsound/AudioStream.h>
30 #include <Nsound/Buffer.h>
31 #include <Nsound/FFTransform.h>
35 #include <Nsound/Plotter.h>
36 
37 #include <cmath>
38 #include <cstdio>
39 #include <iostream>
40 
41 using namespace Nsound;
42 
43 using std::cerr;
44 using std::endl;
45 
46 #define CERR_HEADER __FILE__ << ":" << __LINE__ << ": "
47 
48 //-----------------------------------------------------------------------------
51  const float64 & sample_rate,
52  uint32 n_poles,
53  const float64 & frequency_Hz_low,
54  const float64 & frequency_Hz_high,
55  const float64 & percent_ripple)
56  :
57  Filter(sample_rate),
58  low_(new FilterLowPassIIR(
59  sample_rate,
60  n_poles,
61  frequency_Hz_high,
62  percent_ripple)),
63  high_(new FilterHighPassIIR(
64  sample_rate,
65  n_poles,
66  frequency_Hz_low,
67  percent_ripple)),
68  gain_(1.0)
69 {
70  reset();
71 
73 
75 
76  gain_ = 1.0 / response.getMax();
77 }
78 
79 //-----------------------------------------------------------------------------
82 {
83  delete low_;
84  delete high_;
85 }
86 
87 float64
90 {
91  return high_->getFrequency();
92 }
93 
94 float64
97 {
98  return low_->getFrequency();
99 }
100 
103 filter(const AudioStream & x)
104 {
105  return Filter::filter(x);
106 }
107 
110 filter(const AudioStream & x, const float64 & f)
111 {
112  return Filter::filter(x);
113 }
114 
117 filter(const AudioStream & x, const Buffer & frequencies)
118 {
119  return Filter::filter(x);
120 }
121 
124 filter(const AudioStream & x, const float64 & f_low, const float64 & f_high)
125 {
126  return low_->filter(high_->filter(x, f_low), f_high);
127 }
128 
132  const AudioStream & x,
133  const Buffer & f_low,
134  const Buffer & f_high)
135 {
136  return low_->filter(high_->filter(x, f_low), f_high);
137 }
138 
139 Buffer
141 filter(const Buffer & x)
142 {
143  return Filter::filter(x);
144 }
145 
146 Buffer
148 filter(const Buffer & x, const float64 & f)
149 {
150  return Filter::filter(x);
151 }
152 
153 Buffer
155 filter(const Buffer & x, const Buffer & frequencies)
156 {
157  return Filter::filter(x);
158 }
159 
160 Buffer
162 filter(const Buffer & x, const float64 & f_low, const float64 & f_high)
163 {
164  return low_->filter(high_->filter(x,f_low), f_high);
165 }
166 
167 Buffer
170  const Buffer & x,
171  const Buffer & f_low,
172  const Buffer & f_high)
173 {
174  return low_->filter(high_->filter(x, f_low), f_high);
175 }
176 
177 float64
179 filter(const float64 & x)
180 {
181  return gain_ * low_->filter(high_->filter(x));
182 };
183 
184 float64
186 filter(const float64 & x, const float64 & frequency)
187 {
188  return gain_ * low_->filter(high_->filter(x));
189 }
190 
191 float64
194  const float64 & x,
195  const float64 & frequencies_Hz_low,
196  const float64 & frequencies_Hz_high)
197 {
198  return gain_ * low_->filter(
199  high_->filter(x,frequencies_Hz_low), frequencies_Hz_high);
200 }
201 
202 void
204 plot(boolean show_fc, boolean show_phase)
205 {
206  char title[256];
207  sprintf(title,
208  "Band Pass IIR Frequency Response\n"
209  "order = %d, fl = %0.1f Hz, fh = %0.1f Hz, sr = %0.1f Hz",
210  low_->getKernelSize() * 2,
211  high_->getFrequency(),
212  low_->getFrequency(),
213  sample_rate_);
214 
215  Plotter pylab;
216 
217  uint32 n_rows = 1;
218 
219  if(show_phase)
220  {
221  n_rows = 2;
222  }
223 
224  Filter::plot(show_phase);
225 
226  if(show_fc)
227  {
228  pylab.subplot(n_rows, 1, 1);
229 
230  pylab.axvline(low_->getFrequency(),"color='red'");
231  pylab.axvline(high_->getFrequency(),"color='red'");
232 
233  pylab.title(title);
234  }
235 }
236 
237 void
240 {
241  low_->reset();
242  high_->reset();
243 }
244 
unsigned int uint32
Definition: Nsound.h:153
float64 getFrequency() const
float64 getFrequencyHigh() const
AudioStream filter(const AudioStream &x)
void plot(boolean show_fc=true, boolean show_phase=false)
FilterHighPassIIR * high_
float64 getFrequency() const
double float64
Definition: Nsound.h:146
A class for filtering audio in the frequecy domain.
Base class for IIR Filters, defines the interface.
Definition: Filter.h:49
uint32 kernel_size_
Definition: Filter.h:116
A class for filtering audio in the frequecy domain.
void plot(boolean show_phase=false)
Definition: Filter.cc:262
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
FilterBandPassIIR(const float64 &sample_rate, uint32 kernel_size, const float64 &frequency_Hz_low, const float64 &frequency_Hz_high, const float64 &percent_ripple=0.0)
virtual uint32 getKernelSize() const
Definition: Filter.h:96
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 getFrequencyLow() const
AudioStream filter(const AudioStream &x)
Buffer getFrequencyResponse(const uint32 n_fft=8192)
Definition: Filter.cc:210
AudioStream filter(const AudioStream &x)
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
float64 sample_rate_
Definition: Filter.h:113