Nsound::FilterSlinky Class Reference

#include <Nsound/FilterSlinky.h>

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

List of all members.

Public Member Functions

 FilterSlinky (const float64 &sample_rate, const float64 &delay_time, const float64 &frequency_window)
virtual ~FilterSlinky ()
AudioStream filter (const AudioStream &x)
AudioStream filter (const AudioStream &x, const Buffer &frequencies)
Buffer filter (const Buffer &x)
Buffer filter (const Buffer &x, const Buffer &frequencies)
float64 filter (const float64 &x)
float64 filter (const float64 &x, const float64 &frequency)
void reset ()
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)
virtual uint32 getKernelSize () const
Buffer getPhaseResponse ()
float64 getSampleRate () const
void plot (boolean show_phase=false)

Protected Types

typedef std::vector
< FilterDelay * > 
DelayVector
typedef std::vector
< FilterBandPassIIR * > 
FilterVector

Protected Attributes

DelayVector delay_lines_
FilterVector filters_
float64 sample_rate_
float64 two_pi_over_sample_rate_
uint32 kernel_size_

Detailed Description

Base class for IIR Filters, defines the interface.

Definition at line 53 of file FilterSlinky.h.


Member Typedef Documentation

typedef std::vector< FilterDelay * > Nsound::FilterSlinky::DelayVector [protected]

Definition at line 98 of file FilterSlinky.h.

typedef std::vector< FilterBandPassIIR * > Nsound::FilterSlinky::FilterVector [protected]

Definition at line 99 of file FilterSlinky.h.


Constructor & Destructor Documentation

FilterSlinky::FilterSlinky ( const float64 sample_rate,
const float64 delay_time,
const float64 frequency_window 
)

Definition at line 45 of file FilterSlinky.cc.

References delay_lines_, filters_, and Nsound::Filter::sample_rate_.

00050     :
00051     Filter(sample_rate),
00052     delay_lines_(),
00053     filters_()
00054 {
00055     float64 f = 0.0;
00056 
00057     while( f < sample_rate_ / 2.0)
00058     {
00059         if(f == 0.0)
00060         {
00061             f = frequency_octave;
00062         }
00063         else
00064         {
00065             f += frequency_octave;
00066         }
00067     }
00068 
00069     // Create the highest frequency pass band filter.
00070 
00071     float64 f_high = f;
00072     f -= frequency_octave;
00073     float64 f_low  = f;
00074 
00075     filters_.push_back(
00076         new FilterBandPassIIR(
00077             sample_rate_,
00078             6,
00079             f_low,
00080             f_high,
00081             0.01));
00082 
00083     // Create pass band filters for the remaing frequency octaves.
00084     float64 dt = delay_octave;
00085     while(f >= frequency_octave)
00086     {
00087         f_high = f;
00088         f -= frequency_octave;
00089         f_low = f;
00090 
00091         if(f_high == frequency_octave)
00092         {
00093             f_low = 0.0;
00094         }
00095 
00096         filters_.push_back(
00097         new FilterBandPassIIR(
00098             sample_rate_,
00099             6,
00100             f_low,
00101             f_high,
00102             0.01));
00103 
00104         delay_lines_.push_back(
00105             new FilterDelay(sample_rate_, dt));
00106 
00107         dt += delay_octave;
00108     }
00109 
00110 //~    cout << "n_filters = " << filters_.size() << endl
00111 //~         << "n_delays  = " << delay_lines_.size() << endl;
}

FilterSlinky::~FilterSlinky (  )  [virtual]

Definition at line 115 of file FilterSlinky.cc.

References delay_lines_, filter(), and filters_.

00116 {
00117     FilterVector::iterator filter = filters_.begin();
00118     FilterVector::iterator end    = filters_.end();
00119 
00120     DelayVector::iterator delay = delay_lines_.begin();
00121 
00122     delete *filter;
00123     ++filter;
00124 
00125     while(filter != end)
00126     {
00127         delete *filter;
00128         delete *delay;
00129 
00130         ++filter;
00131         ++delay;
00132     }
00133 }


Member Function Documentation

AudioStream FilterSlinky::filter ( const AudioStream x  ) 

Reimplemented from Nsound::Filter.

Definition at line 139 of file FilterSlinky.cc.

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

Referenced by filter(), reset(), and ~FilterSlinky().

00140 {
00141     uint32 n_channels = x.getNChannels();
00142 
00143     AudioStream y(x.getSampleRate(), n_channels);
00144 
00145     for(uint32 channel = 0; channel < n_channels; ++channel)
00146     {
00147         y[channel] = FilterSlinky::filter(x[channel]);
00148     }
00149 
00150     return y;
00151 }

AudioStream FilterSlinky::filter ( const AudioStream x,
const Buffer frequencies 
)

Reimplemented from Nsound::Filter.

Definition at line 156 of file FilterSlinky.cc.

References filter().

00157 {
00158     return FilterSlinky::filter(x);
00159 }

Buffer FilterSlinky::filter ( const Buffer x  ) 

Reimplemented from Nsound::Filter.

Definition at line 164 of file FilterSlinky.cc.

References filter(), and Nsound::Buffer::getLength().

00165 {
00166     Buffer y;
00167 
00168     uint32 n_samples = x.getLength();
00169 
00170     for(uint32 n = 0; n < n_samples; ++n)
00171     {
00172         y << FilterSlinky::filter(x[n]);
00173     }
00174 
00175     return y;
00176 }

Buffer FilterSlinky::filter ( const Buffer x,
const Buffer frequencies 
)

Reimplemented from Nsound::Filter.

Definition at line 181 of file FilterSlinky.cc.

References filter().

00182 {
00183     return FilterSlinky::filter(x);
00184 }

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

Implements Nsound::Filter.

Definition at line 189 of file FilterSlinky.cc.

References delay_lines_, filter(), and filters_.

00190 {
00191     float64 y = 0.0;
00192 
00193     FilterVector::iterator filter = filters_.begin();
00194     FilterVector::iterator end    = filters_.end();
00195 
00196     DelayVector::iterator delay = delay_lines_.begin();
00197 
00198     // There is always one more filter than delay lines, this is because the
00199     // highest frequency have zero delay.
00200 
00201     y += (*filter)->filter(x);
00202 
00203     ++filter;
00204 
00205     while(filter != end)
00206     {
00207         y += (*delay)->filter((*filter)->filter(x));
00208 
00209         ++filter;
00210         ++delay;
00211     }
00212 
00213     return y;
00214 }

float64 FilterSlinky::filter ( const float64 x,
const float64 frequency 
) [virtual]

Implements Nsound::Filter.

Definition at line 219 of file FilterSlinky.cc.

References filter().

00220 {
00221     return FilterSlinky::filter(x);
00222 }

void FilterSlinky::reset (  )  [virtual]

Implements Nsound::Filter.

Definition at line 227 of file FilterSlinky.cc.

References delay_lines_, filter(), and filters_.

00228 {
00229     FilterVector::iterator filter = filters_.begin();
00230     FilterVector::iterator end  = filters_.end();
00231 
00232     DelayVector::iterator delay = delay_lines_.begin();
00233 
00234     (*filter)->reset();
00235     ++filter;
00236 
00237     while(filter != end)
00238     {
00239         (*filter)->reset();
00240         (*delay)->reset();
00241 
00242         ++filter;
00243         ++delay;
00244     }
00245 
00246 }

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 }

virtual uint32 Nsound::Filter::getKernelSize (  )  const [inline, virtual, inherited]
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 101 of file FilterSlinky.h.

Referenced by filter(), FilterSlinky(), reset(), and ~FilterSlinky().

Definition at line 102 of file FilterSlinky.h.

Referenced by filter(), FilterSlinky(), reset(), and ~FilterSlinky().

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(), 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=(), Nsound::FilterTone::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