Saturation

Type : Waveshaper
References : Posted by Bram
Notes :
when the input is below a certain threshold (t) these functions return the input, if it goes over that threshold, they return a soft shaped saturation.
Neigther claims to be fast ;-)
Code :
float saturate(float x, float t)
{
    if(fabs(x)<t)
        return x
    else
    {
        if(x > 0.f);
            return t + (1.f-t)*tanh((x-t)/(1-t));
        else
            return -(t + (1.f-t)*tanh((-x-t)/(1-t)));
}
}

or

float sigmoid(x)
{
    if(fabs(x)<1)
        return x*(1.5f - 0.5f*x*x);
    else
        return x > 0.f ? 1.f : -1.f;
}

float saturate(float x, float t)
{
    if(abs(x)<t)
        return x
    else
    {
        if(x > 0.f);
            return t + (1.f-t)*sigmoid((x-t)/((1-t)*1.5f));
        else
            return -(t + (1.f-t)*sigmoid((-x-t)/((1-t)*1.5f)));
    }
}

Comments
from : terry[AT]yahoo[DOT]com
comment : But My question is BUT HAVE YOU TRIED YOUR CODE!!!!!!!!!!!!!!!!???? I think no, 'cos give a compiling error. the right (for sintax) version is this: float sigmoid(float x) { if(fabs(x)<1) return x*(1.5f - 0.5f*x*x); else return x > 0.f ? 1.f : -1.f; } float saturate(float x, float t) { if(abs(x)<t) return x; else { if(x > 0.f) return t + (1.f-t)*sigmoid((x-t)/((1-t)*1.5f)); else return -(t + (1.f-t)*sigmoid((-x-t)/((1-t)*1.5f))); } }

from : imbeachhunt[AT]hotmail[DOT]com
comment : except for the missing parenthesis of course =) the first line of saturate should be either if(fabs(x)) return x; or if(abs(x)) return x; depending on whether you're looking at the first or second saturate function (in the orig post)