Nsound  0.9.4
Public Member Functions | Protected Attributes | List of all members
Nsound::FilterLeastSquaresFIR Class Reference

A FIR filter that is defined as the least square error to the desired requency response. More...

#include <Nsound/FilterLeastSquaresFIR.h>

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

Public Member Functions

 FilterLeastSquaresFIR (const float64 &sample_rate, uint32 kernel_size, const Buffer &freq_axis, const Buffer &amplitude_axis, const float64 &beta=5.0)
 
 FilterLeastSquaresFIR (const FilterLeastSquaresFIR &copy)
 
virtual ~FilterLeastSquaresFIR ()
 
Buffer getKernel () const
 
void setKernel (const Buffer &k)
 
Buffer getKernelFrequencies ()
 
Buffer getKernelAmplitudes ()
 
AudioStream filter (const AudioStream &x)
 
AudioStream filter (const AudioStream &x, const float64 &frequency)
 
AudioStream filter (const AudioStream &x, const Buffer &frequencies)
 
Buffer filter (const Buffer &x)
 
Buffer filter (const Buffer &x, const float64 &frequency)
 
Buffer filter (const Buffer &x, const Buffer &frequencies)
 
virtual float64 filter (const float64 &x)
 
float64 filter (const float64 &x, const float64 &frequency_Hz)
 
Buffer getImpulseResponse ()
 
FilterLeastSquaresFIRoperator= (const FilterLeastSquaresFIR &rhs)
 
void plot (boolean show_fc=true, boolean show_phase=false)
 
void reset ()
 
void setWindow (WindowType type)
 
void makeKernel (const Buffer &freq_axis, const Buffer &amplitude_axis)
 
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 Attributes

float64b_
 
float64window_
 
float64x_history_
 
float64x_ptr_
 
float64x_end_ptr_
 
Bufferf_axis_
 
Buffera_axis_
 
float64 sample_rate_
 
float64 two_pi_over_sample_rate_
 
float64 sample_time_
 
uint32 kernel_size_
 
bool is_realtime_
 

Detailed Description

A FIR filter that is defined as the least square error to the desired requency response.

Based on Matlab's firls function.

Definition at line 48 of file FilterLeastSquaresFIR.h.

Constructor & Destructor Documentation

FilterLeastSquaresFIR::FilterLeastSquaresFIR ( const float64 sample_rate,
uint32  kernel_size,
const Buffer freq_axis,
const Buffer amplitude_axis,
const float64 beta = 5.0 
)

Definition at line 51 of file FilterLeastSquaresFIR.cc.

References a_axis_, b_, Nsound::Generator::drawWindowKaiser(), f_axis_, Nsound::Buffer::getPointer(), Nsound::Filter::kernel_size_, makeKernel(), reset(), window_, x_end_ptr_, x_history_, and x_ptr_.

57  :
58  Filter(sample_rate),
59  b_(NULL),
60  window_(NULL),
61  x_history_(NULL),
62  x_ptr_(NULL),
63  x_end_ptr_(NULL),
64  f_axis_(NULL),
65  a_axis_(NULL)
66 {
67  kernel_size_ = kernel_size;
68 
69  b_ = new float64[kernel_size_];
70 
71  x_history_ = new float64[kernel_size_ + 1];
74 
75  // Create the Kaiser window.
77 
78  Generator gen(1.0);
79 
80  Buffer kaiser = gen.drawWindowKaiser(kernel_size_, beta);
81 
82  memcpy(window_, kaiser.getPointer(), sizeof(float64) * kernel_size_);
83 
84  // Allocate f & a axis.
85  f_axis_ = new Buffer(16);
86  a_axis_ = new Buffer(16);
87 
88  FilterLeastSquaresFIR::makeKernel(freq_axis, amplitude_axis);
90 }
void makeKernel(const Buffer &freq_axis, const Buffer &amplitude_axis)
double float64
Definition: Nsound.h:146
uint32 kernel_size_
Definition: Filter.h:116
const float64 * getPointer() const
Returns the raw point to the first sample in the Buffer.
Definition: Buffer.h:1382
Filter(const float64 &sample_rate)
Definition: Filter.cc:41
A Buffer for storing audio samples.
Definition: Buffer.h:60
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
FilterLeastSquaresFIR::FilterLeastSquaresFIR ( const FilterLeastSquaresFIR copy)

Definition at line 95 of file FilterLeastSquaresFIR.cc.

