Nsound  0.9.4
FilterBandRejectIIR.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterBandRejectIIR.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 
50  const float64 & sample_rate,
51  uint32 n_poles,
52  const float64 & frequency_Hz_low,
53  const float64 & frequency_Hz_high,
54  const float64 & percent_ripple)
55  :
56  Filter(sample_rate),
57  low_(new FilterLowPassIIR(
58  sample_rate,
59  n_poles,
60  frequency_Hz_low,
61  percent_ripple)),
62  high_(new FilterHighPassIIR(
63  sample_rate,
64  n_poles,
65  frequency_Hz_high,
66  percent_ripple))
67 {
68  reset();
69 
70  kernel_size_ = n_poles;
71 }
72 
75 {
76  delete low_;
77  delete high_;
78 }
79 
80 float64
83 {
84  return low_->getFrequency();
85 }
86 
87 float64
90 {
91  return high_->getFrequency();
92 }
93 
96 filter(const AudioStream & x)
97 {
98  return Filter::filter(x);
99 }
100 
103 filter(const AudioStream & x, const float64 & f)
104 {
105  return Filter::filter(x);
106 }
107 
110 filter(const AudioStream & x, const Buffer & frequencies)
111 {
112  return Filter::filter(x);
113 }
114 
118  const AudioStream & x,
119  const float64 & f_low,
120  const float64 & f_high)
121 {
122  if(!is_realtime_) reset();
123 
124  return low_->filter(x, f_low)
125  + high_->filter(x, f_high);
126 }
127 
131  const AudioStream & x,
132  const Buffer & frequencies_Hz_low,
133  const Buffer & frequencies_Hz_high)
134 {
135  if(!is_realtime_) reset();
136 
137  return low_->filter(x, frequencies_Hz_low)
138  + high_->filter(x, frequencies_Hz_high);
139 }
140 
141 Buffer
143 filter(const Buffer & x)
144 {
145  return Filter::filter(x);
146 }
147 
148 Buffer
150 filter(const Buffer & x, const float64 & f)
151 {
152  return Filter::filter(x);
153 }
154 
155 Buffer
157 filter(const Buffer & x, const Buffer & frequencies)
158 {
159  return Filter::filter(x);
160 }
161 
162 Buffer
165  const Buffer & x,
166  const float64 & f_low,
167  const float64 & f_high)
168 {
169  if(!is_realtime_) reset();
170 
171  return low_->filter(x, f_low)
172  + high_->filter(x, f_high);
173 }
174 
175 Buffer
178  const Buffer & x,
179  const Buffer & frequencies_Hz_low,
180  const Buffer & frequencies_Hz_high)
181 {
182  if(!is_realtime_) reset();
183 
184  uint32 n_high_freqs = frequencies_Hz_high.getLength();
185  uint32 n_low_freqs = frequencies_Hz_low.getLength();
186 
187  Buffer y;
188 
189  uint32 x_samples = x.getLength();
190 
191  for(uint32 i = 0; i < x_samples; ++i)
192  {
194  x[i],
195  frequencies_Hz_low [i % n_low_freqs],
196  frequencies_Hz_high[i % n_high_freqs]);
197  }
198 
199  return y;
200 }
201 
202 float64
204 filter(const float64 & x)
205 {
206  return low_->filter(x) + high_->filter(x);
207 };
208 
209 float64
211 filter(const float64 & x, const float64 & frequency)
212 {
213  return FilterBandRejectIIR::filter(x);
214 }
215 
216 float64
219  const float64 & x,
220  const float64 & frequencies_Hz_low,
221  const float64 & frequencies_Hz_high)
222 {
223  return low_->filter(x, frequencies_Hz_low)
224  + high_->filter(x, frequencies_Hz_high);
225 }
226 
227 void
229 plot(boolean show_fc, boolean show_phase)
230 {
231  char title[128];
232  sprintf(title,
233  "Band Reject IIR Frequency Response\n"
234  "order = %d, fl = %0.1f Hz, fl = %0.1f Hz, sr = %0.1f Hz",
235  low_->getKernelSize() - 1,
236  low_->getFrequency(),
237  high_->getFrequency(),
238  sample_rate_);
239 
240  Filter::plot(show_phase);
241 
242  Plotter pylab;
243 
244  uint32 n_rows = 1;
245 
246  if(show_phase)
247  {
248  n_rows = 2;
249  }
250 
251  if(show_fc)
252  {
253  pylab.subplot(n_rows, 1, 1);
254 
255  pylab.axvline(low_->getFrequency(),"color='red'");
256  pylab.axvline(high_->getFrequency(),"color='red'");
257 
258  pylab.title(title);
259  }
260 }
261 
262 void
265 {
266  low_->reset();
267  high_->reset();
268 }
269 
unsigned int uint32
Definition: Nsound.h:153
float64 getFrequency() const
AudioStream filter(const AudioStream &x)
void axvline(const float64 &x_pos=0.0, const std::string &kwargs="")
Draws a vertical line at x and spans ymin to ymax (ralitive).
Definition: Plotter.cc:358
void title(const std::string &title, const std::string &kwargs="")
Add a title to the plot at the top and centered.
Definition: Plotter.cc:1127
float64 getFrequency() const
double float64
Definition: Nsound.h:146
A class for filtering audio in the frequecy domain.
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
Base class for IIR Filters, defines the interface.
Definition: Filter.h:49
Axes subplot(const uint32 n_rows, const uint32 n_cols, const uint32 n, const std::string &kwargs="", Axes *sharex=NULL, Axes *sharey=NULL)
Creates a figure in a subplot, subplot(A, B, C, **kwargs)
Definition: Plotter.cc:1031
FilterBandRejectIIR(const float64 &sample_rate, uint32 n_poles, const float64 &frequency_Hz_low, const float64 &frequency_Hz_high, const float64 &percent_ripple=0.0)
uint32 kernel_size_
Definition: Filter.h:116
A class for filtering audio in the frequecy domain.
void plot(boolean show_fc=true, boolean show_phase=false)
void plot(boolean show_phase=false)
Definition: Filter.cc:262
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
AudioStream filter(const AudioStream &x)
bool is_realtime_
Definition: Filter.h:118
virtual uint32 getKernelSize() const
Definition: Filter.h:96
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioStream filter(const AudioStream &x)
float64 sample_rate_
Definition: Filter.h:113