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

A class for filtering audio in the frequecy domain. More...

#include <Nsound/FilterStageIIR.h>

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

Classes

struct  Kernel
 A class to store calculated kernels. More...
 

Public Types

enum  Type { LOW_PASS, HIGH_PASS }
 

Public Member Functions

 FilterStageIIR (Type type, const float64 &sample_rate, uint32 n_poles, const float64 &frequency, const float64 &percent_ripple)
 
 FilterStageIIR (const FilterStageIIR &copy)
 
virtual ~FilterStageIIR ()
 
AudioStream filter (const AudioStream &x)
 
AudioStream filter (const AudioStream &x, const float64 &f)
 
AudioStream filter (const AudioStream &x, const Buffer &frequencies)
 
Buffer filter (const Buffer &x)
 
Buffer filter (const Buffer &x, const float64 &f)
 
Buffer filter (const Buffer &x, const Buffer &frequencies)
 
float64 filter (const float64 &x)
 
float64 filter (const float64 &x, const float64 &frequency)
 
void makeKernel (const float64 &frequency)
 
FilterStageIIRoperator= (const FilterStageIIR &rhs)
 
void reset ()
 
void setRealtime (bool flag)
 
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 Types

typedef std::set< KernelKernelCache
 

Protected Member Functions

void makeIIRKernelHelper (const float64 &frequency, float64 *a, float64 *b, uint32 p)
 

Protected Attributes

Type type_
 
uint32 n_poles_
 
float64 frequency_
 
float64 percent_ripple_
 
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_
 

Detailed Description

A class for filtering audio in the frequecy domain.

Definition at line 49 of file FilterStageIIR.h.

Member Typedef Documentation

typedef std::set<Kernel> Nsound::FilterStageIIR::KernelCache
protected

Definition at line 141 of file FilterStageIIR.h.

Member Enumeration Documentation

Enumerator
LOW_PASS 
HIGH_PASS 

Definition at line 53 of file FilterStageIIR.h.

Constructor & Destructor Documentation

FilterStageIIR::FilterStageIIR ( Type  type,
const float64 sample_rate,
uint32  n_poles,
const float64 frequency,
const float64 percent_ripple 
)

Definition at line 44 of file FilterStageIIR.cc.

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

50  :
51  Filter(sample_rate),
52  type_(type),
53  n_poles_(n_poles),
54  frequency_(frequency),
55  percent_ripple_(percent_ripple),
56  a_(NULL),
57  b_(NULL),
58  x_history_(NULL),
59  x_ptr_(NULL),
60  x_end_ptr_(NULL),
61  y_history_(NULL),
62  y_ptr_(NULL),
63  y_end_ptr_(NULL),
65 {
66  if(n_poles_ > 20)
67  {
68  n_poles_ = 20;
69  }
70  else if(n_poles_ < 2)
71  {
72  n_poles_ = 2;
73  }
74 
75  if(n_poles_ % 2 != 0)
76  {
77  ++n_poles_;
78  }
79 
80  x_history_ = new float64 [n_poles_ + 1];
83 
84  y_history_ = new float64 [n_poles_ + 1];
86  y_end_ptr_ = y_history_ + n_poles_ + 1;
87 
88  reset();
89 }
double float64
Definition: Nsound.h:146
Filter(const float64 &sample_rate)
Definition: Filter.cc:41
FilterStageIIR::FilterStageIIR ( const FilterStageIIR copy)

Definition at line 93 of file FilterStageIIR.cc.

94  :
95  Filter(copy.sample_rate_),
96  type_(copy.type_),
97  n_poles_(copy.n_poles_),
98  frequency_(copy.frequency_),
100  a_(NULL),
101  b_(NULL),
102  x_history_(NULL),
103  x_ptr_(NULL),
104  x_end_ptr_(NULL),
105  y_history_(NULL),
106  y_ptr_(NULL),
107  y_end_ptr_(NULL),
108  kernel_cache_()
109 {
110  *this = copy;
111 }
Filter(const float64 &sample_rate)
Definition: Filter.cc:41
float64 sample_rate_
Definition: Filter.h:113
FilterStageIIR::~FilterStageIIR ( )
virtual

Definition at line 115 of file FilterStageIIR.cc.

