Class: STFTSpectrogram::STFTSlice
- Inherits:
-
Object
- Object
- STFTSpectrogram::STFTSlice
- Defined in:
- lib/stft/stft_slice.rb
Overview
Represents a data slic. Performs FFT
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#high ⇒ Object
writeonly
Sets the attribute high.
-
#low ⇒ Object
writeonly
Sets the attribute low.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
-
#do_fft! ⇒ Object
Performs FFT on this data window.
-
#freqs ⇒ Object
Returns frequencies.
-
#initialize(data, time = 0) ⇒ STFTSlice
constructor
A new instance of STFTSlice.
-
#max_freq ⇒ Object
Returns highest frequency in this time window.
-
#spectrum ⇒ Object
Returns frequencies with their magnitudes in this time window.
-
#strongest_freq ⇒ Object
Returns strongest frequency with its magnitude.
Constructor Details
#initialize(data, time = 0) ⇒ STFTSlice
Returns a new instance of STFTSlice.
13 14 15 16 17 18 19 |
# File 'lib/stft/stft_slice.rb', line 13 def initialize(data, time = 0) @spectrum = {} @data = data.product([0]).flatten @time = time / 1000.0 # ms to seconds @low = 0 @high = 0 end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/stft/stft_slice.rb', line 7 def data @data end |
#high=(value) ⇒ Object (writeonly)
Sets the attribute high
11 12 13 |
# File 'lib/stft/stft_slice.rb', line 11 def high=(value) @high = value end |
#low=(value) ⇒ Object (writeonly)
Sets the attribute low
10 11 12 |
# File 'lib/stft/stft_slice.rb', line 10 def low=(value) @low = value end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
8 9 10 |
# File 'lib/stft/stft_slice.rb', line 8 def time @time end |
Instance Method Details
#do_fft! ⇒ Object
Performs FFT on this data window
22 23 24 25 26 |
# File 'lib/stft/stft_slice.rb', line 22 def do_fft! FFT.do_fft(@data) @data, = @data.drop(2).each_slice((@data.size / 2.0).round).to_a create_spectrum! end |
#freqs ⇒ Object
Returns frequencies
51 52 53 54 55 56 |
# File 'lib/stft/stft_slice.rb', line 51 def freqs ret = @spectrum.keys ret.select! { |f| f >= @low } unless @low.zero? ret.select! { |f| f <= @high } unless @high.zero? ret end |
#max_freq ⇒ Object
Returns highest frequency in this time window
46 47 48 |
# File 'lib/stft/stft_slice.rb', line 46 def max_freq freqs.max end |
#spectrum ⇒ Object
Returns frequencies with their magnitudes in this time window
59 60 61 62 63 64 |
# File 'lib/stft/stft_slice.rb', line 59 def spectrum ret = @spectrum.dup ret.select! { |f, _m| f >= @low } unless @low.zero? ret.select! { |f, _m| f <= @high } unless @high.zero? ret end |
#strongest_freq ⇒ Object
Returns strongest frequency with its magnitude
41 42 43 |
# File 'lib/stft/stft_slice.rb', line 41 def strongest_freq spectrum.max_by { |_k, v| v } end |