Automatic threshold setting for peak hold indicator

Thanks, absolutely willing for sure, but from my code posted above, you can judge my non-specialist level, and very terse code without comments is hard for me to parse; never heard of "awk" before, I must admit. But I will try. I first looked at some of your other comments and understood that "leaky integrator" is a low-pass filter, and I also think "attack" and "decay" are somehow like a charging and discharging capacitor, like in a synthesizer with ADSR curves? And from that, I think what you are trying to say is that a "leaky integrator" with "attack" and "decay" is better than using an "EMA" or a "running median" for the filtering, do I get this first part correctly? Thus, I try to implement that instead of "EMA" or "running median" - and then see to the core issue - automatically establishing a threshold.

i think they are the same.

but it's not just using a filter, it's using a filter to capture the peaks and background level and then comparing the peak to background to recognize a peak meeting your criteria

to make the filter favor a higher/lower average requires different filter constants depending on if the signal is attacking (> avg) or decaying (< avg).

but the final step is the comparison of the 2 filters

look over following

const float KbgAttack = 1. / 128;
const float KbgDecay  = 1. / 128;
const float KpkAttack = 1. / 4;
const float KpkDecay  = 1. / 16;
const float Thresh    = 1.5;

float avgBg = 10;
float avgPk;

// -------------------------------------
float
process (
    float samp )
{
    if (samp > avgPk)
        avgPk += (samp - avgPk) * KpkAttack;
    else
        avgPk += (samp - avgPk) * KpkDecay;

    if (samp > avgBg)
        avgBg += (samp - avgBg) * KbgAttack;
    else
        avgBg += (samp - avgBg) * KbgDecay;

    return  (avgPk / avgBg) > 1.5;
}

I found your leaky integrator useful for something else Noise meter with microphone - Wokwi ESP32, STM32, Arduino Simulator in order to have a fast attack but slow decay (see Wokwi's serial plotter's light blue curve). Now I can, I think, also figure out that threshold issue. Good information in this thread!