References kernel_cache_, x_history_, and y_history_.

116 {
117  delete [] x_history_;
118  delete [] y_history_;
119 
120  FilterStageIIR::KernelCache::iterator itor = kernel_cache_.begin();
121  FilterStageIIR::KernelCache::iterator end = kernel_cache_.end();
122 
123  while(itor != end)
124  {
125  delete [] itor->b_;
126  delete [] itor->a_;
127  ++itor;
128  }
129 }

Member Function Documentation

AudioStream FilterStageIIR::filter ( const AudioStream x)

Definition at line 133 of file FilterStageIIR.cc.

References Nsound::Filter::filter().

Referenced by Nsound::FilterHighPassIIR::filter(), Nsound::FilterLowPassIIR::filter(), and filter().

134 {
135  return Filter::filter(x);
136 };
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
AudioStream FilterStageIIR::filter ( const AudioStream x,
const float64 f 
)

Definition at line 141 of file FilterStageIIR.cc.

References Nsound::Filter::filter().

142 {
143  return Filter::filter(x, f);
144 }
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
AudioStream FilterStageIIR::filter ( const AudioStream x,
const Buffer frequencies 
)

Definition at line 148 of file FilterStageIIR.cc.

References Nsound::Filter::filter().

149 {
150  return Filter::filter(x, frequencies);
151 }
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
Buffer FilterStageIIR::filter ( const Buffer x)

Definition at line 155 of file FilterStageIIR.cc.

References Nsound::Filter::filter().

156 {
157  return Filter::filter(x);
158 }
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
Buffer FilterStageIIR::filter ( const Buffer x,
const float64 f 
)

Definition at line 162 of file FilterStageIIR.cc.

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

163 {
164  // Instead of calling Filter::filter(x,f), we implement this function here
165  // to avoid calling makeKernel(f) for each sample.
166 
167 //~ cout << "f = " << f << endl;
168 
171 
172  Buffer::const_iterator itor = x.begin();
173  Buffer::const_iterator end = x.end();
174 
175  Buffer y(x.getLength());
176  while(itor != end)
177  {
178  y << FilterStageIIR::filter(*itor);
179  ++itor;
180  }
181 
182  return y;
183 }
AudioStream filter(const AudioStream &x)
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
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
A Buffer for storing audio samples.
Definition: Buffer.h:60
void makeKernel(const float64 &frequency)
Buffer FilterStageIIR::filter ( const Buffer x,
const Buffer frequencies 
)

Definition at line 187 of file FilterStageIIR.cc.

References Nsound::Filter::filter().

188 {
189  return Filter::filter(x, frequencies);
190 }
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
float64 FilterStageIIR::filter ( const float64 x)
virtual

Implements Nsound::Filter.

Definition at line 271 of file FilterStageIIR.cc.

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

272 {
273  // Write x to history
274  *x_ptr_ = x;
275 
276  // Increment history pointer
277  ++x_ptr_;
278 
279  // Bounds check
280  if(x_ptr_ >= x_end_ptr_)
281  {
282  x_ptr_ = x_history_;
283  }
284 
285  float64 y = 0.0;
286  float64 * x_hist = x_ptr_;
287  for(float64 * b = b_; b != b_ + n_poles_ + 1; ++b)
288  {
289  // When we enter this loop, x_hist is pointing at x[n + 1]
290  --x_hist;
291 
292  // Bounds check
293  if(x_hist < x_history_)
294  {
295  x_hist = x_end_ptr_ - 1;
296  }
297 
298  y += *b * *x_hist;
299  }
300 
301  float64 * y_hist = y_ptr_;
302  for(float64 * a = a_ + 1; a != a_ + n_poles_ + 1; ++a)
303  {
304  // When we enter this loop, y_hist is pointing at y[n + 1]
305  --y_hist;
306 
307  // Bounds check
308  if(y_hist < y_history_)
309  {
310  y_hist = y_end_ptr_ - 1;
311  }
312 
313  y += *a * *y_hist;
314 
315  }
316 
317  // insert output into history buffer
318  *y_ptr_ = y;
319 
320  // Increment history pointer
321  ++y_ptr_;
322 
323  // Bounds check
324  if(y_ptr_ >= y_end_ptr_)
325  {
326  y_ptr_ = y_history_;
327  }
328 
329  return y;
330 }
double float64
Definition: Nsound.h:146
float64 FilterStageIIR::filter ( const float64 x,
const float64 frequency 
)
virtual

Implements Nsound::Filter.

Definition at line 334 of file FilterStageIIR.cc.

References filter(), and makeKernel().

335 {
336  makeKernel(frequency);
337  return FilterStageIIR::filter(x);
338 }
AudioStream filter(const AudioStream &x)
void makeKernel(const float64 &frequency)
void FilterStageIIR::makeKernel ( const float64 frequency)

Definition at line 342 of file FilterStageIIR.cc.

References a_, b_, HIGH_PASS, kernel_cache_, LOW_PASS, makeIIRKernelHelper(), n_poles_, Nsound::Filter::sample_rate_, and type_.

Referenced by filter(), Nsound::FilterHighPassIIR::makeKernel(), Nsound::FilterLowPassIIR::makeKernel(), and reset().

343 {
345  Kernel new_kernel(static_cast<uint32>(frequency));
346 
347  // See if the kernel is in the cache.
348 
349  FilterStageIIR::
350  KernelCache::const_iterator itor = kernel_cache_.find(new_kernel);
351 
352  if(itor != kernel_cache_.end())
353  {
354  b_ = itor->b_;
355  a_ = itor->a_;
356 
357  return;
358  }
359 
360  // Allocate new kernel.
361  new_kernel.b_ = new float64 [n_poles_ + 1];
362  new_kernel.a_ = new float64 [n_poles_ + 1];
363 
364  b_ = new_kernel.b_;
365  a_ = new_kernel.a_;
366 
367  // No kernel in cache, must make it.
368 
369  if(type_ == LOW_PASS && frequency < 1.0)
370  {
371  // create no pass filter
372  memset(new_kernel.b_, 0, sizeof(float64) * (n_poles_ + 1));
373  memset(new_kernel.a_, 0, sizeof(float64) * (n_poles_ + 1));
374  }
375 
376  else if(type_ == LOW_PASS && frequency >= (sample_rate_ / 2.0))
377  {
378  // create all pass
379  memset(new_kernel.b_, 0, sizeof(float64) * (n_poles_ + 1));
380  memset(new_kernel.a_, 0, sizeof(float64) * (n_poles_ + 1));
381 
382  b_[0] = 1.0;
383  }
384 
385  else if(type_ == HIGH_PASS && frequency < 1.0)
386  {
387  // create all pass
388  memset(new_kernel.b_, 0, sizeof(float64) * (n_poles_ + 1));
389  memset(new_kernel.a_, 0, sizeof(float64) * (n_poles_ + 1));
390 
391  b_[0] = 1.0;
392  }
393 
394  else if(type_ == HIGH_PASS && frequency >= (sample_rate_ / 2.0))
395  {
396  // create no pass filter
397  memset(new_kernel.b_, 0, sizeof(float64) * (n_poles_ + 1));
398  memset(new_kernel.a_, 0, sizeof(float64) * (n_poles_ + 1));
399  }
400 
401  else
402  {
403 
404  float64 B[23];
405  float64 A[23];
406 
407  float64 TB[23];
408  float64 TA[23];
409 
410  memset(B,0,sizeof(float64) * 23);
411  memset(A,0,sizeof(float64) * 23);
412  memset(TB,0,sizeof(float64) * 23);
413  memset(TA,0,sizeof(float64) * 23);
414 
415  B[2] = A[2] = 1.0;
416 
417  for(uint32 p = 1; p <= n_poles_ / 2; ++p)
418  {
419  float64 b[3];
420  float64 a[3];
421 
422  makeIIRKernelHelper(frequency, b, a, p);
423 
424  memcpy(TB,B, sizeof(float64) * 23);
425  memcpy(TA,A, sizeof(float64) * 23);
426 
427  for(uint32 i = 2; i <= 22; ++i)
428  {
429  B[i] = b[0] * TB[i] + b[1] * TB[i - 1] + b[2] * TB[i - 2];
430  A[i] = TA[i] - a[1] * TA[i - 1] - a[2] * TA[i - 2];
431  }
432  }
433 
434  A[2] = 0.0;
435  for(uint32 i = 0; i <= 20; ++i)
436  {
437  B[i] = B[i + 2];
438  A[i] = -1.0 * A[i + 2];
439  }
440 
441  float64 sumB = 0.0;
442  float64 sumA = 0.0;
443 
444  for(int32 i = 0; i <= 22; ++i)
445  {
446  if(type_ == LOW_PASS)
447  {
448  sumB += B[i];
449  sumA += A[i];
450  }
451  else if(type_ == HIGH_PASS)
452  {
453  sumB += B[i] * pow(-1.0,i);
454  sumA += A[i] * pow(-1.0,i);
455  }
456  }
457  float64 gain = sumB / (1.0 - sumA);
458 
459  for(uint32 i = 0; i <= 22; ++i)
460  {
461  B[i] /= gain;
462  }
463 
464  memcpy(b_, B, sizeof(float64) * (n_poles_ + 1));
465  memcpy(a_, A, sizeof(float64) * (n_poles_ + 1));
466  }
467 
468  // Add the new kernel to the cache.
469  kernel_cache_.insert(new_kernel);
470 }
unsigned int uint32
Definition: Nsound.h:153
A class to store calculated kernels.
double float64
Definition: Nsound.h:146
signed int int32
Definition: Nsound.h:142
void makeIIRKernelHelper(const float64 &frequency, float64 *a, float64 *b, uint32 p)
float64 sample_rate_
Definition: Filter.h:113
FilterStageIIR & FilterStageIIR::operator= ( const FilterStageIIR rhs)

Definition at line 195 of file FilterStageIIR.cc.

References Nsound::FilterStageIIR::Kernel::a_, Nsound::FilterStageIIR::Kernel::b_, frequency_, kernel_cache_, n_poles_, percent_ripple_, reset(), type_, x_end_ptr_, x_history_, x_ptr_, y_end_ptr_, y_history_, and y_ptr_.

196 {
197  if(this == &rhs) return *this;
198 
199  uint32 n_poles_orig = n_poles_;
200 
201  type_ = rhs.type_;
202  n_poles_ = rhs.n_poles_;
203  frequency_ = rhs.frequency_;
205 
206  // Clear the kernal cache.
207  KernelCache::iterator itor = kernel_cache_.begin();
208 
209  while(itor != kernel_cache_.end())
210  {
211  delete [] itor->b_;
212  delete [] itor->a_;
213  ++itor;
214  }
215 
216  kernel_cache_.clear();
217 
218  // Copy kernal cache.
219  itor = rhs.kernel_cache_.begin();
220  while(itor != rhs.kernel_cache_.end())
221  {
222  Kernel new_kernel(static_cast<uint32>(itor->frequency_));
223 
224  // Allocate new kernel memory.
225  new_kernel.b_ = new float64 [n_poles_ + 1];
226  new_kernel.a_ = new float64 [n_poles_ + 1];
227 
228  // Copy values.
229  memcpy(new_kernel.b_, itor->b_, sizeof(float64) * (n_poles_ + 1));
230  memcpy(new_kernel.a_, itor->a_, sizeof(float64) * (n_poles_ + 1));
231 
232  // Insert into cache.
233  kernel_cache_.insert(new_kernel);
234  }
235 
236  // Setup history memory.
237  if(n_poles_orig != rhs.n_poles_)
238  {
239  delete [] x_history_;
240  delete [] y_history_;
241 
242  x_history_ = new float64 [n_poles_ + 1];
243  x_ptr_ = x_history_;
245 
246  y_history_ = new float64 [n_poles_ + 1];
247  y_ptr_ = y_history_;
248  y_end_ptr_ = y_history_ + n_poles_ + 1;
249  }
250 
251  reset();
252 
253  return *this;
254 }
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
void FilterStageIIR::reset ( )
virtual

Implements Nsound::Filter.

Definition at line 258 of file FilterStageIIR.cc.

References frequency_, makeKernel(), n_poles_, x_history_, x_ptr_, y_history_, and y_ptr_.

Referenced by filter(), FilterStageIIR(), operator=(), Nsound::FilterHighPassIIR::reset(), and Nsound::FilterLowPassIIR::reset().

259 {
260  memset(x_history_, 0, sizeof(float64) * (n_poles_ + 1));
261  memset(y_history_, 0, sizeof(float64) * (n_poles_ + 1));
262 
263  x_ptr_ = x_history_;
264  y_ptr_ = y_history_;
265 
267 }
double float64
Definition: Nsound.h:146
void makeKernel(const float64 &frequency)
void FilterStageIIR::makeIIRKernelHelper ( const float64 frequency,
float64 a,
float64 b,
uint32  p 
)
protected

Definition at line 474 of file FilterStageIIR.cc.

References HIGH_PASS, LOW_PASS, M_PI, n_poles_, percent_ripple_, Nsound::Filter::sample_rate_, and type_.

Referenced by makeKernel().

479 {
480  float64 n_poles = static_cast<float64>(n_poles_);
481 
482  float64 RP = -1.0 * cos(M_PI / (n_poles * 2.0) + (p - 1) * (M_PI / n_poles));
483  float64 IP = sin(M_PI / (n_poles * 2.0) + (p - 1) * (M_PI / n_poles));
484 
485  float64 ES = 0.0;
486  float64 VX = 0.0;
487  float64 KX = 0.0;
488 
489  if(percent_ripple_ != 0.0)
490  {
491  float64 temp = (1.0 / (1.0 - percent_ripple_));
492 
493  ES = sqrt(temp * temp - 1.0);
494 
495  VX = (1.0 / n_poles) * log((1.0 / ES) + sqrt((1.0 /(ES * ES)) + 1.0));
496  KX = (1.0 / n_poles) * log((1.0 / ES) + sqrt((1.0 /(ES * ES)) - 1.0));
497  KX = (exp(KX) + exp(-KX)) / 2.0;
498  RP = RP * ( (exp(VX) - exp(-1.0 * VX) ) / 2.0) / KX;
499  IP = IP * ( (exp(VX) + exp(-1.0 * VX) ) / 2.0) / KX;
500  }
501 
502  /* DEBUG
503  if(p == 1)
504  {
505  std::cout << endl
506  << "RP = " << RP << endl
507  << "IP = " << IP << endl
508  << "ES = " << ES << endl
509  << "VX = " << VX << endl
510  << "KX = " << KX << endl << endl;
511  }
512  */
513 
514 
515  float64 T = 2.0 * tan(0.5);
516  float64 W = 2.0 * M_PI * (frequency / static_cast<float64>(sample_rate_));
517  float64 M = RP * RP + IP * IP;
518  float64 D = 4.0 - (4.0 * RP * T) + (M * T * T);
519  float64 X0 = T * T / D;
520  float64 X1 = 2.0 * X0;
521  float64 X2 = X0;
522  float64 Y1 = (8.0 - 2.0 * M * T * T) / D;
523  float64 Y2 = (-4.0 - (4.0 * RP * T) - M * T * T) / D;
524 
525  /* DEBUG
526  if(p == 1)
527  {
528  std::cout << "T = " << T << endl
529  << "W = " << W << endl
530  << "M = " << M << endl
531  << "D = " << D << endl
532  << "X0 = " << X0 << endl
533  << "X1 = " << X1 << endl
534  << "X2 = " << X2 << endl
535  << "Y1 = " << Y1 << endl
536  << "Y2 = " << Y2 << endl << endl;
537  }
538  */
539 
540 
541  // Low Pass
542  float64 K = 0.0;
543 
544  if(type_ == LOW_PASS)
545  {
546  K = sin(0.5 - W / 2.0) / sin(0.5 + W / 2.0);
547  }
548 
549  // High Pass
550  else if(type_ == HIGH_PASS)
551  {
552  K = -1.0 * cos(W / 2.0 + 0.5) / cos(W / 2.0 - 0.5);
553  }
554 
555  D = 1.0 + Y1 * K - Y2 * K * K;
556  b[0] = (X0 - X1 * K + X2 * K * K) / D;
557  b[1] = (-2.0 * X0 *K + X1 + X1 * K * K - 2.0 * X2 * K) / D;
558  b[2] = (X0 * K * K - X1 * K + X2) / D;
559  a[1] = (2.0 * K + Y1 + Y1 * K * K - 2.0 * Y2 * K) / D;
560  a[2] = (-1.0 * K * K - Y1 * K + Y2) / D;
561 
562  /* DEBUG
563  if(p == 1)
564  {
565  std::cout << "W = " << W << endl
566  << "K = " << K << endl
567  << "D = " << D << endl
568  << "A0 = " << a[0] << endl
569  << "A1 = " << a[1] << endl
570  << "A2 = " << a[2] << endl
571  << "B1 = " << b[1] << endl
572  << "B2 = " << b[2] << endl << endl;
573  }
574  */
575 
576  if(type_ == HIGH_PASS)
577  {
578  b[1] = -b[1];
579  a[1] = -a[1];
580  }
581 
582  /* DEBUG
583  if(p == 1)
584  {
585  std::cout << "A0 = " << a[0] << endl
586  << "A1 = " << a[1] << endl
587  << "A2 = " << a[2] << endl
588  << "B1 = " << b[1] << endl
589  << "B2 = " << b[2] << endl << endl;
590  }
591  */
592 }
#define M_PI
Definition: Nsound.h:121
double float64
Definition: Nsound.h:146
float64 sample_rate_
Definition: Filter.h:113
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
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::FilterBandRejectIIR::plot(), Nsound::FilterBandPassIIR::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

Member Data Documentation

Type Nsound::FilterStageIIR::type_
protected

Definition at line 112 of file FilterStageIIR.h.

Referenced by makeIIRKernelHelper(), makeKernel(), and operator=().

uint32 Nsound::FilterStageIIR::n_poles_
protected
float64 Nsound::FilterStageIIR::frequency_
protected
float64 Nsound::FilterStageIIR::percent_ripple_
protected

Definition at line 115 of file FilterStageIIR.h.

Referenced by makeIIRKernelHelper(), and operator=().

float64* Nsound::FilterStageIIR::a_
protected

Definition at line 117 of file FilterStageIIR.h.

Referenced by filter(), and makeKernel().

float64* Nsound::FilterStageIIR::b_
protected

Definition at line 118 of file FilterStageIIR.h.

Referenced by filter(), and makeKernel().

float64* Nsound::FilterStageIIR::x_history_
protected

Definition at line 120 of file FilterStageIIR.h.

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

float64* Nsound::FilterStageIIR::x_ptr_
protected

Definition at line 121 of file FilterStageIIR.h.

Referenced by filter(), FilterStageIIR(), operator=(), and reset().

float64* Nsound::FilterStageIIR::x_end_ptr_
protected

Definition at line 122 of file FilterStageIIR.h.

Referenced by filter(), FilterStageIIR(), and operator=().

float64* Nsound::FilterStageIIR::y_history_
protected

Definition at line 124 of file FilterStageIIR.h.

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

float64* Nsound::FilterStageIIR::y_ptr_
protected

Definition at line 125 of file FilterStageIIR.h.

Referenced by filter(), FilterStageIIR(), operator=(), and reset().

float64* Nsound::FilterStageIIR::y_end_ptr_
protected

Definition at line 126 of file FilterStageIIR.h.

Referenced by filter(), FilterStageIIR(), and operator=().

KernelCache Nsound::FilterStageIIR::kernel_cache_
protected

Definition at line 143 of file FilterStageIIR.h.

Referenced by makeKernel(), operator=(), and ~FilterStageIIR().

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(), makeIIRKernelHelper(), Nsound::FilterHighPassFIR::makeKernel(), makeKernel(), Nsound::FilterBandPassVocoder::makeKernel(), Nsound::FilterLeastSquaresFIR::makeKernel(), Nsound::FilterParametricEqualizer::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::FilterCombLowPassFeedback::plot(), Nsound::FilterLowPassIIR::plot(), Nsound::FilterLowPassFIR::plot(), Nsound::FilterAllPass::plot(), Nsound::FilterHighPassFIR::plot(), Nsound::FilterBandPassFIR::plot(), Nsound::FilterLeastSquaresFIR::plot(), Nsound::FilterFlanger::plot(), Nsound::FilterBandRejectFIR::plot(), Nsound::FilterBandPassIIR::plot(), Nsound::FilterBandRejectIIR::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: