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

#include <Nsound/FilterBandRejectFIR.h>

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

Classes

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

Public Member Functions

 FilterBandRejectFIR (const float64 &sample_rate, uint32 kernel_size, const float64 &frequency_Hz_low, const float64 &frequency_Hz_high)
 
virtual ~FilterBandRejectFIR ()
 
float64 getFrequencyLow () const
 
float64 getFrequencyHigh () const
 
AudioStream filter (const AudioStream &x)
 
AudioStream filter (const AudioStream &x, const float64 &f_low, const float64 &f_high)
 
AudioStream filter (const AudioStream &x, const Buffer &low_frequencies, const Buffer &high_frequencies)
 
Buffer filter (const Buffer &x)
 
Buffer filter (const Buffer &x, const float64 &f_low, const float64 &f_high)
 
Buffer filter (const Buffer &x, const Buffer &frequencies_Hz_low, const Buffer &frequencies_Hz_high)
 
virtual float64 filter (const float64 &x)
 
virtual float64 filter (const float64 &x, const float64 &frequency_Hz)
 
virtual float64 filter (const float64 &x, const float64 &frequency_Hz_low, const float64 &frequency_Hz_high)
 
void makeKernel (const float64 &frequency_Hz_low, const float64 &frequency_Hz_high)
 
void plot (boolean show_fc=true, boolean show_phase=false)
 
void reset ()
 
AudioStream filter (const AudioStream &x, const float64 &frequency)
 
AudioStream filter (const AudioStream &x, const Buffer &frequencies)
 
Buffer filter (const Buffer &x, const float64 &frequency)
 
Buffer filter (const Buffer &x, const Buffer &frequencies)
 
float64 getFrequency () const
 
Buffer getImpulseResponse ()
 
Buffer getImpulseResponse (const uint32 n_samples=8192)
 
void makeKernel (const float64 &frequency)
 
void plot (boolean show_phase=false)
 
void setCutoff (const float64 &fc)
 Sets the cut off frequency (Hz). More...
 
void setRealtime (bool flag)
 
Buffer getFrequencyAxis (const uint32 n_fft=8192)
 
Buffer getFrequencyResponse (const uint32 n_fft=8192)
 
virtual uint32 getKernelSize () const
 
Buffer getPhaseResponse ()
 
float64 getSampleRate () const
 

Protected Types

typedef std::set< KernelKernelCache
 

Protected Member Functions

void spectraReversal_ ()
 

Protected Attributes

float64 frequency_2_Hz_
 
KernelCache kernel_cache_
 
FilterLowPassFIR::KernelCache hp_cache_
 
float64b_
 
float64window_
 
float64x_history_
 
float64x_ptr_
 
float64x_end_ptr_
 
float64 frequency_1_Hz_
 
KernelCache lp_cache_
 
float64 sample_rate_
 
float64 two_pi_over_sample_rate_
 
float64 sample_time_
 
uint32 kernel_size_
 
bool is_realtime_
 

Private Member Functions

 FilterBandRejectFIR (const FilterBandRejectFIR &copy)
 
FilterBandRejectFIRoperator= (const FilterBandRejectFIR &rhs)
 

Detailed Description

Definition at line 47 of file FilterBandRejectFIR.h.

Member Typedef Documentation

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

Definition at line 146 of file FilterBandRejectFIR.h.

Constructor & Destructor Documentation

FilterBandRejectFIR::FilterBandRejectFIR ( const float64 sample_rate,
uint32  kernel_size,
const float64 frequency_Hz_low,
const float64 frequency_Hz_high 
)

Definition at line 47 of file FilterBandRejectFIR.cc.

References Nsound::FilterLowPassFIR::frequency_1_Hz_, and reset().

52  :
53  FilterHighPassFIR(sample_rate, kernel_size, frequency_Hz_high),
54  frequency_2_Hz_(frequency_Hz_high),
56 {
57  frequency_1_Hz_ = frequency_Hz_low;
58 
59  reset();
60 }
FilterHighPassFIR(const float64 &sample_rate, uint32 kernel_size, const float64 &cutoff_frequency_Hz)
FilterBandRejectFIR::~FilterBandRejectFIR ( )
virtual

Definition at line 63 of file FilterBandRejectFIR.cc.

References kernel_cache_.

64 {
65  FilterBandRejectFIR::KernelCache::iterator itor =
67 
68  FilterBandRejectFIR::KernelCache::iterator end =
70 
71  while(itor != end)
72  {
73  delete [] itor->b_;
74  ++itor;
75  }
76 }
Nsound::FilterBandRejectFIR::FilterBandRejectFIR ( const FilterBandRejectFIR copy)
inlineprivate

