## 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

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 (14.54 KiB) Viewed 1250 times

and with the Expression Generator plug-in:

firsttrack000.png (8.44 KiB) Viewed 1250 times

and here is the plug-in:
ExpressionGenerator.ny

Installation instructions are as usual: http://manual.audacityteam.org/man/effe ... st_effects
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve

Posts: 46845
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

### Re: Expression Generator

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 (24.09 KiB) Viewed 1248 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 (23.47 KiB) Viewed 1248 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

Posts: 46845
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

### Re: Expression Generator

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 (21.3 KiB) Viewed 1243 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 produces no sound on the original generated waveform,
but works OK when generated waveform is exported and re-opened in Audacity ].
Trebor

Posts: 4423
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows Vista

### Re: Expression Generator

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

Posts: 46845
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

### Re: Expression Generator

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: 4423
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows Vista

### Re: Expression Generator

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

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

'bouncing ball-ish'.png (90.17 KiB) Viewed 1236 times
Trebor

Posts: 4423
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows Vista

### Re: Expression Generator

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

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

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

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

Posts: 4423
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows Vista

### Re: Expression Generator

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

Posts: 46845
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

### Re: Expression Generator

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

Posts: 46845
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

### Re: Expression Generator

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

Posts: 46845
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Next