aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/jsyn/unitgen/SchmidtTrigger.java
blob: 64129ff07388baf8d34606a9ec84bda4bead498b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright 2009 Phil Burk, Mobileer Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jsyn.unitgen;

import com.jsyn.ports.UnitInputPort;
import com.jsyn.ports.UnitOutputPort;

/**
 * SchmidtTrigger unit.
 * <P>
 * Output logic level value with hysteresis. Transition high when input exceeds setLevel. Only go
 * low when input is below resetLevel. This can be used to reject low level noise on the input
 * signal. The default values for setLevel and resetLevel are both 0.0. Setting setLevel to 0.1 and
 * resetLevel to -0.1 will give some hysteresis. The outputPulse is a single sample wide pulse set
 * when the output transitions from low to high.
 * 
 * <PRE>
 * if (output == 0.0)
 *     output = (input &gt; setLevel) ? 1.0 : 0.0;
 * else if (output &gt; 0.0)
 *     output = (input &lt;= resetLevel) ? 0.0 : 1.0;
 * else
 *     output = previous_output;
 * </PRE>
 * 
 * @author Phil Burk (C) 2009 Mobileer Inc
 * @see Compare
 */
public class SchmidtTrigger extends UnitFilter {
    public UnitInputPort setLevel;
    public UnitInputPort resetLevel;
    public UnitOutputPort outputPulse;

    /* Define Unit Ports used by connect() and set(). */
    public SchmidtTrigger() {
        addPort(setLevel = new UnitInputPort("SetLevel"));
        addPort(resetLevel = new UnitInputPort("ResetLevel"));
        addPort(input = new UnitInputPort("Input"));
        addPort(outputPulse = new UnitOutputPort("OutputPulse"));
    }

    @Override
    public void generate(int start, int limit) {
        double[] inPtr = input.getValues();
        double[] pulsePtr = outputPulse.getValues();
        double[] outPtr = output.getValues();
        double[] setPtr = setLevel.getValues();
        double[] resetPtr = resetLevel.getValues();

        double outputValue = outPtr[0];
        boolean state = (outputValue > UnitGenerator.FALSE);
        for (int i = start; i < limit; i++) {
            pulsePtr[i] = UnitGenerator.FALSE;
            if (state) {
                if (inPtr[i] <= resetPtr[i]) {
                    state = false;
                    outputValue = UnitGenerator.FALSE;
                }
            } else {
                if (inPtr[i] > setPtr[i]) {
                    state = true;
                    outputValue = UnitGenerator.TRUE;
                    pulsePtr[i] = UnitGenerator.TRUE; /* Single impulse. */
                }
            }
            outPtr[i] = outputValue;
        }
    }
}