Generating Data
Numerics is all about analyzing and manipulating numeric data. But unless you can read in data from an external
file, source or e.g. with the excellent F# Type Providers,
you may need to generate synthetic or random data locally, or transform existing data into a new form.
The Generate
class can help you in all these scenarios with a set of static functions generating either
an array or an IEnumerable sequence.
There is some overlap with LINQ, in case of F# also with some integrated language features and its fundamental types. This is intended for simplicity and consistency between array and sequence operations, as LINQ only supports sequences.
Linear Range
Generates a linearly spaced array within the inclusive interval between start and stop,
and either a provided step or a step of 1.0. Linear range is equivalent to the
single colon :
and double colon ::
operators in MATLAB.
F# has built in linear range support in array comprehensions with the colon operator:
1: 2: 3: 4: 

In C# you can get the same result with LinearRange
:
1: 2: 

Most of the routines in the Generate
class have variants with a Map
suffix.
Instead of returning an array with the generated numbers, these routines instead
apply the generated numbers to a custom function and return an array with the results.
Similarly, some routines have variants with a Sequence
suffix that return
lazy enumerable sequences instead of arrays.
LinearSpaced and LogSpaced
Generates a linearly or logspaced array within an interval, but other than linear range where the step is provided, here we instead provide the number of values we want. This is equivalent to the linspace and logspace operators in MATLAB.
1: 2: 

In F# you can also use:
1: 2: 3: 4: 5: 

LogSpaced
works the same way but instead of the values \(10^x\) it spaces the decade exponents \(x\) linearly
between the provided two exponents.
1:


Kronecker Delta Impulse
The Kronecker delta \(\delta[n]\) is a fundamental signal in timediscrete signal processing, often referred to as unit impulse. When applied to a system, the resulting output is the system's impulse response. It is closely related to the Dirac delta impulse function \(\delta(x)\) in continuous signal processing.
\[\delta[n] = \begin{cases} 0 &\mbox{if } n \ne 0 \\ 1 & \mbox{if } n = 0\end{cases}\]
The Impulse
routine generates a Kronecker delta impulse, but also accepts a sample delay
parameter \(d\) and amplitude \(A\) such that the resulting generated signal is
\[s[n] = A\cdot\delta[nd] = \begin{cases} 0 &\mbox{if } n \ne d \\ A & \mbox{if } n = d\end{cases}\]
There is also a periodic version in PeriodicImpulse
which accepts an additional period
parameter.
1: 2: 3: 4: 5: 

Heaviside Step
Another fundamental signal in signal processing, the Heaviside step function \(H[n]\) is the integral of the Dirac delta impulse and represents a signal that switches on at a specified time and then stays on indefinitely. In discrete time:
\[H[n] = \begin{cases} 0 &\mbox{if } n < 0 \\ 1 & \mbox{if } n \ge 0\end{cases}\]
The Step
routines generates a Heaviside step, but just like the Kronecker Delta impulse
also accepts a sample delay parameter \(d\) and amplitude \(A\) such that the resulting generated signal is
\[s[n] = A\cdot H[nd] = \begin{cases} 0 &\mbox{if } n < d \\ A & \mbox{if } n \ge d\end{cases}\]
1: 2: 

Periodic Sawtooth
Generates an array of the given length with a periodic upper forward sawtooth signal, i.e. a line starting at zero up to some amplitude \(A\), then drop back to zero instantly and start afresh. The sawtooth can be used to turn any arbitrary function defined over the interval \([0,A)\) into a periodic function by repeating it continuously.
Mathematically, the sawtooth can be described using the fractional part function \(\mathrm{frac}(x) \equiv x  \lfloor x \rfloor\), frequency \(\nu\) and phase \(\theta\) as
\[s(x) = A\cdot\mathrm{frac}\left(x\nu+\frac{\theta}{A}\right)\]
In a trigonometric interpretation the signal represents the angular position \(\alpha\) of a point moving endlessly around a circle with radius \(\frac{A}{2\pi}\) (and thus circumference \(A\)) in constant speed, normalized to strictly \(0\le\alpha < A\).
Generate.Periodic(length,samplingRate,frequency,amplitude,phase,delay)
 Sampling Rate: Number of samples \(r\) per time unit. If the time unit is 1s, the sampling rate has unit Hz.
 Frequency: Frequency \(\nu\) of the signal, in sawtoothperiods per time unit. If the time unit is 1s, the frequency has unit Hz. For a desired number of samples \(n\) per sawtoothperiod and sampling rate \(r\) choose \(\nu=\frac{r}{n}\).
 Amplitude: The theoretical maximum value \(A\), which is never reached and logically equivalent to zero. The circumference of the circle. Typically \(1\) or \(2\pi\).
 Phase: Optional initial value or phase offset. Contributes to \(\theta\).
 Delay: Optional initial delay, in samples. Contributes to \(\theta\).
The equivalent map function accepts a custom map lambda as second argument after the length:
1: 2: 3: 4: 

Sinusoidal
Generates a Sine wave array of the given length. This is equivalent to applying a scaled trigonometric Sine function to a periodic sawtooth of amplitude \(2\pi\).
\[s(x) = A\cdot\sin(2\pi\nu x + \theta)\]
Generate.Sinusoidal(length,samplingRate,frequency,amplitude,mean,phase,delay)
1: 2: 

Random
Generate random sequences by sampling from a random distribution.
Uniform Distribution
Generate sample sequences distributed uniformly between 0 and 1.
1:


Uniform supports mapping to functions with not only one but also two uniform variables
as arguments, with UniformMap
and UniformMap2
. As usual, lazy sequences can be
generated using the variants with the Sequence
suffix, e.g. UniformMap2Sequence
.
NonUniform Distributions
Instead of uniform we can also sample from other distributions.
Normal
 sample an array or sequence form a normal distributionStandard
 sample an array or sequence form a standard distribution
In addition, the Random
functions accept a custom distribution instance to sample
from. See the section about random numbers and probability distributions for details.
Map
Generates a new array or sequence where each new values is the result of applying the provided function the the corresponding value in the input data.
1: 2: 

In F# you'd typically use the Array module to the same effect (and should continue to do so):
1: 2: 

...but no equivalent operation is available in the .NET base class libraries (BCL) for C#. You can use LINQ, but that operates on sequences instead of arrays:
1:


Similarly, with Map2
you can also map a function accepting two inputs to two input arrays:
1: 2: 3: 

Typical F# equivalent:
1: 2: 

And in C# with LINQ:
1:

