NLISP Code

Home

Download

Examples

FAQ

To Do

News

References

Author


NLISP Examples

Example 1

This example plots a sine curve between zero and 2*pi. The code defines a function called example 1 that takes no arguments. The 'let' construct defines an array called x. The value of x comes from the function .rseq, which creates a one dimensional array and takes three arguments; the starting value, the end value, and the number of elements in the array. Here, the array will be from 0 to 2*pi, and will have 100 elements. Next, we call plot. Note that it is inside the 'let' clause (inside the brackets starting before let) so x is valid here. The call to plot plots (.sin x) against x.

This example introduces two functions and an operator that are part of NLISP, and are not included in Common Lisp. The function (.rseq a b n) to generate an array, (.sin x) to get the sine of array x, and (plot y x).

Compile this example with (compile-file "example1.lisp" :load t) and run it with (example1).

Example 1 Code

The plot made by the code shown above is given below.

Example 1

Example 2

Here, we produce a surface plot of a Bessel function, using the function (.bessel-J0 x) imported from the GSL library. The example is very similar to example 1, but we call (.rseq2 a b n) to produce a 2D array (x) and set y to the transpose of x. We calculate the distance from the origin in the horizontal plane (r) and call .bessel-J0 to set z. Finally, we call (surface z x y) to make the plot.The keyword :reset nil is required because we have setup a palette and changed the plot size before calling (surface z x y). By default, the plot routines clear all previous settings before plotting.

Example 2 Code

Note that in this example we use let* rather than let. We must do this if we want to use previously defined variables in the same let clause (e.g. in setting y to the transpose of x with (y (.transpose x)) requires that x is already defined.

Example 2

Example 3

Example 3 is another demonstrates of how to write a wrapper for a GSL routine. First, a low-level interface to the GSL library function is made with def-alien-routine (see the section on Alien object in the CMU CL manual). Then a high level routine (called fft) is made, which is standard, portable Lisp, but requires the non-portable low-level wrapper.

Example 3 Code

The example function asks for a number as a parameter. It then makes an array of zeros, other than array element n which is set to 1+0i. The function then plots the real and imaginary components of the FFT'd array. You can see the frequency of the real and imaginary components increase with n. We also use the function nplot that takes list arguments for the data so that it can draw more than one graph on the same plot. The function nplot takes a list of double-floats for y, so we call .realpart and .imagpart to make the double-float array from the (complex double-float) array. The example plot is made by calling (example3 2).

Example 3

Example 4

Example 4 is a demonstration of a slightly larger program to show off the capabilities of NPLOT. It shows a simple simulator for an airborne pulse-limited radar altimeter. It can plot the expected power of an echo received from a surface as a function of time, and the shape of the expanding radar pulse as it intersects the surface.

The first plot shows the antenna pattern of the radar. The pattern is for a rectangular antenna with dimensions of 0.15m x 0.3m. Call with, e.g. (plot-antenna 100).

Antenna Pattern

The next plot shows the intersection of the expanding radar pulse with the surface. This gives a ring (on a flat surface) that expands as the pulse travels away from the antenna, and through the surface. You can plot any range ring by callling, e.g. (plot-range-ring 100 66). The first number is the array size and the second is the time step. Time steps before 64 are before the pulse reaches the surface, so you won't see anything.

Range Ring

The power at a time t is the integrated power over the surface area when the pulse was intersecting the surface. The power decays rapidly because of the antenna pattern. This is shown in the plot below. Call with, e.g. (plot-echo 60).

Echo Power

The example plots show the results over a flat surface. You could include some topography in the DEM to produce more interesting results.

The source code can be found in example4.lisp in the NLISP distribution.