Sha256: 96edff9403ca0228f26269b75666e7f156355ae98b0fe885b7e87319d412fd88

Contents?: true

Size: 1.09 KB

Versions: 9

Compression:

Stored size: 1.09 KB

Contents

module SPCore
# Implement a "cookbook" lowpass filter using the BiquadFilter class,
# based on the well-known RBJ biquad filter.
class CookbookLowpassFilter < BiquadFilter
  def initialize sample_rate
    super(sample_rate)
  end
  
  def set_critical_freq_and_bw critical_freq, bandwidth
    @critical_freq = critical_freq
    @bandwidth = bandwidth

    # setup variables
    omega = 2.0 * Math::PI * @critical_freq / @sample_rate
    sn = Math::sin(omega)
    cs = Math::cos(omega)
    alpha = sn * Math::sinh(BiquadFilter::LN_2 / 2.0 * @bandwidth * omega / sn)

    #if(q_is_bandwidth)
    #  alpha=tsin*sinh(log(2.0)/2.0*q*omega/tsin);
    #else
    #  alpha=tsin/(2.0*q);
    #end

    b0 = (1.0 - cs) / 2.0
    b1 =  1.0 - cs
    b2 = (1.0 - cs) / 2.0
    a0 =  1.0 + alpha
    a1 = -2.0 * cs
    a2 =  1.0 - alpha

    # precompute the coefficients
    @biquad.b0 = b0 / a0
    @biquad.b1 = b1 / a0
    @biquad.b2 = b2 / a0
    @biquad.a0 = a0 / a0
    @biquad.a1 = a1 / a0
    @biquad.a2 = a2 / a0

    ## zero initial samples
    #@biquad.x1 = @biquad.x2 = 0
    #@biquad.y1 = @biquad.y2 = 0
  end

end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spcore-0.2.1 lib/spcore/filters/iir/cookbook_lowpass_filter.rb
spcore-0.2.0 lib/spcore/filters/iir/cookbook_lowpass_filter.rb
spcore-0.1.9 lib/spcore/filters/iir/cookbook_lowpass_filter.rb
spcore-0.1.8 lib/spcore/filters/iir/cookbook_lowpass_filter.rb
spcore-0.1.7 lib/spcore/filters/iir/cookbook_lowpass_filter.rb
spcore-0.1.6 lib/spcore/filters/iir/cookbook_lowpass_filter.rb
spcore-0.1.5 lib/spcore/filters/iir/cookbook_lowpass_filter.rb
spcore-0.1.4 lib/spcore/filters/iir/cookbook_lowpass_filter.rb
spcore-0.1.3 lib/spcore/filters/iir/cookbook_lowpass_filter.rb