DSPRelated.com
Forums

Schmitt trigger in software

Started by tom00 November 13, 2009
Hello all!

I have to implement a Schmitt trigger in software on a DSP. I was thinking
a while about a good implantation, but I just came to the simple solution
with has nested if conditions (See Pseudo code below). Does anyone has a
faster code??


-Thomas



for n = 1:length(x)
    if flag
        if x(n) > th
            do something
            flag = 0;
        end
    else
        if x(n) < -th
            do soming else
            flag = 1;
        end
    end
end

Table lookup like

State = Array [0..1] [0..15]  of 0..1

State[0] = '0000000000011111'
State[1] = '0000011111111111'

Y := State[Y] [X]

X limited between 0..15, else bigger table

Christen Fihl



tom00 wrote:

> Hello all! > > I have to implement a Schmitt trigger in software on a DSP. I was thinking > a while about a good implantation, but I just came to the simple solution > with has nested if conditions (See Pseudo code below). Does anyone has a > faster code?? > > > -Thomas > > > > for n = 1:length(x) > if flag > if x(n) > th > do something > flag = 0; > end > else > if x(n) < -th > do soming else > flag = 1; > end > end > end
^^^^^^^^^^^^^^^^^^^^^^^^^ Fie, stupident solution. Here we go: const s16 fubar[] = { nonsense, -nonsense }; const s16 void (*do_something[])() = { something_ , else_ }; s16 *x = blablabla; s16 th = 0; while(length--) { th = fubar[u16 state = ((u16)(*x++ +th)) >> 15]; do_something[state]; } VLV
On Saturday, November 14, 2009 1:26:12 AM UTC+13, Christen Fihl wrote:
> Table lookup like > > State = Array [0..1] [0..15] of 0..1 > > State[0] = '0000000000011111' > State[1] = '0000011111111111' > > Y := State[Y] [X] > > X limited between 0..15, else bigger table > > Christen Fihl
Very nice i like it :)
if state change is rare, I'd try also to switch between two separate loops,
one for each threshold. 
C's "goto" might come in handy, if nobody near is allergic to it.

>Here we go: > >const s16 fubar[] = { nonsense, -nonsense }; >const s16 void (*do_something[])() = { something_ , else_ }; > > >s16 *x = blablabla; >s16 th = 0; > >while(length--) > { > th = fubar[u16 state = ((u16)(*x++ +th)) >> 15]; > do_something[state]; > } > > >VLV >
assert( (*x > (s16)0x7FFF - nonsense) || (*x < (s16)0x8000 + nonsense) )
On 3/25/13 1:56 PM, dszabo wrote:
>> Here we go: >> >> const s16 fubar[] = { nonsense, -nonsense }; >> const s16 void (*do_something[])() = { something_ , else_ }; >> >> >> s16 *x = blablabla; >> s16 th = 0; >> >> while(length--) >> { >> th = fubar[u16 state = ((u16)(*x++ +th))>> 15]; >> do_something[state]; >> } >> >>
Vlad, does that make it a Schmitt trigger. or do you have to switch -nonsense and +nonsense in fubar[]? you want a comparator that more sticky than a static comparator. or maybe instead of switching the nonsense in fubar, you subtract the threshold from x instead of adding it. but i think you got the polarity mixed up. you make a Schmidt trigger with an op-amp using positive feedback.
> > assert( (*x> (s16)0x7FFF - nonsense) > || (*x< (s16)0x8000 + nonsense) )
in case there's some wrap around overflow that breaks Vlad's Schmidt trigger? -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
>On 3/25/13 1:56 PM, dszabo wrote: >>> Here we go: >>> >>> const s16 fubar[] = { nonsense, -nonsense }; >>> const s16 void (*do_something[])() = { something_ , else_ }; >>> >>> >>> s16 *x = blablabla; >>> s16 th = 0; >>> >>> while(length--) >>> { >>> th = fubar[u16 state = ((u16)(*x++ +th))>> 15]; >>> do_something[state]; >>> } >>> >>> > >Vlad, does that make it a Schmitt trigger. or do you have to switch >-nonsense and +nonsense in fubar[]? you want a comparator that more >sticky than a static comparator. or maybe instead of switching the >nonsense in fubar, you subtract the threshold from x instead of adding >it. but i think you got the polarity mixed up. you make a Schmidt >trigger with an op-amp using positive feedback. > >> >> assert( (*x> (s16)0x7FFF - nonsense) >> || (*x< (s16)0x8000 + nonsense) ) > >in case there's some wrap around overflow that breaks Vlad's Schmidt >trigger? > > >-- > >r b-j rbj@audioimagination.com > >"Imagination is more important than knowledge." > > >
It's tricky because the actual threshold for *x is equal to '-th' not 'th'. Basically, bit 15 is the sign of *x + th. When th == nonsense, the sign changes when *x is less than -nonsense, and vice versa. Yep, you'll wanna keep an eye on the range of *x values. It's admittedly both clever and hard to read, though. I like it.
On 3/26/2013 10:58 AM, dszabo wrote:
>> On 3/25/13 1:56 PM, dszabo wrote: >>>> Here we go: >>>> >>>> const s16 fubar[] = { nonsense, -nonsense }; >>>> const s16 void (*do_something[])() = { something_ , else_ }; >>>> >>>> >>>> s16 *x = blablabla; >>>> s16 th = 0; >>>> >>>> while(length--) >>>> { >>>> th = fubar[u16 state = ((u16)(*x++ +th))>> 15]; >>>> do_something[state]; >>>> } >>>> >>>> >> >> Vlad, does that make it a Schmitt trigger. or do you have to switch >> -nonsense and +nonsense in fubar[]? you want a comparator that more >> sticky than a static comparator. or maybe instead of switching the >> nonsense in fubar, you subtract the threshold from x instead of adding >> it. but i think you got the polarity mixed up. you make a Schmidt >> trigger with an op-amp using positive feedback. >> >>> >>> assert( (*x> (s16)0x7FFF - nonsense) >>> || (*x< (s16)0x8000 + nonsense) ) >> >> in case there's some wrap around overflow that breaks Vlad's Schmidt >> trigger? >> > > It's tricky because the actual threshold for *x is equal to '-th' not 'th'. > Basically, bit 15 is the sign of *x + th. When th == nonsense, the sign > changes when *x is less than -nonsense, and vice versa. > > Yep, you'll wanna keep an eye on the range of *x values. It's admittedly > both clever and hard to read, though. I like it.
The point of this exersize is about not using if() in a loop. VLV
On 3/26/13 12:42 PM, Vladimir Vassilevsky wrote:
> > The point of this exersize is about not using if() in a loop. >
well, i guessed i missed that point. the thing i was worrying about is that i think the feedback was negative feedback, and that will give you a stable amplifier with rails. i think you messed up the polarity in the loop gain, Vlad. i'll check it again, but you need to be doing positive feedback to do a Schmitt trigger. -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."