41 using namespace Nsound;
47 #define CERR_HEADER __FILE__ << ":" << __LINE__ << ": ***ERROR: "
55 const Buffer & amplitude_axis,
262 const Buffer & amplitude_axis)
274 M_THROW(
"FilterLeastSquaresFIR::makeKernel(): "
275 <<
"freq_axis and amplitude_axis must be same length and even!");
285 if(freq_axis[0] != 0.0)
287 M_THROW(
"FilterLeastSquaresFIR::makeKernel(): "
288 <<
"freq_axis[0] must be 0.0!");
297 if(freq_axis[freq_axis.
getLength() -1 ] != max_freq)
299 M_THROW(
"FilterLeastSquaresFIR::makeKernel(): "
300 <<
"freq_axis[end] (" << freq_axis[freq_axis.
getLength() -1 ]
301 <<
") must be " << max_freq <<
"!");
314 M_THROW(
"FilterLeastSquaresFIR::makeKernel(): "
315 <<
"Frequencies in freq_axis must be increasing!");
326 *
f_axis_ << (freq_axis / max_freq);
375 boolean fullband =
true;
400 boolean is_odd =
true;
427 boolean need_matrix = ! fullband;
433 M_THROW(
"FilterLeastSquaresFIR::makeKernel(): "
434 <<
"FIXME: need_matix is true!");
464 mm = (a[i+1] - a[i]) / t_df;
465 b1 = a[i] - mm * f[i];
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];
480 Buffer f1 = PI_x_2 * k * f[i+1];
481 Buffer f2 = PI_x_2 * k * f[i];
483 std::stringstream ss;
485 float64 abs_w2 = std::fabs(w[(i+1)/2] * w[(i+1)/2]);
493 b[j] += (mm / PI_x_PI_x_4
494 * (std::cos(f1[j]) - std::cos(f2[j]))
533 if(x == 0.0) x =
M_PI;
535 sinc = std::sin(x) / x;
539 if(x == 0.0) x =
M_PI;
541 sinc1 = std::sin(x) / x;
543 b[j] += ( fi1 * (mm * fi1 + b1) * sinc1
544 - fi * (mm * fi + b1) * sinc )
579 a = w[0] * w[0] * 4.0 * b;
605 M_THROW(
"FilterLeastSquaresFIR::makeKernel(): "
606 <<
"kernel_size_ != h.getLength()!");
668 plot(
boolean show_fc,
boolean show_phase)
672 "Least Square FIR Frequency Response\n"
673 "order = %d, sr = %0.1f Hz",
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
void makeKernel(const Buffer &freq_axis, const Buffer &litude_axis)
virtual ~FilterLeastSquaresFIR()
Buffer drawWindowKaiser(const float64 &duration, const float64 &beta=5.0) const
Draws a Kaiser window.
AudioStream filter(const AudioStream &x)
void setWindow(WindowType type)
void plot(boolean show_fc=true, boolean show_phase=false)
void title(const std::string &title, const std::string &kwargs="")
Add a title to the plot at the top and centered.
Buffer drawWindow(const float64 &duration, WindowType type) const
Draws a window of the specified type.
Buffer getDerivative(uint32 n) const
Returns the nth derivative of the Buffer.
Buffer getKernelFrequencies()
uint32 getLength() const
Returns the number of samples in the Buffer.
Base class for IIR Filters, defines the interface.
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)
FilterLeastSquaresFIR(const float64 &sample_rate, uint32 kernel_size, const Buffer &freq_axis, const Buffer &litude_axis, const float64 &beta=5.0)
void plot(boolean show_phase=false)
AudioStream filter(const AudioStream &x)
FilterLeastSquaresFIR & operator=(const FilterLeastSquaresFIR &rhs)
Buffer getKernelAmplitudes()
const float64 * getPointer() const
Returns the raw point to the first sample in the Buffer.
A Buffer for storing audio samples.
float64 getMin() const
Returns the minimum sample value in the Buffer.
A FIR filter that is defined as the least square error to the desired requency response.
Buffer drawLine(const float64 &duration, const float64 &litude_start, const float64 &litude_finish) const
This method draws a linear line beteween 2 points.
Buffer getReverse() const
Reverses the samples in a copy of this Buffer.
void setKernel(const Buffer &k)
A class the provides draw utilities and a wavetable oscillator.