Definition at line 152 of file FilterBandRejectFIR.h.

153  :
154  FilterHighPassFIR(copy.sample_rate_, 3, copy.frequency_1_Hz_),
155  frequency_2_Hz_(0.0),
156  kernel_cache_(){};
FilterHighPassFIR(const float64 &sample_rate, uint32 kernel_size, const float64 &cutoff_frequency_Hz)

Member Function Documentation

float64 FilterBandRejectFIR::getFrequencyLow ( ) const

Definition at line 80 of file FilterBandRejectFIR.cc.

References Nsound::FilterLowPassFIR::frequency_1_Hz_.

81 {
82  return frequency_1_Hz_;
83 }
float64 FilterBandRejectFIR::getFrequencyHigh ( ) const

Definition at line 87 of file FilterBandRejectFIR.cc.

References frequency_2_Hz_.

88 {
89  return frequency_2_Hz_;
90 }
AudioStream FilterBandRejectFIR::filter ( const AudioStream x)

Definition at line 95 of file FilterBandRejectFIR.cc.

References Nsound::Filter::filter().

Referenced by filter(), and main().

96 {
97  return Filter::filter(x);
98 }
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
AudioStream FilterBandRejectFIR::filter ( const AudioStream x,
const float64 f_low,
const float64 f_high 
)

Definition at line 102 of file FilterBandRejectFIR.cc.

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

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

Definition at line 121 of file FilterBandRejectFIR.cc.

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

125 {
126  if(!is_realtime_) reset();
127 
128  uint32 n_channels = x.getNChannels();
129 
130  AudioStream y(x.getSampleRate(), n_channels);
131 
132  for(uint32 channel = 0; channel < n_channels; ++channel)
133  {
134  y[channel] = FilterBandRejectFIR::filter(
135  x[channel],
136  frequencies_Hz_low,
137  frequencies_Hz_high);
138  }
139 
140  return y;
141 }
unsigned int uint32
Definition: Nsound.h:153
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
AudioStream filter(const AudioStream &x)
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
bool is_realtime_
Definition: Filter.h:118
Buffer FilterBandRejectFIR::filter ( const Buffer x)

Definition at line 145 of file FilterBandRejectFIR.cc.

References Nsound::FilterLowPassFIR::filter().

146 {
147  return FilterLowPassFIR::filter(x);
148 }
AudioStream filter(const AudioStream &x)
Buffer FilterBandRejectFIR::filter ( const Buffer x,
const float64 f_low,
const float64 f_high 
)

Definition at line 152 of file FilterBandRejectFIR.cc.

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

156 {
157  if(!is_realtime_) reset();
158 
159  // Instead of calling filter(x,f_low, f_high), we implement this function
160  // here to avoid calling makeKernel(f) for each sample.
161 
162  FilterBandRejectFIR::makeKernel(f_low, f_high);
163 
164  Buffer::const_iterator itor = x.begin();
165  Buffer::const_iterator end = x.end();
166 
167  Buffer y(x.getLength());
168  while(itor != end)
169  {
170  y << FilterLowPassFIR::filter(*itor);
171  ++itor;
172  }
173 
174  return y;
175 }
void makeKernel(const float64 &frequency_Hz_low, const float64 &frequency_Hz_high)
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
bool is_realtime_
Definition: Filter.h:118
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioStream filter(const AudioStream &x)
Buffer FilterBandRejectFIR::filter ( const Buffer x,
const Buffer frequencies_Hz_low,
const Buffer frequencies_Hz_high 
)

Definition at line 179 of file FilterBandRejectFIR.cc.

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

183 {
184  if(!is_realtime_) reset();
185 
186  uint32 n_high_freqs = frequencies_Hz_high.getLength();
187  uint32 n_low_freqs = frequencies_Hz_low.getLength();
188 
189  Buffer y;
190 
191  uint32 x_samples = x.getLength();
192 
193  for(uint32 i = 0; i < x_samples; ++i)
194  {
196  x[i],
197  frequencies_Hz_low [i % n_low_freqs],
198  frequencies_Hz_high[i % n_high_freqs]);
199  }
200 
201  return y;
202 }
unsigned int uint32
Definition: Nsound.h:153
AudioStream filter(const AudioStream &x)
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
bool is_realtime_
Definition: Filter.h:118
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 FilterBandRejectFIR::filter ( const float64 x)
virtual

Reimplemented from Nsound::FilterHighPassFIR.

Definition at line 206 of file FilterBandRejectFIR.cc.

References Nsound::FilterLowPassFIR::filter().