References a_axis_, b_, f_axis_, Nsound::Filter::kernel_size_, window_, x_end_ptr_, x_history_, and x_ptr_.

96  :
97  Filter(copy.sample_rate_),
98  b_(NULL),
99  window_(NULL),
100  x_history_(NULL),
101  x_ptr_(NULL),
102  x_end_ptr_(NULL),
103  f_axis_(NULL),
104  a_axis_(NULL)
105 {
106  kernel_size_ = copy.kernel_size_;
107 
108  b_ = new float64[kernel_size_];
109 
110  x_history_ = new float64[kernel_size_ + 1];
111  x_ptr_ = x_history_;
113 
114  // Create the Kaiser window.
116 
117  // Allocate f & a axis.
118  f_axis_ = new Buffer(16);
119  a_axis_ = new Buffer(16);
120 
121  // do the rest in the oprator=()
122  *this = copy;
123 }
double float64
Definition: Nsound.h:146
uint32 kernel_size_
Definition: Filter.h:116
Filter(const float64 &sample_rate)
Definition: Filter.cc:41
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 sample_rate_
Definition: Filter.h:113
FilterLeastSquaresFIR::~FilterLeastSquaresFIR ( )
virtual

Definition at line 127 of file FilterLeastSquaresFIR.cc.

References a_axis_, b_, f_axis_, window_, and x_history_.

128 {
129  delete [] b_;
130  delete [] window_;
131  delete [] x_history_;
132  delete f_axis_;
133  delete a_axis_;
134 }

Member Function Documentation

Buffer FilterLeastSquaresFIR::getKernel ( ) const

Definition at line 138 of file FilterLeastSquaresFIR.cc.

References b_, and Nsound::Filter::kernel_size_.

Referenced by Nsound::Buffer::_get_resample().

139 {
140  Buffer k(kernel_size_);
141  for(uint32 i = 0; i < kernel_size_; ++i)
142  {
143  k << b_[i];
144  }
145 
146  return k;
147 }
unsigned int uint32
Definition: Nsound.h:153
uint32 kernel_size_
Definition: Filter.h:116
A Buffer for storing audio samples.
Definition: Buffer.h:60
void FilterLeastSquaresFIR::setKernel ( const Buffer k)

Definition at line 151 of file FilterLeastSquaresFIR.cc.

References b_, Nsound::Generator::drawWindowKaiser(), Nsound::Buffer::getLength(), Nsound::Buffer::getPointer(), Nsound::Filter::kernel_size_, reset(), window_, x_end_ptr_, x_history_, and x_ptr_.

Referenced by Nsound::Buffer::_get_resample().

152 {
153  if(k.getLength() != kernel_size_)
154  {
155  delete [] b_;
156  delete [] window_;
157  delete [] x_history_;
158 
159  kernel_size_ = k.getLength();
160 
161  b_ = new float64[kernel_size_];
162 
163  x_history_ = new float64[kernel_size_ + 1];
164  x_ptr_ = x_history_;
166 
167  // Create the Kaiser window.
169 
170  Generator gen(1.0);
171 
172  Buffer kaiser = gen.drawWindowKaiser(kernel_size_, 5.0);
173 
174  memcpy(window_, kaiser.getPointer(), sizeof(float64) * kernel_size_);
175 
177  }
178 
179  for(uint32 i = 0; i < kernel_size_; ++i)
180  {
181  b_[i] = k[i];
182  }
183 
184  return;
185 }
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
uint32 kernel_size_
Definition: Filter.h:116
const float64 * getPointer() const
Returns the raw point to the first sample in the Buffer.
Definition: Buffer.h:1382
A Buffer for storing audio samples.
Definition: Buffer.h:60
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
Buffer FilterLeastSquaresFIR::getKernelFrequencies ( )

Definition at line 189 of file FilterLeastSquaresFIR.cc.

References f_axis_.

190 {
191  return *f_axis_;
192 }
Buffer FilterLeastSquaresFIR::getKernelAmplitudes ( )

Definition at line 196 of file FilterLeastSquaresFIR.cc.

References a_axis_.

197 {
198  return *a_axis_;
199 }
AudioStream FilterLeastSquaresFIR::filter ( const AudioStream x)

Definition at line 203 of file FilterLeastSquaresFIR.cc.

References Nsound::Filter::filter().

Referenced by Nsound::Buffer::_get_resample(), filter(), and FilterLeastSquaresFIR_UnitTest().

204 {
205  return Filter::filter(x);
206 }
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
AudioStream Nsound::FilterLeastSquaresFIR::filter ( const AudioStream x,
const float64 frequency 
)
inline

Definition at line 80 of file FilterLeastSquaresFIR.h.

References filter().

81  { return filter(x); };
AudioStream filter(const AudioStream &x)
AudioStream Nsound::FilterLeastSquaresFIR::filter ( const AudioStream x,
const Buffer frequencies 
)
inline

Definition at line 85 of file FilterLeastSquaresFIR.h.

References filter().

86  { return filter(x); };
AudioStream filter(const AudioStream &x)
Buffer FilterLeastSquaresFIR::filter ( const Buffer x)

Definition at line 210 of file FilterLeastSquaresFIR.cc.

References Nsound::Filter::filter().

211 {
212  return Filter::filter(x);
213 }
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
Buffer Nsound::FilterLeastSquaresFIR::filter ( const Buffer x,
const float64 frequency 
)
inline

Definition at line 93 of file FilterLeastSquaresFIR.h.

References filter().

94  { return filter(x); };
AudioStream filter(const AudioStream &x)
Buffer Nsound::FilterLeastSquaresFIR::filter ( const Buffer x,
const Buffer frequencies 
)
inline

Definition at line 98 of file FilterLeastSquaresFIR.h.

References filter().

99  { return filter(x); };
AudioStream filter(const AudioStream &x)
float64 FilterLeastSquaresFIR::filter ( const float64 x)
virtual

Implements Nsound::Filter.

Definition at line 217 of file FilterLeastSquaresFIR.cc.

References b_, Nsound::Filter::kernel_size_, x_end_ptr_, x_history_, and x_ptr_.

218 {
219  // Write x to history
220  *x_ptr_ = x;
221 
222  // Increment history pointer
223  ++x_ptr_;
224 
225  // Bounds check.
226  if(x_ptr_ >= x_end_ptr_)
227  {
228  x_ptr_ = x_history_;
229  }
230 
231  // Perform the convolution.
232 
233  // y[n] = kernel_[0] * x[n]
234  // + kernel_[1] * x[n - 1]
235  // + kernel_[2] * x[n - 2]
236  // ...
237  // + kernel_[N] * x[n - N]
238 
239  float64 y = 0.0;
240  float64 * history = x_ptr_;
241  for(float64 * b = b_; b != b_ + kernel_size_; ++b)
242  {
243  // When we enter this loop, history is pointing at x[n + 1].
244  --history;
245 
246  // Bounds check
247  if(history < x_history_)
248  {
249  history = x_end_ptr_ - 1;
250  }
251 
252  y += *b * *history;
253  }
254 
255  return y;
256 }
double float64
Definition: Nsound.h:146
uint32 kernel_size_
Definition: Filter.h:116
float64 Nsound::FilterLeastSquaresFIR::filter ( const float64 x,
const float64 frequency_Hz 
)
inlinevirtual

Implements Nsound::Filter.

Definition at line 107 of file FilterLeastSquaresFIR.h.

References filter().

108  { return filter(x); };
AudioStream filter(const AudioStream &x)
Buffer Nsound::FilterLeastSquaresFIR::getImpulseResponse ( )
inline

Definition at line 112 of file FilterLeastSquaresFIR.h.

References Nsound::Filter::getImpulseResponse().

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

Definition at line 628 of file FilterLeastSquaresFIR.cc.

References a_axis_, b_, f_axis_, Nsound::Filter::kernel_size_, reset(), Nsound::Filter::sample_rate_, window_, x_end_ptr_, x_history_, and x_ptr_.

629 {
630  if(this == &rhs)
631  {
632  return *this;
633  }
634 
635  if(kernel_size_ != rhs.kernel_size_)
636  {
637  delete [] b_;
638  delete [] window_;
639  delete [] x_history_;
640 
642 
643  b_ = new float64[kernel_size_];
644 
645  x_history_ = new float64[kernel_size_ + 1];
646  x_ptr_ = x_history_;
648 
649  // Create the Kaiser window.
651  }
652 
654 
655  *f_axis_ = *rhs.f_axis_;
656  *a_axis_ = *rhs.a_axis_;
657 
658  memcpy(b_, rhs.b_, sizeof(float64) * kernel_size_);
659  memcpy(window_, rhs.window_, sizeof(float64) * kernel_size_);
660 
662 
663  return *this;
664 }
double float64
Definition: Nsound.h:146
uint32 kernel_size_
Definition: Filter.h:116
float64 sample_rate_
Definition: Filter.h:113
void FilterLeastSquaresFIR::plot ( boolean  show_fc = true,
boolean  show_phase = false 
)

