Square Roots and If-Thens

Hey I am working on a continuously-variable-response expander plugin. I have the following basic formula (distilled from the quadratic equation) which I know works on a mathematical level:

the formula is: output = 1 - (sqrt (1 - (abs s))). [not in Nyquist syntax obviously]

Here’s the actual plugin as I have it now:

;nyquist plug-in
;version 1
;type process
;name “soft expander”
(defun expand (s) (diff 1 (sqrt (diff 1 (s-abs s)))))
(expand s)

The problems I am having are twofold:

A. How to apply the square root operation to the (1 - (abs s)). I am getting either “…did not return data” or a completely-used-up CPU and a frozen Audacity…it’s becoming obvious that I don’t get how square roots work in Nyquist.

B. Logical operation. The formula as it is basically rectifies the signal. I would like a simple formula that applies all the way from -1 to +1, but if an “if s is positive, return positive result; if s is negative, return the negative of the result” is necessary I am happy.

So I’m pretty much ready to have my mind blown if anybody wants to hack through this and/or help me get a handle on the way Nyquist handles these things so I can look for a more elegant solution.

Hi - we now have a brand new board specially for Nyquist, so I’ve moved your post to it.

“sqrt” only works with number, not with sounds.
For the square root of a sound (positive values) you can either use:

``````(s-sqrt s)
``````

or

``````(s-exp (mult (s-log s) 0.5))
``````

Note the warning:
“Note that the ln of 0 is undefined (some implementations return negative infinity), so use this function with care.”

To process only positive values:

``````(sum 1 (clip (sum s -1) 1))
``````

To process only negative values, invert the waveform first (multiply by -1)
To make the result negative, invert it again.

To combine the positive part with the negative part, add them together either with (sum …) or (sim …)

Thanks for the reply Steve, and I’m glad there’s a place for Nyquist-specific questions here.

Is there no way using Nyquist to evaluate the square root of an expression that contains a sound? The (sqrt (1-s)) function is irreducible mathematically as far as I know. I worked last night and this morning to come up with some kind of workaround, but am still coming up empty-handed.

Yes, as I wrote in my previous post:

``````(s-sqrt ...)
``````

so for (sqrt (1-s)) you would write

``````(s-sqrt (diff 1 s))
``````

Thanks, steve! You are two for two making my day between this and my soft-clipping limiter.

Rookie mistake on my part. I called the function “expand” without realizing that it was a native Nyquist function.
So I applied the “clip” function and ended up with a delightful expander plug-in that works to counteract the effect of the soft-clipper you helped me with. I suspect the expander will work well to ameliorate some of the damage done to any heavily-compressed material, as long as it’s not hard-clipped.

So, anyway, here is the soft clipper:

;nyquist plug-in
;version 1
;type process
;name “soft-clipping brickwall limiter”

;action “Burnishing…”

And here is the expander:

;nyquist plug-in
;version 1
;type process
;name “soft-knee expander”

(defun goforitandexpandthatthing (s) (sum (diff 1 (s-sqrt (diff 1 (sum 1 (clip (sum s -1) 1))))) (mult -1 (diff 1 (s-sqrt (s-abs (diff -1 (sum -1 (clip (sum s 1) 1))))))))) (goforitandexpandthatthing s)

Thanks for making this work.