207 {
208  return FilterLowPassFIR::filter(x);
209 }
AudioStream filter(const AudioStream &x)
float64 FilterBandRejectFIR::filter ( const float64 x,
const float64 frequency_Hz 
)
virtual

Reimplemented from Nsound::FilterHighPassFIR.

Definition at line 213 of file FilterBandRejectFIR.cc.

References Nsound::FilterLowPassFIR::filter().

214 {
215  return FilterLowPassFIR::filter(x);
216 }
AudioStream filter(const AudioStream &x)
float64 FilterBandRejectFIR::filter ( const float64 x,
const float64 frequency_Hz_low,
const float64 frequency_Hz_high 
)
virtual

Definition at line 220 of file FilterBandRejectFIR.cc.

References Nsound::FilterLowPassFIR::filter(), and makeKernel().

224 {
225  makeKernel(frequencies_Hz_low, frequencies_Hz_high);
226  return FilterLowPassFIR::filter(x);
227 }
void makeKernel(const float64 &frequency_Hz_low, const float64 &frequency_Hz_high)
AudioStream filter(const AudioStream &x)
void FilterBandRejectFIR::makeKernel ( const float64 frequency_Hz_low,
const float64 frequency_Hz_high 
)

Definition at line 231 of file FilterBandRejectFIR.cc.

References Nsound::FilterLowPassFIR::b_, Nsound::FilterBandRejectFIR::Kernel::b_, kernel_cache_, Nsound::Filter::kernel_size_, Nsound::FilterHighPassFIR::makeKernel(), and Nsound::FilterLowPassFIR::makeKernel().

Referenced by filter(), and reset().

232 {
233  // Here we create a key into the kernel cache. I'm only storing kernels
234  // with freqs chopped off to the 10th Hz. So filtering with a kernel
235  // designed at 440.1567 Hz will get stored as 440.1. Any other frequency
236  // that starts at 440.1 will not get a kernel calculated and just use the
237  // one in the cache. So the max diff between what's passed in and what's
238  // stored in the cache is 0.09999 Hz.
239 
240  FilterBandRejectFIR::Kernel new_kernel(
241  static_cast<uint32>(low_frequency),
242  static_cast<uint32>(high_frequency));
243 
244  // See if the kernel is in the cache.
245 
246  FilterBandRejectFIR::KernelCache::const_iterator itor =
247  FilterBandRejectFIR::kernel_cache_.find(new_kernel);
248 
249  if(itor != FilterBandRejectFIR::kernel_cache_.end())
250  {
251  // The kernel was found in the cache.
252  b_ = itor->b_;
253  return;
254  }
255 
256  // The filter wasn't in the cache, need to make it.
257  new_kernel.b_ = new float64[kernel_size_];
258 
259  if(low_frequency < 0.10 && high_frequency >= 0.10)
260  {
261  // Create simple high pass filter.
262  FilterHighPassFIR::makeKernel(high_frequency);
263 
264 //~ // b_ is now pointing at a high pass kernel held in the high pass cache
265 //~ memcpy(new_kernel.b_, b_, kernel_size_ * sizeof(float64));
266 
267  return;
268 
269  }
270  else if(low_frequency >= 0.10 && high_frequency < 0.10)
271  {
272  // Create simple low pass filter.
273  FilterLowPassFIR::makeKernel(low_frequency);
274 
275 //~ // b_ is now pointing at a high pass kernel held in the low pass cache
276 //~ memcpy(new_kernel.b_, b_, kernel_size_ * sizeof(float64));
277 
278  return;
279 
280  }
281  else if(low_frequency <= 0.10 && high_frequency < 0.10)
282  {
283  // Create simple All pass filter
284  new_kernel.b_[0] = 1.0;
285 
286  for(uint32 i = 1; i < kernel_size_; ++i)
287  {
288  new_kernel.b_[i] = 0.00000;
289  }
290  }
291  else // non-zero frequencies
292  {
293  FilterLowPassFIR::makeKernel(low_frequency);
294 
295  // Now b_ is pointing to a low pass kernel held in the low pass cache.
296  memcpy(new_kernel.b_, b_, kernel_size_ * sizeof(float64));
297 
298  FilterHighPassFIR::makeKernel(high_frequency);
299 
300  // b_ is now pointing at a high pass kernel held in the high pass cache
301 
302  // To create the band reject, simply add the low pass and high pass
303  // kernels.
304 
305  for(uint32 i = 0; i < kernel_size_; ++i)
306  {
307  // low kernel += high kernel
308  new_kernel.b_[i] += b_[i];
309  }
310  }
311 
312  // point at the new kernel
313  b_ = new_kernel.b_;
314 
315  // insert into cache
316  FilterBandRejectFIR::kernel_cache_.insert(new_kernel);
317 }
unsigned int uint32
Definition: Nsound.h:153
A class to store calculated kernels.
double float64
Definition: Nsound.h:146
void makeKernel(const float64 &frequency1)
void makeKernel(const float64 &frequency)
uint32 kernel_size_
Definition: Filter.h:116
void FilterBandRejectFIR::plot ( boolean  show_fc = true,
boolean  show_phase = false 
)

