Schmitt triggers

Post any examples or modules that you want to share here
User avatar
Spogg
Posts: 3368
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England
Contact:

Schmitt triggers

Post 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.
Attachments
Schmitt dev 3 .fsm
3.06
(59.08 KiB) Downloaded 811 times
Tepeix
Posts: 361
Joined: Sat Oct 16, 2021 3:11 pm

Re: Schmitt triggers

Post 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;
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Schmitt triggers

Post 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?
"There lies the dog buried" (German saying translated literally)
User avatar
tiffy
Posts: 400
Joined: Wed May 08, 2013 12:14 pm

Re: Schmitt triggers

Post 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
User avatar
Spogg
Posts: 3368
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England
Contact:

Re: Schmitt triggers

Post 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.
Attachments
Schmitt dev 5.fsm
3.06
(64.31 KiB) Downloaded 797 times
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Schmitt triggers

Post 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?
"There lies the dog buried" (German saying translated literally)
User avatar
Spogg
Posts: 3368
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England
Contact:

Re: Schmitt triggers

Post 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.
Attachments
Window comparator green.fsm
3.06
(56.41 KiB) Downloaded 779 times
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Schmitt triggers

Post 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.
"There lies the dog buried" (German saying translated literally)
User avatar
Spogg
Posts: 3368
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England
Contact:

Re: Schmitt triggers

Post 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…
Attachments
Tulamide switch dev 1.fsm
3.06 green only.
(56.18 KiB) Downloaded 783 times
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Schmitt triggers

Post 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.
"There lies the dog buried" (German saying translated literally)
Post Reply