Class: STFTSpectrogram::Spectrogram

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

Overview

Represents time-frequency spectrogram

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(audio, window_size, window_overlap) ⇒ Spectrogram

Returns a new instance of Spectrogram.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/stft/spectrogram.rb', line 9

def initialize(audio, window_size, window_overlap)
  if window_size <= window_overlap
    raise ArgumentError, 'Window size cannot be <= window overlap!'
  end

  @windows = []
  @transformed = false
  audio.window_size = window_size
  audio.window_overlap = window_overlap
  split_to_windows(audio)
  audio.reset
end

Instance Attribute Details

#windowsObject (readonly)

Returns the value of attribute windows.



7
8
9
# File 'lib/stft/spectrogram.rb', line 7

def windows
  @windows
end

Instance Method Details

#filter(low, high) ⇒ Object

Sets low and high frequency filters



47
48
49
50
51
52
# File 'lib/stft/spectrogram.rb', line 47

def filter(low, high)
  @windows.each do |w|
    w.low = low
    w.high = high
  end
end

#freqsObject

Returns an array with all frequencies



41
42
43
44
# File 'lib/stft/spectrogram.rb', line 41

def freqs
  return [] unless transformed?
  @windows[0].freqs
end

#max_freqObject

Gets the highest frequency



35
36
37
38
# File 'lib/stft/spectrogram.rb', line 35

def max_freq
  return 0 unless transformed?
  @windows[0].max_freq
end

#transform!Object

Performs FFT on all timed data windows



29
30
31
32
# File 'lib/stft/spectrogram.rb', line 29

def transform!
  @windows.each(&:do_fft!)
  @transformed = true
end

#transformed?Boolean

Returns true if FFT was already performed

Returns:

  • (Boolean)


55
56
57
# File 'lib/stft/spectrogram.rb', line 55

def transformed?
  @transformed
end