Nsound  0.9.4
Public Types | Public Member Functions | Protected Attributes | Private Member Functions | List of all members
Nsound::FilterParametricEqualizer Class Reference

Implementation of Zoelzer's parametric equalizer filters, with some modifications by the author. More...

#include <Nsound/FilterParametricEqualizer.h>

Inheritance diagram for Nsound::FilterParametricEqualizer:
Inheritance graph
[legend]

Public Types

enum  Type { PEAKING, LOW_SHELF, HIGH_SHELF }
 

Public Member Functions

 FilterParametricEqualizer (const Type &type, const float64 &sample_rate, const float64 &frequency, const float64 &resonance=0.707106781187, const float64 &boost_dB=0.0)
 boost is in dB More...
 
virtual ~FilterParametricEqualizer ()
 
AudioStream filter (const AudioStream &x)
 
AudioStream filter (const AudioStream &x, const Buffer &frequencies)
 
AudioStream filter (const AudioStream &x, const Buffer &frequencies, const Buffer &resonance)
 
AudioStream filter (const AudioStream &x, const Buffer &frequencies, const Buffer &resonance, const Buffer &boost_dB)
 boost is in dB More...
 
Buffer filter (const Buffer &x)
 
Buffer filter (const Buffer &x, const Buffer &frequencies)
 
Buffer filter (const Buffer &x, const Buffer &frequencies, const Buffer &resonance)
 
Buffer filter (const Buffer &x, const Buffer &frequencies, const Buffer &resonance, const Buffer &boost_dB)
 boost is in dB More...
 
float64 filter (const float64 &x)
 
float64 filter (const float64 &x, const float64 &frequency)
 
float64 filter (const float64 &x, const float64 &frequency, const float64 &resonance)
 boost is in dB More...
 
float64 filter (const float64 &x, const float64 &frequency, const float64 &resonance, const float64 &boost_dB)
 boost is in dB More...
 
void makeKernel (const float64 &frequency, const float64 &resonance, const float64 &boost)
 
void plot (boolean show_fc=true, boolean show_phase=false)
 
void reset ()
 
void setRealtime (bool flag)
 
AudioStream filter (const AudioStream &x, const float64 &frequency)
 
Buffer filter (const Buffer &x, const float64 &frequency)
 
Buffer getFrequencyAxis (const uint32 n_fft=8192)
 
Buffer getFrequencyResponse (const uint32 n_fft=8192)
 
Buffer getImpulseResponse (const uint32 n_samples=8192)
 
virtual uint32 getKernelSize () const
 
Buffer getPhaseResponse ()
 
float64 getSampleRate () const
 
void plot (boolean show_phase=false)
 

Protected Attributes

Type type_
 
float64 frequency_
 
float64 resonance_
 
float64 boost_
 
float64a_
 
float64b_
 
float64x_history_
 
float64x_ptr_
 
float64x_end_ptr_
 
float64y_history_
 
float64y_ptr_
 
float64y_end_ptr_
 
KernelCache kernel_cache_
 
float64 sample_rate_
 
float64 two_pi_over_sample_rate_
 
float64 sample_time_
 
uint32 kernel_size_
 
bool is_realtime_
 

Private Member Functions

 FilterParametricEqualizer (const FilterParametricEqualizer &copy)
 
FilterParametricEqualizeroperator= (const FilterParametricEqualizer &rhs)
 

Detailed Description

Implementation of Zoelzer's parametric equalizer filters, with some modifications by the author.

Below are the formulas for the 3 different types of parametric equalizer filters.

f = Frequency in Hz sr = Sample Rate in samples per second V = Percent boost (1.0 = no boost, < 1.0 = cut signal, > 1.0 boost signal) Q = Resonance or Q of the filter, sqrt(1/2) == no resonance

omega = 2*pi*f/sr K = tan(omega/2)

a0 = 1 + sqrt(2*V)*K + V*K^2 a1 = 2*(V*K^2 - 1) a2 = 1 - sqrt(2*V)*K + V*K^2

b0 = 1 + K/Q + K^2 b1 = 2*(K^2 - 1) b2 = 1 - K/Q + K^2

The formula for the High Shelf filter is:

omega = 2*pi*f/sr K = tan((pi-omega)/2)

a0 = 1 + sqrt(2*V)*K + V*K^2 a1 = -2*(V*K^2 - 1) a1 = 1 - sqrt(2*V)*K + V*K^2

b0 = 1 + K/Q + K^2 b1 = -2*(K^2 - 1) b2 = 1 - K/Q + K^2

The formula for the Peaking filter is:

omega = 2*pi*f/sr K = tan(omega/2)

a0 = 1 + V*K/2 + K^2 a1 = 2*(K^2 - 1) a2 = 1 - V*K/2 + K^2

b0 = 1 + K/Q + K^2 b1 = 2*(K^2 - 1) b2 = 1 - K/Q + K^2

Definition at line 98 of file FilterParametricEqualizer.h.

Member Enumeration Documentation

Constructor & Destructor Documentation

FilterParametricEqualizer::FilterParametricEqualizer ( const Type type,
const float64 sample_rate,
const float64 frequency,
const float64 resonance = 0.707106781187,
const float64 boost_dB = 0.0 
)

boost is in dB

Definition at line 45 of file FilterParametricEqualizer.cc.

References N_POLES, reset(), x_end_ptr_, x_history_, x_ptr_, y_end_ptr_, y_history_, and y_ptr_.

51  :
52  Filter(sample_rate),
53  type_(type),
54  frequency_(frequency),
55  resonance_(resonance),
56  boost_(boost),
57  a_(new float64 [N_POLES]),
58  b_(new float64 [N_POLES]),
59  x_history_(NULL),
60  x_ptr_(NULL),
61  x_end_ptr_(NULL),
62  y_history_(NULL),
63  y_ptr_(NULL),
64  y_end_ptr_(NULL),
66 {
67  x_history_ = new float64 [N_POLES + 1];
70 
71  y_history_ = new float64 [N_POLES + 1];
73  y_end_ptr_ = y_history_ + N_POLES + 1;
74 
75  reset();
76 }
static const uint64 N_POLES
double float64
Definition: Nsound.h:146
Filter(const float64 &sample_rate)
Definition: Filter.cc:41
FilterParametricEqualizer::~FilterParametricEqualizer ( )
virtual

Definition at line 80 of file FilterParametricEqualizer.cc.

References a_, b_, x_history_, and y_history_.

Nsound::FilterParametricEqualizer::FilterParametricEqualizer ( const FilterParametricEqualizer copy)
private

Member Function Documentation

AudioStream FilterParametricEqualizer::filter ( const AudioStream x)

Definition at line 90 of file FilterParametricEqualizer.cc.

References Nsound::AudioStream::getNChannels(), and Nsound::AudioStream::getSampleRate().

Referenced by filter(), FilterParametricEqualizer_UnitTest(), and Nsound::DrumKickBass::play().

91 {
92  uint32 n_channels = x.getNChannels();
93 
94  AudioStream y(x.getSampleRate(), n_channels);
95 
96  for(uint32 channel = 0; channel < n_channels; ++channel)
97  {
98  y[channel] = filter(x[channel]);
99  }
100 
101  return y;
102 }
unsigned int uint32
Definition: Nsound.h:153
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
AudioStream filter(const AudioStream &x)
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
AudioStream FilterParametricEqualizer::filter ( const AudioStream x,
const Buffer frequencies 
)

Definition at line 106 of file FilterParametricEqualizer.cc.

References filter(), Nsound::AudioStream::getNChannels(), and Nsound::AudioStream::getSampleRate().

107 {
108  uint32 n_channels = x.getNChannels();
109 
110  AudioStream y(x.getSampleRate(), n_channels);
111 
112  for(uint32 channel = 0; channel < n_channels; ++channel)
113  {
114  y[channel] = filter(x[channel], frequencies);
115  }
116 
117  return y;
118 }
unsigned int uint32
Definition: Nsound.h:153
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
AudioStream filter(const AudioStream &x)
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
AudioStream FilterParametricEqualizer::filter ( const AudioStream x,
const Buffer frequencies,
const Buffer resonance 
)

Definition at line 122 of file FilterParametricEqualizer.cc.

References filter(), Nsound::AudioStream::getNChannels(), and Nsound::AudioStream::getSampleRate().

126 {
127  uint32 n_channels = x.getNChannels();
128 
129  AudioStream y(x.getSampleRate(), n_channels);
130 
131  for(uint32 channel = 0; channel < n_channels; ++channel)
132  {
133  y[channel] = filter(x[channel], frequencies, resonance);
134  }
135 
136  return y;
137 }
unsigned int uint32
Definition: Nsound.h:153
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
AudioStream filter(const AudioStream &x)
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
AudioStream FilterParametricEqualizer::filter ( const AudioStream x,
const Buffer frequencies,
const Buffer resonance,
const Buffer boost_dB 
)

boost is in dB

Definition at line 141 of file FilterParametricEqualizer.cc.

References filter(), Nsound::AudioStream::getNChannels(), and Nsound::AudioStream::getSampleRate().

146 {
147  uint32 n_channels = x.getNChannels();
148 
149  AudioStream y(x.getSampleRate(), n_channels);
150 
151  for(uint32 channel = 0; channel < n_channels; ++channel)
152  {
153  y[channel] = filter(x[channel], frequencies, resonance, boost);
154  }
155 
156  return y;
157 }
unsigned int uint32
Definition: Nsound.h:153
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
AudioStream filter(const AudioStream &x)
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
Buffer FilterParametricEqualizer::filter ( const Buffer x)

Definition at line 162 of file FilterParametricEqualizer.cc.

References filter(), Nsound::Buffer::getLength(), and reset().

163 {
164  reset();
165 
166  uint32 n_samples = x.getLength();
167 
168  Buffer y;
169 
170  for(uint32 n = 0; n < n_samples; ++n)
171  {
172  y << filter(x[n]);
173  }
174 
175  return y;
176 }
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
AudioStream filter(const AudioStream &x)
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer FilterParametricEqualizer::filter ( const Buffer x,
const Buffer frequencies 
)

Definition at line 180 of file FilterParametricEqualizer.cc.

References filter(), Nsound::Buffer::getLength(), and reset().

181 {
182  reset();
183 
184  uint32 n_samples = x.getLength();
185  uint32 n_freqs = frequencies.getLength();
186 
187  Buffer y;
188 
189  for(uint32 n = 0; n < n_samples; ++n)
190  {
191  y << filter(x[n], frequencies[n % n_freqs]);
192  }
193 
194  return y;
195 }
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
AudioStream filter(const AudioStream &x)
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer FilterParametricEqualizer::filter ( const Buffer x,
const Buffer frequencies,
const Buffer resonance 
)

Definition at line 199 of file FilterParametricEqualizer.cc.

References filter(), Nsound::Buffer::getLength(), and reset().

203 {
204  reset();
205 
206  uint32 n_samples = x.getLength();
207  uint32 n_freqs = frequencies.getLength();
208  uint32 n_rezzies = resonance.getLength();
209 
210  Buffer y;
211 
212  for(uint32 n = 0; n < n_samples; ++n)
213  {
214  y << filter(x[n], frequencies[n % n_freqs], resonance[n % n_rezzies]);
215  }
216 
217  return y;
218 }
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
AudioStream filter(const AudioStream &x)
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer FilterParametricEqualizer::filter ( const Buffer x,
const Buffer frequencies,
const Buffer resonance,
const Buffer boost_dB 
)

boost is in dB

Definition at line 222 of file FilterParametricEqualizer.cc.

References filter(), Nsound::Buffer::getLength(), and reset().

227 {
228  reset();
229 
230  uint32 n_samples = x.getLength();
231  uint32 n_freqs = frequencies.getLength();
232  uint32 n_rezzies = resonance.getLength();
233  uint32 n_boosts = boost.getLength();
234 
235  Buffer y;
236 
237  for(uint32 n = 0; n < n_samples; ++n)
238  {
239  y << filter(x[n],
240  frequencies[n % n_freqs],
241  resonance[n % n_rezzies],
242  boost[n % n_boosts]);
243  }
244 
245  return y;
246 }
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
AudioStream filter(const AudioStream &x)
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 FilterParametricEqualizer::filter ( const float64 x)
virtual

Implements Nsound::Filter.

Definition at line 250 of file FilterParametricEqualizer.cc.

References a_, b_, N_POLES, x_end_ptr_, x_history_, x_ptr_, y_end_ptr_, y_history_, and y_ptr_.

251 {
252  // Write x to history
253  *x_ptr_ = x;
254 
255  // Increment history pointer
256  ++x_ptr_;
257 
258  // Bounds check
259  if(x_ptr_ >= x_end_ptr_)
260  {
261  x_ptr_ = x_history_;
262  }
263 
264  float64 y = 0.0;
265  float64 * x_hist = x_ptr_;
266  for(float64 * b = b_; b < b_ + N_POLES; ++b)
267  {
268  // When we enter this loop, x_hist is pointing at x[n + 1]
269  --x_hist;
270 
271  // Bounds check
272  if(x_hist < x_history_)
273  {
274  x_hist = x_end_ptr_ - 1;
275  }
276 
277  y += *b * *x_hist;
278  }
279 
280  float64 * y_hist = y_ptr_;
281  for(float64 * a = a_ + 1; a < a_ + N_POLES; ++a)
282  {
283  // When we enter this loop, y_hist is pointing at y[n + 1]
284  --y_hist;
285 
286  // Bounds check
287  if(y_hist < y_history_)
288  {
289  y_hist = y_end_ptr_ - 1;
290  }
291 
292  y -= *a * *y_hist;
293 
294  }
295 
296  // insert output into history buffer
297  *y_ptr_ = y;
298 
299  // Increment history pointer
300  ++y_ptr_;
301 
302  // Bounds check
303  if(y_ptr_ >= y_end_ptr_)
304  {
305  y_ptr_ = y_history_;
306  }
307 
308  return y;
309 }
static const uint64 N_POLES
double float64
Definition: Nsound.h:146
float64 FilterParametricEqualizer::filter ( const float64 x,
const float64 frequency 
)
virtual

Implements Nsound::Filter.

Definition at line 313 of file FilterParametricEqualizer.cc.

References boost_, filter(), makeKernel(), and resonance_.

