For fast simulations I sometimes need an ideal comparator. I guess it is not a built-in component. How can it be implemented by ideal sources or VCVS ect.?
First create a B-source
Then set the value of the B-source to an expression like below:
Now B1 is the comparator with 1V/0V output.
Another example function can be something like:
I don't know how it can be done by using VCVS.
While not disregarding user16307's answer (it is a valid one), I have to warn you that behavioural sources, while versatile in terms of mathematical expressions, tend to suffer from limited dynamic range and slower simulation speed when higher numbers are involved. If your application does not involve great dynamic ranges (on both
x axes), or high values to be processed (say hundreds, or kV, upwards), then behavioural sources are just fine.
Else, you should know there is an A-device (in
[Digital]): the Schmitt trigger. It comes in different flavours, with differential/single input and single/complementary ouputs. I highly recommend this one over anything else, since A-devices work natively, are the fastest and most accurate, plus they have control over their rise/fall times and convergence is taken care of behind the scene (i.e. no discontinuities during switchings).
If your reference is a fixed voltage, that doesn't change during the simulation, you can use the single input with the internal parameter
vt set to the reference voltage.
vh is optional, but useful to set, sometimes, maybe to circumvent possible noisy inputs, or maybe simply to make the behaviour closer to reality. If not, use whichever input you want, or need, for the variable reference voltage.
Another way is to use the
table() function with a VCVS or a VCCS, thought the latter is preferable due to it being recommended by the manual as being convergent-friendly, compared to its voltage cousin.
To exemplify the two proposed examples, here's a picture:
vt can be have any value, representing the trigger point, and
vh is the hysteresis voltage, can be null (for the VCCS, it can be negative, as well).
ref is the fixed (if used as a parameter, encolsed in curly braces), or variable (for the rest) reference voltage, and the
10p capacitor is meant to aid convergence (avoid steep changes that can cause hiccups to the simulator, e.g. timestep too small, or similar).
LTSPICE DOES have the equivalent of an ideal comparator. It is the "Voltage Controlled Switch", listed as "SW" in the top level symbol list. This component looks like an open switch (OFF) when the differential input voltage is greater than the threshold (Vt) and like a closed switch (ON) when the input is less than the threshold. So if Vt is set to 0 volts and the negative terminal of the output is tied to ground the SW component will act as an ideal comparator with an open drain output. The SW component also allows you to specify a hysteresis value for the input.
I found a more elegant solution. based on the comment from "a concerned citizen".
Get a diffschmitt out of the logic library, right click on it and enter this text in the spice line:
Vhigh=5 Vh=0 Vt=0 I'm using 5V logic so Vhigh is 5. Vh controls the hysteresis, but for an absolutely ideal comparator a value of 0 recommented.