Class: DSP::Decimator

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

Constant Summary collapse

DEFAULT_TAPS =

An odd tap count keeps the FIR linear phase, so decimation adds no group delay skew.

Returns:

  • (::Integer)
31
CUTOFF_RATIO =

Cutoff as a fraction of the decimated sample rate, leaving a transition margin below Nyquist.

Returns:

  • (::Float)
0.45

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factor, taps: DEFAULT_TAPS) ⇒ Decimator

Returns a new instance of Decimator.

Parameters:

  • (::Integer)
  • taps: (::Integer) (defaults to: DEFAULT_TAPS)

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dsprb/decimator.rb', line 15

def initialize(factor, taps: DEFAULT_TAPS)
  @factor = Integer(factor)
  @taps = Integer(taps)

  raise ArgumentError, "factor must be at least one, got #{@factor}" unless @factor >= 1
  raise ArgumentError, "taps must be a positive odd number, got #{@taps}" unless @taps.positive? && @taps.odd?

  @middle = (@taps - 1) / 2
  @kernel = build_kernel.freeze
  freeze
end

Instance Attribute Details

#factor::Integer (readonly)

Returns the value of attribute factor.

Returns:

  • (::Integer)


11
12
13
# File 'lib/dsprb/decimator.rb', line 11

def factor
  @factor
end

#kernel::Array[::Float] (readonly)

Returns the value of attribute kernel.

Returns:

  • (::Array[::Float])


11
12
13
# File 'lib/dsprb/decimator.rb', line 11

def kernel
  @kernel
end

#taps::Integer (readonly)

Returns the value of attribute taps.

Returns:

  • (::Integer)


11
12
13
# File 'lib/dsprb/decimator.rb', line 11

def taps
  @taps
end

Class Method Details

.factor_for(sample_rate, target) ⇒ ::Integer

Parameters:

  • (::Numeric)
  • (::Numeric)

Returns:

  • (::Integer)


13
# File 'lib/dsprb/decimator.rb', line 13

def self.factor_for(sample_rate, target) = [(sample_rate / target).floor, 1].max

Instance Method Details

#call(waveform) ⇒ Waveform

Parameters:

Returns:



27
28
29
30
31
# File 'lib/dsprb/decimator.rb', line 27

def call(waveform)
  return waveform if @factor == 1

  Waveform.new(samples: filter(waveform.samples), sample_rate: waveform.sample_rate / @factor)
end