dsprb 2.0.0 → 2.1.1
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 +130 -0
- data/CITATION.cff +30 -0
- data/LICENSE +15 -9
- data/README.md +392 -108
- data/lib/dsprb/biquad.rb +3 -1
- data/lib/dsprb/decimator.rb +1 -1
- data/lib/dsprb/formants.rb +37 -36
- data/lib/dsprb/fourier.rb +1 -1
- data/lib/dsprb/mfcc.rb +10 -1
- data/lib/dsprb/polynomial.rb +123 -0
- data/lib/dsprb/spectral_moments.rb +3 -1
- data/lib/dsprb/spectrum.rb +1 -1
- data/lib/dsprb/version.rb +1 -1
- data/lib/dsprb/yin.rb +74 -9
- data/lib/dsprb.rb +4 -1
- data/sig/dsprb.rbs +19 -3
- metadata +11 -8
data/lib/dsprb/formants.rb
CHANGED
|
@@ -2,18 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
module DSP
|
|
4
4
|
class Formants
|
|
5
|
-
Peak = Data.define(:frequency, :amplitude)
|
|
5
|
+
Peak = Data.define(:frequency, :amplitude, :bandwidth) do
|
|
6
|
+
def initialize(frequency:, amplitude:, bandwidth: nil) = super
|
|
7
|
+
end
|
|
6
8
|
|
|
7
9
|
# Conventional ceiling for adult speech; the first four formants fall below it.
|
|
8
10
|
DEFAULT_MAXIMUM_HZ = 5500.0
|
|
9
11
|
DEFAULT_COUNT = 4
|
|
10
|
-
DEFAULT_RESOLUTION = 512
|
|
11
12
|
|
|
12
|
-
#
|
|
13
|
+
# Deprecated and ignored.
|
|
14
|
+
DEFAULT_RESOLUTION = 512
|
|
13
15
|
DEFAULT_SEPARATION_HZ = 250.0
|
|
14
16
|
|
|
15
|
-
#
|
|
16
|
-
DEFAULT_EDGE_MARGIN_HZ =
|
|
17
|
+
# Praat's margin: a root this close to 0 Hz or Nyquist shapes the spectral slope.
|
|
18
|
+
DEFAULT_EDGE_MARGIN_HZ = 50.0
|
|
19
|
+
|
|
20
|
+
# Above the bandwidth of 99 % of F1 and F2 in recorded vowels, below that of the spare
|
|
21
|
+
# pole pair a tenth-order fit leaves in the wide F1-F2 gap of [i].
|
|
22
|
+
MAXIMUM_BANDWIDTH_HZ = 1000.0
|
|
17
23
|
|
|
18
24
|
MINIMUM_SAMPLES = 40
|
|
19
25
|
|
|
@@ -37,9 +43,9 @@ module DSP
|
|
|
37
43
|
|
|
38
44
|
Check.positive!(@count, "count")
|
|
39
45
|
Check.at_least!(@resolution, 2, "resolution")
|
|
46
|
+
Check.non_negative!(@edge_margin, "edge_margin")
|
|
40
47
|
|
|
41
48
|
@order = (2 * @count) + 2
|
|
42
|
-
@envelope = Lpc::Envelope.for(points: @resolution, order: @order)
|
|
43
49
|
freeze
|
|
44
50
|
end
|
|
45
51
|
|
|
@@ -52,7 +58,7 @@ module DSP
|
|
|
52
58
|
model = fit(reduced)
|
|
53
59
|
return [] if model.nil?
|
|
54
60
|
|
|
55
|
-
|
|
61
|
+
resonances(model.coefficients, reduced.sample_rate).first(@count)
|
|
56
62
|
end
|
|
57
63
|
|
|
58
64
|
private
|
|
@@ -67,42 +73,37 @@ module DSP
|
|
|
67
73
|
Lpc.levinson(correlation, @order)
|
|
68
74
|
end
|
|
69
75
|
|
|
70
|
-
|
|
76
|
+
# A pole r e^{i w} of the predictor resonates at w fs / 2 pi with a
|
|
77
|
+
# bandwidth of -ln(r) fs / pi.
|
|
78
|
+
def resonances(coefficients, sample_rate)
|
|
71
79
|
nyquist = sample_rate / 2.0
|
|
72
|
-
span = envelope.length - 1
|
|
73
|
-
found = []
|
|
74
|
-
|
|
75
|
-
index = 1
|
|
76
|
-
while index < span
|
|
77
|
-
found << refine(envelope, index, nyquist, span) if rising?(envelope, index)
|
|
78
|
-
index += 1
|
|
79
|
-
end
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
envelope[index] > envelope[index - 1] && envelope[index] >= envelope[index + 1]
|
|
86
|
-
end
|
|
81
|
+
Polynomial
|
|
82
|
+
.roots(coefficients)
|
|
83
|
+
.filter_map do |root|
|
|
84
|
+
next unless root.imaginary > Float::EPSILON * root.abs
|
|
87
85
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
frequency = nyquist * (index + Parabolic.vertex_offset(previous, current, following)) / span
|
|
93
|
-
return nil unless frequency > @edge_margin && frequency < nyquist - @edge_margin
|
|
86
|
+
frequency = root.arg * nyquist / Math::PI
|
|
87
|
+
bandwidth = -Math.log(root.abs) * sample_rate / Math::PI
|
|
88
|
+
next unless frequency > @edge_margin && frequency < nyquist - @edge_margin
|
|
89
|
+
next unless bandwidth < MAXIMUM_BANDWIDTH_HZ
|
|
94
90
|
|
|
95
|
-
|
|
91
|
+
Peak.new(frequency: frequency, amplitude: level(coefficients, root.arg), bandwidth: bandwidth)
|
|
92
|
+
end
|
|
93
|
+
.sort_by(&:frequency)
|
|
96
94
|
end
|
|
97
95
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
96
|
+
# ln 1 / |A(e^{i w})|^2, the all-pole envelope at the resonance.
|
|
97
|
+
def level(coefficients, angle)
|
|
98
|
+
real = 0.0
|
|
99
|
+
imaginary = 0.0
|
|
100
|
+
coefficients.each_with_index do |coefficient, lag|
|
|
101
|
+
real += coefficient * Math.cos(angle * lag)
|
|
102
|
+
imaginary -= coefficient * Math.sin(angle * lag)
|
|
105
103
|
end
|
|
104
|
+
|
|
105
|
+
total = (real * real) + (imaginary * imaginary)
|
|
106
|
+
Math.log((total < Lpc::DENOMINATOR_FLOOR ? Lpc::UNBOUNDED_MAGNITUDE : 1.0 / total) + LOG_FLOOR)
|
|
106
107
|
end
|
|
107
108
|
end
|
|
108
109
|
end
|
data/lib/dsprb/fourier.rb
CHANGED
|
@@ -108,7 +108,7 @@ module DSP
|
|
|
108
108
|
attr_reader :size, :bins
|
|
109
109
|
|
|
110
110
|
def initialize(size)
|
|
111
|
-
@size = Check.at_least!(Integer(size), MINIMUM_SIZE, "size")
|
|
111
|
+
@size = Check.power_of_two!(Check.at_least!(Integer(size), MINIMUM_SIZE, "size"), "FFT size")
|
|
112
112
|
@half = @size / 2
|
|
113
113
|
@inner = Radix2.new(@half)
|
|
114
114
|
@bins = @half + 1
|
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
|
|
@@ -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
|
|
@@ -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
|
@@ -7,7 +7,7 @@ module DSP
|
|
|
7
7
|
# Defaults to the real-input transform; a complex one such as
|
|
8
8
|
# Fourier::Radix2 may be injected instead.
|
|
9
9
|
def initialize(size, transform: nil)
|
|
10
|
-
@size = Integer(size)
|
|
10
|
+
@size = Check.power_of_two!(Integer(size), "FFT size")
|
|
11
11
|
@bins = (@size / 2) + 1
|
|
12
12
|
@transform = transform || Fourier::Real.new(@size)
|
|
13
13
|
@real_input = @transform.respond_to?(:power)
|
data/lib/dsprb/version.rb
CHANGED
data/lib/dsprb/yin.rb
CHANGED
|
@@ -13,8 +13,8 @@ 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
|
-
# the O(window * lags) search
|
|
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
20
|
MAXIMUM_LAG = 10_000
|
|
@@ -90,7 +90,7 @@ module DSP
|
|
|
90
90
|
confidence << 0.0
|
|
91
91
|
else
|
|
92
92
|
normalized, lag = search(samples, position, window, shortest, longest)
|
|
93
|
-
frequency, weight = evaluate(normalized, lag, shortest, longest, rate)
|
|
93
|
+
frequency, weight = evaluate(normalized, lag, shortest - 1, longest, rate)
|
|
94
94
|
f0 << frequency
|
|
95
95
|
confidence << weight
|
|
96
96
|
end
|
|
@@ -119,9 +119,13 @@ module DSP
|
|
|
119
119
|
# The cumulative mean normalization only looks back, so lags accumulate in
|
|
120
120
|
# order and the walk stops at the first qualifying dip. A frame without one
|
|
121
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.
|
|
122
124
|
def search(samples, position, window, shortest, longest)
|
|
125
|
+
first = shortest - 1
|
|
123
126
|
normalized = Array.new(longest + 1, 1.0)
|
|
124
|
-
|
|
127
|
+
below = opening(samples, position, window, first)
|
|
128
|
+
running = below
|
|
125
129
|
best = shortest
|
|
126
130
|
lowest = Float::INFINITY
|
|
127
131
|
chosen = nil
|
|
@@ -130,7 +134,7 @@ module DSP
|
|
|
130
134
|
while lag <= longest
|
|
131
135
|
raw = difference(samples, position, window, lag)
|
|
132
136
|
running += raw
|
|
133
|
-
value = running.zero? ? 1.0 : raw *
|
|
137
|
+
value = running.zero? ? 1.0 : raw * lag / running
|
|
134
138
|
normalized[lag] = value
|
|
135
139
|
|
|
136
140
|
if value < lowest
|
|
@@ -140,7 +144,7 @@ module DSP
|
|
|
140
144
|
|
|
141
145
|
previous = lag - 1
|
|
142
146
|
if chosen.nil? &&
|
|
143
|
-
previous
|
|
147
|
+
previous >= shortest &&
|
|
144
148
|
previous < longest &&
|
|
145
149
|
normalized[previous] < @threshold &&
|
|
146
150
|
normalized[previous] <= value
|
|
@@ -151,7 +155,68 @@ module DSP
|
|
|
151
155
|
lag += 1
|
|
152
156
|
end
|
|
153
157
|
|
|
154
|
-
|
|
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]
|
|
164
|
+
end
|
|
165
|
+
|
|
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
|
|
185
|
+
|
|
186
|
+
ahead = 0.0
|
|
187
|
+
offset = 1
|
|
188
|
+
while offset <= span
|
|
189
|
+
ahead += samples[position + offset] - reference
|
|
190
|
+
offset += 1
|
|
191
|
+
end
|
|
192
|
+
|
|
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
|
|
206
|
+
|
|
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
|
|
217
|
+
|
|
218
|
+
total = (span * energy) + shifted_total - (2.0 * cross)
|
|
219
|
+
total.negative? ? 0.0 : total
|
|
155
220
|
end
|
|
156
221
|
|
|
157
222
|
def difference(samples, position, window, lag)
|
|
@@ -166,8 +231,8 @@ module DSP
|
|
|
166
231
|
total
|
|
167
232
|
end
|
|
168
233
|
|
|
169
|
-
def evaluate(normalized, lag,
|
|
170
|
-
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
|
|
171
236
|
|
|
172
237
|
offset = Parabolic.vertex_offset(normalized[lag - 1], normalized[lag], normalized[lag + 1])
|
|
173
238
|
frequency = rate / (lag + offset)
|
data/lib/dsprb.rb
CHANGED
|
@@ -19,6 +19,7 @@ require_relative "dsprb/mfcc"
|
|
|
19
19
|
require_relative "dsprb/decimator"
|
|
20
20
|
require_relative "dsprb/autocorrelation"
|
|
21
21
|
require_relative "dsprb/lpc"
|
|
22
|
+
require_relative "dsprb/polynomial"
|
|
22
23
|
require_relative "dsprb/formants"
|
|
23
24
|
require_relative "dsprb/pitch_track"
|
|
24
25
|
require_relative "dsprb/yin"
|
|
@@ -40,12 +41,14 @@ module DSP
|
|
|
40
41
|
|
|
41
42
|
def mfcc(
|
|
42
43
|
waveform,
|
|
43
|
-
size:
|
|
44
|
+
size: nil,
|
|
44
45
|
coefficients: Mfcc::DEFAULT_COEFFICIENTS,
|
|
45
46
|
window_seconds: Mfcc::DEFAULT_WINDOW_SECONDS,
|
|
46
47
|
hop_seconds: Mfcc::DEFAULT_HOP_SECONDS,
|
|
47
48
|
**options
|
|
48
49
|
)
|
|
50
|
+
size ||= Mfcc.size_for(waveform.seconds_to_samples(window_seconds))
|
|
51
|
+
|
|
49
52
|
Mfcc
|
|
50
53
|
.for(sample_rate: waveform.sample_rate, size: size, coefficients: coefficients, **options)
|
|
51
54
|
.sequence(waveform, window_seconds: window_seconds, hop_seconds: hop_seconds)
|
data/sig/dsprb.rbs
CHANGED
|
@@ -36,7 +36,7 @@ module DSP
|
|
|
36
36
|
|
|
37
37
|
def self.mfcc: (
|
|
38
38
|
Waveform,
|
|
39
|
-
?size: ::Integer
|
|
39
|
+
?size: ::Integer?,
|
|
40
40
|
?coefficients: ::Integer,
|
|
41
41
|
?window_seconds: ::Float,
|
|
42
42
|
?hop_seconds: ::Float,
|
|
@@ -232,6 +232,7 @@ module DSP
|
|
|
232
232
|
|
|
233
233
|
class Mfcc
|
|
234
234
|
DEFAULT_COEFFICIENTS: ::Integer
|
|
235
|
+
%a{deprecated: the default size follows the frame length; see Mfcc.size_for}
|
|
235
236
|
DEFAULT_SIZE: ::Integer
|
|
236
237
|
DEFAULT_WINDOW_SECONDS: ::Float
|
|
237
238
|
DEFAULT_HOP_SECONDS: ::Float
|
|
@@ -240,7 +241,8 @@ module DSP
|
|
|
240
241
|
attr_reader spectrum: Spectrum
|
|
241
242
|
attr_reader coefficients: ::Integer
|
|
242
243
|
|
|
243
|
-
def self.
|
|
244
|
+
def self.size_for: (::Integer) -> ::Integer
|
|
245
|
+
def self.for: (sample_rate: ::Numeric, ?size: ::Integer?, ?coefficients: ::Integer, **untyped) -> Mfcc
|
|
244
246
|
def initialize: (filterbank: MelFilterbank, ?coefficients: ::Integer, ?spectrum: Spectrum?) -> void
|
|
245
247
|
def call: (spectrum) -> ::Array[::Float]
|
|
246
248
|
|
|
@@ -303,26 +305,40 @@ module DSP
|
|
|
303
305
|
def self.advance: (::Array[::Float], ::Integer, ::Float) -> ::Array[::Float]
|
|
304
306
|
end
|
|
305
307
|
|
|
308
|
+
module Polynomial
|
|
309
|
+
MAXIMUM_ITERATIONS: ::Integer
|
|
310
|
+
TOLERANCE: ::Float
|
|
311
|
+
STARTING_ANGLE: ::Float
|
|
312
|
+
|
|
313
|
+
def self.roots: (::Array[::Numeric]) -> ::Array[::Complex]
|
|
314
|
+
end
|
|
315
|
+
|
|
306
316
|
class Formants
|
|
307
317
|
class Peak
|
|
308
318
|
attr_reader frequency: ::Float
|
|
309
319
|
attr_reader amplitude: ::Float
|
|
320
|
+
attr_reader bandwidth: ::Float?
|
|
310
321
|
|
|
311
|
-
def self.new: (frequency: ::Float, amplitude: ::Float) -> Peak
|
|
322
|
+
def self.new: (frequency: ::Float, amplitude: ::Float, ?bandwidth: ::Float?) -> Peak
|
|
312
323
|
end
|
|
313
324
|
|
|
314
325
|
DEFAULT_MAXIMUM_HZ: ::Float
|
|
315
326
|
DEFAULT_COUNT: ::Integer
|
|
327
|
+
%a{deprecated: ignored since 2.1.0}
|
|
316
328
|
DEFAULT_RESOLUTION: ::Integer
|
|
329
|
+
%a{deprecated: ignored since 2.1.0}
|
|
317
330
|
DEFAULT_SEPARATION_HZ: ::Float
|
|
318
331
|
DEFAULT_EDGE_MARGIN_HZ: ::Float
|
|
332
|
+
MAXIMUM_BANDWIDTH_HZ: ::Float
|
|
319
333
|
MINIMUM_SAMPLES: ::Integer
|
|
320
334
|
ENERGY_FLOOR: ::Float
|
|
321
335
|
LOG_FLOOR: ::Float
|
|
322
336
|
|
|
323
337
|
attr_reader maximum_frequency: ::Float
|
|
324
338
|
attr_reader count: ::Integer
|
|
339
|
+
%a{deprecated: ignored since 2.1.0}
|
|
325
340
|
attr_reader resolution: ::Integer
|
|
341
|
+
%a{deprecated: ignored since 2.1.0}
|
|
326
342
|
attr_reader separation: ::Float
|
|
327
343
|
attr_reader edge_margin: ::Float
|
|
328
344
|
attr_reader order: ::Integer
|
metadata
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dsprb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Den Patin
|
|
7
|
+
- Den Patin 丁丹旭
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
@@ -54,15 +54,17 @@ dependencies:
|
|
|
54
54
|
description: 'Acoustic analysis of speech in pure Ruby: WAV decoding, radix-2 FFT
|
|
55
55
|
and power spectra, generalized cosine windows, mel filterbanks and MFCC, the YIN
|
|
56
56
|
fundamental frequency estimator, linear predictive coding by the Levinson-Durbin
|
|
57
|
-
recursion with formant
|
|
58
|
-
Butterworth biquads, windowed-sinc decimation and
|
|
59
|
-
dependencies, no native extensions, deterministic
|
|
57
|
+
recursion with formant frequencies and bandwidths from the roots of the predictor
|
|
58
|
+
polynomial, spectral moments, Butterworth biquads, windowed-sinc decimation and
|
|
59
|
+
short-term energy measures. No dependencies, no native extensions, deterministic
|
|
60
|
+
output.'
|
|
60
61
|
email: hi@dpat.in
|
|
61
62
|
executables: []
|
|
62
63
|
extensions: []
|
|
63
64
|
extra_rdoc_files: []
|
|
64
65
|
files:
|
|
65
66
|
- CHANGELOG.md
|
|
67
|
+
- CITATION.cff
|
|
66
68
|
- LICENSE
|
|
67
69
|
- README.md
|
|
68
70
|
- lib/dsprb.rb
|
|
@@ -82,6 +84,7 @@ files:
|
|
|
82
84
|
- lib/dsprb/mfcc.rb
|
|
83
85
|
- lib/dsprb/parabolic.rb
|
|
84
86
|
- lib/dsprb/pitch_track.rb
|
|
87
|
+
- lib/dsprb/polynomial.rb
|
|
85
88
|
- lib/dsprb/prepared.rb
|
|
86
89
|
- lib/dsprb/resolver.rb
|
|
87
90
|
- lib/dsprb/scales.rb
|
|
@@ -95,7 +98,7 @@ files:
|
|
|
95
98
|
- sig/dsprb.rbs
|
|
96
99
|
homepage: https://github.com/taiwancards/dsprb
|
|
97
100
|
licenses:
|
|
98
|
-
-
|
|
101
|
+
- MIT
|
|
99
102
|
metadata:
|
|
100
103
|
source_code_uri: https://github.com/taiwancards/dsprb
|
|
101
104
|
changelog_uri: https://github.com/taiwancards/dsprb/blob/main/CHANGELOG.md
|
|
@@ -108,14 +111,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
108
111
|
requirements:
|
|
109
112
|
- - ">="
|
|
110
113
|
- !ruby/object:Gem::Version
|
|
111
|
-
version: '3.
|
|
114
|
+
version: '3.4'
|
|
112
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
116
|
requirements:
|
|
114
117
|
- - ">="
|
|
115
118
|
- !ruby/object:Gem::Version
|
|
116
119
|
version: '0'
|
|
117
120
|
requirements: []
|
|
118
|
-
rubygems_version: 4.0.
|
|
121
|
+
rubygems_version: 4.0.20
|
|
119
122
|
specification_version: 4
|
|
120
123
|
summary: Speech signal processing and acoustic feature extraction for numeric signals
|
|
121
124
|
test_files: []
|