Class: SPCore::Saturation

Inherits:
Object
  • Object
show all
Defined in:
lib/spcore/util/saturation.rb

Overview

Provide simple saturation methods, that limit input above the given threshold value.

Class Method Summary collapse

Class Method Details

.gompertz(input, threshold) ⇒ Object

A Gompertz-sigmoid-based saturation when input is above threshold.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spcore/util/saturation.rb', line 23

def self.gompertz input, threshold
  a = threshold
  b = -4
  c = -2
  x = input.abs
  y = 2 * a * Math::exp(b * Math::exp(c * x))
  
  if input > 0.0
    return y
  else
    return -y
  end
end

.sigmoid(input, threshold) ⇒ Object

Sigmoid-based saturation when input is above threshold. From musicdsp.org, posted by Bram.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/spcore/util/saturation.rb', line 6

def self.sigmoid input, threshold
  input_abs = input.abs
  if input_abs < threshold
    return input
  else
    #y = threshold + (1.0 - threshold) * mock_sigmoid((input_abs - threshold) / ((1.0 - threshold) * 1.5))
    y = threshold + (1.0 - threshold) * Math::tanh((input_abs - threshold)/(1-threshold))
    
    if input > 0.0
      return y
    else
      return -y
    end
  end
end