Class: STFTSpectrogram::STFTSlice

Inherits:
Object
  • Object
show all
Defined in:
lib/stft/stft_slice.rb

Overview

Represents a data slic. Performs FFT

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject (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

Parameters:

  • value

    the value to set the attribute high to.



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

Parameters:

  • value

    the value to set the attribute low to.



10
11
12
# File 'lib/stft/stft_slice.rb', line 10

def low=(value)
  @low = value
end

#timeObject (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

#freqsObject

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_freqObject

Returns highest frequency in this time window



46
47
48
# File 'lib/stft/stft_slice.rb', line 46

def max_freq
  freqs.max
end

#spectrumObject

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_freqObject

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