Class: DSP::Mfcc
- Inherits:
-
Object
- Object
- DSP::Mfcc
- Defined in:
- lib/dsprb/mfcc.rb,
sig/dsprb.rbs
Constant Summary collapse
- DEFAULT_COEFFICIENTS =
13- DEFAULT_SIZE =
512- DEFAULT_WINDOW_SECONDS =
0.025- DEFAULT_HOP_SECONDS =
0.010
Instance Attribute Summary collapse
-
#coefficients ⇒ ::Integer
readonly
Returns the value of attribute coefficients.
-
#filterbank ⇒ MelFilterbank
readonly
Returns the value of attribute filterbank.
-
#spectrum ⇒ Spectrum
readonly
Returns the value of attribute spectrum.
Class Method Summary collapse
Instance Method Summary collapse
- #call(power) ⇒ ::Array[::Float]
-
#initialize(filterbank:, coefficients: DEFAULT_COEFFICIENTS, spectrum: nil) ⇒ Mfcc
constructor
A new instance of Mfcc.
- #sequence(waveform, window_seconds: DEFAULT_WINDOW_SECONDS, hop_seconds: DEFAULT_HOP_SECONDS, window: Window::DEFAULT, preemphasis: Framing::DEFAULT_PREEMPHASIS) ⇒ Object
Constructor Details
#initialize(filterbank:, coefficients: DEFAULT_COEFFICIENTS, spectrum: nil) ⇒ Mfcc
Returns a new instance of Mfcc.
19 20 21 22 23 24 25 26 27 |
# File 'lib/dsprb/mfcc.rb', line 19 def initialize(filterbank:, coefficients: DEFAULT_COEFFICIENTS, spectrum: nil) @filterbank = filterbank @coefficients = Integer(coefficients) @spectrum = spectrum || Spectrum.new(filterbank.size) raise ArgumentError, "coefficients must be positive, got #{@coefficients}" unless @coefficients.positive? freeze end |
Instance Attribute Details
#coefficients ⇒ ::Integer (readonly)
Returns the value of attribute coefficients.
10 11 12 |
# File 'lib/dsprb/mfcc.rb', line 10 def coefficients @coefficients end |
#filterbank ⇒ MelFilterbank (readonly)
Returns the value of attribute filterbank.
10 11 12 |
# File 'lib/dsprb/mfcc.rb', line 10 def filterbank @filterbank end |
#spectrum ⇒ Spectrum (readonly)
Returns the value of attribute spectrum.
10 11 12 |
# File 'lib/dsprb/mfcc.rb', line 10 def spectrum @spectrum end |
Class Method Details
.for(sample_rate:, size: DEFAULT_SIZE, coefficients: DEFAULT_COEFFICIENTS, **options) ⇒ Mfcc
12 13 14 15 16 17 |
# File 'lib/dsprb/mfcc.rb', line 12 def self.for(sample_rate:, size: DEFAULT_SIZE, coefficients: DEFAULT_COEFFICIENTS, **) new( filterbank: MelFilterbank.new(sample_rate: sample_rate, size: size, **), coefficients: coefficients ) end |
Instance Method Details
#call(power) ⇒ ::Array[::Float]
29 |
# File 'lib/dsprb/mfcc.rb', line 29 def call(power) = Dct.two(@filterbank.log_energies(power), @coefficients) |
#sequence(waveform, window_seconds: DEFAULT_WINDOW_SECONDS, hop_seconds: DEFAULT_HOP_SECONDS, window: Window::DEFAULT, preemphasis: Framing::DEFAULT_PREEMPHASIS) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dsprb/mfcc.rb', line 31 def sequence( waveform, window_seconds: DEFAULT_WINDOW_SECONDS, hop_seconds: DEFAULT_HOP_SECONDS, window: Window::DEFAULT, preemphasis: Framing::DEFAULT_PREEMPHASIS ) assert_rate!(waveform) length = waveform.seconds_to_samples(window_seconds) taper = Window.resolve(window).call(length) emphasized = preemphasis ? Framing.preemphasis(waveform.samples, coefficient: preemphasis) : waveform.samples Framing .frames(emphasized, window: length, hop: waveform.seconds_to_samples(hop_seconds)) .map { |frame| call(@spectrum.power(Window.apply(frame, taper))) } end |