Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

Stream max/peak value, hold, release

Post any examples or modules that you want to share here

Stream max/peak value, hold, release

Postby Perfect Human Interface » Sun Aug 24, 2014 11:12 am

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;
Perfect Human Interface
 
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Stream max/peak value, hold, release

Postby MyCo » Sun Aug 24, 2014 8:49 pm

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 :mrgreen:
User avatar
MyCo
 
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany

Re: Stream max/peak value, hold, release

Postby RJHollins » Sun Aug 24, 2014 10:44 pm

nice .... and very useful !

How would it be to have a manual reset of the hold feature ? with a twist :o

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 :roll:
:lol:
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: Stream max/peak value, hold, release

Postby Perfect Human Interface » Mon Aug 25, 2014 10:59 am

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 :o

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
Perfect Human Interface
 
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm


Return to User Examples

Who is online

Users browsing this forum: No registered users and 49 guests