Nsound  0.9.4
FilterCombLowPassFeedback.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterCombLowPassFeedback.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>
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 & delay_time_seconds,
51  const float64 & feedback_gain,
52  const float64 & low_pass_frequency_Hz)
53  :
54  Filter(sample_rate),
55  delay_(NULL),
56  feedback_gain_(feedback_gain),
57  damp1_(0.0),
58  damp2_(0.0),
59  y_history_(0.0)
60 {
61  delay_ = new FilterDelay(sample_rate_, delay_time_seconds);
62 
64 
65  if(feedback_gain_ < 0.0)
66  {
67  feedback_gain_ = 0.0;
68  }
69 
70  if(feedback_gain_ >= 1.0)
71  {
72  feedback_gain_ = 0.999999;
73  }
74 
75  damp1_ = low_pass_frequency_Hz / sample_rate_;
76 
77  if(damp1_ > 0.5)
78  {
79  damp1_ = 0.5;
80  }
81 
82  damp2_ = 1.0 - damp1_;
83 }
84 
85 //-----------------------------------------------------------------------------
88  :
89  Filter(copy.sample_rate_),
90  delay_(new FilterDelay(*copy.delay_)),
91  feedback_gain_(copy.feedback_gain_),
92  damp1_(copy.damp1_),
93  damp2_(copy.damp2_),
94  y_history_(copy.y_history_)
95 {
96 }
97 
98 
99 //-----------------------------------------------------------------------------
102 {
103  delete delay_;
104 }
105 
108 filter(const AudioStream & x)
109 {
111  return Filter::filter(x);
112 }
113 
116 filter(const AudioStream & x, const Buffer & frequencies)
117 {
119  return Filter::filter(x, frequencies);
120 }
121 
122 Buffer
124 filter(const Buffer & x)
125 {
127  return Filter::filter(x);
128 }
129 
130 Buffer
132 filter(const Buffer & x, const Buffer & frequencies)
133 {
135  return Filter::filter(x, frequencies);
136 }
137 
138 float64
140 filter(const float64 & x)
141 {
143 
145 
146  return d;
147 }
148 
149 float64
151 filter(const float64 & x, const float64 & frequency_Hz)
152 {
153  damp1_ = frequency_Hz / sample_rate_;
154 
155  if(damp1_ > 0.5)
156  {
157  damp1_ = 0.5;
158  }
159 
160  damp2_ = 1.0 - damp1_;
161 
163 }
164 
165 //-----------------------------------------------------------------------------
169 {
170  *delay_ = *rhs.delay_;
172  damp1_ = rhs.damp1_;
173  damp2_ = rhs.damp2_;
174  y_history_ = rhs.y_history_;
175 
176  return *this;
177 }
178 
179 void
181 plot(boolean show_fc, boolean show_phase)
182 {
183  char title[128];
184  sprintf(title,
185  "Comb Low Pass Feedback Frequency Response\n"
186  "order = %d, fc = %0.1f Hz, sr = %0.1f Hz",
187  2,
189  sample_rate_);
190 
191  Filter::plot(show_phase);
192 
193  Plotter pylab;
194 
195  uint32 n_rows = 1;
196 
197  if(show_phase)
198  {
199  n_rows = 2;
200  }
201 
202  if(show_fc)
203  {
204  pylab.subplot(n_rows, 1, 1);
205 
206  pylab.axvline(damp1_ * sample_rate_,"color='red'");
207 
208  pylab.title(title);
209  }
210 }
211 
212 void
215 {
216  delay_->reset();
217  y_history_ = 0.0;
218 }
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
FilterCombLowPassFeedback(const float64 &sample_rate, const float64 &delay_time_seconds, const float64 &feedback_gain, const float64 &low_pass_frequency_Hz)
AudioStream filter(const AudioStream &x)
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
void plot(boolean show_fc=true, boolean show_phase=false)
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
FilterCombLowPassFeedback & operator=(const FilterCombLowPassFeedback &rhs)
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
void plot(boolean show_phase=false)
Definition: Filter.cc:262
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
A class for filtering audio in the frequecy domain.
A Buffer for storing audio samples.
Definition: Buffer.h:60
A class for filtering audio in the frequecy domain.
Definition: FilterDelay.h:47
float64 sample_rate_
Definition: Filter.h:113