Square Roots and If-Thens

Using Nyquist scripts in Audacity.
Post and download new plug-ins.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
Post Reply
Roquentin
Posts: 18
Joined: Sat May 01, 2010 5:29 pm
Operating System: Windows 10

Square Roots and If-Thens

Post by Roquentin » Wed May 19, 2010 9:27 pm

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.

steve
Site Admin
Posts: 47293
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu
Contact:

Re: Square Roots and If-Thens

Post by steve » Wed May 19, 2010 10:56 pm

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:

Code: Select all

(s-sqrt s)
or

Code: Select all

(s-exp (mult (s-log s) 0.5))
See here for s-log http://www.audacity-forum.de/download/e ... l#index371
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:

Code: Select all

(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 ....)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Roquentin
Posts: 18
Joined: Sat May 01, 2010 5:29 pm
Operating System: Windows 10

Re: Square Roots and If-Thens

Post by Roquentin » Thu May 20, 2010 4:42 pm

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.

steve
Site Admin
Posts: 47293
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu
Contact:

Re: Square Roots and If-Thens

Post by steve » Thu May 20, 2010 5:23 pm

Roquentin wrote:Is there no way using Nyquist to evaluate the square root of an expression that contains a sound?
Yes, as I wrote in my previous post:

Code: Select all

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

Code: Select all

(s-sqrt (diff 1 s))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Roquentin
Posts: 18
Joined: Sat May 01, 2010 5:29 pm
Operating System: Windows 10

Re: Square Roots and If-Thens

Post by Roquentin » Thu May 20, 2010 9:29 pm

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..."

(defun limit (s) (mult 1.999 (mult s (diff 1.0 (mult 0.5 (s-abs s))))))
(limit s)
------------------------------------------

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.

Post Reply