Nsound  0.9.4
FilterBandPassFIR.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterBandPassFIR.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 kernel_size,
53  const float64 & frequency_Hz_low,
54  const float64 & frequency_Hz_high)
55  :
56  Filter(sample_rate),
57  low_(new FilterLowPassFIR(sample_rate, kernel_size, frequency_Hz_high)),
58  high_(new FilterHighPassFIR(sample_rate, kernel_size, frequency_Hz_low))
59 {
60  kernel_size_ = kernel_size * 2;
61 }
62 
63 //-----------------------------------------------------------------------------
66 {
67  delete low_;
68  delete high_;
69 }
70 
71 float64
74 {
75  return high_->getFrequency();
76 }
77 
78 float64
81 {
82  return low_->getFrequency();
83 }
84 
87 filter(const AudioStream & x)
88 {
89  return Filter::filter(x);
90 }
91 
94 filter(const AudioStream & x, const float64 & f_low, const float64 & f_high)
95 {
96  return low_->filter(high_->filter(x, f_low), f_high);
97 }
98 
102  const AudioStream & x,
103  const Buffer & f_low,
104  const Buffer & f_high)
105 {
106  return low_->filter(high_->filter(x,f_low), f_high);
107 }
108 
109 Buffer
111 filter(const Buffer & x)
112 {
113  return Filter::filter(x);
114 }
115 
116 Buffer
118 filter(const Buffer & x, const float64 & f_low, const float64 & f_high)
119 {
120  return low_->filter(high_->filter(x,f_low), f_high);
121 }
122 
123 Buffer
126  const Buffer & x,
127  const Buffer & f_low,
128  const Buffer & f_high)
129 {
130  return low_->filter(high_->filter(x, f_low), f_high);
131 }
132 
133 float64
135 filter(const float64 & x)
136 {
137  return low_->filter(high_->filter(x));
138 };
139 
140 float64
142 filter(const float64 & x, const float64 & frequency)
143 {
144  return low_->filter(high_->filter(x));
145 }
146 
147 float64
150  const float64 & x,
151  const float64 & f_low,
152  const float64 & f_high)
153 {
154  return low_->filter(high_->filter(x, f_low), f_high);
155 }
156 
157 void
159 plot(boolean show_fc,boolean show_phase)
160 {
161  char title[128];
162  sprintf(title,
163  "Band Pass FIR Frequency Response\n"
164  "order = %d, fl = %0.1f Hz, fl = %0.1f Hz, sr = %0.1f Hz",
165  low_->getKernelSize() - 1,
166  high_->getFrequency(),
167  low_->getFrequency(),
168  sample_rate_);
169 
170  Filter::plot(show_phase);
171 
172  Plotter pylab;
173 
174  uint32 n_rows = 1;
175 
176  if(show_phase)
177  {
178  n_rows = 2;
179  }
180 
181  if(show_fc)
182  {
183  pylab.subplot(n_rows, 1, 1);
184 
185  pylab.axvline(low_->getFrequency(), "color='red'");
186  pylab.axvline(high_->getFrequency(), "color='red'");
187 
188  pylab.title(title);
189  }
190 }
191 
192 void
195 {
196  low_->reset();
197  high_->reset();
198 }
199 
unsigned int uint32
Definition: Nsound.h:153
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
float64 getFrequencyHigh() const
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
AudioStream filter(const AudioStream &x)
double float64
Definition: Nsound.h:146
FilterHighPassFIR * high_
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
float64 getFrequency() const
uint32 kernel_size_
Definition: Filter.h:116
void plot(boolean show_phase=false)
Definition: Filter.cc:262
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
FilterBandPassFIR(const float64 &sample_rate, uint32 kernel_size, const float64 &frequency_Hz_low, const float64 &frequency_Hz_high)
virtual uint32 getKernelSize() const
Definition: Filter.h:96
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 getFrequency() const
void plot(boolean show_fc=true, boolean show_phase=false)
AudioStream filter(const AudioStream &x)
AudioStream filter(const AudioStream &x)
float64 getFrequencyLow() const
float64 sample_rate_
Definition: Filter.h:113