class STFTSpectrogram::STFTSlice
Represents a data slic. Performs FFT
Attributes
data[R]
high[W]
low[W]
time[R]
Public Class Methods
new(data, time = 0)
click to toggle source
# 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
Public Instance Methods
do_fft!()
click to toggle source
Performs FFT
on this data window
# 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()
click to toggle source
Returns frequencies
# 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()
click to toggle source
Returns highest frequency in this time window
# File lib/stft/stft_slice.rb, line 46 def max_freq freqs.max end
spectrum()
click to toggle source
Returns frequencies with their magnitudes in this time window
# 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()
click to toggle source
Returns strongest frequency with its magnitude
# File lib/stft/stft_slice.rb, line 41 def strongest_freq spectrum.max_by { |_k, v| v } end
Private Instance Methods
create_spectrum!()
click to toggle source
Creates a spectrogram from the transformed data - frequencies with their magnitudes Spectrogram
is saved in a Hashmap, with frequency as the key
# File lib/stft/stft_slice.rb, line 31 def create_spectrum! # l, _r = @data.drop(2).each_slice((@data.size / 2.0).round).to_a @data.each_slice(2).with_index do |(real, img), i| freq = (i * (AudioFile.sample_rate / @data.length)) magnitude = Math.sqrt(real * real + img * img) @spectrum[freq] = magnitude end end