Page 1 of 1
Stream max/peak value, hold, release
Posted: Sun Aug 24, 2014 11:12 am
by Perfect Human Interface
Here's a very simple bit of DSP code I thought I'd share that determines the maximum value of a stream input, holds it for a period of time, and then gradually "releases" it.
(Special thanks to "oddson.")
Code: Select all
streamin x; // signal
streamin r; // decay rate (per sample)
streamin hold; // hold time in samples
streamout y; // "max signal" output
float h, xMax; // h is hold counter
xMax = xMax - (h>=hold & r); // subtract from peak while h reaches hold value
h = h + (h<hold & 1) - (x>=xMax & h); // increment counter only if less than hold value with reset to 0 if new input peak is greater
xMax = max(x,xMax) ; // retains former peak unless new peak or at reset
y = xMax;
Re: Stream max/peak value, hold, release
Posted: Sun Aug 24, 2014 8:49 pm
by MyCo
In this code, you don't need the intermediate variable "xMax". You can use output variables (but NOT input variables) like regular variables. The code could look like this (untested):
Code: Select all
streamin x; // signal
streamin r; // decay rate (per sample)
streamin hold; // hold time in samples
streamout y; // "max signal" output
float h; // h is hold counter
y = y - (h>=hold & r); // subtract from peak while h reaches hold value
h = h + (h<hold & 1) - (x>=y & h); // increment counter only if less than hold value with reset to 0 if new input peak is greater
y = max(x,y) ; // retains former peak unless new peak or at reset
In DSP does every clock cycle matter

Re: Stream max/peak value, hold, release
Posted: Sun Aug 24, 2014 10:44 pm
by RJHollins
nice .... and very useful !
How would it be to have a manual reset of the hold feature ? with a twist
When audio starts, the hold is auto reset. During play, have infinite hold with a manual [button] reset.
Early in my short SM daze I tried to modify a PEAK module that was posted ... i messed it up real good

Re: Stream max/peak value, hold, release
Posted: Mon Aug 25, 2014 10:59 am
by Perfect Human Interface
MyCo wrote:You can use output variables (but NOT input variables) like regular variables.
I couldn't remember how that went. Thanks MyCo.

RJHollins wrote:How would it be to have a manual reset of the hold feature ? with a twist
When audio starts, the hold is auto reset. During play, have infinite hold with a manual [button] reset.
I believe something like this (untested):
Code: Select all
streamin x; // signal
streamin hold; // hold on/off (1/0)
streamout y; // output
y = max(x,y) ; // retains highest peak
y = (hold==0 & x); // output = input if hold is off
You'd have to reset it on audio start externally.
----EDIT----
Here's the code to use for audio (bipolar) streams. I just replaced x with abs(x).
Code: Select all
streamin x; // signal
streamin r; // decay rate (per sample)
streamin hold; // hold time in samples
streamout y; // "max signal" output
float h; // h is hold counter
y = y - (h>=hold & r); // subtract from peak while h reaches hold value
h = h + (h<hold & 1) - (abs(x)>=y & h); // increment counter only if less than hold value with reset to 0 if new input peak is greater
y = max(abs(x),y) ; // retains former peak unless new peak or at reset