Nsound::FilterTone Class Reference

#include <Nsound/FilterTone.h>

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

List of all members.

Classes

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

Public Member Functions

 FilterTone (const float64 &sample_rate, const float64 &half_power_frequency)
virtual ~FilterTone ()
AudioStream filter (const AudioStream &x)
AudioStream filter (const AudioStream &x, const Buffer &hp_frequencies)
Buffer filter (const Buffer &x)
Buffer filter (const Buffer &x, const Buffer &hp_frequencies)
float64 filter (const float64 &x)
float64 filter (const float64 &x, const float64 &hp_frequency)
float64 getFrequency () const
uint32 getKernelSize () const
void plot (boolean show_fc=true, boolean show_phase=false)
void reset ()
void makeKernel (const float64 &half_power_frequency)
AudioStream filter (const AudioStream &x, const float64 &frequency)
Buffer filter (const Buffer &x, const float64 &frequency)
Buffer getFrequencyAxis (const uint32 n_fft=8192)
Buffer getFrequencyResponse (const uint32 n_fft=8192)
Buffer getImpulseResponse (const uint32 n_samples=8192)
Buffer getPhaseResponse ()
float64 getSampleRate () const
void plot (boolean show_phase=false)

Protected Types

typedef std::set< KernelKernelCache

Protected Attributes

float64 hp_frequency_
float64 b_
float64 a_
float64 last_output_
KernelCache kernel_cache_
float64 sample_rate_
float64 two_pi_over_sample_rate_
uint32 kernel_size_

Detailed Description

Definition at line 47 of file FilterTone.h.


Member Typedef Documentation

typedef std::set<Kernel> Nsound::FilterTone::KernelCache [protected]

Definition at line 127 of file FilterTone.h.


Constructor & Destructor Documentation

FilterTone::FilterTone ( const float64 sample_rate,
const float64 half_power_frequency 
)

Definition at line 42 of file FilterTone.cc.

References reset().

00044     :
00045     Filter(sample_rate),
00046     hp_frequency_(half_power_frequency),
00047     b_(0.0),
00048     a_(0.0),
00049     last_output_(0.0),
00050     kernel_cache_()
00051 {
00052     reset();
}

FilterTone::~FilterTone (  )  [virtual]

Definition at line 56 of file FilterTone.cc.

00057 {
00058 
00059 }


Member Function Documentation

AudioStream FilterTone::filter ( const AudioStream x  ) 

Reimplemented from Nsound::Filter.

Definition at line 64 of file FilterTone.cc.

Referenced by filter(), Nsound::Pluck::generate(), Nsound::GuitarBass::play(), and Nsound::FluteSlide::play().

00065 {
00066     return Filter::filter(x);
00067 }

AudioStream FilterTone::filter ( const AudioStream x,
const Buffer hp_frequencies 
)

Reimplemented from Nsound::Filter.

Definition at line 72 of file FilterTone.cc.

References filter().

00073 {
00074     return Filter::filter(x, hp_frequencies);
00075 }

Buffer FilterTone::filter ( const Buffer x  ) 

Reimplemented from Nsound::Filter.

Definition at line 80 of file FilterTone.cc.

References filter().

00081 {
00082     return Filter::filter(x);
00083 }

Buffer FilterTone::filter ( const Buffer x,
const Buffer hp_frequencies 
)

Reimplemented from Nsound::Filter.

Definition at line 88 of file FilterTone.cc.

References filter().

00089 {
00090     return Filter::filter(x, hp_frequencies);
00091 }

float64 FilterTone::filter ( const float64 x  )  [virtual]

Implements Nsound::Filter.

Definition at line 96 of file FilterTone.cc.

References a_, b_, and last_output_.

00097 {
00098     float64 y = b_ * x - a_ * last_output_;
00099 
00100     last_output_ = y;
00101 
00102     return y;
00103 }

float64 FilterTone::filter ( const float64 x,
const float64 hp_frequency 
) [virtual]

Implements Nsound::Filter.

Definition at line 108 of file FilterTone.cc.

References filter(), and makeKernel().

00109 {
00110     makeKernel(hp_frequency);
00111 
00112     return FilterTone::filter(x);
00113 }

float64 Nsound::FilterTone::getFrequency (  )  const [inline]

Definition at line 86 of file FilterTone.h.

References hp_frequency_.

00086 {return hp_frequency_;};

uint32 Nsound::FilterTone::getKernelSize (  )  const [inline, virtual]

Reimplemented from Nsound::Filter.

Definition at line 90 of file FilterTone.h.

00090 { return 2; };

void FilterTone::plot ( boolean  show_fc = true,
boolean  show_phase = false 
)

Definition at line 150 of file FilterTone.cc.

