Class: DTW::Aligner

Inherits:
Object
  • Object
show all
Defined in:
lib/dtwrb/aligner.rb,
sig/dtwrb.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metric: Metrics::DEFAULT, band: Bands::DEFAULT) ⇒ Aligner

Returns a new instance of Aligner.

Parameters:

  • metric: (metric) (defaults to: Metrics::DEFAULT)
  • band: (band) (defaults to: Bands::DEFAULT)


7
8
9
10
11
# File 'lib/dtwrb/aligner.rb', line 7

def initialize(metric: Metrics::DEFAULT, band: Bands::DEFAULT)
  @metric = Metrics.resolve(metric)
  @band = Bands.resolve(band)
  freeze
end

Instance Attribute Details

#band_Band (readonly)

Returns the value of attribute band.

Returns:



5
6
7
# File 'lib/dtwrb/aligner.rb', line 5

def band
  @band
end

#metric_Metric (readonly)

Returns the value of attribute metric.

Returns:



5
6
7
# File 'lib/dtwrb/aligner.rb', line 5

def metric
  @metric
end

Instance Method Details

#align(sequence_a, sequence_b) ⇒ Alignment

Parameters:

  • (sequence)
  • (sequence)

Returns:



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dtwrb/aligner.rb', line 13

def align(sequence_a, sequence_b)
  frames_a = Sequence.vectorize(sequence_a)
  frames_b = Sequence.vectorize(sequence_b)
  return Alignment::UNDEFINED if frames_a.empty? || frames_b.empty?

  grid = accumulate(frames_a, frames_b)
  Alignment.new(
    path: backtrack(grid, frames_a.length, frames_b.length),
    cost: grid[frames_a.length][frames_b.length]
  )
end

#distance(sequence_a, sequence_b) ⇒ ::Float

Parameters:

  • (sequence)
  • (sequence)

Returns:

  • (::Float)


25
# File 'lib/dtwrb/aligner.rb', line 25

def distance(sequence_a, sequence_b) = align(sequence_a, sequence_b).distance