This document is a part of the WSxM documentation and shows some examples of functions you can generate with the Function Generator of WSxM. The first section (Basic Examples) introduces simplest functions of this feature, and the second one (Advanced Examples) explains more complex expressions such as conditional, loops and image processing.
1. Basic Examples
1.1. F (x)
1.2. F(x, y)
1.3. F(r, theta)
2. Advanced examples
2.1. Conditionals
2.2. Series
2.3. Atoms
2.4. SPM images
2.4.1. Processes
2.4.2. Other examples
2.5. One-dimensional SPMs
2.6. Predefined variables
2.7. Changing scales and units
The simplest function you can generate with Function Generator is an f(x) function like this:
F(x) = x2 - 0.25, that produces this curve:

Function Generator let you create two-dimensional functions, when selected f(x, y) as dimension.
For example, you can make a function like F(x, y) = sqrt(x2 + y2)

The third function type supported by Function Generator is F (r, theta).
You can use polar coordinates to generate your expression, as shown in this other example:

Note that, although you are using polar coordinates, the min / max values corresponds to "x" and "y".
The values of "r" and "theta" are calculated by Function Generator. You cannot use "x" and "y" in your expression.
In this first example, we will use the IF function to write an expression that will define a continuous one-dimensional function with three intervals.
Our function values are the following:
Cos(x) if x < p / 2
(-x / p ) + 1 / 2 if p / 2 < =x <= 3 * p / 2
Sin(x) if x > 3 * p / 2
and the expression: F(x) = IF ( x < PI / 2, cos(x), IF (x < 3 * PI / 2, (-x) / PI + 0.5, sin(x)))
The resulting curve is shown below.

There are two types of series available in Function Generator: Summations and multiplicative series.
Summation:
The following example shows how works this kind of series.
We will approximate f(x) = x2 by the series S (2 * i), from i = 0 to x.
In this case the expression will be:
F(x) = sum ( i, 0, x, 0.25, 2 * i )
Note that 0.25 is the "step".

Multiplicative series:
With this other example, you will see how this kind of series works. We will generate P (x - i), where i goes from -1 to 4.
To do this, we will set the limits for x-axis from -1 to 4 and the expression to F(x) = Prod (i, -1, 4, 1, x-i).

This function cuts the x-axis at x = -1, 0, 1, 2, 3 and 4.
You can use Function Generator to generate an atom like this:

by simply entering this expression: F(x, y) = exp( - ((10*x)^2 + (10*y)^2 ))
You can use the "sum" function to generate a 5-atoms row.
The expression may be now:
F(x, y) = sum(i, -2, 2, 1, exp(- ( (10 * x + 4 * i)^2 + (10 * y)^2 ) ) )

Of course, you can nest a "sum" function into another "sum" to create a mesh.
The mathematical expression will be: SS e(- ( (10 * x + 4 * i)^2 + (10 * y + 4 * j)^2 ) )
where both summations go from -2 to 2 (variables "i" and "j" respectively).
So, the expression used in Function Generator may be:
F(x, y) = sum(i, -2, 2, 1, sum(j, -2, 2, 1, exp(- ( (10 * x + 4 * i)^2 + (10 * y + 4 * j)^2 ) ) ) )
The result is shown in the figure below:

In the following examples, we will use an SPM image that is 512 x 512 points and saved as "cell.top". Here it is:

We will use Function Generator to apply several simple processes to the image above.
For each of these examples, you must perform some simple steps:
Process: Copy
To make a copy of an opened image, you only have to enter this simple expression
F(x, y) = 2dSPM(x, y)

Process: Reverse
To invert an image, you can enter this other expression
F(x, y) = - 2dSPM( x, y )

Note that there is no problem with changing the intervals and number of points.
Processes: Mirror
You can use Function Generator to apply a Mirror process over an SPM image with this function
F(x, y) = 2dSPM( 37 - x, 37 - y )

We have put "37" because it is the maximum value (and the length) in both intervals but, as you will see later, you can write a more generic function using predefined variables.
Processes: Zoom
You can apply a zoom process to a section of an opened image.
You can see an example of a Zoom-In here:

To change the zoom level, you must multiply (or divide) by a factor when passing arguments x and y.
Note that the values passed as arguments to the SPM functions are used to select a point into the original image, referring to its value range.
So if you multiply these arguments by a value between 0 and 1, the range selected into the first image will be smaller and the image will be zoomed-in.
However, if you apply a factor greater than 1, the values selected will be more spaced and the image will be zoomed-out, as shown in this figure:

In this case, most of the image generated is zero. That is because, when applying a factor greater than 1, values will early go out of range and SPM functions return zero when one or both of its arguments are out of image limits.
In general, the expression must seem something like that:
F(x, y) = 2dSPM ( x / a + b, y / c + d )
Where a and c controls the zoom level, while b and d let you select the region.
If you increase b or d, the region will move right or up, repectively.
You can reduce or increase the zoom level by changing parameters a and c (and they can be different).
You can combine two or more functions to create many different kind of In the following example, we have combined the IF and 2dSPM functions to invert a circular region of the original image "cell.top":

In this other example, we will mix some SPM images to create a unique image.
Suppose that we have opened two SPM images, cell.top (with id 1949) and cell2.stp (that is a 90º rotation of cell.top, and with id 1950).
We will combine these two images into another one by using the expression:
IF ( x < 18 , 2dSPM( x+0.25 , y , spm_1949 ), 2dSPM( x , y , spm_1950 ))

Finally, we will merge up four zoomed-out images to have only one image with them all.
The expression in this case is the following:
2dSPM(x/0.5, y/0.5 -37) + (-2dSPM(x/0.5 - 37, y/0.5 -37)) + abs(2dSPM(x/0.5, y/0.5)) + 10000 * cos(ln(1+ abs(2dSPM(x/0.5 -37, y/0.5))))

You can create new curves by using some values of other ones. To do this, Function Generator provides 1dSPM functions.
In the next example, we will use the folowing curve:

You can generate this two-line curve by creating separately the following curves:
F(x) = x2
F(x) = sin(10 * x)
and then using the "Merge Curves" tool.
Now, you can get the average line with Function Generator by using the expression
F(x) = (1dSPM(x, crv_1149, 0) + 1dSPM(x, crv_1149, 1)) / 2

Note that "crv_1149" is the identifier of the two-line curve previously generated, and may be different.
The indexes 0 (optional) and 1 select a line in the curve. Lines 0 and 1 corresponds to functions sin(x) and x2 respectively.
Function Generator also provides functions 1dSPMMin / 1dSPMMax, that gives you the minimum (or maximum) value of a line or curve.
You can see how they work in these two examples.
First, if you don't specify the line, the whole curve will be selected. So, you will get the minimum and maximum value of the whole curve.

However, you can select the line, as shown below:

You can use the predefined variables provided by Function Generator in your expressions.
For example, you can rewrite the expression of rotation process example
F(x, y) = 2dSPM( 37 - x, 37 - y )
as
F(x, y) = 2dSPM( xmax + xmin - x, ymax + ymin - y ).
The resulting image will be the same even if you change the intervals:

This feature can help you write expressions that are more generic, and which you can use with many different images.
You can change the scale and the units of a curve with Function Generator. Suppose that you have an image like this:

and you want to change the units from nanometers (nm) to angstroms (Å). You can use 1dSPM function like this:
F(x) = 1dspm(x) * 100
Now you only have to apply a factor to the values of the first curve and change the units in the dialog boxes:
