Class: DSP::Biquad
- Inherits:
-
Data
- Object
- Data
- DSP::Biquad
- Defined in:
- lib/dsprb/biquad.rb,
sig/dsprb.rbs
Constant Summary collapse
- BUTTERWORTH_Q =
Q of a Butterworth section: the maximally flat passband alignment.
Math.sqrt(2.0) / 2.0
- MINIMUM_NORMALIZED =
Normalized cutoff bounds, keeping the design away from DC and Nyquist.
1e-4- MAXIMUM_NORMALIZED =
0.49
Instance Attribute Summary collapse
-
#a1 ⇒ Object
readonly
Returns the value of attribute a1.
-
#a2 ⇒ Object
readonly
Returns the value of attribute a2.
-
#b0 ⇒ Object
readonly
Returns the value of attribute b0.
-
#b1 ⇒ Object
readonly
Returns the value of attribute b1.
-
#b2 ⇒ Object
readonly
Returns the value of attribute b2.
Class Method Summary collapse
- .highpass(sample_rate:, cutoff:, q: BUTTERWORTH_Q) ⇒ Biquad
- .lowpass(sample_rate:, cutoff:, q: BUTTERWORTH_Q) ⇒ Biquad
- .new ⇒ Biquad
Instance Method Summary collapse
-
#apply(samples, sections: 1) ⇒ ::Array[::Float]
Cascading n identical sections yields a filter of order 2n.
-
#call(samples) ⇒ ::Array[::Float]
Direct form II transposed, which keeps the state to two accumulators.
- #to_h ⇒ ::Hash[::Symbol, ::Float]
Instance Attribute Details
#a1 ⇒ Object (readonly)
Returns the value of attribute a1
4 5 6 |
# File 'lib/dsprb/biquad.rb', line 4 def a1 @a1 end |
#a2 ⇒ Object (readonly)
Returns the value of attribute a2
4 5 6 |
# File 'lib/dsprb/biquad.rb', line 4 def a2 @a2 end |
#b0 ⇒ Object (readonly)
Returns the value of attribute b0
4 5 6 |
# File 'lib/dsprb/biquad.rb', line 4 def b0 @b0 end |
#b1 ⇒ Object (readonly)
Returns the value of attribute b1
4 5 6 |
# File 'lib/dsprb/biquad.rb', line 4 def b1 @b1 end |
#b2 ⇒ Object (readonly)
Returns the value of attribute b2
4 5 6 |
# File 'lib/dsprb/biquad.rb', line 4 def b2 @b2 end |
Class Method Details
.highpass(sample_rate:, cutoff:, q: BUTTERWORTH_Q) ⇒ Biquad
14 |
# File 'lib/dsprb/biquad.rb', line 14 def self.highpass(sample_rate:, cutoff:, q: BUTTERWORTH_Q) = design(sample_rate, cutoff, q, high: true) |
.lowpass(sample_rate:, cutoff:, q: BUTTERWORTH_Q) ⇒ Biquad
12 |
# File 'lib/dsprb/biquad.rb', line 12 def self.lowpass(sample_rate:, cutoff:, q: BUTTERWORTH_Q) = design(sample_rate, cutoff, q, high: false) |
.new ⇒ Biquad
373 |
# File 'sig/dsprb.rbs', line 373
def self.new: (b0: ::Float, b1: ::Float, b2: ::Float, a1: ::Float, a2: ::Float) -> Biquad
|
Instance Method Details
#apply(samples, sections: 1) ⇒ ::Array[::Float]
Cascading n identical sections yields a filter of order 2n.
59 60 61 62 63 |
# File 'lib/dsprb/biquad.rb', line 59 def apply(samples, sections: 1) raise ArgumentError, "sections must be positive, got #{sections}" unless sections.positive? sections.times.reduce(samples) { |signal, _| call(signal) } end |
#call(samples) ⇒ ::Array[::Float]
Direct form II transposed, which keeps the state to two accumulators.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dsprb/biquad.rb', line 40 def call(samples) first = 0.0 second = 0.0 out = Array.new(samples.length, 0.0) index = 0 while index < samples.length input = samples[index] output = (b0 * input) + first first = (b1 * input) - (a1 * output) + second second = (b2 * input) - (a2 * output) out[index] = output index += 1 end out end |
#to_h ⇒ ::Hash[::Symbol, ::Float]
379 |
# File 'sig/dsprb.rbs', line 379
def to_h: () -> ::Hash[::Symbol, ::Float]
|