Nsound  0.9.4
FilterTone.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterTone.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2007 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/FilterTone.h>
32 #include <Nsound/Plotter.h>
33 
34 #include <cmath>
35 #include <cstdio>
36 #include <iostream>
37 
38 using namespace Nsound;
39 
40 //-----------------------------------------------------------------------------
42 FilterTone(const float64 & sample_rate, const float64 & half_power_frequency)
43  :
44  Filter(sample_rate),
45  hp_frequency_(half_power_frequency),
46  b_(0.0),
47  a_(0.0),
48  last_output_(0.0),
49  kernel_cache_()
50 {
51  reset();
52 }
53 
54 //-----------------------------------------------------------------------------
57 {
58 
59 }
60 
63 filter(const AudioStream & x)
64 {
65  return Filter::filter(x);
66 }
67 
70 filter(const AudioStream & x, const Buffer & hp_frequencies)
71 {
72  return Filter::filter(x, hp_frequencies);
73 }
74 
75 Buffer
77 filter(const Buffer & x)
78 {
79  return Filter::filter(x);
80 }
81 
82 Buffer
84 filter(const Buffer & x, const Buffer & hp_frequencies)
85 {
86  return Filter::filter(x, hp_frequencies);
87 }
88 
89 float64
91 filter(const float64 & x)
92 {
93  float64 y = b_ * x - a_ * last_output_;
94 
95  last_output_ = y;
96 
97  return y;
98 }
99 
100 float64
102 filter(const float64 & x, const float64 & hp_frequency)
103 {
104  makeKernel(hp_frequency);
105 
106  return FilterTone::filter(x);
107 }
108 
109 void
111 makeKernel(const float64 & half_power_frequency)
112 {
114  Kernel new_kernel(static_cast<uint32>(half_power_frequency * 10));
115 
116  // See if the kernel is in the cache.
117  KernelCache::const_iterator itor = kernel_cache_.find(new_kernel);
118 
119  if(itor != kernel_cache_.end())
120  {
121  // The kernel was found in the cache.
122  b_ = itor->b_;
123  a_ = itor->a_;
124  return;
125  }
126 
127  // The filter wasn't in the cache, need to make it.
128 
129  float64 temp = 2.0 - ::cos(two_pi_over_sample_rate_ * half_power_frequency);
130 
131  a_ = -1.0 * (temp - ::sqrt(temp * temp - 1.0));
132  b_ = 1.0 + a_;
133 
134  new_kernel.b_ = b_;
135  new_kernel.a_ = a_;
136 
137  kernel_cache_.insert(new_kernel);
138 }
139 
140 void
142 plot(boolean show_fc, boolean show_phase)
143 {
144  char title[128];
145  sprintf(title,
146  "Tone Filter Frequency Response\n"
147  "order = %d, fc = %0.1f Hz, sr = %0.1f Hz",
149 
150  Filter::plot(show_phase);
151 
152  Plotter pylab;
153 
154  uint32 n_rows = 1;
155 
156  if(show_phase)
157  {
158  n_rows = 2;
159  }
160 
161  if(show_fc)
162  {
163  pylab.subplot(n_rows, 1, 1);
164 
165  pylab.axvline(hp_frequency_,"color='red'");
166 
167  pylab.title(title);
168  }
169 
170 }
171 
172 void
175 {
176  last_output_ = 0.0;
178 }
179 
180 //-----------------------------------------------------------------------------
182 Kernel::
183 Kernel(const uint32 & frequency)
184  :
185  b_(0.0),
186  a_(0.0),
187  frequency_(frequency)
188 {
189 }
190 
191 bool
193 Kernel::
194 operator<(const Kernel & rhs) const
195 {
196  return frequency_ < rhs.frequency_;
197 }
198 
unsigned int uint32
Definition: Nsound.h:153
Kernel(const uint32 &frequency)
Definition: FilterTone.cc:183
void makeKernel(const float64 &half_power_frequency)
Definition: FilterTone.cc:111
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
FilterTone(const float64 &sample_rate, const float64 &half_power_frequency)
Definition: FilterTone.cc:42
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
double float64
Definition: Nsound.h:146
bool operator<(const Kernel &rhs) const
Definition: FilterTone.cc:194
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
KernelCache kernel_cache_
Definition: FilterTone.h:114
void plot(boolean show_phase=false)
Definition: Filter.cc:262
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
void plot(boolean show_fc=true, boolean show_phase=false)
Definition: FilterTone.cc:142
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioStream filter(const AudioStream &x)
Definition: FilterTone.cc:63
virtual ~FilterTone()
Definition: FilterTone.cc:56
A class to store calculated kernels.
Definition: FilterTone.h:100
float64 two_pi_over_sample_rate_
Definition: Filter.h:114
float64 last_output_
Definition: FilterTone.h:97
float64 hp_frequency_
Definition: FilterTone.h:94
float64 sample_rate_
Definition: Filter.h:113