Nsound  0.9.4
FilterBandPassVocoder.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterBandPassVocoder.cc 878 2014-11-23 04:51:23Z weegreenblobbie $
4 //
5 // Copyright (c) 2009 to Present 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>
33 #include <Nsound/Plotter.h>
34 
35 #include <cmath>
36 #include <cstdio>
37 #include <iostream>
38 
39 using namespace Nsound;
40 
41 using std::cerr;
42 using std::endl;
43 
44 #define CERR_HEADER __FILE__ << ":" << __LINE__ << ": "
45 
46 //-----------------------------------------------------------------------------
49  const float64 & sample_rate,
50  const float64 & frequency_low,
51  const float64 & frequency_high)
52  :
53  Filter(sample_rate),
54  frequency_low_(frequency_low),
55  frequency_high_(frequency_high),
56  omega2_(0.0),
57  f_(0.0),
58  att_(0.0),
59  low1_(0.0),
60  low2_(0.0),
61  mid1_(0.0),
62  mid2_(0.0),
63  high1_(0.0),
64  high2_(0.0)
65 {
66  reset();
67 }
68 
69 //-----------------------------------------------------------------------------
72 {
73 }
74 
77 filter(const AudioStream & x)
78 {
79  return Filter::filter(x);
80 }
81 
84 filter(const AudioStream & x, const Buffer & frequencies)
85 {
86  return Filter::filter(x);
87 }
88 
92  const AudioStream & x,
93  const Buffer & frequencies_Hz_low,
94  const Buffer & frequencies_Hz_high)
95 {
96  uint32 n_channels = x.getNChannels();
97 
98  AudioStream y(x.getSampleRate(), n_channels);
99 
100  for(uint32 channel = 0; channel < n_channels; ++channel)
101  {
102  y[channel] = FilterBandPassVocoder::filter(
103  x[channel],
104  frequencies_Hz_low,
105  frequencies_Hz_high);
106  }
107 
108  return y;
109 }
110 
111 Buffer
113 filter(const Buffer & x)
114 {
115  return Filter::filter(x);
116 }
117 
118 Buffer
120 filter(const Buffer & x, const Buffer & frequencies)
121 {
122  return Filter::filter(x);
123 }
124 
125 Buffer
128  const Buffer & x,
129  const Buffer & frequencies_Hz_low,
130  const Buffer & frequencies_Hz_high)
131 {
132  Buffer::const_circular_iterator low = frequencies_Hz_low.cbegin();
133  Buffer::const_circular_iterator high = frequencies_Hz_high.cbegin();
134 
135  Buffer y;
136 
137  for(uint32 i = 0; i < x.getLength(); ++i, ++low, ++high)
138  {
139  y << FilterBandPassVocoder::filter(x[i], *low, *high);
140  }
141 
142  return y;
143 }
144 
145 float64
147 filter(const float64 & x)
148 {
149  high1_ = x - f_ * mid1_ - low1_;
150  mid1_ += high1_ * omega2_;
151  low1_ += mid1_;
152 
153  high2_ = low1_ - f_ * mid2_ - low2_;
154  mid2_ += high2_ * omega2_;
155  low2_ += mid2_;
156 
157  return high2_ * att_;
158 };
159 
160 float64
162 filter(const float64 & x, const float64 & frequency)
163 {
165 }
166 
167 float64
170  const float64 & x,
171  const float64 & frequencies_Hz_low,
172  const float64 & frequencies_Hz_high)
173 {
174  makeKernel(frequencies_Hz_low, frequencies_Hz_high);
176 }
177 
178 void
180 plot(boolean show_fc, boolean show_phase)
181 {
182  char title[256];
183  sprintf(title,
184  "Band Pass IIR Frequency Response\n"
185  "fl = %0.1f Hz, fh = %01f Hz, omega2 = %0.1f Hz, f_ = %0.1f Hz, att_ = %0.1f",
188  omega2_,
189  f_,
190  att_);
191 
192  Filter::plot(show_phase);
193 
194  Plotter pylab;
195 
196  uint32 n_rows = 1;
197 
198  if(show_phase)
199  {
200  n_rows = 2;
201  }
202 
203  if(show_fc)
204  {
205  pylab.subplot(n_rows, 1, 1);
206 
207  pylab.axvline(frequency_low_,"color='red'");
208  pylab.axvline(frequency_high_,"color='red'");
209 
210  pylab.title(title);
211  }
212 }
213 
214 void
217 {
218  low1_ =
219  low2_ =
220  mid1_ =
221  mid2_ =
222  high1_ =
223  high2_ = 0.0;
224 
226 }
227 
228 void
230 makeKernel(const float64 & f_low, const float64 & f_high)
231 {
232  float64 fc = f_high - 0.5 * (f_high - f_low);
233 
234  float64 omega = fc * 2.0 * M_PI / sample_rate_;
235 
236  omega2_ = omega * omega;
237 
238  f_ = 0.4 / omega;
239 
240  att_ = 1.0 / (6.0 + ((std::exp(fc / sample_rate_) - 1.0) * 10.0));
241 }
unsigned int uint32
Definition: Nsound.h:153
FilterBandPassVocoder(const float64 &sample_rate, const float64 &frequency_low, const float64 &frequency_high)
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
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
#define M_PI
Definition: Nsound.h:121
void plot(boolean show_fc=true, boolean show_phase=false)
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
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
AudioStream filter(const AudioStream &x)
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
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
void plot(boolean show_phase=false)
Definition: Filter.cc:262
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
A Buffer for storing audio samples.
Definition: Buffer.h:60
void makeKernel(const float64 &f_low, const float64 &f_high)
float64 sample_rate_
Definition: Filter.h:113