Expression Generator

Share your Audacity/Nyquist plug-ins here, or test drive the latest plug-ins submitted by Audacity users.

After testing a plug-in from this forum, please post feedback for the plug-in author.

Expression Generator

Permanent link to this post Posted by steve » Sun Mar 19, 2017 8:07 pm

Here's a plug-in written in response to this forum topic: viewtopic.php?f=39&t=95258
The idea of the plug-in is that an arbitrary function "f(x)" may be entered, and the plug-in will generate a waveform by evaluating the expression for a given range of "x".

This version of the plug-in takes its default expression from dmonty's initial enquiry:
Code: Select all
x^3 * e^(sin ( x * 3)) * 0.00001


I'm sure some user's would like to be able to enter the expression in this familiar arithmetic form, but I'll leave that for someone else to write. To use this plug-in, the expression must be written as a LISP S-Expression. This allows us to use Nyquist's built-in evaluation function rather than having to write a whole new parser. Taking the example above and writing it as a Nyquist / LISP expression:
Code: Select all
(* x x x (power (exp 1.0) (sin (* x 3))) 0.00001)


Some things to note:
Euler's number "e" is not defined in Nyquist, but can easily be derived using the EXP function.
For x^3, we could write (power x 3.0) but as we are also performing multiplication, it is as easy (and a bit shorter) to simply multiply three x's.

A plot of this expression can be created with WolframAlpha
https://www.wolframalpha.com/input/?i=p ... in+(+x+*+3))+*+0.00001,+x%3D0+to+33

wa.png
wa.png (14.54 KiB) Viewed 443 times



and with the Expression Generator plug-in:

firsttrack000.png
firsttrack000.png (8.44 KiB) Viewed 443 times


and here is the plug-in:
ExpressionGenerator.ny
(1.61 KiB) Downloaded 45 times

Installation instructions are as usual: http://manual.audacityteam.org/man/effe ... st_effects
For more information about Nyquist, see: http://wiki.audacityteam.org/wiki/Nyquist
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44458
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Expression Generator

Permanent link to this post Posted by steve » Sun Mar 19, 2017 9:11 pm

Thought it might be useful to add some examples.

1) Generate a single cycle sine tone:
A sine tone is defined by the function (SIN x).
In Nyquist, the "x" value is in radians, so for a single cycle we need values of x to go through 2xPI radians.
2xPI is about 6.283185307

fullwindow-Expression Generator-000.png
fullwindow-Expression Generator-000.png (24.09 KiB) Viewed 441 times


Alternatively, if we want to reverse the phase (offset the phase by 180 degrees), we can use values of x from -3.141592654 to +3.141592654


2) Generate a 440 Hz tone for 1 second:
Rather than having to work out how many radians x needs to go through, we can simply make x go through the desired number of cycles (440), and enter the expression
Code: Select all
(sin (* 2 pi x))


fullwindow-Expression Generator-001.png
fullwindow-Expression Generator-001.png (23.47 KiB) Viewed 441 times


and one more example, for values of x from 0 to 1:
Code: Select all
(* 0.5 (+ 1 (sin (* (+ 0.75 x) 2 pi))) (sin (* 880 pi x)))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44458
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Expression Generator

Permanent link to this post Posted by Trebor » Mon Mar 20, 2017 6:57 am

I've tried your ExpressionGenerator with tan ...

Code: Select all
(* x x x (power (tan x) (sin (* x 300))))

Upper trace created by ''ExpressionGenerator'' is displayed with spurious intermittent rectangles, (which do not survive export, the lower trace).png
Upper trace created by ''ExpressionGenerator'' is displayed with spurious intermittent rectangles, (which do not survive export, the lower trace).png (21.3 KiB) Viewed 436 times

The waveform displayed has intermittent spurious rectangles, (the upper trace shown).
However when that waveform is exported and re-opened in audacity the rectangles are gone, (the lower trace).

( Also the play-at-speed button Image produces no sound on the original generated waveform,
but works OK when generated waveform is exported and re-opened in Audacity ].
Trebor
 
Posts: 3771
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows Vista

Re: Expression Generator

Permanent link to this post Posted by steve » Mon Mar 20, 2017 8:34 am

Trebor wrote:I've tried your ExpressionGenerator with tan ...

Code: Select all
(* x x x (power (tan x) (sin (* x 300))))


Upper trace created by ''ExpressionGenerator'' is displayed with spurious intermittent rectangles, (which do not survive export, the lower trace).png

The waveform displayed has intermittent spurious rectangles, (the upper trace shown)


What happens when x = PI/2 ?
What do you think 'should' happen when x = PI/2 ?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44458
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Expression Generator

Permanent link to this post Posted by Trebor » Mon Mar 20, 2017 11:49 am

steve wrote:What do you think 'should' happen when x = PI/2 ?

Just seemed odd that when exported then re-opened the display and playback were OK.
value must have crashed the rendering of the waveform, (to produce the rectangles glitches).
Trebor
 
Posts: 3771
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows Vista

Re: Expression Generator

Permanent link to this post Posted by Trebor » Mon Mar 20, 2017 11:53 am

Code: Select all
(sin (tan (/ x))) 

bouncing ball-ish.png
bouncing ball-ish.png (9.64 KiB) Viewed 429 times

'bouncing ball-ish'.png
'bouncing ball-ish'.png (90.17 KiB) Viewed 429 times
Trebor
 
Posts: 3771
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows Vista

Re: Expression Generator

Permanent link to this post Posted by Trebor » Mon Mar 20, 2017 12:51 pm

Code: Select all
(* .5 (abs (cos (/ 100 x ))))

'(abs (cos '.png
'(abs (cos '.png (11.78 KiB) Viewed 428 times

(abs (cos.png
(abs (cos.png (166.76 KiB) Viewed 428 times


What's the full list of functions & operations we have to play with ?
Trebor
 
Posts: 3771
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows Vista

Re: Expression Generator

Permanent link to this post Posted by steve » Mon Mar 20, 2017 1:07 pm

Tan(PI/2) is "undefined". It is neither +∞ or -∞. It is "not a number" (a "NaN"), and that is the problem. Audacity does not handle NaNs very well (but better now than in older releases). It's the NaNs that cause the corruption. When exported, the NaNs are replaced by INF or -INF, which Audacity handles better.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44458
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Expression Generator

Permanent link to this post Posted by steve » Mon Mar 20, 2017 1:09 pm

Trebor wrote:What's the full list of functions & operations we have to play with ?

Just about everything here: http://www.cs.cmu.edu/~rbd/doc/nyquist/indx.html
provided that the expression returns a number (for the sample value).
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44458
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Expression Generator

Permanent link to this post Posted by steve » Mon Mar 20, 2017 1:20 pm

One thing to watch out for: If you apply an arithmetic operator to integers, then integer arithmetic is used. If you want a fractional (floating point) result, then either one of the numbers must be a floating point number, or you must explicitly convert the number to floating point:

Code: Select all
(setf *float-format* "%f") ; number format for debug output

(print (/ 1 4)) ; returns integer 0
(print (/ 4)) ; returns integer 0
(print (/ 1.0 4)) ; returns 0.25
(print (/ 4.0)) ; returns 0.25
(print (/ 1.0 4.0)) ; returns 0.25
(print (/ 1 (float 4))) ; returns 0.25
(print (/ (float 4))) ; returns 0.25
(print (/ (float 1) 4)) ; returns 0.25
(print (float (/ 1 4))) ; returns float 0.0
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44458
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Next

Return to New Plug-Ins



Who is online

Users browsing this forum: No registered users and 2 guests