Definition at line 668 of file FilterLeastSquaresFIR.cc.

References Nsound::Filter::kernel_size_, Nsound::Filter::plot(), Nsound::Filter::sample_rate_, Nsound::Plotter::subplot(), and Nsound::Plotter::title().

669 {
670  char title[256];
671  sprintf(title,
672  "Least Square FIR Frequency Response\n"
673  "order = %d, sr = %0.1f Hz",
675 
676  Filter::plot(show_phase);
677 
678  Plotter pylab;
679 
680  uint32 n_rows = 1;
681 
682  if(show_phase)
683  {
684  n_rows = 2;
685  }
686 
687  if(show_fc)
688  {
689  pylab.subplot(n_rows, 1, 1);
690 
691  pylab.title(title);
692  }
693 }
unsigned int uint32
Definition: Nsound.h:153
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
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
uint32 kernel_size_
Definition: Filter.h:116
void plot(boolean show_phase=false)
Definition: Filter.cc:262
float64 sample_rate_
Definition: Filter.h:113
void FilterLeastSquaresFIR::reset ( )
virtual

Resets interal history buffer and sets the cut off frequency to the one used at declaration.

Implements Nsound::Filter.

Definition at line 697 of file FilterLeastSquaresFIR.cc.

References Nsound::Filter::kernel_size_, x_history_, and x_ptr_.

Referenced by FilterLeastSquaresFIR(), makeKernel(), operator=(), and setKernel().

698 {
699  memset(x_history_, 0, sizeof(float64) * (kernel_size_ + 1));
700  x_ptr_ = x_history_;
701 }
double float64
Definition: Nsound.h:146
uint32 kernel_size_
Definition: Filter.h:116
void FilterLeastSquaresFIR::setWindow ( WindowType  type)

Definition at line 705 of file FilterLeastSquaresFIR.cc.

References a_axis_, Nsound::Generator::drawWindow(), f_axis_, Nsound::Buffer::getPointer(), Nsound::Filter::kernel_size_, makeKernel(), Nsound::Filter::sample_rate_, and window_.

Referenced by FilterLeastSquaresFIR_UnitTest().

706 {
707  Generator gen(1.0);
708 
709  Buffer window = gen.drawWindow(kernel_size_, type);
710 
711  memcpy(window_, window.getPointer(), sizeof(float64)*kernel_size_);
712 
713  // Make new kernel with window.
714  Buffer f(*f_axis_);
715  Buffer a(*a_axis_);
716 
717  f *= 0.5 * sample_rate_;
718 
719  makeKernel(f, a);
720 }
void makeKernel(const Buffer &freq_axis, const Buffer &amplitude_axis)
double float64
Definition: Nsound.h:146
uint32 kernel_size_
Definition: Filter.h:116
const float64 * getPointer() const
Returns the raw point to the first sample in the Buffer.
Definition: Buffer.h:1382
A Buffer for storing audio samples.
Definition: Buffer.h:60
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
float64 sample_rate_
Definition: Filter.h:113
void FilterLeastSquaresFIR::makeKernel ( const Buffer freq_axis,
const Buffer amplitude_axis 
)

Definition at line 260 of file FilterLeastSquaresFIR.cc.

References a_axis_, b_, Nsound::Generator::drawLine(), f_axis_, Nsound::Buffer::getDerivative(), Nsound::Buffer::getLength(), Nsound::Buffer::getMin(), Nsound::Buffer::getPointer(), Nsound::Buffer::getReverse(), Nsound::Filter::kernel_size_, M_PI, M_THROW, reset(), Nsound::Filter::sample_rate_, Nsound::Buffer::subbuffer(), and window_.

Referenced by FilterLeastSquaresFIR(), FilterLeastSquaresFIR_UnitTest(), and setWindow().

263 {
264  // Reset the buffers.
265  *f_axis_ = Buffer(16);
266  *a_axis_ = Buffer(16);
267 
268  float64 max_freq = sample_rate_ / 2.0;
269 
270  // Validate freq_axis and amplitude_axis.
271  if(freq_axis.getLength() != amplitude_axis.getLength() ||
272  freq_axis.getLength() % 2 != 0)
273  {
274  M_THROW("FilterLeastSquaresFIR::makeKernel(): "
275  << "freq_axis and amplitude_axis must be same length and even!");
276 
277  *f_axis_ << 0.0 << max_freq;
278  *a_axis_ << 1.0 << 1.0;
279 
281  return;
282  }
283 
284  // Veirfy all samples inf freq_axis are in assending order.
285  if(freq_axis[0] != 0.0)
286  {
287  M_THROW("FilterLeastSquaresFIR::makeKernel(): "
288  << "freq_axis[0] must be 0.0!");
289 
290  *f_axis_ << 0.0 << max_freq;
291  *a_axis_ << 1.0 << 1.0;
292 
294  return;
295  }
296 
297  if(freq_axis[freq_axis.getLength() -1 ] != max_freq)
298  {
299  M_THROW("FilterLeastSquaresFIR::makeKernel(): "
300  << "freq_axis[end] (" << freq_axis[freq_axis.getLength() -1 ]
301  << ") must be " << max_freq << "!");
302 
303  *f_axis_ << 0.0 << max_freq;
304  *a_axis_ << 1.0 << 1.0;
305 
307  return;
308  }
309 
310  // Test all samples are in assending order.
311  Buffer df = freq_axis.getDerivative(1);
312  if(df.getMin() < 0.0)
313  {
314  M_THROW("FilterLeastSquaresFIR::makeKernel(): "
315  << "Frequencies in freq_axis must be increasing!");
316 
317  *f_axis_ << 0.0 << max_freq;
318  *a_axis_ << 1.0 << 1.0;
319 
321  return;
322  }
323 
324  // All is okay.
325 
326  *f_axis_ << (freq_axis / max_freq);
327  *a_axis_ << amplitude_axis;
328 
329 //~ // Debug
330 //~ std::cout << "faxis:" << *f_axis_ << std::endl;
331 
332  Generator gen(1.0);
333 
334  Buffer f(*f_axis_/2.0);
335  Buffer a(*a_axis_);
336 
337  Buffer w = gen.drawLine(freq_axis.getLength() / 2, 1.0, 1.0);
338 
339 //~ cout << "N = " << kernel_size_ << endl
340 //~ << "F = " << endl;
341 //~
342 //~ for(uint32 i = 0; i < f.getLength(); ++i)
343 //~ {
344 //~ cout << " " << f[i] << endl;
345 //~ }
346 
347 //~ cout << "M = " << endl;
348 //~
349 //~ for(uint32 i = 0; i < a.getLength(); ++i)
350 //~ {
351 //~ cout << " " << a[i] << endl;
352 //~ }
353 
354 //~ cout << "W = " << endl;
355 //~
356 //~ for(uint32 i = 0; i < w.getLength(); ++i)
357 //~ {
358 //~ cout << " " << w[i] << endl;
359 //~ }
360 
361  df = f.getDerivative(1);
362 
363  // throw out last value
364  df = df.subbuffer(0, df.getLength() -1);
365 
366 //~ cout << "dF = " << endl;
367 
368 //~ for(uint32 i = 0; i < df.getLength(); ++i)
369 //~ {
370 //~ cout << " " << df[i] << endl;
371 //~ }
372 
373  // Check for full band
374 
375  boolean fullband = true;
376 
377 //~ cerr << CERR_HEADER << endl;
378 
379  if(df.getLength() > 1)
380  {
381  for(uint32 i = 1; i < df.getLength() - 1; i += 2)
382  {
383 
384 //~ cerr << "df[" << i << "] = " << df[i] << endl;
385 
386  if(df[i] != 0.0)
387  {
388  fullband = false;
389  break;
390  }
391  }
392  }
393 
394 //~ cout << "fullband = " << fullband << endl;
395 
396  int32 L = (kernel_size_ - 1) / 2;
397 
398 //~ cout << "L = " << L << endl;
399 
400  boolean is_odd = true;
401 
402  if(kernel_size_ % 2 == 0)
403  {
404  is_odd = false;
405  }
406 
407 //~ cout << "Nodd = " << is_odd << endl;
408 
409  Buffer m;
410 
411  if(L > 0)
412  {
413  m = gen.drawLine(L, 0, L);
414  }
415 
416  m << L;
417 
418  if(!is_odd)
419  {
420  m += 0.5;
421  }
422 
423 //~ cout << "length(m) = " << m.getLength() << endl;
424 //~ cout << "m[0] = " << m[0] << endl;
425 //~ cout << "m[end] = " << m[m.getLength()-1] << endl;
426 
427  boolean need_matrix = ! fullband;
428 
429 //~ cout << "need_matrix = " << need_matrix << endl;
430 
431  if(need_matrix)
432  {
433  M_THROW("FilterLeastSquaresFIR::makeKernel(): "
434  << "FIXME: need_matix is true!");
435  }
436 
437  Buffer k(m);
438 
439  float64 b0 = 0.0;
440 
441  if(is_odd)
442  {
443  k = m.subbuffer(1, m.getLength()-1);
444  }
445 
446 //~ cout << "length(k) = " << k.getLength() << endl;
447 //~ cout << "k[0] = " << k[0] << endl;
448 //~ cout << "k[end] = " << k[k.getLength()-1] << endl;
449 
450  float64 PI_x_2 = 2.0 * M_PI;
451 //~ float64 PI_x_4 = 4.0 * M_PI;
452  float64 PI_x_PI_x_4 = 4.0 * M_PI * M_PI;
453 
454  Buffer b = 0.0 * k;
455  Buffer kk = k * k;
456 
457  for(uint32 i = 0; i < f.getLength(); i += 2)
458  {
459  float64 t_df = f[i+1] - f[i];
460 
461  float64 mm = 0.0;
462  float64 b1 = 0.0;
463 
464  mm = (a[i+1] - a[i]) / t_df; // slope
465  b1 = a[i] - mm * f[i]; // y-intercept
466 
467 //~ cout << endl;
468 //~ cout << "s = " << i+1 << endl;
469 //~ cout << "m = " << mm << endl;
470 //~ cout << "b1 = " << b1 << endl;
471 
472  if(is_odd)
473  {
474  b0 += (b1 * t_df + mm / 2.0 * (f[i+1] * f[i+1] - f[i]*f[i]))
475  * w[(i+1)/2] * w[(i+1)/2];
476  }
477 
478 //~ cout << "b0 = " << b0 << endl;
479 
480  Buffer f1 = PI_x_2 * k * f[i+1];
481  Buffer f2 = PI_x_2 * k * f[i];
482 
483  std::stringstream ss;
484 
485  float64 abs_w2 = std::fabs(w[(i+1)/2] * w[(i+1)/2]);
486 
487 //~ cout << "W[" << i << "] = " << w[(i+1)/2] << endl;
488 //~ cout << "abs(W^2) = " << abs_w2 << endl;
489 
490 
491  for(uint32 j = 0; j < b.getLength(); ++j)
492  {
493  b[j] += (mm / PI_x_PI_x_4
494  * (std::cos(f1[j]) - std::cos(f2[j]))
495  / (k[j] * k[j])
496  ) * abs_w2;
497  }
498 
499  float64 fi = f[i];
500  float64 fi1 = f[i+1];
501 
502 //~ ss.str("");
503 //~ ss << "s = " << (i+1) << ", b1";
504 //~ b.plot(ss.str());
505 
506 //~ cout << "F[" << (i) << "] = " << fi << endl;
507 //~ cout << "F[" << (i+1) << "] = " << fi1 << endl;
508 
509 //~ // DEBUG
510 //~ Buffer SINC;
511 //~ for(uint32 j = 0; j < b.getLength(); ++j)
512 //~ {
513 //~ float64 x = PI_x_2 * k[j] * fi1;
514 //~
515 //~ if(x == 0.0) x = M_PI;
516 //~
517 //~ SINC << std::sin(x) / x;
518 //~ }
519 //~
520 //~ ss.str("");
521 //~ ss << "s = " << (i+1) << ", sinc";
522 //~ SINC.plot(ss.str());
523 
524  for(uint32 j = 0; j < b.getLength(); ++j)
525  {
526  float64 t = PI_x_2 * k[j];
527  float64 x = 0.0;
528  float64 sinc = 0.0;
529  float64 sinc1 = 0.0;
530 
531  x = t*fi;
532 
533  if(x == 0.0) x = M_PI;
534 
535  sinc = std::sin(x) / x;
536 
537  x = t*fi1;
538 
539  if(x == 0.0) x = M_PI;
540 
541  sinc1 = std::sin(x) / x;
542 
543  b[j] += ( fi1 * (mm * fi1 + b1) * sinc1
544  - fi * (mm * fi + b1) * sinc )
545  * abs_w2;
546  }
547 
548 //~ ss << "s = " << (i+1) << ", f1";
549 //~ f1.plot(ss.str());
550 //~ ss.str("");
551 //~ ss << "s = " << (i+1) << ", f2";
552 //~ f2.plot(ss.str());
553 //~
554 //~ ss.str("");
555 //~ ss << "s = " << (i+1) << ", b2";
556 //~ b.plot(ss.str());
557 
558  }
559 
560 //~ cout << "below loop" << endl
561 //~ << "b0 = " << b0 << endl;
562 
563 //~ b.plot("below loop: b");
564 
565  if(is_odd)
566  {
567  Buffer t;
568  t << b0 << b;
569  b = t;
570  }
571 
572 //~ b.plot("below loop: b2");
573 
574 //~ a = b;
575  Buffer h;
576 
577  if(! need_matrix)
578  {
579  a = w[0] * w[0] * 4.0 * b;
580 
581  if(is_odd)
582  {
583  a[0] *= 0.5;
584  }
585  }
586 
587  if(is_odd)
588  {
589  float64 a0 = a[0];
590  Buffer a2 = 0.5 * a.subbuffer(1, a.getLength());
591  h = a2.getReverse() << a0 << a2;
592  }
593  else
594  {
595  h = a.getReverse() << a;
596 
597  h *= 0.5;
598  }
599 
600 //~ h.plot("h");
601 //~ Plotter::show();
602 
603  if(kernel_size_ != h.getLength())
604  {
605  M_THROW("FilterLeastSquaresFIR::makeKernel(): "
606  << "kernel_size_ != h.getLength()!");
607 
608  for(uint32 i = 0; i < kernel_size_; ++i)
609  {
610  b_[i] = 0.0;
611  }
612  }
613  else
614  {
615  memcpy(b_, h.getPointer(), sizeof(float64)*kernel_size_);
616  }
617 
618  // Apply window
619  for(uint32 i = 0; i < kernel_size_; ++i)
620  {
621  b_[i] *= window_[i];
622  }
623 }
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
Definition: Buffer.cc:2073
unsigned int uint32
Definition: Nsound.h:153
#define M_PI
Definition: Nsound.h:121
double float64
Definition: Nsound.h:146
Buffer getDerivative(uint32 n) const
Returns the nth derivative of the Buffer.
Definition: Buffer.h:518
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
uint32 kernel_size_
Definition: Filter.h:116
#define M_THROW(message)
Definition: Macros.h:108
const float64 * getPointer() const
Returns the raw point to the first sample in the Buffer.
Definition: Buffer.h:1382
A Buffer for storing audio samples.
Definition: Buffer.h:60
signed int int32
Definition: Nsound.h:142
float64 getMin() const
Returns the minimum sample value in the Buffer.
Definition: Buffer.cc:1004
Buffer getReverse() const
Reverses the samples in a copy of this Buffer.
Definition: Buffer.h:1558
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
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(), 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(), 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

float64* Nsound::FilterLeastSquaresFIR::b_
protected
float64* Nsound::FilterLeastSquaresFIR::window_
protected
float64* Nsound::FilterLeastSquaresFIR::x_history_
protected
float64* Nsound::FilterLeastSquaresFIR::x_ptr_
protected

Definition at line 140 of file FilterLeastSquaresFIR.h.

Referenced by filter(), FilterLeastSquaresFIR(), operator=(), reset(), and setKernel().

float64* Nsound::FilterLeastSquaresFIR::x_end_ptr_
protected

Definition at line 141 of file FilterLeastSquaresFIR.h.

Referenced by filter(), FilterLeastSquaresFIR(), operator=(), and setKernel().

Buffer* Nsound::FilterLeastSquaresFIR::f_axis_
protected
Buffer* Nsound::FilterLeastSquaresFIR::a_axis_
protected
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(), makeKernel(), Nsound::FilterParametricEqualizer::makeKernel(), Nsound::FilterPhaser::operator=(), 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(), plot(), Nsound::FilterFlanger::plot(), Nsound::FilterBandRejectFIR::plot(), Nsound::FilterBandPassIIR::plot(), Nsound::FilterBandRejectIIR::plot(), Nsound::FilterParametricEqualizer::plot(), Nsound::FilterLowPassIIR::setCutoff(), and 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: