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

#include <Nsound/FilterIIR.h>

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

Public Member Functions

 FilterIIR (const float64 &sample_rate, uint32 n_poles)
 
 FilterIIR (const FilterIIR &copy)
 
virtual ~FilterIIR ()
 
Buffer designFrequencyResponse (const Buffer &frequency_response, const float64 &max_error=0.01, const int32 max_iterations=1000)
 Designs a filter kernel using a genetic algorithm that trys to match the provided frequency response. More...
 
Buffer designImpulseResponse (const Buffer &impulse_response, const float64 &max_error=0.01, const int32 max_iterations=1000)
 Designs a filter kernel using a genetic algorithm that trys to match the provided impulse response. More...
 
AudioStream filter (const AudioStream &x)
 
AudioStream filter (const AudioStream &x, const Buffer &frequencies)
 
Buffer filter (const Buffer &x)
 
Buffer filter (const Buffer &x, const Buffer &frequencies)
 
float64 filter (const float64 &x)
 
float64 filter (const float64 &x, const float64 &frequency)
 
uint32 getKernelSize () const
 
Buffer getImpulseResponse (const uint32 n_samples=8192)
 
FilterIIRoperator= (const FilterIIR &rhs)
 
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 getPhaseResponse ()
 
float64 getSampleRate () const
 
void plot (boolean show_phase=false)
 

Protected Types

enum  SignalType { FREQUENCY_RESSPONSE = 0, IMPULSE_RESPONSE }
 

Protected Member Functions

Buffer designKernel (const Buffer &response, const float64 &max_rms_error, const int32 max_iterations, const SignalType type)
 Designs a filter kernel that trys to match the provide frequency response. More...
 
float64 getRMS (const Kernel &kernel, const Buffer &response, const SignalType type)
 
void savePlot (const Kernel &k, const Buffer &response, uint32 n, const float64 &error)
 

Protected Attributes

uint32 n_poles_
 
Kernelkernel_
 
float64x_history_
 
float64x_ptr_
 
float64x_end_ptr_
 
float64y_history_
 
float64y_ptr_
 
float64y_end_ptr_
 
RngTauswortherng_
 
float64 sample_rate_
 
float64 two_pi_over_sample_rate_
 
float64 sample_time_
 
uint32 kernel_size_
 
bool is_realtime_
 

Friends

std::ostream & operator<< (std::ostream &out, const FilterIIR &rhs)
 Prints the coeffents. More...
 

Detailed Description

WARNING: This is Experimental, you should not use this class as it may not be working or will change in future releases of Nsound.

Definition at line 52 of file FilterIIR.h.

Member Enumeration Documentation

Enumerator
FREQUENCY_RESSPONSE 
IMPULSE_RESPONSE 

Definition at line 132 of file FilterIIR.h.

Constructor & Destructor Documentation

FilterIIR::FilterIIR ( const float64 sample_rate,
uint32  n_poles 
)

Definition at line 59 of file FilterIIR.cc.

References kernel_, n_poles_, reset(), rng_, x_end_ptr_, x_history_, x_ptr_, y_end_ptr_, y_history_, and y_ptr_.

62  :
63  Filter(sample_rate),
64  n_poles_(n_poles),
65  kernel_(NULL),
66  x_history_(NULL),
67  x_ptr_(NULL),
68  x_end_ptr_(NULL),
69  y_history_(NULL),
70  y_ptr_(NULL),
71  y_end_ptr_(NULL),
72  rng_(NULL)
73 {
75 
76  x_history_ = new float64 [n_poles_ + 1];
79 
80  y_history_ = new float64 [n_poles_ + 1];
82  y_end_ptr_ = y_history_ + n_poles_ + 1;
83 
84  rng_ = new RngTausworthe();
85 
86  reset();
87 }
float64 * x_ptr_
Definition: FilterIIR.h:169
float64 * x_history_
Definition: FilterIIR.h:168
double float64
Definition: Nsound.h:146
float64 * y_history_
Definition: FilterIIR.h:172
Kernel * kernel_
Definition: FilterIIR.h:166
RngTausworthe * rng_
Definition: FilterIIR.h:176
float64 * x_end_ptr_
Definition: FilterIIR.h:170
Filter(const float64 &sample_rate)
Definition: Filter.cc:41
float64 * y_end_ptr_
Definition: FilterIIR.h:174
float64 * y_ptr_
Definition: FilterIIR.h:173
FilterIIR::FilterIIR ( const FilterIIR copy)

Definition at line 91 of file FilterIIR.cc.

References n_poles_, reset(), rng_, x_history_, and y_history_.

92  :
93  Filter(copy.sample_rate_),
94  n_poles_(copy.n_poles_),
95  kernel_(NULL),
96  x_history_(NULL),
97  x_ptr_(NULL),
98  x_end_ptr_(NULL),
99  y_history_(NULL),
100  y_ptr_(NULL),
101  y_end_ptr_(NULL),
102  rng_(NULL)
103 {
104  x_history_ = new float64 [n_poles_];
105  y_history_ = new float64 [n_poles_];
106 
107  rng_ = new RngTausworthe();
108 
109  *this = copy;
110 
111  reset();
112 }
float64 * x_ptr_
Definition: FilterIIR.h:169
float64 * x_history_
Definition: FilterIIR.h:168
double float64
Definition: Nsound.h:146
float64 * y_history_
Definition: FilterIIR.h:172
Kernel * kernel_
Definition: FilterIIR.h:166
RngTausworthe * rng_
Definition: FilterIIR.h:176
float64 * x_end_ptr_
Definition: FilterIIR.h:170
Filter(const float64 &sample_rate)
Definition: Filter.cc:41
float64 * y_end_ptr_
Definition: FilterIIR.h:174
float64 * y_ptr_
Definition: FilterIIR.h:173
float64 sample_rate_
Definition: Filter.h:113
FilterIIR::~FilterIIR ( )
virtual

Definition at line 116 of file FilterIIR.cc.

References kernel_, rng_, x_history_, and y_history_.

117 {
118  delete kernel_;
119  delete [] x_history_;
120  delete [] y_history_;
121 
122  delete rng_;
123 }
float64 * x_history_
Definition: FilterIIR.h:168
float64 * y_history_
Definition: FilterIIR.h:172
Kernel * kernel_
Definition: FilterIIR.h:166
RngTausworthe * rng_
Definition: FilterIIR.h:176

Member Function Documentation

Buffer FilterIIR::designFrequencyResponse ( const Buffer frequency_response,
const float64 max_error = 0.01,
const int32  max_iterations = 1000 
)

Designs a filter kernel using a genetic algorithm that trys to match the provided frequency response.

Designs a filter kernel that trys to match the provide frequency response. The method uses a genetic algorithm to arrive at a fit solution and retuns the error per generation. The error signal is nice to plot to get an idea of how quickly the solution was found.

Definition at line 127 of file FilterIIR.cc.

References designKernel(), and FREQUENCY_RESSPONSE.

Referenced by main().

131 {
132  return designKernel(
133  frequency_response,
134  max_error,
135  max_iterations,
137 }
Buffer designKernel(const Buffer &response, const float64 &max_rms_error, const int32 max_iterations, const SignalType type)
Designs a filter kernel that trys to match the provide frequency response.
Definition: FilterIIR.cc:249
Buffer FilterIIR::designImpulseResponse ( const Buffer impulse_response,
const float64 max_error = 0.01,
const int32  max_iterations = 1000 
)

Designs a filter kernel using a genetic algorithm that trys to match the provided impulse response.

Designs a filter kernel that trys to match the provide impulse response The method uses a genetic algorithm to arrive at a fit solution and retuns the error per generation. The error signal is nice to plot to get an idea of how quickly the solution was found.

Definition at line 141 of file FilterIIR.cc.

References designKernel(), and IMPULSE_RESPONSE.

145 {
146  return designKernel(
147  impulse_response,
148  max_error,
149  max_iterations,
151 }
Buffer designKernel(const Buffer &response, const float64 &max_rms_error, const int32 max_iterations, const SignalType type)
Designs a filter kernel that trys to match the provide frequency response.
Definition: FilterIIR.cc:249
AudioStream Nsound::FilterIIR::filter ( const AudioStream x)
inline

Definition at line 89 of file FilterIIR.h.

References Nsound::Filter::filter().

Referenced by filter().

90  { return Filter::filter(x);};
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
AudioStream Nsound::FilterIIR::filter ( const AudioStream x,
const Buffer frequencies 
)
inline

Definition at line 93 of file FilterIIR.h.

References Nsound::Filter::filter().

94  { return Filter::filter(x);};
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
Buffer Nsound::FilterIIR::filter ( const Buffer x)
inline

Definition at line 97 of file FilterIIR.h.

References Nsound::Filter::filter().

98  {return Filter::filter(x);};
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
Buffer Nsound::FilterIIR::filter ( const Buffer x,
const Buffer frequencies 
)
inline

Definition at line 101 of file FilterIIR.h.

References filter().

102  {return filter(x);};
AudioStream filter(const AudioStream &x)
Definition: FilterIIR.h:89
float64 FilterIIR::filter ( const float64 x)
virtual

Implements Nsound::Filter.

Definition at line 609 of file FilterIIR.cc.

References Nsound::Kernel::getA(), Nsound::Kernel::getB(), kernel_, n_poles_, x_end_ptr_, x_history_, x_ptr_, y_end_ptr_, y_history_, and y_ptr_.

610 {
611  // Write x to history
612  *x_ptr_ = x;
613 
614  // Increment history pointer
615  ++x_ptr_;
616 
617  // Bounds check
618  if(x_ptr_ >= x_end_ptr_)
619  {
620  x_ptr_ = x_history_;
621  }
622 
623  float64 y = 0.0;
624  float64 * x_hist = x_ptr_;
625  const float64 * bb = kernel_->getB();
626  const float64 * b_end = bb + n_poles_;
627  for(const float64 * b = bb; b < b_end; ++b)
628  {
629  // When we enter this loop, x_hist is pointing at x[n + 1]
630  --x_hist;
631 
632  // Bounds check
633  if(x_hist < x_history_)
634  {
635  x_hist = x_end_ptr_ - 1;
636  }
637 
638  y += *b * *x_hist;
639  }
640 
641  float64 * y_hist = y_ptr_;
642  const float64 * aa = kernel_->getA();
643  const float64 * a_end = aa + n_poles_;
644  for(const float64 * a = aa + 1; a < a_end; ++a)
645  {
646  // When we enter this loop, y_hist is pointing at y[n + 1]
647  --y_hist;
648 
649  // Bounds check
650  if(y_hist < y_history_)
651  {
652  y_hist = y_end_ptr_ - 1;
653  }
654 
655  y += *a * *y_hist;
656  }
657 
658  // insert output into history buffer
659  *y_ptr_ = y;
660 
661  // Increment history pointer
662  ++y_ptr_;
663 
664  // Bounds check
665  if(y_ptr_ >= y_end_ptr_)
666  {
667  y_ptr_ = y_history_;
668  }
669 
670  return y;
671 }
float64 * x_ptr_
Definition: FilterIIR.h:169
float64 getB(uint32 i)
Returns kernel 'b[i]' coef.
Definition: Kernel.h:86
float64 * x_history_
Definition: FilterIIR.h:168
double float64
Definition: Nsound.h:146
float64 * y_history_
Definition: FilterIIR.h:172
Kernel * kernel_
Definition: FilterIIR.h:166
float64 * x_end_ptr_
Definition: FilterIIR.h:170
float64 * y_end_ptr_
Definition: FilterIIR.h:174
float64 getA(uint32 i)
Returns kernel 'a[i]' coef.
Definition: Kernel.h:70
float64 * y_ptr_
Definition: FilterIIR.h:173
float64 FilterIIR::filter ( const float64 x,
const float64 frequency 
)
virtual

Implements Nsound::Filter.

Definition at line 675 of file FilterIIR.cc.

References filter().

676 {
677  return FilterIIR::filter(x);
678 }
AudioStream filter(const AudioStream &x)
Definition: FilterIIR.h:89
uint32 Nsound::FilterIIR::getKernelSize ( ) const
inlinevirtual

Reimplemented from Nsound::Filter.

Definition at line 111 of file FilterIIR.h.

References n_poles_.

111 {return n_poles_;};
Buffer Nsound::FilterIIR::getImpulseResponse ( const uint32  n_samples = 8192)
inline

Definition at line 114 of file FilterIIR.h.

References Nsound::Filter::getImpulseResponse(), and reset().

Referenced by getRMS().

115  { reset(); return Filter::getImpulseResponse(n_samples); };
Buffer getImpulseResponse(const uint32 n_samples=8192)
Definition: Filter.cc:225
FilterIIR & FilterIIR::operator= ( const FilterIIR rhs)

Definition at line 683 of file FilterIIR.cc.

References kernel_, n_poles_, rng_, Nsound::Filter::sample_rate_, x_history_, and y_history_.

684 {
685  if(this == &rhs)
686  {
687  return *this;
688  }
689 
691 
692  if(n_poles_ != rhs.n_poles_)
693  {
694  delete [] x_history_;
695  delete [] y_history_;
696 
697  n_poles_ = rhs.n_poles_;
698 
699  x_history_ = new float64 [n_poles_];
700  y_history_ = new float64 [n_poles_];
701  }
702 
703  memcpy(x_history_, rhs.x_history_, sizeof(float64) * n_poles_);
704  memcpy(y_history_, rhs.y_history_, sizeof(float64) * n_poles_);
705 
706  *kernel_ = *rhs.kernel_;
707  *rng_ = *rhs.rng_;
708 
709  return *this;
710 }
float64 * x_history_
Definition: FilterIIR.h:168
double float64
Definition: Nsound.h:146
float64 * y_history_
Definition: FilterIIR.h:172
Kernel * kernel_
Definition: FilterIIR.h:166
RngTausworthe * rng_
Definition: FilterIIR.h:176
float64 sample_rate_
Definition: Filter.h:113
void FilterIIR::reset ( )
virtual

Implements Nsound::Filter.

Definition at line 724 of file FilterIIR.cc.

References n_poles_, x_history_, x_ptr_, y_history_, and y_ptr_.

Referenced by FilterIIR(), getImpulseResponse(), getRMS(), and savePlot().

725 {
726  memset(x_history_, 0, sizeof(float64) * (n_poles_ + 1));
727  memset(y_history_, 0, sizeof(float64) * (n_poles_ + 1));
728 
729  x_ptr_ = x_history_;
730  y_ptr_ = y_history_;
731 }
float64 * x_ptr_
Definition: FilterIIR.h:169
float64 * x_history_
Definition: FilterIIR.h:168
double float64
Definition: Nsound.h:146
float64 * y_history_
Definition: FilterIIR.h:172
float64 * y_ptr_
Definition: FilterIIR.h:173
Buffer FilterIIR::designKernel ( const Buffer response,
const float64 max_rms_error,
const int32  max_iterations,
const SignalType  type 
)
protected

Designs a filter kernel that trys to match the provide frequency response.

This method is VERY EXPERMENTAL! Use at your own risk!

Designs a filter kernel that trys to match the provide frequency response. The method uses a genetic algorithm to arrive at a fit solution and retuns the error per generation. The error signal is nice to plot to get an idea of how quickly the solution was found.

Definition at line 249 of file FilterIIR.cc.

References Nsound::Kernel::ga_interleave(), Nsound::Kernel::ga_swap_ab(), Nsound::RngTausworthe::get(), Nsound::Kernel::getA(), Nsound::Kernel::getB(), Nsound::Buffer::getLength(), getRMS(), Nsound::Kernel::getSum(), kernel_, M_ASSERT_VALUE, n_poles_, PRINT_LINE, Nsound::Kernel::randomize(), rng_, Nsound::Kernel::setA(), and Nsound::Kernel::setB().

Referenced by designFrequencyResponse(), and designImpulseResponse().

254 {
255  // The reference response curve must be a power of two to guarentee correct
256  // comparision to the designed kernel response.
257 
258  uint32 p2 = 2;
259  uint32 ref_size = ref_response.getLength();
260 
261  while( p2 < ref_size )
262  {
263  p2 <<= 1;
264  }
265 
266  M_ASSERT_VALUE(ref_size, ==, p2);
267 
268  if(ref_size != p2) ::exit(1);
269 
270  // Create the initial 2 parents
271  Kernel * mom = new Kernel(n_poles_, n_poles_);
272 
273  float64 LARGE_RMS = 1.0;
274  float64 STALE_RMS = 0.001;
275 
276  float64 rms_error = LARGE_RMS;
277  int32 dead_count = 0;
278  while(rms_error > LARGE_RMS && dead_count < 1000)
279  {
280  mom->randomize(-0.1, 0.1);
281  rms_error = getRMS(*mom, ref_response, type);
282  dead_count++;
283  }
284 
285  M_ASSERT_VALUE(rms_error, <=, LARGE_RMS);
286 
287  if(rms_error > LARGE_RMS) ::exit(1);
288 
289  Kernel * dad = new Kernel(n_poles_, n_poles_);
290 
291  dad->randomize(-0.1, 0.1);
292 
293  int32 N_CHILDREN = 14;
294 
295  // Allocate N children
296  Kernel ** child = new Kernel * [N_CHILDREN];
297 
298  for(int32 i = 0; i < N_CHILDREN; ++i)
299  {
300  child[i] = new Kernel(n_poles_, n_poles_);
301  }
302 
303  Buffer rms_history;
304 
305  rms_history << rms_error;
306 
307  float64 dad_rms_error = getRMS(*dad, ref_response, type);
308 
309  cout << std::setprecision(10)
310  << "mom's RMS = " << rms_error << endl
311  << "dad's RMS = " << dad_rms_error << endl;
312 
313  float64 rms_error1 = LARGE_RMS;
314  float64 rms_error2 = LARGE_RMS;
315 
316  Kernel * low1 = NULL;
317  Kernel * low2 = NULL;
318 
319  float64 last_rms_error = rms_error;
320  int32 stale_count = 0;
321  int32 major_stale_count = 0;
322  int32 count = 0;
323 
324  while(count < max_iterations && rms_error > max_rms_error)
325  {
326  ++count;
327 
328  // First two children just swap mom & dads a & b coeffs;
329  *child[0] = *mom;
330  *child[1] = *dad;
331  child[0]->ga_swap_ab(*child[1]);
332 
333  #ifdef GA_DEBUG
334  cout << __LINE__ << "\tmom.getSum() = " << mom->getSum()
335  << "\terror = " << error << endl << endl;
336  #endif
337 
338  // Next two children interleave coeffs
339  *child[2] = *mom;
340  *child[3] = *dad;
341 
342  child[2]->ga_interleave(*child[3]);
343 
344  #ifdef GA_DEBUG
345  cout << __LINE__ << "\tmom.getSum() = " << mom->getSum()
346  << "\terror = " << error << endl << endl;
347  #endif
348 
349  // The next two children randomly mutate one coeff from mom & dad
350  *child[4] = *mom;
351  *child[5] = *dad;
352 
353  for(uint32 i = 4; i < 6; ++i)
354  {
355  float64 delta = rng_->get(-0.1f, 0.1f);
356 
357  uint32 index = rng_->get(0, n_poles_);
358 
359  child[i]->setB(child[i]->getB(index) + delta, index);
360 
361  delta = rng_->get(-0.1f, 0.1f);
362 
363  index = rng_->get(0, n_poles_);
364 
365  child[i]->setA(child[i]->getA(index) + delta, index);
366  }
367 
368  #ifdef GA_DEBUG
369  cout << __LINE__ << "\tmom.getSum() = " << mom->getSum()
370  << "\terror = " << error << endl << endl;
371  #endif
372 
373  // The next two children randomly mutate sign
374  *child[6] = *mom;
375  *child[7] = *dad;
376 
377  for(uint32 i = 6; i < 8; ++i)
378  {
379  uint32 index = rng_->get(0, n_poles_);
380 
381  float64 a_or_b = rng_->get(-1.0f, 1.0f);
382 
383  if(a_or_b > 0.0)
384  {
385  child[i]->setB(child[i]->getB(index) * -1.0, index);
386  }
387  else
388  {
389  child[i]->setA(child[i]->getA(index) * -1.0, index);
390  }
391  }
392 
393  #ifdef GA_DEBUG
394  cout << __LINE__ << "\tmom.getSum() = " << mom->getSum()
395  << "\terror = " << error << endl << endl;
396  #endif
397 
398  // These two children randomly multiply one coef.
399  *child[8] = *mom;
400  *child[9] = *dad;
401 
402  for(uint32 i = 8; i < 10; ++i)
403  {
404  uint32 index = rng_->get(0, n_poles_);
405 
406  float64 a_or_b = rng_->get(-1.0f, 1.0f);
407 
408  float64 delta = rng_->get(-0.1f, 0.1f);
409 
410  if(a_or_b > 0.0)
411  {
412  child[i]->setB(child[i]->getB(index) * delta, index);
413  }
414  else
415  {
416  child[i]->setA(child[i]->getA(index) * delta, index);
417  }
418  }
419 
420  #ifdef GA_DEBUG
421  cout << __LINE__ << "\tmom.getSum() = " << mom->getSum()
422  << "\terror = " << error << endl << endl;
423  #endif
424 
425  // These two children randomly swap mom & dad coeffs
426  *child[10] = *mom;
427  *child[11] = *dad;
428 
429  uint32 start_index = rng_->get(0, n_poles_);
430  uint32 stop_index = rng_->get(0, n_poles_);
431 
432  if(stop_index >= n_poles_) stop_index = n_poles_ - 1;
433 
434  if(start_index > stop_index)
435  {
436  uint32 temp = start_index;
437  start_index = stop_index;
438  stop_index = temp;
439  }
440 
441  for(uint32 j = start_index; j < stop_index; ++j)
442  {
443  child[10]->setB(dad->getB(j), j);
444  child[11]->setB(mom->getB(j), j);
445  }
446 
447  #ifdef GA_DEBUG
448  cout << __LINE__ << "\tmom.getSum() = " << mom->getSum()
449  << "\terror = " << error << endl << endl;
450  #endif
451 
452  start_index = rng_->get(0, n_poles_);
453  stop_index = rng_->get(0, n_poles_);
454 
455  if(stop_index >= n_poles_) stop_index = n_poles_ - 1;
456 
457  if(start_index > stop_index)
458  {
459  int32 temp = start_index;
460  start_index = stop_index;
461  stop_index = temp;
462  }
463 
464  for(uint32 j = start_index; j < stop_index; ++j)
465  {
466  child[10]->setA(dad->getA(j), j);
467  child[11]->setA(mom->getA(j), j);
468  }
469 
470  #ifdef GA_DEBUG
471  cout << __LINE__ << "\tmom.getSum() = " << mom->getSum()
472  << "\terror = " << error << endl << endl;
473  #endif
474 
475  // These two children randomly add mom & dad coeffs
476  *child[12] = *mom;
477  *child[13] = *dad;
478 
479  for(uint32 i = 12; i < 14; ++i)
480  {
481  for(uint32 j = 0; j < n_poles_; ++j)
482  {
483  child[i]->setB(child[i]->getB(j) + rng_->get(-0.1f, 0.1f), j);
484  child[i]->setA(child[i]->getA(j) + rng_->get(-0.1f, 0.1f), j);
485  }
486  }
487 
488  // Score the kiddies.
489 
490  rms_error1 = getRMS(*mom, ref_response, type);
491  rms_error2 = getRMS(*dad, ref_response, type);
492 
493  low1 = mom;
494  low2 = dad;
495 
496  for(int32 i = 0; i < N_CHILDREN; ++i)
497  {
498  float64 rms = getRMS(*child[i], ref_response, type);
499 
500  if(rms > LARGE_RMS) continue;
501 
502  if(rms < rms_error1)
503  {
504  rms_error1 = rms;
505  low1 = child[i];
506  }
507  else if(rms < rms_error2)
508  {
509  rms_error2 = rms;
510  low2 = child[i];
511  }
512  }
513 
514  #ifdef GA_DEBUG
515  cout << __LINE__ << "\tmom.getSum() = " << mom->getSum()
516  << "\terror = " << error << endl << endl;
517  #endif
518 
519  *mom = *low1;
520  *dad = *low2;
521 
522  rms_error = getRMS(*mom, ref_response, type);
523  dad_rms_error = getRMS(*dad, ref_response, type);
524 
525  rms_history << rms_error;
526 
527  #ifdef GA_DEBUG
528  cout << __LINE__ << "\tmom.getSum() = " << mom->getSum()
529  << "\terror = " << error << endl;
530  #endif
531 
532  // Check if dad is unstable.
533  if(dad_rms_error > LARGE_RMS)
534  {
535  cout << PRINT_LINE << "dad's RMS > 10, ballistic!" << endl;
536  dad->randomize(-0.01f, 0.01f);
537  }
538 
539  // Check if our evolution is getting stale
540  if(std::fabs(rms_error - last_rms_error) < STALE_RMS)
541  {
542  ++stale_count;
543  }
544  else
545  {
546  stale_count = 0;
547  }
548 
549  last_rms_error = rms_error;
550 
551  if(count % 200 == 0)
552  {
553  cout << "Generation " << count << endl
554  << " 1st maximum RMS = " << rms_error << endl
555  << " 2nd maximum RMS = " << dad_rms_error << endl;
556 //~ << " major_stale_count = " << major_stale_count << endl;
557  }
558 
559  if(stale_count > 6)
560  {
561  for(uint32 i = 0; i < n_poles_; ++i)
562  {
563  dad->setB(dad->getB(i) + rng_->get(-0.10501f, 0.10501f), i);
564  dad->setA(dad->getA(i) + rng_->get(-0.10501f, 0.10501f), i);
565  }
566 
567  stale_count = 0;
568  ++major_stale_count;
569 
570  if(major_stale_count > 1)
571  {
572  for(uint32 i = 0; i < n_poles_; ++i)
573  {
574  dad->setB(dad->getB(i) * rng_->get(-0.501f, 0.501f), i);
575  dad->setA(dad->getA(i) * rng_->get(-0.501f, 0.501f), i);
576  }
577 
578  major_stale_count = 0;
579  }
580  }
581 
582  #ifdef GA_DEBUG
583  cout << __LINE__ << "\tmom.getSum() = " << mom->getSum()
584  << "\terror = " << error << endl;
585  #endif
586 
587  }
588 
589  cout << "count = " << count << " RMS = " << rms_error << endl;
590 
591  // copy mom into the filter.
592  *kernel_ = *mom;
593 
594  for(int32 i = 0; i < N_CHILDREN; ++i)
595  {
596  delete child[i];
597  }
598 
599  delete [] child;
600  delete mom;
601  delete dad;
602 
603  return rms_history;
604 }
unsigned int uint32
Definition: Nsound.h:153
void ga_interleave(Kernel &rhs)
Genitic Algorithm Helper Function: interleave B coefs with B, A with A.
Definition: Kernel.cc:296
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
#define PRINT_LINE
Definition: FilterIIR.cc:55
float64 getB(uint32 i)
Returns kernel 'b[i]' coef.
Definition: Kernel.h:86
float64 getSum() const
Definition: Kernel.cc:154
void setB(const float64 *b)
Copy getBLength() values from the array a into the Kernel. Use setLength() to set the number of value...
Definition: Kernel.cc:233
double float64
Definition: Nsound.h:146
void ga_swap_ab(Kernel &rhs)
Genitic Algorithm Helper Function: swaps the A & B coefs.
Definition: Kernel.cc:275
float64 getRMS(const Kernel &kernel, const Buffer &response, const SignalType type)
Definition: FilterIIR.cc:211
Kernel * kernel_
Definition: FilterIIR.h:166
void randomize(const float64 &min=-1.0, const float64 &max=1.0)
Sets all coefs to random float64 values between min & max.
Definition: Kernel.cc:194
RngTausworthe * rng_
Definition: FilterIIR.h:176
A Buffer for storing audio samples.
Definition: Buffer.h:60
signed int int32
Definition: Nsound.h:142
uint32 get()
Get a random number.
float64 getA(uint32 i)
Returns kernel 'a[i]' coef.
Definition: Kernel.h:70
void setA(const float64 *a)
Copy getALength() values from the array a into the Kernel. Use setLength() to set the number of value...
Definition: Kernel.cc:211
float64 FilterIIR::getRMS ( const Kernel kernel,
const Buffer response,
const SignalType  type 
)
protected

Definition at line 211 of file FilterIIR.cc.

References FREQUENCY_RESSPONSE, Nsound::Filter::getFrequencyResponse(), getImpulseResponse(), Nsound::Buffer::getLength(), Nsound::Buffer::getSum(), kernel_, and reset().

Referenced by designKernel().

215 {
216  Kernel * orig_k = kernel_;
217 
218  kernel_ = const_cast<Kernel *>(&k);
219 
220  reset();
221 
222  Buffer r;
223 
224  if(type == FREQUENCY_RESSPONSE)
225  {
226  r = getFrequencyResponse(ref_response.getLength() * 2);
227  }
228  else
229  {
230  r = getImpulseResponse(ref_response.getLength() * 2);
231  }
232 
233  kernel_ = orig_k;
234 
235  Buffer delta = ref_response - r;
236  delta *= delta;
237 
238  float64 rms = delta.getSum() / static_cast<float64>(delta.getLength());
239 
240  rms = ::sqrt(rms);
241 
242  return rms;
243 }
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
Kernel * kernel_
Definition: FilterIIR.h:166
Buffer getImpulseResponse(const uint32 n_samples=8192)
Definition: FilterIIR.h:114
float64 getSum() const
Returns the sum of all samples.
Definition: Buffer.cc:1118
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer getFrequencyResponse(const uint32 n_fft=8192)
Definition: Filter.cc:210
void FilterIIR::savePlot ( const Kernel k,
const Buffer response,
uint32  n,
const float64 error 
)
protected

Definition at line 735 of file FilterIIR.cc.

References Nsound::Plotter::figure(), Nsound::Filter::getFrequencyAxis(), Nsound::Filter::getFrequencyResponse(), Nsound::Buffer::getLength(), Nsound::Buffer::getMax(), kernel_, Nsound::Plotter::plot(), reset(), Nsound::Plotter::text(), Nsound::Plotter::title(), Nsound::Plotter::xlabel(), Nsound::Plotter::xlim(), Nsound::Plotter::ylabel(), and Nsound::Plotter::ylim().

740 {
741  Kernel * orig_k = kernel_;
742 
743  kernel_ = const_cast<Kernel *>(&k);
744 
745  Buffer fr_ref = fr_reference;
747  Buffer faxis = getFrequencyAxis();
748 
749  while(fr_ref.getLength() < fr.getLength())
750  {
751  fr_ref << 0.0;
752  }
753 
754  float64 max = fr_ref.getMax();
755 
756  Plotter pylab;
757 
758  pylab.figure();
759  pylab.plot(faxis, fr_ref);
760  pylab.plot(faxis, fr);
761 
762  pylab.xlabel("Frequency Hz");
763  pylab.ylabel("Frequency Response");
764 
765  char buffer[256];
766 
767  sprintf(buffer, "Generation %06d", n);
768 
769  pylab.title(std::string(buffer));
770 
771  pylab.xlim(0.0, faxis[fr_reference.getLength() - 1]);
772  pylab.ylim(0.0, 1.1 * max);
773 
774  sprintf(buffer, "Error = %5.2f", error);
775 
776  pylab.text(
777  faxis[static_cast<uint32>(faxis.getLength() * 0.66)],
778  0.66,
779  std::string(buffer));
780 
781  sprintf(buffer, "generation_%06d.png", n);
782 
783 //~ pylab.savefig(std::string(buffer));
784 
785  kernel_ = orig_k;
786 
787  reset();
788 }
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
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 xlim(const float64 &xmin, const float64 &xmax)
Sets the limit for the x & y axis.
Definition: Plotter.cc:389
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
void ylim(const float64 &ymin, const float64 &ymax)
Definition: Plotter.cc:422
Kernel * kernel_
Definition: FilterIIR.h:166
Buffer getFrequencyAxis(const uint32 n_fft=8192)
Definition: Filter.cc:185
void text(const float64 &x, const float64 &y, const std::string &text, const std::string &kwargs="")
Adds text at the x, y data point.
Definition: Plotter.cc:1092
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
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 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(), getRMS(), main(), Nsound::Filter::plot(), and 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::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 Nsound::FilterParametricEqualizer::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

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  out,
const FilterIIR rhs 
)
friend

Prints the coeffents.

Member Data Documentation

uint32 Nsound::FilterIIR::n_poles_
protected

Definition at line 164 of file FilterIIR.h.

Referenced by designKernel(), filter(), FilterIIR(), getKernelSize(), operator=(), and reset().

Kernel* Nsound::FilterIIR::kernel_
protected
float64* Nsound::FilterIIR::x_history_
protected

Definition at line 168 of file FilterIIR.h.

Referenced by filter(), FilterIIR(), operator=(), reset(), and ~FilterIIR().

float64* Nsound::FilterIIR::x_ptr_
protected

Definition at line 169 of file FilterIIR.h.

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

float64* Nsound::FilterIIR::x_end_ptr_
protected

Definition at line 170 of file FilterIIR.h.

Referenced by filter(), and FilterIIR().

float64* Nsound::FilterIIR::y_history_
protected

Definition at line 172 of file FilterIIR.h.

Referenced by filter(), FilterIIR(), operator=(), reset(), and ~FilterIIR().

float64* Nsound::FilterIIR::y_ptr_
protected

Definition at line 173 of file FilterIIR.h.

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

float64* Nsound::FilterIIR::y_end_ptr_
protected

Definition at line 174 of file FilterIIR.h.

Referenced by filter(), and FilterIIR().

RngTausworthe* Nsound::FilterIIR::rng_
protected

Definition at line 176 of file FilterIIR.h.

Referenced by designKernel(), FilterIIR(), operator=(), and ~FilterIIR().

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(), Nsound::FilterParametricEqualizer::makeKernel(), Nsound::FilterPhaser::operator=(), Nsound::FilterLeastSquaresFIR::operator=(), Nsound::FilterFlanger::operator=(), 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(), Nsound::FilterParametricEqualizer::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: