Nsound  0.9.4
FilterAllPass.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterAllPass.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/FilterAllPass.h>
32 #include <Nsound/FilterDelay.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 & max_delay_time_seconds,
51  const float64 & gain)
52  :
53  Filter(sample_rate),
54  x_delay_(NULL),
55  y_delay_(NULL),
56  gain_(gain),
57  y_history_(0.0)
58 {
59  x_delay_ = new FilterDelay(sample_rate_, max_delay_time_seconds);
60  y_delay_ = new FilterDelay(sample_rate_, max_delay_time_seconds);
61 
63 }
64 
65 //-----------------------------------------------------------------------------
68  :
69  Filter(copy.sample_rate_),
70  x_delay_(NULL),
71  y_delay_(NULL),
72  gain_(copy.gain_),
73  y_history_(0.0)
74 {
75  x_delay_ = new FilterDelay(*copy.x_delay_);
76  y_delay_ = new FilterDelay(*copy.y_delay_);
77 
78  *this = copy;
79 }
80 
81 //-----------------------------------------------------------------------------
84 {
85  delete x_delay_;
86  delete y_delay_;
87 }
88 
91 filter(const AudioStream & x)
92 {
93  return Filter::filter(x);
94 }
95 
98 filter(const AudioStream & x, const float64 & delay_in_seconds)
99 {
100  return Filter::filter(x, delay_in_seconds);
101 }
102 
105 filter(const AudioStream & x, const Buffer & delay_in_seconds)
106 {
107  return Filter::filter(x, delay_in_seconds);
108 }
109 
110 Buffer
112 filter(const Buffer & x)
113 {
114  return Filter::filter(x);
115 }
116 
117 Buffer
119 filter(const Buffer & x, const float64 & delay_in_seconds)
120 {
121  return Filter::filter(x, delay_in_seconds);
122 }
123 
124 Buffer
126 filter(const Buffer & x, const Buffer & delay_in_seconds)
127 {
128  return Filter::filter(x, delay_in_seconds);
129 }
130 
131 float64
133 filter(const float64 & x)
134 {
135  float64 y = gain_ * x + x_delay_->filter(x)
137 
138  y_history_ = y;
139 
140  return y;
141 }
142 
143 float64
145 filter(const float64 & x, const float64 & delay)
146 {
147  float64 y = gain_ * x + x_delay_->filter(x, delay)
148  - gain_ * y_delay_->filter(y_history_, delay);
149 
150  y_history_ = y;
151 
152  return y;
153 }
154 
155 //-----------------------------------------------------------------------------
159 {
160  if(this == &rhs)
161  {
162  return *this;
163  }
164 
165  *x_delay_ = *rhs.x_delay_;
166  *y_delay_ = *rhs.y_delay_;
167 
168  gain_ = rhs.gain_;
169  y_history_ = rhs.y_history_;
170 
171  return *this;
172 }
173 
174 void
176 plot(boolean show_fc, boolean show_phase)
177 {
178  char title[128];
179  sprintf(title,
180  "All Pass Frequency Response\n"
181  "sr = %0.1f Hz",
182  sample_rate_);
183 
184  Filter::plot(show_phase);
185 
186  Plotter pylab;
187 
188  uint32 n_rows = 1;
189  uint32 n_cols = 1;
190 
191  if(show_phase)
192  {
193  n_rows = 2;
194  }
195 
196  if(show_fc)
197  {
198  pylab.subplot(n_rows, n_cols, 1);
199 
200  pylab.title(title);
201  }
202 }
203 
204 void
207 {
208  x_delay_->reset();
209  y_delay_->reset();
210  y_history_ = 0.0;
211 }
unsigned int uint32
Definition: Nsound.h:153
FilterAllPass(const float64 &sample_rate, const float64 &max_delay_time_seconds, const float64 &gain)
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
AudioStream filter(const AudioStream &x)
Definition: FilterDelay.cc:98
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
FilterAllPass & operator=(const FilterAllPass &rhs)
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
FilterDelay * y_delay_
Definition: FilterAllPass.h:97
FilterDelay * x_delay_
Definition: FilterAllPass.h:96
A Buffer for storing audio samples.
Definition: Buffer.h:60
A class for filtering audio in the frequecy domain.
Definition: FilterDelay.h:47
AudioStream filter(const AudioStream &x)
float64 sample_rate_
Definition: Filter.h:113