Page 1 of 2

Schmitt triggers

PostPosted: Wed Jul 27, 2022 1:59 pm
by Spogg
Hi stoners!
I needed a Schmitt trigger to try in my current project.

For those who don’t know, it’s a switch which turns on at a set level and off at a lower level. This change of switching thresholds is called hysteresis. The common use case is to handle noisy or unstable control signals, like you might get from an envelope follower.
I bet you all knew this, but just in case! :lol:

This has been discussed before on the forum, but I made my own; one in stream and one in green float. You can set the base switching threshold and the hysteresis required.

Any comments and improvements or Ruby/ASM versions are very welcome.

Re: Schmitt triggers

PostPosted: Wed Jul 27, 2022 4:54 pm
by Tepeix
Interesting trigger ;)

Here's 2 asm versions. I'm not sure which is faster.

The first one use the same logic as you, comparing the last out value with 1.
The second multiply the Hyst and out.

v1
Code: Select all
streamin in; streamin ref;
streamin hyst; streamout out;
float f1=1;
movaps xmm0,in;
movaps xmm2,f1;
movaps xmm3,xmm2;
cmpps xmm2,out,0;

andps xmm2,hyst;
addps xmm0,xmm2;

cmpps xmm0,ref,6;
andps xmm0,xmm3;
movaps out,xmm0;


v2
Code: Select all
streamin in; streamin ref;
streamin hyst; streamout out;
float f1=1;
movaps xmm0,in;
movaps xmm1,out;
mulps xmm1,hyst;
addps xmm0,xmm1;

cmpps xmm0,ref,6;
andps xmm0,f1;
movaps out,xmm0;

Re: Schmitt triggers

PostPosted: Thu Jul 28, 2022 2:17 am
by tulamide
Is there a flaw in this, or do I misunderstand?

Scenario (I tried the green version): I set threshold to 0.5 and Hysterisis to 0.7
Expected: Two possible outcomes. Either Hysterisis is only allowed to become as big a number as threshold, or it should switch to false, once I got over 0.7 and go back down.
Experienced: Switches to true at 0.5 and never switches back to false.

In general: Why is this made so difficult? Why not just label two values as positive and negative threshold?

Re: Schmitt triggers

PostPosted: Thu Jul 28, 2022 8:09 am
by tiffy
For what my penny is worth; for the sake of clarity, why not call the two inputs to the Schmidt trigger:

1) Open Threshold
2) Closing Threshold

As far as my understanding of a Schmidt Trigger, it opens or switches on at the first set threshold value, and then it closes or switches off at the second set threshold value.

I don't know but there may be other reasons for naming the inputs differently than what I suggested.

regards

Re: Schmitt triggers

PostPosted: Thu Jul 28, 2022 10:51 am
by Spogg
Great stuff guys, many thanks!
@Tepeix:
Both work but I’ve saved V2 simply because it seems to use less code. Thanks for that!

@tulamide: with threshold set to 0.5 and Hysteresis set to 0.7 the switch will be true above 0.5 and go back to False below -0.2.

@All:
I think the comments about it being too strange to set a level and hysteresis are perfectly valid. I was thinking about the hysteresis being a fixed amount and the operating point being adjustable (in my project). I was also influenced by my analogue electronics days, whereby the output of an op-amp comparator would modify the reference level or input voltage. It’s my age you see! :lol:

So I made alternative versions in stream and green and these have High ref and Low ref settings. I also included the V2 ASM by Tepeix.

Re: Schmitt triggers

PostPosted: Thu Jul 28, 2022 3:49 pm
by tulamide
Spogg wrote:@tulamide: with threshold set to 0.5 and Hysteresis set to 0.7 the switch will be true above 0.5 and go back to False below -0.2.

Oh! I see. So it is an inherent part of this trigger algo, that the closing threshold has to be lower than the opening one? Is there no use case for one that closes above the opening threshold?

Re: Schmitt triggers

PostPosted: Fri Jul 29, 2022 8:31 am
by Spogg
tulamide wrote:...Is there no use case for one that closes above the opening threshold?


Well yes, but it wouldn’t be called a Schmitt trigger! :lol:

What you describe is called a window comparator (well it was in my day) which gives a True output when the input is between two thresholds.

I needed one ages ago so I made one and I’ve attached it here. One use case is if you stack them with a common input, and set the thresholds appropriately, you can have a range of True outputs from a single green signal.

Re: Schmitt triggers

PostPosted: Fri Jul 29, 2022 9:16 am
by tulamide
Spogg wrote:
tulamide wrote:...Is there no use case for one that closes above the opening threshold?


Well yes, but it wouldn’t be called a Schmitt trigger! :lol:

What you describe is called a window comparator (well it was in my day) which gives a True output when the input is between two thresholds.

I needed one ages ago so I made one and I’ve attached it here. One use case is if you stack them with a common input, and set the thresholds appropriately, you can have a range of True outputs from a single green signal.

Thank you for the example.

But I think we talked past each other. What I have in mind is the following:
Value range be 0-1
positive threshold 0.4
negative threshold 0.7

We start at 0 and go up. At 0.4 the output switches to true. We keep increasing, we pass 0.7, even 0.8 - the output still is true. But now we decrease the value, we're at 0.8, 0.75, 0.71, now we hit 0.7 - and the output switches to false. We continue decreasing. 0.6, 0.5, 0.4, 0.3 - still false. We increase again now, and - you guessed it - once we cross 0.4, it turns true again. And so on.

I described it so detailed to make the difference clear. The positive threshold will only react, if it is increasing to the threshold, while the negative threshold will only react, if it is decreasing to the threshold.

You can then have situations like 0.7 and 0.4, where the negative threshold is passed without changing the output, because it increases to that value, and only at 0.7 it will switch to true. That would be the opposite of the first described situation. with just two thresholds and a specified property.

But maybe that's not needed in DSP.

Re: Schmitt triggers

PostPosted: Fri Jul 29, 2022 10:56 am
by Spogg
Oh I seeeeee!

What you describe is a Tulamide switch so I made one in green, but I’m not sure of a use case though.

Also there may be a better way to do it…

Re: Schmitt triggers

PostPosted: Fri Jul 29, 2022 5:29 pm
by tulamide
Spogg wrote:Oh I seeeeee!

What you describe is a Tulamide switch so I made one in green, but I’m not sure of a use case though.

Also there may be a better way to do it…

:o
My own trigger switch! Cool!
In green it is already the most lightweight, I could think of as well. But the twisted minds of ASM gurus may come up with something even more efficient.

It is indeed difficult to think of a use case. But maybe someday somebody will need it! It's in my toolbox.