314 {
315  makeKernel(frequency, resonance_, boost_);
316 
317  return filter(x);
318 }
AudioStream filter(const AudioStream &x)
void makeKernel(const float64 &frequency, const float64 &resonance, const float64 &boost)
float64 FilterParametricEqualizer::filter ( const float64 x,
const float64 frequency,
const float64 resonance 
)

boost is in dB

Definition at line 322 of file FilterParametricEqualizer.cc.

References boost_, filter(), and makeKernel().

326 {
327  makeKernel(frequency, resonance, boost_);
328 
329  return filter(x);
330 }
AudioStream filter(const AudioStream &x)
void makeKernel(const float64 &frequency, const float64 &resonance, const float64 &boost)
float64 FilterParametricEqualizer::filter ( const float64 x,
const float64 frequency,
const float64 resonance,
const float64 boost_dB 
)

boost is in dB

Definition at line 334 of file FilterParametricEqualizer.cc.

References filter(), and makeKernel().

339 {
340  makeKernel(frequency, resonance, boost);
341 
342  return filter(x);
343 }
AudioStream filter(const AudioStream &x)
void makeKernel(const float64 &frequency, const float64 &resonance, const float64 &boost)
void FilterParametricEqualizer::makeKernel ( const float64 frequency,
const float64 resonance,
const float64 boost 
)

Definition at line 347 of file FilterParametricEqualizer.cc.

References a_, b_, HIGH_SHELF, kernel_cache_, LOW_SHELF, M_PI, N_POLES, PEAKING, Nsound::Filter::sample_rate_, Nsound::Kernel::setA(), Nsound::Kernel::setB(), Nsound::Filter::two_pi_over_sample_rate_, and type_.

Referenced by filter(), and reset().

351 {
352  Kernel new_kernel(N_POLES, N_POLES,
353  static_cast<int32>(std::fabs(frequency)),
354  static_cast<int32>(resonance * 1e3),
355  static_cast<int32>(boost * 1e3));
356 
357  // See if the kernel is in the cache.
358  KernelCache::iterator itor = kernel_cache_.find(new_kernel);
359 
360  // Kernel was found in the cache, set pointers and return.
361  if(itor != kernel_cache_.end())
362  {
363  memcpy(a_, itor->getA(), sizeof(float64) * N_POLES);
364  memcpy(b_, itor->getB(), sizeof(float64) * N_POLES);
365 
366  return;
367  }
368 
369  memset(a_, 0, sizeof(float64) * N_POLES);
370  memset(b_, 0, sizeof(float64) * N_POLES);
371 
372  float64 omega = two_pi_over_sample_rate_ * frequency;
373 
374  switch(type_)
375  {
376  case PEAKING:
377  {
378 
379 //~ K = tan(omega/2)
380 //~
381 //~ b0 = 1 + V*K + K^2
382 //~ b1 = 2*(K^2 - 1)
383 //~ b2 = 1 - V*K + K^2
384 //~
385 //~ a0 = 1 + K/Q + K^2
386 //~ a1 = 2*(K^2 - 1)
387 //~ a2 = 1 - K/Q + K^2
388 
389  float64 K = tan(omega / 2.0);
390  float64 KK = K * K;
391  float64 vkdq = boost * K / resonance;
392 
393  b_[0] = 1.0 + vkdq + KK;
394  b_[1] = 2.0 * (KK - 1.0);
395  b_[2] = 1.0 - vkdq + KK;
396 
397  a_[0] = 1.0 + K / resonance + KK;
398  a_[1] = b_[1];
399  a_[2] = 1.0 - K / resonance + KK;
400 
401  break;
402  }
403 
404  case LOW_SHELF:
405  {
406 
407 //~ K = tan(omega/2)
408 //~
409 //~ b0 = 1 + sqrt(2*V)*K + V*K^2
410 //~ b1 = 2*(V*K^2 - 1)
411 //~ b2 = 1 - sqrt(2*V)*K + V*K^2
412 //~
413 //~ a0 = 1 + K/Q + K^2
414 //~ a1 = 2*(K^2 - 1)
415 //~ a2 = 1 - K/Q + K^2
416 
417  float64 omega = 2.0 * M_PI * frequency / sample_rate_;
418  float64 K = tan(omega / 2.0);
419  float64 KK = K * K;
420 
421  float64 sqvk = sqrt(2.0 * boost) * K;
422  float64 vkk = boost * KK;
423 
424  b_[0] = 1.0 + sqvk + vkk;
425  b_[1] = 2.0 * (vkk - 1.0);
426  b_[2] = 1.0 - sqvk + vkk;
427 
428  a_[0] = 1.0 + K / resonance + KK;
429  a_[1] = 2.0 * (KK - 1.0);
430  a_[2] = 1.0 - K / resonance + KK;
431 
432  break;
433  }
434 
435  case HIGH_SHELF:
436  {
437 
438 //~ K = tan((pi-omega)/2)
439 //~
440 //~ b0 = 1 + sqrt(2*V)*K + V*K^2
441 //~ b1 = -2*(V*K^2 - 1)
442 //~ b1 = 1 - sqrt(2*V)*K + V*K^2
443 //~
444 //~ a0 = 1 + K/Q + K^2
445 //~ a1 = -2*(K^2 - 1)
446 //~ a2 = 1 - K/Q + K^2
447 
448  float64 K = tan((M_PI - omega) * 0.5);
449  float64 KK = K * K;
450  float64 vkk = boost * KK;
451 
452  float64 sq = sqrt(2.0 * boost);
453 
454  b_[0] = 1.0 + sq * K + vkk;
455  b_[1] = -2.0 * (vkk - 1.0);
456  b_[2] = 1.0 - sq * K + vkk;
457 
458  a_[0] = 1.0 + K / resonance + KK;
459  a_[1] = -2.0 * (KK - 1.0);
460  a_[2] = 1.0 - K / resonance + KK;
461 
462  break;
463  }
464  }
465 
466  a_[0] = 1.0 / a_[0];
467 
468  a_[1] *= a_[0];
469  a_[2] *= a_[0];
470 
471  b_[0] *= a_[0];
472  b_[1] *= a_[0];
473  b_[2] *= a_[0];
474 
475  new_kernel.setB(b_);
476  new_kernel.setA(a_);
477 
478  // Add the new kernel to the cache.
479  kernel_cache_.insert(new_kernel);
480 }
#define M_PI
Definition: Nsound.h:121
static const uint64 N_POLES
double float64
Definition: Nsound.h:146
float64 two_pi_over_sample_rate_
Definition: Filter.h:114
float64 sample_rate_
Definition: Filter.h:113
void FilterParametricEqualizer::plot ( boolean  show_fc = true,
boolean  show_phase = false 
)

Definition at line 485 of file FilterParametricEqualizer.cc.

References Nsound::Plotter::axvline(), frequency_, N_POLES, Nsound::Filter::plot(), Nsound::Filter::sample_rate_, and Nsound::Plotter::title().

486 {
487  Filter::plot(show_phase);
488 
489  if(show_fc)
490  {
491  Plotter pylab;
492 
493  pylab.axvline(frequency_,"color='red'");
494 
495  char title[128];
496  sprintf(title,
497  "Parametric Equalizing Frequency Response\n"
498  "order = %.0f, f = %0.1f Hz, sr = %0.1f Hz",
499  static_cast<float64>(N_POLES - 1),
500  frequency_,
501  static_cast<float64>(sample_rate_));
502  pylab.title(title);
503  }
504 }
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
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
static const uint64 N_POLES
void plot(boolean show_phase=false)
Definition: Filter.cc:262
float64 sample_rate_
Definition: Filter.h:113
void FilterParametricEqualizer::reset ( )
virtual

Implements Nsound::Filter.

Definition at line 508 of file FilterParametricEqualizer.cc.

References boost_, frequency_, makeKernel(), N_POLES, resonance_, x_history_, x_ptr_, y_history_, and y_ptr_.

Referenced by filter(), and FilterParametricEqualizer().

509 {
510  memset(x_history_, 0, sizeof(float64) * (N_POLES + 1));
511  memset(y_history_, 0, sizeof(float64) * (N_POLES + 1));
512 
513  x_ptr_ = x_history_;
514  y_ptr_ = y_history_;
515 
517 }
static const uint64 N_POLES
double float64
Definition: Nsound.h:146
void makeKernel(const float64 &frequency, const float64 &resonance, const float64 &boost)
FilterParametricEqualizer& Nsound::FilterParametricEqualizer::operator= ( const FilterParametricEqualizer rhs)
private
void Nsound::Filter::setRealtime ( bool  flag)
inlineinherited

Definition at line 57 of file Filter.h.

References Nsound::Filter::is_realtime_.

57 {is_realtime_ = flag;}
bool is_realtime_
Definition: Filter.h:118
AudioStream Filter::filter ( const AudioStream x,
const float64 frequency 
)
inherited

Definition at line 76 of file Filter.cc.

References Nsound::Filter::filter(), Nsound::AudioStream::getNChannels(), Nsound::AudioStream::getSampleRate(), Nsound::Filter::is_realtime_, M_THROW, and Nsound::Filter::reset().

77 {
78  if(!is_realtime_) reset();
79 
80  uint32 n_channels = x.getNChannels();
81 
82  if(is_realtime_ && n_channels > 1)
83  {
84  M_THROW("In real-time mode, a filter per audio channel must be used!");
85  }
86 
87  AudioStream y(x.getSampleRate(), n_channels);
88 
89  for(uint32 channel = 0; channel < n_channels; ++channel)
90  {
91  y[channel] = filter(x[channel], frequency);
92  }
93 
94  return y;
95 }
unsigned int uint32
Definition: Nsound.h:153
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
virtual void reset()=0
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
#define M_THROW(message)
Definition: Macros.h:108
bool is_realtime_
Definition: Filter.h:118
Buffer Filter::filter ( const Buffer x,
const float64 frequency 
)
inherited

Definition at line 140 of file Filter.cc.

References Nsound::Buffer::begin(), Nsound::Buffer::end(), Nsound::Filter::filter(), Nsound::Buffer::getLength(), Nsound::Filter::is_realtime_, and Nsound::Filter::reset().

141 {
142  if(!is_realtime_) reset();
143 
144  Buffer::const_iterator itor = x.begin();
145  Buffer::const_iterator end = x.end();
146 
147  Buffer y(x.getLength());
148 
149  while(itor != end)
150  {
151  y << filter(*itor, frequency);
152 
153  ++itor;
154  }
155 
156  return y;
157 }
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
virtual void reset()=0
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
bool is_realtime_
Definition: Filter.h:118
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer Filter::getFrequencyAxis ( const uint32  n_fft = 8192)
inherited

Definition at line 185 of file Filter.cc.

References Nsound::FFTransform::roundUp2(), and Nsound::Filter::sample_rate_.

Referenced by main(), Nsound::Filter::plot(), and Nsound::FilterIIR::savePlot().

186 {
187  uint32 fft_chunk_size = FFTransform::roundUp2(
188  static_cast<int32>(n_fft));
189 
190  uint32 n_samples = fft_chunk_size / 2 + 1;
191 
192  float64 f_step = (1.0 / (static_cast<float64>(fft_chunk_size) / 2.0))
193  * (sample_rate_ / 2.0);
194 
195  Buffer f_axis;
196 
197  float64 f = 0.0;
198 
199  for(uint32 i = 0; i < n_samples; ++i)
200  {
201  f_axis << f;
202  f += f_step;
203  }
204 
205  return f_axis;
206 };
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
static int32 roundUp2(int32 raw)
Returns nearest power of 2 >= raw.
Definition: FFTransform.cc:274
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 sample_rate_
Definition: Filter.h:113
Buffer Filter::getFrequencyResponse ( const uint32  n_fft = 8192)
inherited

Definition at line 210 of file Filter.cc.

References Nsound::FFTransform::fft(), Nsound::Filter::getImpulseResponse(), and Nsound::Filter::sample_rate_.

Referenced by Nsound::FilterBandPassIIR::FilterBandPassIIR(), FilterLeastSquaresFIR_UnitTest(), Nsound::FilterIIR::getRMS(), main(), Nsound::Filter::plot(), and Nsound::FilterIIR::savePlot().

211 {
213 
214 //~ fft.setWindow(HANNING);
215 
216  FFTChunkVector vec;
217 
218  vec = fft.fft(getImpulseResponse(), n_fft);
219 
220  return vec[0].getMagnitude();
221 }
Buffer getImpulseResponse(const uint32 n_samples=8192)
Definition: Filter.cc:225
A Class that performes the Fast Fouier Transfrom on a Buffer.
Definition: FFTransform.h:57
std::vector< FFTChunk > FFTChunkVector
Definition: FFTChunk.h:119
float64 sample_rate_
Definition: Filter.h:113
Buffer Filter::getImpulseResponse ( const uint32  n_samples = 8192)
inherited

Definition at line 225 of file Filter.cc.

References Nsound::Filter::filter(), Nsound::Filter::is_realtime_, and Nsound::Filter::reset().

Referenced by Nsound::Filter::getFrequencyResponse(), Nsound::FilterHighPassFIR::getImpulseResponse(), Nsound::FilterLowPassFIR::getImpulseResponse(), Nsound::FilterLeastSquaresFIR::getImpulseResponse(), Nsound::FilterIIR::getImpulseResponse(), and Nsound::Filter::getPhaseResponse().

226 {
227  if(!is_realtime_) reset();
228 
229  Buffer response(n_samples);
230 
231  response << filter(1.0);
232 
233  for(uint32 i = 1; i < n_samples; ++i)
234  {
235  response << filter(0.0);
236  }
237 
238  if(!is_realtime_) reset();
239 
240  return response;
241 }
unsigned int uint32
Definition: Nsound.h:153
virtual void reset()=0
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
bool is_realtime_
Definition: Filter.h:118
A Buffer for storing audio samples.
Definition: Buffer.h:60
virtual uint32 Nsound::Filter::getKernelSize ( ) const
inlinevirtualinherited
Buffer Filter::getPhaseResponse ( )
inherited

Definition at line 245 of file Filter.cc.

References Nsound::FFTransform::fft(), Nsound::Filter::getImpulseResponse(), Nsound::Buffer::getLength(), Nsound::Filter::sample_rate_, and Nsound::Buffer::subbuffer().

