dsprb 1.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +182 -0
- data/CITATION.cff +30 -0
- data/LICENSE +15 -9
- data/README.md +395 -97
- data/lib/dsprb/autocorrelation.rb +2 -2
- data/lib/dsprb/biquad.rb +6 -5
- data/lib/dsprb/check.rb +43 -0
- data/lib/dsprb/curve.rb +3 -3
- data/lib/dsprb/dct.rb +34 -14
- data/lib/dsprb/decimator.rb +37 -18
- data/lib/dsprb/energy.rb +4 -4
- data/lib/dsprb/formants.rb +43 -46
- data/lib/dsprb/fourier.rb +91 -17
- data/lib/dsprb/framing.rb +2 -2
- data/lib/dsprb/lpc.rb +52 -30
- data/lib/dsprb/mel_filterbank.rb +5 -6
- data/lib/dsprb/mfcc.rb +13 -4
- data/lib/dsprb/polynomial.rb +123 -0
- data/lib/dsprb/prepared.rb +21 -0
- data/lib/dsprb/spectral_moments.rb +3 -1
- data/lib/dsprb/spectrum.rb +17 -10
- data/lib/dsprb/version.rb +1 -1
- data/lib/dsprb/wav.rb +1 -1
- data/lib/dsprb/waveform.rb +1 -4
- data/lib/dsprb/window.rb +18 -3
- data/lib/dsprb/yin.rb +135 -52
- data/lib/dsprb.rb +6 -1
- data/sig/dsprb.rbs +86 -3
- metadata +13 -8
data/lib/dsprb/mfcc.rb
CHANGED
|
@@ -3,13 +3,21 @@
|
|
|
3
3
|
module DSP
|
|
4
4
|
class Mfcc
|
|
5
5
|
DEFAULT_COEFFICIENTS = 13
|
|
6
|
+
|
|
7
|
+
# Deprecated: the default size follows the frame length (size_for).
|
|
6
8
|
DEFAULT_SIZE = 512
|
|
9
|
+
|
|
7
10
|
DEFAULT_WINDOW_SECONDS = 0.025
|
|
8
11
|
DEFAULT_HOP_SECONDS = 0.010
|
|
9
12
|
|
|
10
13
|
attr_reader :filterbank, :spectrum, :coefficients
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
# The smallest transform that holds a frame of this many samples.
|
|
16
|
+
def self.size_for(length) = [1 << (Integer(length) - 1).bit_length, Fourier::Real::MINIMUM_SIZE].max
|
|
17
|
+
|
|
18
|
+
def self.for(sample_rate:, size: nil, coefficients: DEFAULT_COEFFICIENTS, **options)
|
|
19
|
+
size ||= size_for((sample_rate * DEFAULT_WINDOW_SECONDS).round)
|
|
20
|
+
|
|
13
21
|
new(
|
|
14
22
|
filterbank: MelFilterbank.new(sample_rate: sample_rate, size: size, **options),
|
|
15
23
|
coefficients: coefficients
|
|
@@ -21,12 +29,12 @@ module DSP
|
|
|
21
29
|
@coefficients = Integer(coefficients)
|
|
22
30
|
@spectrum = spectrum || Spectrum.new(filterbank.size)
|
|
23
31
|
|
|
24
|
-
|
|
25
|
-
|
|
32
|
+
Check.positive!(@coefficients, "coefficients")
|
|
33
|
+
@dct = Dct::Two.for(@filterbank.filters, @coefficients)
|
|
26
34
|
freeze
|
|
27
35
|
end
|
|
28
36
|
|
|
29
|
-
def call(power) =
|
|
37
|
+
def call(power) = @dct.call(@filterbank.log_energies(power))
|
|
30
38
|
|
|
31
39
|
def sequence(
|
|
32
40
|
waveform,
|
|
@@ -38,6 +46,7 @@ module DSP
|
|
|
38
46
|
assert_rate!(waveform)
|
|
39
47
|
|
|
40
48
|
length = waveform.seconds_to_samples(window_seconds)
|
|
49
|
+
Check.at_least!(@spectrum.size, length, "FFT size")
|
|
41
50
|
taper = Window.resolve(window).call(length)
|
|
42
51
|
emphasized = preemphasis ? Framing.preemphasis(waveform.samples, coefficient: preemphasis) : waveform.samples
|
|
43
52
|
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSP
|
|
4
|
+
module Polynomial
|
|
5
|
+
MAXIMUM_ITERATIONS = 100
|
|
6
|
+
TOLERANCE = 1e-12
|
|
7
|
+
|
|
8
|
+
# Rotates the starting circle off the real axis, where the roots of a real
|
|
9
|
+
# polynomial come in conjugate pairs and a symmetric start would stall.
|
|
10
|
+
STARTING_ANGLE = 0.4
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
# Roots of c[0] z^n + c[1] z^(n-1) + ... + c[n] by the Aberth-Ehrlich
|
|
15
|
+
# iteration from a fixed circle whose radius is the geometric mean of the
|
|
16
|
+
# root moduli. Each root is updated in place as soon as its correction is
|
|
17
|
+
# known, and the walk stops when the largest relative correction is below
|
|
18
|
+
# the tolerance or after a fixed number of sweeps.
|
|
19
|
+
def roots(coefficients)
|
|
20
|
+
leading = coefficients.index { |value| !value.zero? }
|
|
21
|
+
return [] if leading.nil?
|
|
22
|
+
|
|
23
|
+
trailing = coefficients.rindex { |value| !value.zero? }
|
|
24
|
+
zeros = Array.new(coefficients.length - 1 - trailing) { Complex(0.0, 0.0) }
|
|
25
|
+
monic = coefficients[leading..trailing].map { |value| value.to_f / coefficients[leading] }
|
|
26
|
+
degree = monic.length - 1
|
|
27
|
+
return zeros if degree.zero?
|
|
28
|
+
|
|
29
|
+
real, imaginary = start(monic, degree)
|
|
30
|
+
iterate(monic, degree, real, imaginary)
|
|
31
|
+
|
|
32
|
+
Array.new(degree) { |index| Complex(real[index], imaginary[index]) } + zeros
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def start(monic, degree)
|
|
36
|
+
radius = monic[degree].abs ** (1.0 / degree)
|
|
37
|
+
angles = Array.new(degree) { |index| (2.0 * Math::PI * index / degree) + STARTING_ANGLE }
|
|
38
|
+
|
|
39
|
+
[angles.map { |angle| radius * Math.cos(angle) }, angles.map { |angle| radius * Math.sin(angle) }]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def iterate(monic, degree, real, imaginary)
|
|
43
|
+
sweep = 0
|
|
44
|
+
while sweep < MAXIMUM_ITERATIONS
|
|
45
|
+
largest = 0.0
|
|
46
|
+
root = 0
|
|
47
|
+
while root < degree
|
|
48
|
+
correction = correct(monic, degree, real, imaginary, root)
|
|
49
|
+
largest = correction if correction > largest
|
|
50
|
+
root += 1
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
break if largest < TOLERANCE
|
|
54
|
+
|
|
55
|
+
sweep += 1
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Newton's ratio p/p' from one Horner pass, then Aberth's repulsion from
|
|
60
|
+
# the other estimates. Returns the size of the step relative to the root.
|
|
61
|
+
def correct(monic, degree, real, imaginary, root)
|
|
62
|
+
at_real = real[root]
|
|
63
|
+
at_imaginary = imaginary[root]
|
|
64
|
+
|
|
65
|
+
value_real = 1.0
|
|
66
|
+
value_imaginary = 0.0
|
|
67
|
+
slope_real = 0.0
|
|
68
|
+
slope_imaginary = 0.0
|
|
69
|
+
index = 1
|
|
70
|
+
while index <= degree
|
|
71
|
+
next_slope = (slope_real * at_real) - (slope_imaginary * at_imaginary) + value_real
|
|
72
|
+
slope_imaginary = (slope_real * at_imaginary) + (slope_imaginary * at_real) + value_imaginary
|
|
73
|
+
slope_real = next_slope
|
|
74
|
+
next_value = (value_real * at_real) - (value_imaginary * at_imaginary) + monic[index]
|
|
75
|
+
value_imaginary = (value_real * at_imaginary) + (value_imaginary * at_real)
|
|
76
|
+
value_real = next_value
|
|
77
|
+
index += 1
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
slope_norm = (slope_real * slope_real) + (slope_imaginary * slope_imaginary)
|
|
81
|
+
return 0.0 if slope_norm.zero?
|
|
82
|
+
|
|
83
|
+
ratio_real = ((value_real * slope_real) + (value_imaginary * slope_imaginary)) / slope_norm
|
|
84
|
+
ratio_imaginary = ((value_imaginary * slope_real) - (value_real * slope_imaginary)) / slope_norm
|
|
85
|
+
|
|
86
|
+
repulsion_real = 0.0
|
|
87
|
+
repulsion_imaginary = 0.0
|
|
88
|
+
other = 0
|
|
89
|
+
while other < degree
|
|
90
|
+
unless other == root
|
|
91
|
+
gap_real = at_real - real[other]
|
|
92
|
+
gap_imaginary = at_imaginary - imaginary[other]
|
|
93
|
+
gap_norm = (gap_real * gap_real) + (gap_imaginary * gap_imaginary)
|
|
94
|
+
unless gap_norm.zero?
|
|
95
|
+
repulsion_real += gap_real / gap_norm
|
|
96
|
+
repulsion_imaginary -= gap_imaginary / gap_norm
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
other += 1
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
divisor_real = 1.0 - ((ratio_real * repulsion_real) - (ratio_imaginary * repulsion_imaginary))
|
|
104
|
+
divisor_imaginary = -((ratio_real * repulsion_imaginary) + (ratio_imaginary * repulsion_real))
|
|
105
|
+
divisor_norm = (divisor_real * divisor_real) + (divisor_imaginary * divisor_imaginary)
|
|
106
|
+
if divisor_norm.zero?
|
|
107
|
+
step_real = ratio_real
|
|
108
|
+
step_imaginary = ratio_imaginary
|
|
109
|
+
else
|
|
110
|
+
step_real = ((ratio_real * divisor_real) + (ratio_imaginary * divisor_imaginary)) / divisor_norm
|
|
111
|
+
step_imaginary = ((ratio_imaginary * divisor_real) - (ratio_real * divisor_imaginary)) / divisor_norm
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
real[root] = at_real - step_real
|
|
115
|
+
imaginary[root] = at_imaginary - step_imaginary
|
|
116
|
+
|
|
117
|
+
step = (step_real * step_real) + (step_imaginary * step_imaginary)
|
|
118
|
+
Math.sqrt(step / ((at_real * at_real) + (at_imaginary * at_imaginary)))
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
private_class_method :start, :iterate, :correct
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSP
|
|
4
|
+
# Shares one instance per parameter set. What a stage precomputes depends on
|
|
5
|
+
# nothing but its own sizes, so framing a signal need not rebuild it per frame.
|
|
6
|
+
module Prepared
|
|
7
|
+
def self.extended(base)
|
|
8
|
+
base.instance_variable_set(:@prepared, {})
|
|
9
|
+
base.instance_variable_set(:@prepared_mutex, Mutex.new)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def prepared(key)
|
|
13
|
+
found = @prepared[key]
|
|
14
|
+
return found unless found.nil?
|
|
15
|
+
|
|
16
|
+
@prepared_mutex.synchronize { @prepared[key] ||= yield }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def prepared_count = @prepared.size
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module DSP
|
|
4
|
-
SpectralMoments = Data.define(:centroid, :spread, :skewness, :kurtosis)
|
|
4
|
+
SpectralMoments = Data.define(:centroid, :spread, :skewness, :kurtosis)
|
|
5
|
+
|
|
6
|
+
class SpectralMoments
|
|
5
7
|
# Below this the band carries no measurable energy and the moments are undefined.
|
|
6
8
|
ENERGY_FLOOR = 1e-12
|
|
7
9
|
VARIANCE_FLOOR = 1e-12
|
data/lib/dsprb/spectrum.rb
CHANGED
|
@@ -4,17 +4,30 @@ module DSP
|
|
|
4
4
|
class Spectrum
|
|
5
5
|
attr_reader :size, :bins, :transform
|
|
6
6
|
|
|
7
|
+
# Defaults to the real-input transform; a complex one such as
|
|
8
|
+
# Fourier::Radix2 may be injected instead.
|
|
7
9
|
def initialize(size, transform: nil)
|
|
8
|
-
@size = Integer(size)
|
|
9
|
-
@transform = transform || Fourier::Radix2.new(@size)
|
|
10
|
+
@size = Check.power_of_two!(Integer(size), "FFT size")
|
|
10
11
|
@bins = (@size / 2) + 1
|
|
12
|
+
@transform = transform || Fourier::Real.new(@size)
|
|
13
|
+
@real_input = @transform.respond_to?(:power)
|
|
11
14
|
freeze
|
|
12
15
|
end
|
|
13
16
|
|
|
14
|
-
def power(frame)
|
|
17
|
+
def power(frame) = @real_input ? @transform.power(frame) : complex_power(frame)
|
|
18
|
+
|
|
19
|
+
def magnitude(frame) = power(frame).map { |value| Math.sqrt(value) }
|
|
20
|
+
|
|
21
|
+
def frequencies(sample_rate) = Array.new(@bins) { |bin| bin * sample_rate / @size.to_f }
|
|
22
|
+
|
|
23
|
+
def bin_of(frequency, sample_rate) = (frequency * @size / sample_rate.to_f).round
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def complex_power(frame)
|
|
15
28
|
real = Array.new(@size, 0.0)
|
|
16
29
|
imaginary = Array.new(@size, 0.0)
|
|
17
|
-
limit =
|
|
30
|
+
limit = frame.length < @size ? frame.length : @size
|
|
18
31
|
index = 0
|
|
19
32
|
while index < limit
|
|
20
33
|
real[index] = frame[index].to_f
|
|
@@ -25,11 +38,5 @@ module DSP
|
|
|
25
38
|
|
|
26
39
|
Array.new(@bins) { |bin| (real[bin] * real[bin]) + (imaginary[bin] * imaginary[bin]) }
|
|
27
40
|
end
|
|
28
|
-
|
|
29
|
-
def magnitude(frame) = power(frame).map { |value| Math.sqrt(value) }
|
|
30
|
-
|
|
31
|
-
def frequencies(sample_rate) = Array.new(@bins) { |bin| bin * sample_rate / @size.to_f }
|
|
32
|
-
|
|
33
|
-
def bin_of(frequency, sample_rate) = (frequency * @size / sample_rate.to_f).round
|
|
34
41
|
end
|
|
35
42
|
end
|
data/lib/dsprb/version.rb
CHANGED
data/lib/dsprb/wav.rb
CHANGED
|
@@ -12,7 +12,7 @@ module DSP
|
|
|
12
12
|
HEADER_BYTES = 12
|
|
13
13
|
CHUNK_HEADER_BYTES = 8
|
|
14
14
|
|
|
15
|
-
# Unsigned 8-bit
|
|
15
|
+
# Unsigned 8-bit is the one depth WAVE stores with a midpoint offset.
|
|
16
16
|
EIGHT_BIT_MIDPOINT = 128.0
|
|
17
17
|
|
|
18
18
|
module_function
|
data/lib/dsprb/waveform.rb
CHANGED
|
@@ -3,10 +3,7 @@
|
|
|
3
3
|
module DSP
|
|
4
4
|
Waveform = Data.define(:samples, :sample_rate) do
|
|
5
5
|
def initialize(samples:, sample_rate:)
|
|
6
|
-
|
|
7
|
-
raise ArgumentError, "sample rate must be positive, got #{rate}" unless rate.positive?
|
|
8
|
-
|
|
9
|
-
super(samples: samples, sample_rate: rate)
|
|
6
|
+
super(samples: samples, sample_rate: Check.positive!(Float(sample_rate), "sample rate"))
|
|
10
7
|
end
|
|
11
8
|
|
|
12
9
|
def length = samples.length
|
data/lib/dsprb/window.rb
CHANGED
|
@@ -2,17 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
module DSP
|
|
4
4
|
module Window
|
|
5
|
-
#
|
|
5
|
+
# w[i] = sum_k (-1)^k a_k cos(2 pi k i / (N - 1)).
|
|
6
6
|
HAMMING = [0.54, 0.46].freeze
|
|
7
7
|
HANN = [0.5, 0.5].freeze
|
|
8
8
|
BLACKMAN = [0.42, 0.5, 0.08].freeze
|
|
9
9
|
|
|
10
|
+
extend Prepared
|
|
11
|
+
|
|
10
12
|
module_function
|
|
11
13
|
|
|
12
14
|
def rectangular(length)
|
|
13
15
|
assert_length!(length)
|
|
14
16
|
|
|
15
|
-
Array.new(length, 1.0)
|
|
17
|
+
Window.prepared([:rectangular, length]) { Array.new(length, 1.0).freeze }
|
|
16
18
|
end
|
|
17
19
|
|
|
18
20
|
def hamming(length) = cosine_sum(length, HAMMING)
|
|
@@ -23,13 +25,26 @@ module DSP
|
|
|
23
25
|
|
|
24
26
|
def cosine_sum(length, coefficients)
|
|
25
27
|
assert_length!(length)
|
|
28
|
+
|
|
29
|
+
Window.prepared([:cosine_sum, length, coefficients]) { build_cosine_sum(length, coefficients).freeze }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def build_cosine_sum(length, coefficients)
|
|
26
33
|
return [1.0] if length == 1
|
|
27
34
|
|
|
28
35
|
span = (length - 1).to_f
|
|
36
|
+
terms = coefficients.length
|
|
29
37
|
|
|
30
38
|
Array.new(length) do |index|
|
|
31
39
|
phase = 2.0 * Math::PI * index / span
|
|
32
|
-
|
|
40
|
+
total = 0.0
|
|
41
|
+
term = 0
|
|
42
|
+
while term < terms
|
|
43
|
+
total += (term.even? ? coefficients[term] : -coefficients[term]) * Math.cos(term * phase)
|
|
44
|
+
term += 1
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
total
|
|
33
48
|
end
|
|
34
49
|
end
|
|
35
50
|
|
data/lib/dsprb/yin.rb
CHANGED
|
@@ -13,22 +13,31 @@ module DSP
|
|
|
13
13
|
# A global minimum above this is too shallow to call the frame voiced.
|
|
14
14
|
DEFAULT_VOICED_THRESHOLD = 0.35
|
|
15
15
|
|
|
16
|
-
#
|
|
17
|
-
#
|
|
16
|
+
# A quarter of 22050 Hz, over five times the 1000 Hz needed for the default
|
|
17
|
+
# 500 Hz maximum, and low enough to keep the O(window * lags) search cheap.
|
|
18
18
|
DEFAULT_WORKING_RATE = 5512.5
|
|
19
19
|
|
|
20
|
-
# Bounds the lag search for pathological rate and frequency combinations.
|
|
21
20
|
MAXIMUM_LAG = 10_000
|
|
22
21
|
|
|
23
|
-
attr_reader
|
|
22
|
+
attr_reader(
|
|
23
|
+
:minimum_frequency,
|
|
24
|
+
:maximum_frequency,
|
|
25
|
+
:hop_seconds,
|
|
26
|
+
:threshold,
|
|
27
|
+
:voiced_threshold,
|
|
28
|
+
:working_rate,
|
|
29
|
+
:minimum_rms
|
|
30
|
+
)
|
|
24
31
|
|
|
32
|
+
# minimum_rms reports frames quieter than it unvoiced without searching.
|
|
25
33
|
def initialize(
|
|
26
34
|
minimum_frequency: DEFAULT_MINIMUM_HZ,
|
|
27
35
|
maximum_frequency: DEFAULT_MAXIMUM_HZ,
|
|
28
36
|
hop_seconds: DEFAULT_HOP_SECONDS,
|
|
29
37
|
threshold: DEFAULT_THRESHOLD,
|
|
30
38
|
voiced_threshold: DEFAULT_VOICED_THRESHOLD,
|
|
31
|
-
working_rate: DEFAULT_WORKING_RATE
|
|
39
|
+
working_rate: DEFAULT_WORKING_RATE,
|
|
40
|
+
minimum_rms: 0.0
|
|
32
41
|
)
|
|
33
42
|
@minimum_frequency = Float(minimum_frequency)
|
|
34
43
|
@maximum_frequency = Float(maximum_frequency)
|
|
@@ -36,13 +45,14 @@ module DSP
|
|
|
36
45
|
@threshold = Float(threshold)
|
|
37
46
|
@voiced_threshold = Float(voiced_threshold)
|
|
38
47
|
@working_rate = Float(working_rate)
|
|
48
|
+
@minimum_rms = Float(minimum_rms)
|
|
39
49
|
|
|
40
50
|
validate!
|
|
41
51
|
freeze
|
|
42
52
|
end
|
|
43
53
|
|
|
44
54
|
def call(waveform)
|
|
45
|
-
reduced = Decimator.
|
|
55
|
+
reduced = Decimator.to(waveform, @working_rate)
|
|
46
56
|
rate = reduced.sample_rate
|
|
47
57
|
shortest = (rate / @maximum_frequency).floor.clamp(2, MAXIMUM_LAG)
|
|
48
58
|
longest = (rate / @minimum_frequency).ceil.clamp(shortest + 2, MAXIMUM_LAG)
|
|
@@ -63,8 +73,9 @@ module DSP
|
|
|
63
73
|
)
|
|
64
74
|
end
|
|
65
75
|
|
|
66
|
-
|
|
67
|
-
|
|
76
|
+
Check.positive!(@hop_seconds, "hop_seconds")
|
|
77
|
+
Check.positive!(@working_rate, "working_rate")
|
|
78
|
+
Check.non_negative!(@minimum_rms, "minimum_rms")
|
|
68
79
|
end
|
|
69
80
|
|
|
70
81
|
def track(samples, rate:, window:, hop:, shortest:, longest:)
|
|
@@ -74,10 +85,16 @@ module DSP
|
|
|
74
85
|
|
|
75
86
|
position = 0
|
|
76
87
|
while position + window + longest <= samples.length
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
88
|
+
if quiet?(samples, position, window)
|
|
89
|
+
f0 << 0.0
|
|
90
|
+
confidence << 0.0
|
|
91
|
+
else
|
|
92
|
+
normalized, lag = search(samples, position, window, shortest, longest)
|
|
93
|
+
frequency, weight = evaluate(normalized, lag, shortest - 1, longest, rate)
|
|
94
|
+
f0 << frequency
|
|
95
|
+
confidence << weight
|
|
96
|
+
end
|
|
97
|
+
|
|
81
98
|
times << (position / rate)
|
|
82
99
|
position += hop
|
|
83
100
|
end
|
|
@@ -85,71 +102,137 @@ module DSP
|
|
|
85
102
|
PitchTrack.new(f0: f0, confidence: confidence, times: times, hop_seconds: hop / rate)
|
|
86
103
|
end
|
|
87
104
|
|
|
88
|
-
def
|
|
89
|
-
|
|
90
|
-
normalized = Array.new(longest + 1, 1.0)
|
|
91
|
-
running = 0.0
|
|
105
|
+
def quiet?(samples, position, window)
|
|
106
|
+
return false unless @minimum_rms.positive?
|
|
92
107
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
108
|
+
total = 0.0
|
|
109
|
+
index = 0
|
|
110
|
+
while index < window
|
|
111
|
+
value = samples[position + index]
|
|
112
|
+
total += value * value
|
|
113
|
+
index += 1
|
|
98
114
|
end
|
|
99
115
|
|
|
100
|
-
|
|
116
|
+
Math.sqrt(total / window) < @minimum_rms
|
|
101
117
|
end
|
|
102
118
|
|
|
103
|
-
|
|
104
|
-
|
|
119
|
+
# The cumulative mean normalization only looks back, so lags accumulate in
|
|
120
|
+
# order and the walk stops at the first qualifying dip. A frame without one
|
|
121
|
+
# has filled the whole array by the end, which the global minimum needs.
|
|
122
|
+
# The lag below the range is filled only when the dip sits at the shortest
|
|
123
|
+
# lag and the refinement needs both neighbors.
|
|
124
|
+
def search(samples, position, window, shortest, longest)
|
|
125
|
+
first = shortest - 1
|
|
126
|
+
normalized = Array.new(longest + 1, 1.0)
|
|
127
|
+
below = opening(samples, position, window, first)
|
|
128
|
+
running = below
|
|
129
|
+
best = shortest
|
|
130
|
+
lowest = Float::INFINITY
|
|
131
|
+
chosen = nil
|
|
105
132
|
|
|
106
133
|
lag = shortest
|
|
107
134
|
while lag <= longest
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
135
|
+
raw = difference(samples, position, window, lag)
|
|
136
|
+
running += raw
|
|
137
|
+
value = running.zero? ? 1.0 : raw * lag / running
|
|
138
|
+
normalized[lag] = value
|
|
139
|
+
|
|
140
|
+
if value < lowest
|
|
141
|
+
lowest = value
|
|
142
|
+
best = lag
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
previous = lag - 1
|
|
146
|
+
if chosen.nil? &&
|
|
147
|
+
previous >= shortest &&
|
|
148
|
+
previous < longest &&
|
|
149
|
+
normalized[previous] < @threshold &&
|
|
150
|
+
normalized[previous] <= value
|
|
151
|
+
chosen = previous
|
|
152
|
+
break
|
|
114
153
|
end
|
|
115
154
|
|
|
116
|
-
raw[lag] = total
|
|
117
155
|
lag += 1
|
|
118
156
|
end
|
|
119
157
|
|
|
120
|
-
|
|
158
|
+
period = chosen || best
|
|
159
|
+
if period == shortest && below.positive?
|
|
160
|
+
normalized[first] = difference(samples, position, window, first) * first / below
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
[normalized, period]
|
|
121
164
|
end
|
|
122
165
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
166
|
+
# Sum of d(1..span), the lags the paper's mean includes below the search.
|
|
167
|
+
# With S_i the sum of the span samples after x_i and E_j the energy of the
|
|
168
|
+
# window shifted by j,
|
|
169
|
+
#
|
|
170
|
+
# sum_{j=1..span} d(j) = span E_0 + sum_{j=1..span} E_j - 2 sum_i x_i S_i
|
|
171
|
+
#
|
|
172
|
+
# which costs one pass over the window rather than one per lag. The terms
|
|
173
|
+
# cancel to rounding residue when an offset dwarfs the variation, so x is
|
|
174
|
+
# measured from the mean of its first span + 1 samples; d does not depend
|
|
175
|
+
# on that choice.
|
|
176
|
+
def opening(samples, position, window, span)
|
|
177
|
+
reference = 0.0
|
|
178
|
+
offset = 0
|
|
179
|
+
while offset <= span
|
|
180
|
+
reference += samples[position + offset]
|
|
181
|
+
offset += 1
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
reference /= span + 1
|
|
127
185
|
|
|
128
|
-
|
|
186
|
+
ahead = 0.0
|
|
187
|
+
offset = 1
|
|
188
|
+
while offset <= span
|
|
189
|
+
ahead += samples[position + offset] - reference
|
|
190
|
+
offset += 1
|
|
129
191
|
end
|
|
130
192
|
|
|
131
|
-
|
|
132
|
-
|
|
193
|
+
energy = 0.0
|
|
194
|
+
cross = 0.0
|
|
195
|
+
current = samples[position] - reference
|
|
196
|
+
cursor = position + 1
|
|
197
|
+
limit = position + window
|
|
198
|
+
while cursor <= limit
|
|
199
|
+
value = samples[cursor]
|
|
200
|
+
energy += current * current
|
|
201
|
+
cross += current * ahead
|
|
202
|
+
ahead += samples[cursor + span] - value
|
|
203
|
+
current = value - reference
|
|
204
|
+
cursor += 1
|
|
205
|
+
end
|
|
133
206
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
207
|
+
shifted = energy
|
|
208
|
+
shifted_total = 0.0
|
|
209
|
+
offset = 0
|
|
210
|
+
while offset < span
|
|
211
|
+
entering = samples[position + window + offset] - reference
|
|
212
|
+
leaving = samples[position + offset] - reference
|
|
213
|
+
shifted += (entering * entering) - (leaving * leaving)
|
|
214
|
+
shifted_total += shifted
|
|
215
|
+
offset += 1
|
|
216
|
+
end
|
|
137
217
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
lowest = normalized[lag]
|
|
142
|
-
best = lag
|
|
143
|
-
end
|
|
218
|
+
total = (span * energy) + shifted_total - (2.0 * cross)
|
|
219
|
+
total.negative? ? 0.0 : total
|
|
220
|
+
end
|
|
144
221
|
|
|
145
|
-
|
|
222
|
+
def difference(samples, position, window, lag)
|
|
223
|
+
total = 0.0
|
|
224
|
+
index = 0
|
|
225
|
+
while index < window
|
|
226
|
+
delta = samples[position + index] - samples[position + index + lag]
|
|
227
|
+
total += delta * delta
|
|
228
|
+
index += 1
|
|
146
229
|
end
|
|
147
230
|
|
|
148
|
-
|
|
231
|
+
total
|
|
149
232
|
end
|
|
150
233
|
|
|
151
|
-
def evaluate(normalized, lag,
|
|
152
|
-
return [0.0, 0.0] unless lag >
|
|
234
|
+
def evaluate(normalized, lag, first, longest, rate)
|
|
235
|
+
return [0.0, 0.0] unless lag > first && lag < longest
|
|
153
236
|
|
|
154
237
|
offset = Parabolic.vertex_offset(normalized[lag - 1], normalized[lag], normalized[lag + 1])
|
|
155
238
|
frequency = rate / (lag + offset)
|
data/lib/dsprb.rb
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative "dsprb/version"
|
|
4
4
|
require_relative "dsprb/errors"
|
|
5
|
+
require_relative "dsprb/check"
|
|
6
|
+
require_relative "dsprb/prepared"
|
|
5
7
|
require_relative "dsprb/resolver"
|
|
6
8
|
require_relative "dsprb/parabolic"
|
|
7
9
|
require_relative "dsprb/waveform"
|
|
@@ -17,6 +19,7 @@ require_relative "dsprb/mfcc"
|
|
|
17
19
|
require_relative "dsprb/decimator"
|
|
18
20
|
require_relative "dsprb/autocorrelation"
|
|
19
21
|
require_relative "dsprb/lpc"
|
|
22
|
+
require_relative "dsprb/polynomial"
|
|
20
23
|
require_relative "dsprb/formants"
|
|
21
24
|
require_relative "dsprb/pitch_track"
|
|
22
25
|
require_relative "dsprb/yin"
|
|
@@ -38,12 +41,14 @@ module DSP
|
|
|
38
41
|
|
|
39
42
|
def mfcc(
|
|
40
43
|
waveform,
|
|
41
|
-
size:
|
|
44
|
+
size: nil,
|
|
42
45
|
coefficients: Mfcc::DEFAULT_COEFFICIENTS,
|
|
43
46
|
window_seconds: Mfcc::DEFAULT_WINDOW_SECONDS,
|
|
44
47
|
hop_seconds: Mfcc::DEFAULT_HOP_SECONDS,
|
|
45
48
|
**options
|
|
46
49
|
)
|
|
50
|
+
size ||= Mfcc.size_for(waveform.seconds_to_samples(window_seconds))
|
|
51
|
+
|
|
47
52
|
Mfcc
|
|
48
53
|
.for(sample_rate: waveform.sample_rate, size: size, coefficients: coefficients, **options)
|
|
49
54
|
.sequence(waveform, window_seconds: window_seconds, hop_seconds: hop_seconds)
|