Class: Digiproc::AnalogSignal

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_strategyObject

Returns the value of attribute companding_strategy.



6
7
8
# File 'lib/signals/analog_signal.rb', line 6

def companding_strategy
  @companding_strategy
end

#eqnObject (readonly)

Returns the value of attribute eqn.



7
8
9
# File 'lib/signals/analog_signal.rb', line 7

def eqn
  @eqn
end

#quant_maxObject

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_minObject

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_bitsObject

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_samplesObject (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_samplesObject (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_rateObject

Returns the value of attribute sample_rate.



6
7
8
# File 'lib/signals/analog_signal.rb', line 6

def sample_rate
  @sample_rate
end

#signalObject

Returns the value of attribute signal.



6
7
8
# File 'lib/signals/analog_signal.rb', line 6

def signal
  @signal
end

#sizeObject

Returns the value of attribute size.



6
7
8
# File 'lib/signals/analog_signal.rb', line 6

def size
  @size
end

Instance Method Details

#digitizeObject

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 = expand(samples) if not companding_strategy.nil?
    @quantized_samples = samples
    samples
end

#normalized_quantization_rms_errorObject

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_dsObject

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