Class: SPCore::CookbookBandpassFilter

Inherits:
BiquadFilter show all
Defined in:
lib/spcore/filters/iir/cookbook_bandpass_filter.rb

Overview

based on the well-known RBJ biquad filter.

Constant Summary

Constants inherited from BiquadFilter

BiquadFilter::LN_2

Instance Method Summary collapse

Methods inherited from BiquadFilter

#bandwidth=, #critical_freq=, #get_freq_magnitude_response, #process_sample

Constructor Details

#initialize(sample_rate) ⇒ CookbookBandpassFilter

Returns a new instance of CookbookBandpassFilter.



5
6
7
# File 'lib/spcore/filters/iir/cookbook_bandpass_filter.rb', line 5

def initialize sample_rate
  super(sample_rate)
end

Instance Method Details

#set_critical_freq_and_bw(critical_freq, bandwidth) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/spcore/filters/iir/cookbook_bandpass_filter.rb', line 9

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)

  b0 = alpha
  b1 = 0.0
  b2 = -alpha
  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