Class: SPCore::SignalGenerator

Inherits:
Object
  • Object
show all
Includes:
Hashmake::HashMakeable
Defined in:
lib/spcore/generation/signal_generator.rb

Overview

Provides methods for generating a Signal that contains test waveforms or noise.

Constant Summary collapse

ARG_SPECS =

used to process hashed args in #initialize.

{
  :size => arg_spec(:reqd => true, :type => Fixnum, :validator => ->(a){ a > 0 }),
  :sample_rate => arg_spec(:reqd => true, :type => Fixnum, :validator => ->(a){ a > 0 })
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ SignalGenerator

A new instance of SignalGenerator.

Parameters:

  • args (Hash)

    Required keys are :sample_rate and :size.



16
17
18
# File 'lib/spcore/generation/signal_generator.rb', line 16

def initialize args
  hash_make args, ARG_SPECS
end

Instance Attribute Details

#sample_rateObject (readonly)

Returns the value of attribute sample_rate.



12
13
14
# File 'lib/spcore/generation/signal_generator.rb', line 12

def sample_rate
  @sample_rate
end

#sizeObject (readonly)

Returns the value of attribute size.



12
13
14
# File 'lib/spcore/generation/signal_generator.rb', line 12

def size
  @size
end

Instance Method Details

#make_noise(amplitude = 1.0) ⇒ Object

Generate a Signal object with noise data.



21
22
23
24
25
26
27
28
# File 'lib/spcore/generation/signal_generator.rb', line 21

def make_noise amplitude = 1.0
  output = Array.new(@size)
  output.each_index do |i|
    output[i] = rand * amplitude
  end
  
  return Signal.new(:sample_rate => @sample_rate, :data => output)
end

#make_signal(freqs, extra_osc_args = {}) ⇒ Object

Generate a Signal object with waveform data at the given frequencies.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/spcore/generation/signal_generator.rb', line 31

def make_signal freqs, extra_osc_args = {}
  args = { :sample_rate => @sample_rate }.merge! extra_osc_args
  oscs = []
  freqs.each do |freq|
    oscs.push Oscillator.new args.merge(:frequency => freq)
  end
  
  output = Array.new(@size, 0.0)
  @size.times do |n|
    oscs.each do |osc|
      output[n] += osc.sample
    end
  end
  
  return Signal.new(:sample_rate => @sample_rate, :data => output)
end