References Nsound::Plotter::axvline(), hp_frequency_, Nsound::Filter::sample_rate_, Nsound::Plotter::subplot(), and Nsound::Plotter::title().

00151 {
00152     char title[128];
00153     sprintf(title,
00154         "Tone Filter Frequency Response\n"
00155         "order = %d, fc = %0.1f Hz, sr = %0.1f Hz",
00156         2, hp_frequency_, sample_rate_);
00157 
00158     Filter::plot(show_phase);
00159 
00160     Plotter pylab;
00161 
00162     int32 topplot = 111;
00163 
00164     if(show_phase)
00165     {
00166         topplot = 211;
00167     }
00168 
00169     if(show_fc)
00170     {
00171         pylab.subplot(topplot);
00172 
00173         pylab.axvline(hp_frequency_,"color='red'");
00174 
00175         pylab.title(title);
00176     }
00177 
00178 }

void FilterTone::reset (  )  [virtual]

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

Implements Nsound::Filter.

Definition at line 183 of file FilterTone.cc.

References hp_frequency_, last_output_, and makeKernel().

Referenced by FilterTone().

00184 {
00185     last_output_ = 0.0;
00186     makeKernel(hp_frequency_);
00187 }

void FilterTone::makeKernel ( const float64 half_power_frequency  ) 

Definition at line 118 of file FilterTone.cc.

References a_, b_, kernel_cache_, and Nsound::Filter::two_pi_over_sample_rate_.

Referenced by filter(), and reset().

00119 {
00120     FilterTone::
00121     Kernel new_kernel(static_cast<uint32>(half_power_frequency * 10));
00122 
00123     // See if the kernel is in the cache.
00124     KernelCache::const_iterator itor = kernel_cache_.find(new_kernel);
00125 
00126     if(itor != kernel_cache_.end())
00127     {
00128         // The kernel was found in the cache.
00129         b_ = itor->b_;
00130         a_ = itor->a_;
00131         return;
00132     }
00133 
00134     // The filter wasn't in the cache, need to make it.
00135 
00136     float64 temp = 2.0 - ::cos(two_pi_over_sample_rate_ * half_power_frequency);
00137 
00138     a_ = -1.0 * (temp - ::sqrt(temp * temp - 1.0));
00139     b_ = 1.0 + a_;
00140 
00141     new_kernel.b_ = b_;
00142     new_kernel.a_ = a_;
00143 
00144     kernel_cache_.insert(new_kernel);
00145 }

AudioStream Filter::filter ( const AudioStream x,
const float64 frequency 
) [inherited]

Reimplemented in Nsound::FilterAllPass, Nsound::FilterBandPassIIR, Nsound::FilterBandRejectIIR, Nsound::FilterDelay, Nsound::FilterFlanger, Nsound::FilterHighPassFIR, Nsound::FilterHighPassIIR, Nsound::FilterLeastSquaresFIR, Nsound::FilterLowPassFIR, Nsound::FilterLowPassIIR, and Nsound::FilterStageIIR.

Definition at line 72 of file Filter.cc.

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

00073 {
00074     reset();
00075 
00076     uint32 n_channels = x.getNChannels();
00077 
00078     AudioStream y(x.getSampleRate(), n_channels);
00079 
00080     for(uint32 channel = 0; channel < n_channels; ++channel)
00081     {
00082         y[channel] = filter(x[channel], frequency);
00083     }
00084 
00085     return y;
00086 }

Buffer Filter::filter ( const Buffer x,
const float64 frequency 
) [inherited]
Buffer Filter::getFrequencyAxis ( const uint32  n_fft = 8192  )  [inherited]

Definition at line 176 of file Filter.cc.

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

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

00177 {
00178     uint32 fft_chunk_size = FFTransform::roundUp2(
00179         static_cast<int32>(n_fft));
00180 
00181     uint32 n_samples = fft_chunk_size / 2 + 1;
00182 
00183     float64 f_step = (1.0 / (static_cast<float64>(fft_chunk_size) / 2.0))
00184                    * (sample_rate_ / 2.0);
00185 
00186     Buffer f_axis;
00187 
00188     float64 f = 0.0;
00189 
00190     for(uint32 i = 0; i < n_samples; ++i)
00191     {
00192         f_axis << f;
00193         f += f_step;
00194     }
00195 
00196     return f_axis;
00197 };

Buffer Filter::getFrequencyResponse ( const uint32  n_fft = 8192  )  [inherited]

Definition at line 202 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().

00203 {
00204     FFTransform fft(sample_rate_);
00205 
00206 //~    fft.setWindow(HANNING);
00207 
00208     FFTChunkVector vec;
00209 
00210     vec = fft.fft(getImpulseResponse(), n_fft);
00211 
00212     return vec[0].getMagnitude();
00213 }

Buffer Filter::getImpulseResponse ( const uint32  n_samples = 8192  )  [inherited]

Reimplemented in Nsound::FilterIIR.

Definition at line 218 of file Filter.cc.

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

Referenced by Nsound::Filter::getFrequencyResponse(), and Nsound::Filter::getPhaseResponse().

00219 {
00220     reset();
00221 
00222     Buffer response(n_samples);
00223 
00224     response << filter(1.0);
00225 
00226     for(uint32 i = 1; i < n_samples; ++i)
00227     {
00228         response << filter(0.0);
00229     }
00230 
00231     reset();
00232 
00233     return response;
00234 }

Buffer Filter::getPhaseResponse (  )  [inherited]

Definition at line 239 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().

00240 {
00241     uint32 n_samples = static_cast<uint32>(sample_rate_ * 2);
00242 
00243     FFTransform fft(n_samples);
00244 
00245     FFTChunkVector vec;
00246 
00247     vec = fft.fft(getImpulseResponse(), n_samples);
00248 
00249     Buffer phase = vec[0].getPhase();
00250 
00251     return phase.subbuffer(0, phase.getLength() / 2 + 1);
00252 }

float64 Nsound::Filter::getSampleRate (  )  const [inline, inherited]

Definition at line 117 of file Filter.h.

References Nsound::Filter::sample_rate_.

00117 { return sample_rate_; };

void Filter::plot ( boolean  show_phase = false  )  [inherited]

Definition at line 257 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().

00258 {
00259     Buffer x = getFrequencyAxis();
00260     Buffer fr = getFrequencyResponse().getdB();
00261 
00262     Plotter pylab;
00263 
00264     pylab.figure();
00265 
00266     int subplot_int = 111;
00267 
00268     if(show_phase)
00269     {
00270         subplot_int = 211;
00271     }
00272 
00273     pylab.subplot(subplot_int);
00274 
00275     // Frequency response
00276     pylab.plot(x,fr, "blue");
00277 
00278     pylab.xlabel("Frequency (Hz)");
00279     pylab.ylabel("Frequency Response (dB)");
00280 
00281     // Phase response
00282     if(show_phase)
00283     {
00284         pylab.subplot(subplot_int + 1);
00285 
00286         Buffer pr = getPhaseResponse().getdB();
00287 
00288         pylab.plot(x,pr);
00289 
00290         pylab.xlabel("Frequency (Hz)");
00291         pylab.ylabel("Phase Response (dB)");
00292     }
00293 
00294     float64 ymax = fr.getMax();
00295     float64 height = ymax - -60.0;
00296 
00297     pylab.ylim(-60.0, ymax + 0.05 * height);
00298 
00299 }


Member Data Documentation

Definition at line 109 of file FilterTone.h.

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

Definition at line 110 of file FilterTone.h.

Referenced by filter(), and makeKernel().

Definition at line 111 of file FilterTone.h.

Referenced by filter(), and makeKernel().

Definition at line 112 of file FilterTone.h.

Referenced by filter(), and reset().

Definition at line 129 of file FilterTone.h.

Referenced by makeKernel().

float64 Nsound::Filter::sample_rate_ [protected, inherited]

Definition at line 131 of file Filter.h.

Referenced by Nsound::FilterPhaser::filter(), Nsound::FilterDelay::filter(), Nsound::FilterCombLowPassFeedback::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::FilterStageIIR::makeKernel(), Nsound::FilterParametricEqualizer::makeKernel(), Nsound::FilterLeastSquaresFIR::makeKernel(), Nsound::FilterHighPassFIR::makeKernel(), Nsound::FilterBandPassVocoder::makeKernel(), Nsound::FilterPhaser::operator=(), Nsound::FilterLeastSquaresFIR::operator=(), Nsound::FilterIIR::operator=(), Nsound::FilterFlanger::operator=(), plot(), Nsound::FilterPhaser::plot(), Nsound::FilterParametricEqualizer::plot(), Nsound::FilterLowPassIIR::plot(), Nsound::FilterLowPassFIR::plot(), Nsound::FilterLeastSquaresFIR::plot(), Nsound::FilterHighPassIIR::plot(), Nsound::FilterHighPassFIR::plot(), Nsound::FilterFlanger::plot(), Nsound::FilterCombLowPassFeedback::plot(), Nsound::FilterBandRejectIIR::plot(), Nsound::FilterBandRejectFIR::plot(), Nsound::FilterBandPassIIR::plot(), Nsound::FilterBandPassFIR::plot(), Nsound::FilterAllPass::plot(), Nsound::FilterLowPassIIR::setCutoff(), and Nsound::FilterLeastSquaresFIR::setWindow().

uint32 Nsound::Filter::kernel_size_ [protected, inherited]

The documentation for this class was generated from the following files:
Generated on Sun Apr 15 20:10:50 2012 for nsound by  doxygen 1.6.3