Class: PulseAnalysis::Analysis

Inherits:
Object
  • Object
show all
Defined in:
lib/pulse-analysis/analysis.rb

Constant Summary collapse

MINIMUM_PULSES =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sound, options = {}) ⇒ Analysis

Returns a new instance of Analysis.

Parameters:

Options Hash (options):

  • :amplitude_threshold (Float)

    Pulses above this amplitude will be analyzed

  • :length_threshold (Integer)

    Pulse periods longer than this value will be analyzed



13
14
15
16
17
18
# File 'lib/pulse-analysis/analysis.rb', line 13

def initialize(sound, options = {})
  @amplitude_threshold = options[:amplitude_threshold]
  @length_threshold = options[:length_threshold]
  populate_sound(sound)
  @data = AudioData.new(@sound)
end

Instance Attribute Details

#abberationsArray<Integer> (readonly)

Non-zero pulse timing abberations derived from the periods

Returns:

  • (Array<Integer>)


75
76
77
# File 'lib/pulse-analysis/analysis.rb', line 75

def abberations
  @abberations
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/pulse-analysis/analysis.rb', line 7

def data
  @data
end

#periodsObject (readonly)

Returns the value of attribute periods.



7
8
9
# File 'lib/pulse-analysis/analysis.rb', line 7

def periods
  @periods
end

#soundObject (readonly)

Returns the value of attribute sound.



7
8
9
# File 'lib/pulse-analysis/analysis.rb', line 7

def sound
  @sound
end

Instance Method Details

#amplitude_thresholdFloat

The threshold (0..1) at which pulses will register as high

Returns:

  • (Float)


81
82
83
# File 'lib/pulse-analysis/analysis.rb', line 81

def amplitude_threshold
  @amplitude_threshold ||= calculate_amplitude_threshold
end

#average_abberationFloat

Average sequential abberation between pulses

Returns:

  • (Float)


69
70
71
# File 'lib/pulse-analysis/analysis.rb', line 69

def average_abberation
  @average_abberation ||= calculate_average_abberation
end

#average_periodFloat

Average number of samples between pulses

Returns:

  • (Float)


31
32
33
# File 'lib/pulse-analysis/analysis.rb', line 31

def average_period
  @average_period ||= @periods.inject(&:+).to_f / num_pulses
end

#largest_abberationInteger

Largest sequential abberation between pulses

Returns:

  • (Integer)


63
64
65
# File 'lib/pulse-analysis/analysis.rb', line 63

def largest_abberation
  @largest_abberation ||= abberations.max || 0
end

#longest_periodInteger

Longest number of samples between pulse

Returns:

  • (Integer)


43
44
45
# File 'lib/pulse-analysis/analysis.rb', line 43

def longest_period
  @longest_period ||= @periods.max
end

#num_pulsesInteger

Number of usable pulses in the audio file

Returns:

  • (Integer)


37
38
39
# File 'lib/pulse-analysis/analysis.rb', line 37

def num_pulses
  @num_pulses ||= @periods.count
end

#runBoolean

Run the analysis

Returns:

  • (Boolean)


22
23
24
25
26
27
# File 'lib/pulse-analysis/analysis.rb', line 22

def run
  prepare
  populate_periods
  validate
  true
end

#shortest_periodInteger

Shortest number of samples between pulse

Returns:

  • (Integer)


49
50
51
# File 'lib/pulse-analysis/analysis.rb', line 49

def shortest_period
  @shortest_period ||= @periods.min
end

#tempo_bpmFloat

Tempo of the audio file in beats per minute (BPM) Assumes that the pulse is 16th notes as per the Innerclock Litmus Test

Returns:

  • (Float)


57
58
59
# File 'lib/pulse-analysis/analysis.rb', line 57

def tempo_bpm
  @tempo_bpm ||= calculate_tempo_bpm
end

#valid?Boolean

Validate that the analysis has produced meaningful results

Returns:

  • (Boolean)


99
100
101
# File 'lib/pulse-analysis/analysis.rb', line 99

def valid?
  @periods.count > MINIMUM_PULSES
end

#validateBoolean

Validate that analysis on the given data can produce meaningful results

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
# File 'lib/pulse-analysis/analysis.rb', line 88

def validate
  if valid?
    true
  else
    message = "Could not produce a valid analysis."
    raise(message)
  end
end