Class: DSP::Spectrum
- Inherits:
-
Object
- Object
- DSP::Spectrum
- Defined in:
- lib/dsprb/spectrum.rb,
sig/dsprb.rbs
Instance Attribute Summary collapse
-
#bins ⇒ ::Integer
readonly
Returns the value of attribute bins.
-
#size ⇒ ::Integer
readonly
Returns the value of attribute size.
-
#transform ⇒ _Transform
readonly
Returns the value of attribute transform.
Instance Method Summary collapse
- #bin_of(frequency, sample_rate) ⇒ ::Integer
- #frequencies(sample_rate) ⇒ ::Array[::Float]
-
#initialize(size, transform: nil) ⇒ Spectrum
constructor
Defaults to the real-input transform; a complex one such as Fourier::Radix2 may be injected instead.
- #magnitude(frame) ⇒ spectrum
- #power(frame) ⇒ spectrum
Constructor Details
#initialize(size, transform: nil) ⇒ Spectrum
Defaults to the real-input transform; a complex one such as Fourier::Radix2 may be injected instead.
9 10 11 12 13 14 15 |
# File 'lib/dsprb/spectrum.rb', line 9 def initialize(size, transform: nil) @size = Integer(size) @bins = (@size / 2) + 1 @transform = transform || Fourier::Real.new(@size) @real_input = @transform.respond_to?(:power) freeze end |
Instance Attribute Details
#bins ⇒ ::Integer (readonly)
Returns the value of attribute bins.
5 6 7 |
# File 'lib/dsprb/spectrum.rb', line 5 def bins @bins end |
#size ⇒ ::Integer (readonly)
Returns the value of attribute size.
5 6 7 |
# File 'lib/dsprb/spectrum.rb', line 5 def size @size end |
#transform ⇒ _Transform (readonly)
Returns the value of attribute transform.
5 6 7 |
# File 'lib/dsprb/spectrum.rb', line 5 def transform @transform end |
Instance Method Details
#bin_of(frequency, sample_rate) ⇒ ::Integer
23 |
# File 'lib/dsprb/spectrum.rb', line 23 def bin_of(frequency, sample_rate) = (frequency * @size / sample_rate.to_f).round |
#frequencies(sample_rate) ⇒ ::Array[::Float]
21 |
# File 'lib/dsprb/spectrum.rb', line 21 def frequencies(sample_rate) = Array.new(@bins) { |bin| bin * sample_rate / @size.to_f } |
#magnitude(frame) ⇒ spectrum
19 |
# File 'lib/dsprb/spectrum.rb', line 19 def magnitude(frame) = power(frame).map { |value| Math.sqrt(value) } |
#power(frame) ⇒ spectrum
17 |
# File 'lib/dsprb/spectrum.rb', line 17 def power(frame) = @real_input ? @transform.power(frame) : complex_power(frame) |