Page 1 of 1

True sample and hold

Posted: Sat Sep 24, 2016 11:08 pm
by BobF
Hello gang,

Does any one have a real sample and hold or could design one for me. That is one that samples (captures, grabs) the voltage of a continuously varying analog signal and holds (locks, freezes) its value at a constant level for a specified minimum period of time. This is the definition of a analog hardware sample and hold. I need a Flowstone version that does exactly the same thing.

Many thanks in advance if you have one or the possibilits of designing one!

Later then, BobF.....

Re: True sample and hold

Posted: Sun Sep 25, 2016 2:39 am
by RJHollins
Are you thinking something like a VCA ? [voltage control amplifier].

This 'sample/hold' value ... are you talking volume ? If so ... PEAK level? RMS ?

Re: True sample and hold

Posted: Sun Sep 25, 2016 3:34 am
by BobF
Hi RJHOLLINS,

No! For now look up some definitions of a analog sample and hold. I will TRY to come up with a better explanation later if no one figures it out.

Thanks, BobF.....

Re: True sample and hold

Posted: Sun Sep 25, 2016 4:14 am
by martinvicanek
This will sample (and hold) the input value when the trigger goes from <=0 to >0:

Code: Select all

streamin in;   streamout out;
streamin sampleTrigger;

float prevTrigger;
float update;

update = (sampleTrigger>0)&(prevTrigger<=0);
out = out + (in - out)&update;
prevTrigger = sampleTrigger;

Re: True sample and hold

Posted: Sun Sep 25, 2016 4:29 am
by martinvicanek
And this one will hold for a specified time (=number of samples):

Code: Select all

streamin in;   streamout out;
streamin sampleTrigger;
streamin duration;

float trigger;
float prevTrigger;
float time;
float update;

trigger = (sampleTrigger>0)&(prevTrigger<=0);
prevTrigger = sampleTrigger;

time = min(time + 1,duration);
time = time - time&trigger;

update = trigger | (time>=duration);
out = out + (in - out)&update;

Re: True sample and hold

Posted: Sun Sep 25, 2016 4:51 am
by tulamide
I was just working on this, because I thought this would make a perfect entry in dsp programming. It is relatively simple. Yet, my code wasn't nearly as effective as yours, Martin. Especially, I don't understand this line in your second code:

Code: Select all

out = out + (in - out)&update;

I can read it, but I don't get the human logic behind it. Could you elaborate, please? Just if you can spare a minute!

Re: True sample and hold

Posted: Sun Sep 25, 2016 9:13 am
by martinvicanek
That's essentially

if update then out = in
else out = out

How is that? The "update" vasiable is a bit pattern, either all 1 or all 0. In the first case the expression evaluates to

out = out + (in - out)&1 = out + in - out = in

In the second case we get

out = out + (in - out)&0 = out + 0 = out

The objective of using bit masking insead of an if-then-else structure is to avoid branching so the code can be better optimized by the compiler. Specifically for FS, bitmasks work in the poly section of a synth whereas if-then-else does not.

Re: True sample and hold

Posted: Sun Sep 25, 2016 2:07 pm
by BobF
Thanks a million Martin,

I will defiantly put these to the test later today.

Also thank you tulamide for taking a look at it..

I will let you all know later today or tomorrow if it all works for my application.

Again thanks all, BobF.....

Re: True sample and hold

Posted: Sun Sep 25, 2016 11:12 pm
by tulamide
BobF, I am excited to see what you will come up with!

Martin, thank you for the explanation. I know about bitmasking and why it is used, but when actually reading your thoughts that led you to this code it makes much more sense. It is sooooooooooo damn difficult for me to think in bitmasks rather than in branching, and I don't know why. I mean it is just computer logic and I am around computers for ages. In fact as a kid I built my own circuits to understand the then new invention of an IC. I love logic, yet this dsp code seems to slip aways under my thoughts and always tries to fool me. :oops: