Skip to content

Commit

Permalink
Added schmitt_trigger doc
Browse files Browse the repository at this point in the history
  • Loading branch information
djowel committed Sep 13, 2024
1 parent cc788de commit 337bb55
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*** xref:reference/misc/one_pole_lowpass.adoc[One Pole Low Pass]
*** xref:reference/misc/dc_block.adoc[DC Block]
*** xref:reference/misc/dynamic_smoother.adoc[Dynamic Smoother]
*** xref:reference/misc/schmitt_trigger.adoc[Schmitt Trigger]
** xref:reference/synth.adoc[Synthesizers]
*** xref:reference/synth/sin_osc.adoc[Sine Wave Oscillator]
Expand Down
2 changes: 0 additions & 2 deletions docs/modules/ROOT/pages/reference/misc/dynamic_smoother.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ include::../../common.adoc[]

The `dynamic_smoother` class is based on link:https://cytomic.com/files/dsp/DynamicSmoothing.pdf[Dynamic Smoothing Using Self Modulating Filter] (© Andrew Simper, Cytomic, 2014, andy@cytomic.com), a robust and inexpensive dynamic smoothing algorithm based on using the bandpass output of a 2-pole multimode filter to modulate its own cutoff frequency. The bandpass signal dynamically adjusts the cutoff frequency to provide faster tracking when the input signal changes significantly. It is ideal for de-noising and smoothing operations, such as for analog potentiometers or MIDI controllers, but is also useful in cleaning up audio signals for analytical processes such as pitch detection.

in preparation for analysis.

== Include

```c++
Expand Down
70 changes: 70 additions & 0 deletions docs/modules/ROOT/pages/reference/misc/schmitt_trigger.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
= Schmitt Trigger

include::../../common.adoc[]

== Overview

The output of a simple comparator is determined by its inputs. The output is `1` if the input signal is greater than the reference signal plus a specified hysteresis. Otherwise, the output is `0` if the input signal is less than the reference signal minus the specified hysteresis.

== Include

```c++
#include <q/fx/schmitt_trigger.hpp>
```

== Declaration

```c++
struct schmitt_trigger
{
schmitt_trigger(float hysteresis);
schmitt_trigger(decibel hysteresis);

bool operator()(float s, float ref);
bool operator()() const;
};
```

== Expressions

=== Notation

`st`, `a`, `b` :: Objects of type `schmitt_trigger`.
`h` :: Floating point value representing hysteresis.
`hdb` :: Object of type `decibel` representing the hysteresis in decibels.
`sps` :: Floating point value representing samples per second.
`s` :: Input sample.
`ref` :: A reference signal.

=== Constructor

=== Constructors and Assignment

[cols="1,1"]
|===
| Expression | Semantics

| `schmitt_trigger(h)` | Construct a `schmitt_trigger` with specified hysteresis, `h`.
| `schmitt_trigger(hdb)` | Construct a `schmitt_trigger` with specified hysteresis, `h` (in dB).
| `a = b` | Assign `b` to `a`.
| `a = s` | Set the latest result to `s`.
|===

NOTE: C++ brace initialization may also be used.

[cols="1,1,1"]
|===
| Expression | Semantics | Return Type

| `st()` | Return the latest result. | `float`
| `st(s, ref)` | Processes the input sample `s`. The
output is `1` if the input signal `s`
is greater than the reference signal
`ref` plus the specified hysteresis.
Otherwise, the output is `0` if the
input signal `s` is less than the
reference signal `ref` minus the
specified hysteresis. | `bool`
|===


20 changes: 8 additions & 12 deletions q_lib/include/q/fx/schmitt_trigger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ namespace cycfi::q
{
////////////////////////////////////////////////////////////////////////////
// The output of a simple comparator is determined by its inputs. The
// output is high (1) if the positive input (pos) is greater than the
// negative input (neg). Otherwise, the output is low (0).
//
// The schmitt trigger adds some hysteresis to improve noise immunity and
// minimize multiple triggering by adding and subtracting a certain
// fraction back to the negative input (neg). Hysteresis is the fraction
// (should be less than < 1.0) that determines how much is added or
// subtracted. By doing so, the comparator "bar" is raised or lowered
// depending on the previous state.
// output is `1` if the input signal is greater than the reference signal
// plus a specified hysteresis. Otherwise, the output is `0` if the input
// signal `is less than the reference signal minus the specified
// hysteresis. Hysteresis should be a fraction greater than or equal to
// zero, and less than 1.0, or less than 0_dB, if specified in decibels.
//
// Note: the result is a bool.
////////////////////////////////////////////////////////////////////////////
Expand All @@ -36,11 +32,11 @@ namespace cycfi::q
: _hysteresis(lin_float(hysteresis))
{}

bool operator()(float pos, float neg)
bool operator()(float s, float ref)
{
if (!y && pos > (neg + _hysteresis))
if (!y && s > (ref + _hysteresis))
y = 1;
else if (y && pos < (neg - _hysteresis))
else if (y && s < (ref - _hysteresis))
y = 0;
return y;
}
Expand Down

0 comments on commit 337bb55

Please sign in to comment.