Definition at line 321 of file FilterBandRejectFIR.cc.

References Nsound::Plotter::axvline(), Nsound::FilterLowPassFIR::frequency_1_Hz_, frequency_2_Hz_, Nsound::Filter::kernel_size_, Nsound::Filter::plot(), Nsound::Filter::sample_rate_, Nsound::Plotter::subplot(), and Nsound::Plotter::title().

Referenced by main().

322 {
323  char title[128];
324  sprintf(title,
325  "Band Reject FIR Frequency Response\n"
326  "order = %d, fl = %0.1f Hz, fl = %0.1f Hz, sr = %0.1f Hz",
327  kernel_size_ - 1,
330  sample_rate_);
331 
332  Filter::plot(show_phase);
333 
334  Plotter pylab;
335 
336  uint32 n_rows = 1;
337 
338  if(show_phase)
339  {
340  n_rows = 2;
341  }
342 
343  if(show_fc)
344  {
345  pylab.subplot(n_rows, 1, 1);
346 
347  pylab.axvline(frequency_1_Hz_,"color='red'");
348  pylab.axvline(frequency_2_Hz_,"color='red'");
349 
350  pylab.title(title);
351  }
352 }
unsigned int uint32
Definition: Nsound.h:153
void axvline(const float64 &x_pos=0.0, const std::string &kwargs="")
Draws a vertical line at x and spans ymin to ymax (ralitive).
Definition: Plotter.cc:358
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 FilterBandRejectFIR::reset ( )
virtual

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

Reimplemented from Nsound::FilterHighPassFIR.

Definition at line 356 of file FilterBandRejectFIR.cc.

References Nsound::FilterLowPassFIR::frequency_1_Hz_, frequency_2_Hz_, makeKernel(), and Nsound::FilterLowPassFIR::reset().

Referenced by filter(), and FilterBandRejectFIR().

357 {
360 }
void makeKernel(const float64 &frequency_Hz_low, const float64 &frequency_Hz_high)
FilterBandRejectFIR& Nsound::FilterBandRejectFIR::operator= ( const FilterBandRejectFIR rhs)
inlineprivate

Definition at line 159 of file FilterBandRejectFIR.h.

159 {return *this;};
AudioStream FilterHighPassFIR::filter ( const AudioStream x,
const float64 frequency 
)
inherited

Definition at line 81 of file FilterHighPassFIR.cc.

References Nsound::Filter::filter().

82 {
83  return Filter::filter(x, f);
84 }
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
AudioStream FilterHighPassFIR::filter ( const AudioStream x,
const Buffer frequencies 
)
inherited

Definition at line 88 of file FilterHighPassFIR.cc.

References Nsound::Filter::filter().

89 {
90  return Filter::filter(x, frequencies);
91 }
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
Buffer FilterHighPassFIR::filter ( const Buffer x,
const float64 frequency 
)
inherited

Definition at line 102 of file FilterHighPassFIR.cc.

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

103 {
104  // Instead of calling Filter::filter(x,f), we implement this function here
105  // to avoid calling makeKernel(f) for each sample.
106 
109 
110  Buffer::const_iterator itor = x.begin();
111  Buffer::const_iterator end = x.end();
112 
113  Buffer y;
114  while(itor != end)
115  {
116  y << FilterLowPassFIR::filter(*itor);
117  ++itor;
118  }
119 
120  return y;
121 }
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
void makeKernel(const float64 &frequency)
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
AudioStream filter(const AudioStream &x)
Buffer FilterHighPassFIR::filter ( const Buffer x,
const Buffer frequencies 
)
inherited

Definition at line 125 of file FilterHighPassFIR.cc.

References Nsound::Filter::filter().

126 {
127  return Filter::filter(x, frequencies);
128 }
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
float64 Nsound::FilterHighPassFIR::getFrequency ( ) const
inlineinherited
Buffer FilterHighPassFIR::getImpulseResponse ( )
inherited

Definition at line 147 of file FilterHighPassFIR.cc.

References Nsound::Filter::getImpulseResponse().

148 {
150 }
Buffer getImpulseResponse(const uint32 n_samples=8192)
Definition: Filter.cc:225
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
void FilterHighPassFIR::makeKernel ( const float64 frequency)
inherited

Definition at line 154 of file FilterHighPassFIR.cc.

References Nsound::FilterLowPassFIR::b_, Nsound::FilterLowPassFIR::Kernel::b_, Nsound::FilterHighPassFIR::hp_cache_, Nsound::Filter::kernel_size_, Nsound::FilterLowPassFIR::makeKernel(), Nsound::Filter::sample_rate_, and Nsound::FilterHighPassFIR::spectraReversal_().

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

155 {
156  // Here we create a key into the kernel cache. I'm only storing kernels
157  // with freqs chopped off to the 10th Hz. So filtering with a kernel
158  // designed at 440.1567 Hz will get stored as 440.1. Any other frequency
159  // that starts at 440.1 will not get a kernel calculated and just use the
160  // one in the cache.
161 
162  FilterLowPassFIR::Kernel new_kernel(
163  static_cast<uint32>(cutoff_frequency_Hz));
164 
165  // See if the kernel is in the cache.
166 
167  FilterLowPassFIR::
168  KernelCache::
169  const_iterator itor = hp_cache_.find(new_kernel);
170 
171  if(itor != hp_cache_.end())
172  {
173  // The kernel was found in the cache.
174  b_ = itor->b_;
175  return;
176  }
177 
178  // The filter wasn't in the cache, need to make it.
179  new_kernel.b_ = new float64[kernel_size_];
180 
181  if(cutoff_frequency_Hz < 0.10)
182  {
183  // Create All pass filter.
184  new_kernel.b_[0] = 1.0;
185 
186  b_ = new_kernel.b_;
187  }
188  else
189  {
190  float64 fc = sample_rate_ / 2.0 - cutoff_frequency_Hz;
191 
193 
194  // Now b_ is pointing to a low pass kernel, copy it into new_kernel.
195  memcpy(new_kernel.b_, b_, sizeof(float64) * kernel_size_);
196 
197  // Point to new kernel.
198  b_ = new_kernel.b_;
199 
200  // Perform the spectra inversion on the kernel to turn it into a high
201  // pass filter.
203  }
204 
205  // Add the new kernel to the cache.
206  hp_cache_.insert(new_kernel);
207 }
FilterLowPassFIR::KernelCache hp_cache_
double float64
Definition: Nsound.h:146
void makeKernel(const float64 &frequency1)
uint32 kernel_size_
Definition: Filter.h:116
A class to store calculated kernels.
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(), 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
void FilterHighPassFIR::spectraReversal_ ( )
protectedinherited

Definition at line 258 of file FilterHighPassFIR.cc.

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

Referenced by Nsound::FilterHighPassFIR::makeKernel().

259 {
260  for(uint32 i = 0; i < kernel_size_; ++i)
261  {
262  if(i % 2)
263  {
264  b_[i] *= -1.00;
265  }
266  }
267 }
unsigned int uint32
Definition: Nsound.h:153
uint32 kernel_size_
Definition: Filter.h:116
void FilterLowPassFIR::setCutoff ( const float64 fc)
inherited

Sets the cut off frequency (Hz).

Definition at line 366 of file FilterLowPassFIR.cc.

References Nsound::FilterLowPassFIR::frequency_1_Hz_, and Nsound::FilterLowPassFIR::reset().

367 {
368  frequency_1_Hz_ = fc;
369  reset();
370 }
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
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

Member Data Documentation

float64 Nsound::FilterBandRejectFIR::frequency_2_Hz_
protected

Definition at line 131 of file FilterBandRejectFIR.h.

Referenced by getFrequencyHigh(), plot(), and reset().

KernelCache Nsound::FilterBandRejectFIR::kernel_cache_
protected

Definition at line 148 of file FilterBandRejectFIR.h.

Referenced by makeKernel(), and ~FilterBandRejectFIR().

FilterLowPassFIR::KernelCache Nsound::FilterHighPassFIR::hp_cache_
protectedinherited
float64* Nsound::FilterLowPassFIR::b_
protectedinherited
float64* Nsound::FilterLowPassFIR::window_
protectedinherited
float64* Nsound::FilterLowPassFIR::x_history_
protectedinherited
float64* Nsound::FilterLowPassFIR::x_ptr_
protectedinherited
float64* Nsound::FilterLowPassFIR::x_end_ptr_
protectedinherited
float64 Nsound::FilterLowPassFIR::frequency_1_Hz_
protectedinherited
KernelCache Nsound::FilterLowPassFIR::lp_cache_
protectedinherited
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=(), 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(), 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: