Class: DSP::Biquad

Inherits:
Data
  • Object
show all
Defined in:
lib/dsprb/biquad.rb,
sig/dsprb.rbs

Constant Summary collapse

BUTTERWORTH_Q =

Q of a Butterworth section: the maximally flat passband alignment.

Returns:

  • (::Float)
Math.sqrt(2.0) / 2.0
MINIMUM_NORMALIZED =

Normalized cutoff bounds, keeping the design away from DC and Nyquist.

Returns:

  • (::Float)
1e-4
MAXIMUM_NORMALIZED =

Returns:

  • (::Float)
0.49

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#a1Object (readonly)

Returns the value of attribute a1

Returns:

  • (Object)

    the current value of a1



4
5
6
# File 'lib/dsprb/biquad.rb', line 4

def a1
  @a1
end

#a2Object (readonly)

Returns the value of attribute a2

Returns:

  • (Object)

    the current value of a2



4
5
6
# File 'lib/dsprb/biquad.rb', line 4

def a2
  @a2
end

#b0Object (readonly)

Returns the value of attribute b0

Returns:

  • (Object)

    the current value of b0



4
5
6
# File 'lib/dsprb/biquad.rb', line 4

def b0
  @b0
end

#b1Object (readonly)

Returns the value of attribute b1

Returns:

  • (Object)

    the current value of b1



4
5
6
# File 'lib/dsprb/biquad.rb', line 4

def b1
  @b1
end

#b2Object (readonly)

Returns the value of attribute b2

Returns:

  • (Object)

    the current value of 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

Parameters:

  • sample_rate: (::Numeric)
  • cutoff: (::Numeric)
  • q: (::Numeric) (defaults to: BUTTERWORTH_Q)

Returns:



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

Parameters:

  • sample_rate: (::Numeric)
  • cutoff: (::Numeric)
  • q: (::Numeric) (defaults to: BUTTERWORTH_Q)

Returns:



12
# File 'lib/dsprb/biquad.rb', line 12

def self.lowpass(sample_rate:, cutoff:, q: BUTTERWORTH_Q) = design(sample_rate, cutoff, q, high: false)

.newBiquad

Parameters:

  • b0: (::Float)
  • b1: (::Float)
  • b2: (::Float)
  • a1: (::Float)
  • a2: (::Float)

Returns:



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.

Parameters:

  • (samples)
  • sections: (::Integer) (defaults to: 1)

Returns:

  • (::Array[::Float])

Raises:

  • (ArgumentError)


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.

Parameters:

  • (samples)

Returns:

  • (::Array[::Float])


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]

Returns:

  • (::Hash[::Symbol, ::Float])


379
# File 'sig/dsprb.rbs', line 379

def to_h: () -> ::Hash[::Symbol, ::Float]