Class: Digiproc::AnalogSignal
- Inherits:
-
Object
- Object
- Digiproc::AnalogSignal
- Defined in:
- lib/signals/analog_signal.rb
Overview
Class for performing actions upon an Analog, Continuous Time signal defined by an equation Initialized with a lambda or proc, the signal is sampled, companded, quantized, and can be turned into a ‘Digiproc::DigitalSignal`
Instance Attribute Summary collapse
-
#companding_strategy ⇒ Object
Returns the value of attribute companding_strategy.
-
#eqn ⇒ Object
readonly
Returns the value of attribute eqn.
-
#quant_max ⇒ Object
Returns the value of attribute quant_max.
-
#quant_min ⇒ Object
Returns the value of attribute quant_min.
-
#quantization_bits ⇒ Object
Returns the value of attribute quantization_bits.
-
#quantized_samples ⇒ Object
readonly
Returns the value of attribute quantized_samples.
-
#raw_samples ⇒ Object
readonly
Returns the value of attribute raw_samples.
-
#sample_rate ⇒ Object
Returns the value of attribute sample_rate.
-
#signal ⇒ Object
Returns the value of attribute signal.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
-
#digitize ⇒ Object
No arguments, outputs [Array] digital samples Will sample, compand (compress, expand), and quantize samples (companding and quantizing optional) See analog_signals examples.
-
#initialize(eqn:, sample_rate: 0.0001, size: 100, companding_strategy: nil, quantization_bits: Float::INFINITY, quant_max: nil, quant_min: nil) ⇒ AnalogSignal
constructor
See examples/analog_signals/companding for full options in initializer fn = ->(x){ Math.sin(x) } asig = Digiproc::AnalogSignal.new(eqn: fn).
-
#normalized_quantization_rms_error ⇒ Object
Returns [Float] normalized quantization RMS error.
-
#to_ds ⇒ Object
Returns a Digiproc::DigitalSignal of the digital signals.
Constructor Details
#initialize(eqn:, sample_rate: 0.0001, size: 100, companding_strategy: nil, quantization_bits: Float::INFINITY, quant_max: nil, quant_min: nil) ⇒ AnalogSignal
See examples/analog_signals/companding for full options in initializer fn = ->(x){ Math.sin(x) } asig = Digiproc::AnalogSignal.new(eqn: fn)
13 14 15 16 17 |
# File 'lib/signals/analog_signal.rb', line 13 def initialize(eqn: ,sample_rate: 0.0001, size: 100, companding_strategy: nil, quantization_bits: Float::INFINITY, quant_max: nil, quant_min: nil) @eqn, @sample_rate, @size, @quantization_bits, @quant_max, @quant_min = eqn, sample_rate, size, quantization_bits, quant_max, quant_min @signal = eqn @companding_strategy = (companding_strategy.is_a?(Class) and !!companding_strategy) ? companding_strategy.new : companding_strategy end |
Instance Attribute Details
#companding_strategy ⇒ Object
Returns the value of attribute companding_strategy.
6 7 8 |
# File 'lib/signals/analog_signal.rb', line 6 def companding_strategy @companding_strategy end |
#eqn ⇒ Object (readonly)
Returns the value of attribute eqn.
7 8 9 |
# File 'lib/signals/analog_signal.rb', line 7 def eqn @eqn end |
#quant_max ⇒ Object
Returns the value of attribute quant_max.
6 7 8 |
# File 'lib/signals/analog_signal.rb', line 6 def quant_max @quant_max end |
#quant_min ⇒ Object
Returns the value of attribute quant_min.
6 7 8 |
# File 'lib/signals/analog_signal.rb', line 6 def quant_min @quant_min end |
#quantization_bits ⇒ Object
Returns the value of attribute quantization_bits.
6 7 8 |
# File 'lib/signals/analog_signal.rb', line 6 def quantization_bits @quantization_bits end |
#quantized_samples ⇒ Object (readonly)
Returns the value of attribute quantized_samples.
7 8 9 |
# File 'lib/signals/analog_signal.rb', line 7 def quantized_samples @quantized_samples end |
#raw_samples ⇒ Object (readonly)
Returns the value of attribute raw_samples.
7 8 9 |
# File 'lib/signals/analog_signal.rb', line 7 def raw_samples @raw_samples end |
#sample_rate ⇒ Object
Returns the value of attribute sample_rate.
6 7 8 |
# File 'lib/signals/analog_signal.rb', line 6 def sample_rate @sample_rate end |
#signal ⇒ Object
Returns the value of attribute signal.
6 7 8 |
# File 'lib/signals/analog_signal.rb', line 6 def signal @signal end |
#size ⇒ Object
Returns the value of attribute size.
6 7 8 |
# File 'lib/signals/analog_signal.rb', line 6 def size @size end |
Instance Method Details
#digitize ⇒ Object
No arguments, outputs [Array] digital samples Will sample, compand (compress, expand), and quantize samples (companding and quantizing optional) See analog_signals examples
23 24 25 26 27 28 29 30 31 |
# File 'lib/signals/analog_signal.rb', line 23 def digitize samples = sample @raw_samples = samples samples = compress(samples) if not companding_strategy.nil? samples = quantize(samples, self.quantization_bits, self.quant_max, self.quant_min) if self.quantization_bits < Float::INFINITY samples = (samples) if not companding_strategy.nil? @quantized_samples = samples samples end |
#normalized_quantization_rms_error ⇒ Object
Returns [Float] normalized quantization RMS error. See examples/analog_signal/companding
41 42 43 44 45 46 47 48 |
# File 'lib/signals/analog_signal.rb', line 41 def normalized_quantization_rms_error total = 0 x_max = self.raw_samples.max self.raw_samples.each_with_index do |x, i| total += ((x.to_f - quantized_samples[i]) / x_max) ** 2 end (1.0 / size) * (total ** (0.5)) end |
#to_ds ⇒ Object
Returns a Digiproc::DigitalSignal of the digital signals
35 36 37 |
# File 'lib/signals/analog_signal.rb', line 35 def to_ds Digiproc::DigitalSignal.new(data: digitize) end |