Referenced by Nsound::Filter::plot().

246 {
247  uint32 n_samples = static_cast<uint32>(sample_rate_ * 2);
248 
249  FFTransform fft(n_samples);
250 
251  FFTChunkVector vec;
252 
253  vec = fft.fft(getImpulseResponse(), n_samples);
254 
255  Buffer phase = vec[0].getPhase();
256 
257  return phase.subbuffer(0, phase.getLength() / 2 + 1);
258 }
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
Definition: Buffer.cc:2073
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
Buffer getImpulseResponse(const uint32 n_samples=8192)
Definition: Filter.cc:225
A Class that performes the Fast Fouier Transfrom on a Buffer.
Definition: FFTransform.h:57
A Buffer for storing audio samples.
Definition: Buffer.h:60
std::vector< FFTChunk > FFTChunkVector
Definition: FFTChunk.h:119
float64 sample_rate_
Definition: Filter.h:113
float64 Nsound::Filter::getSampleRate ( ) const
inlineinherited

Definition at line 102 of file Filter.h.

References Nsound::Filter::sample_rate_.

102 { return sample_rate_; };
float64 sample_rate_
Definition: Filter.h:113
void Filter::plot ( boolean  show_phase = false)
inherited

Definition at line 262 of file Filter.cc.

References Nsound::Plotter::figure(), Nsound::Buffer::getdB(), Nsound::Filter::getFrequencyAxis(), Nsound::Filter::getFrequencyResponse(), Nsound::Buffer::getMax(), Nsound::Filter::getPhaseResponse(), Nsound::Plotter::plot(), Nsound::Plotter::subplot(), Nsound::Plotter::xlabel(), Nsound::Plotter::ylabel(), and Nsound::Plotter::ylim().

Referenced by main(), Nsound::FilterLowPassMoogVcf::plot(), Nsound::FilterPhaser::plot(), Nsound::FilterTone::plot(), Nsound::FilterHighPassIIR::plot(), Nsound::FilterCombLowPassFeedback::plot(), Nsound::FilterLowPassIIR::plot(), Nsound::FilterLowPassFIR::plot(), Nsound::FilterHighPassFIR::plot(), Nsound::FilterAllPass::plot(), Nsound::FilterBandPassVocoder::plot(), Nsound::FilterBandPassFIR::plot(), Nsound::FilterLeastSquaresFIR::plot(), Nsound::FilterFlanger::plot(), Nsound::FilterBandRejectFIR::plot(), Nsound::FilterBandPassIIR::plot(), Nsound::FilterBandRejectIIR::plot(), and plot().

263 {
264  Buffer x = getFrequencyAxis();
266 
267  Plotter pylab;
268 
269  pylab.figure();
270 
271  uint32 n_rows = 1;
272  uint32 n_cols = 1;
273 
274  if(show_phase)
275  {
276  n_rows = 2;
277  }
278 
279  pylab.subplot(n_rows, n_cols, 1);
280 
281  // Frequency response
282  pylab.plot(x,fr, "blue");
283 
284  pylab.xlabel("Frequency (Hz)");
285  pylab.ylabel("Frequency Response (dB)");
286 
287  // Phase response
288  if(show_phase)
289  {
290  pylab.subplot(n_rows, n_cols, 2);
291 
292  Buffer pr = getPhaseResponse().getdB();
293 
294  pylab.plot(x,pr);
295 
296  pylab.xlabel("Frequency (Hz)");
297  pylab.ylabel("Phase Response (dB)");
298  }
299 
300  float64 ymax = fr.getMax();
301  float64 height = ymax - -60.0;
302 
303  pylab.ylim(-60.0, ymax + 0.05 * height);
304 }
unsigned int uint32
Definition: Nsound.h:153
void xlabel(const std::string &label, const std::string &kwargs="")
Add a label x axis.
Definition: Plotter.cc:1154
void plot(const Buffer &y, const std::string &fmt="", const std::string &kwargs="")
Plots the Buffer on the current figure.
Definition: Plotter.cc:765
void figure(const std::string &kwargs="") const
Creates a new figure window to plot in.
Definition: Plotter.cc:455
Buffer getPhaseResponse()
Definition: Filter.cc:245
double float64
Definition: Nsound.h:146
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 ylim(const float64 &ymin, const float64 &ymax)
Definition: Plotter.cc:422
Buffer getFrequencyAxis(const uint32 n_fft=8192)
Definition: Filter.cc:185
void ylabel(const std::string &label, const std::string &kwargs="")
Add a label y axis.
Definition: Plotter.cc:1180
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer getFrequencyResponse(const uint32 n_fft=8192)
Definition: Filter.cc:210
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
Buffer getdB() const
Returns the Buffer in dB.
Definition: Buffer.h:487

Member Data Documentation

Type Nsound::FilterParametricEqualizer::type_
protected

Definition at line 196 of file FilterParametricEqualizer.h.

Referenced by makeKernel().

float64 Nsound::FilterParametricEqualizer::frequency_
protected

Definition at line 197 of file FilterParametricEqualizer.h.

Referenced by plot(), and reset().

float64 Nsound::FilterParametricEqualizer::resonance_
protected

Definition at line 198 of file FilterParametricEqualizer.h.

Referenced by filter(), and reset().

float64 Nsound::FilterParametricEqualizer::boost_
protected

Definition at line 199 of file FilterParametricEqualizer.h.

Referenced by filter(), and reset().

float64* Nsound::FilterParametricEqualizer::a_
protected

Definition at line 201 of file FilterParametricEqualizer.h.

Referenced by filter(), makeKernel(), and ~FilterParametricEqualizer().

float64* Nsound::FilterParametricEqualizer::b_
protected

Definition at line 202 of file FilterParametricEqualizer.h.

Referenced by filter(), makeKernel(), and ~FilterParametricEqualizer().

float64* Nsound::FilterParametricEqualizer::x_history_
protected
float64* Nsound::FilterParametricEqualizer::x_ptr_
protected

Definition at line 205 of file FilterParametricEqualizer.h.

Referenced by filter(), FilterParametricEqualizer(), and reset().

float64* Nsound::FilterParametricEqualizer::x_end_ptr_
protected

Definition at line 206 of file FilterParametricEqualizer.h.

Referenced by filter(), and FilterParametricEqualizer().

float64* Nsound::FilterParametricEqualizer::y_history_
protected
float64* Nsound::FilterParametricEqualizer::y_ptr_
protected

Definition at line 209 of file FilterParametricEqualizer.h.

Referenced by filter(), FilterParametricEqualizer(), and reset().

float64* Nsound::FilterParametricEqualizer::y_end_ptr_
protected

Definition at line 210 of file FilterParametricEqualizer.h.

Referenced by filter(), and FilterParametricEqualizer().

KernelCache Nsound::FilterParametricEqualizer::kernel_cache_
protected

Definition at line 212 of file FilterParametricEqualizer.h.

Referenced by makeKernel().

float64 Nsound::Filter::sample_rate_
protectedinherited

Definition at line 113 of file Filter.h.

Referenced by Nsound::FilterPhaser::filter(), Nsound::FilterCombLowPassFeedback::filter(), Nsound::FilterDelay::filter(), Nsound::FilterAllPass::FilterAllPass(), Nsound::FilterCombLowPassFeedback::FilterCombLowPassFeedback(), Nsound::FilterDelay::FilterDelay(), Nsound::FilterFlanger::FilterFlanger(), Nsound::FilterPhaser::FilterPhaser(), Nsound::FilterSlinky::FilterSlinky(), Nsound::Filter::getFrequencyAxis(), Nsound::Filter::getFrequencyResponse(), Nsound::Filter::getPhaseResponse(), Nsound::Filter::getSampleRate(), Nsound::FilterStageIIR::makeIIRKernelHelper(), Nsound::FilterHighPassFIR::makeKernel(), Nsound::FilterStageIIR::makeKernel(), Nsound::FilterBandPassVocoder::makeKernel(), Nsound::FilterLeastSquaresFIR::makeKernel(), makeKernel(), Nsound::FilterPhaser::operator=(), Nsound::FilterLeastSquaresFIR::operator=(), Nsound::FilterFlanger::operator=(), Nsound::FilterIIR::operator=(), Nsound::FilterLowPassMoogVcf::plot(), Nsound::FilterPhaser::plot(), Nsound::FilterTone::plot(), Nsound::FilterHighPassIIR::plot(), Nsound::FilterLowPassIIR::plot(), Nsound::FilterCombLowPassFeedback::plot(), Nsound::FilterLowPassFIR::plot(), Nsound::FilterHighPassFIR::plot(), Nsound::FilterAllPass::plot(), Nsound::FilterBandPassFIR::plot(), Nsound::FilterLeastSquaresFIR::plot(), Nsound::FilterFlanger::plot(), Nsound::FilterBandRejectFIR::plot(), Nsound::FilterBandRejectIIR::plot(), Nsound::FilterBandPassIIR::plot(), plot(), Nsound::FilterLowPassIIR::setCutoff(), and Nsound::FilterLeastSquaresFIR::setWindow().

float64 Nsound::Filter::two_pi_over_sample_rate_
protectedinherited
float64 Nsound::Filter::sample_time_
protectedinherited

Definition at line 115 of file Filter.h.

Referenced by Nsound::FilterLowPassMoogVcf::_make_filter().

uint32 Nsound::Filter::kernel_size_
protectedinherited
bool Nsound::Filter::is_realtime_
protectedinherited

The documentation for this class was generated from the following files: