Fourier Transform
The Fourier and inverse Fourier transforms convert a time-series into a power spectrum and viseversa. These are well-known transformations that are employed for many applications including finding and characterizing periodicities in time-series analysis and regression; fast convolution and de-convolution; transfer function modeling in systems analysis; solving systems of differential equations; Bayesian analysis using characteristic functions; and signal filtering.
The discrete Fourier transform involves a time domain (corresponding to an index) and a frequency
domain (corresponding to a frequency index). The time points are equally spaced at internals
of Δt, and the frequency points are equally spaced at ΔF . Both index have n
points. The intervals spacings are related as
The quantity 1/(Δt) is called the sampling frequency, which should be at least twice the smallest frequency that is present in the underlying continuous signal. See FFT on the Analytica Wiki for additional details.
FFT(x, t, freq)
Computes the Discrete Fourier Transform (DFT) of the time-series x, indexed by t, and returns the discrete frequency spectrum as a complex array indexed by freq. It performs this calculation using the Fast Fourier Transform (FFT) algorithm. The indexes t and freq must have the same length. This is a complex-valued function -- even if x is real-valued, the result in general will be an array of complex numbers. You can apply the Abs() function to the result to obtain the magnitude of the frequency component if you are not interested in the phase. The discrete Fourier transform is defined as
The computation is fastest when the length of the indexes is a power of 2. It is also reasonably efficient when all the prime factors of the length of the index is small. So, for example, it is fairly efficient when the indexes are a power of 10, since the length factors to powers of 2 and 5, both of which are still small. The computation time increases approximately quadratically with the square of the largest factor.
Library: Advanced math
Examples: For usage examples, please see FFT on the Analytica Wiki.
FFTInv(x, freq, t)
Computes the inverse Discrete Fourier Transform (DFT) using the Fast Fourier Transform (FFT) algorithm. The frequency spectrum, x, is indexed by freq, and typically consists of complex numbers encoding both the magnitude and phase of each frequency. Returns the time-series with the indicated spectrum, indexed by t. The indexes freq and t must have the same length.
The inverse DFT is defined as
The efficiency of FFTInv is the same as for FFT(), maximally efficient when the length of the indexes is a power or 2.
Library: Advanced math
In mathematics, a set is a collection of unique elements. A set itself may be treated as a self-contained entity, such that you might represent an array of sets. In such applications, you do not wish for the elements of the set to be treated as an array dimension, or to interact with array abstraction; instead, you want the set to be treated as a single atomic entity, to which operations such as set intersection and set union might be applied. Thus, a simple list representation would not be suitable, since the list itself would act as an implicit dimension, and hence would interact with array abstraction.
Representation of a set: A natural way to represent a set in Analytica is as a reference to a list of elements. References operations are covered in a more general context in References and data structures, but in the context of sets, you can simple view the reference operator, \L, as an operator that takes a list of elements, L, and returns a set, and the dereference operator, #S, as an operator returns a list of elements from a set.
Literal sets: The following syntax is used to write an explicit set of literal elements:
\(['a','b','c','d'])
You cannot omit the round parentheses, i.e., \['a','b','c','d']
, because square brackets
following a reference operator are used to specify the indexes consumed by the reference.
Creating sets from an array: Given a 2-D array, A
, indexed by I
and J, the expression \[I]A
returns a 1-D array of sets indexed by J
. For each element of J
, the vector indexed by I
becomes a set. At this point, duplicate elements have not been removed, so it isn’t a strict set yet; however, it is now ready to be treated as a set by the set functions described in this section.
Variable Array_s :=
Index_b ▶ | |||
---|---|---|---|
Index_a: ▼ | 1 | 2 | 3 |
a | 7 | -3 | 1 |
b | -4 | -1 | 6 |
c | 5 | 0 | -2 |
\[Index_a]Array_s →
Index_b ▶ | |||
---|---|---|---|
1 | 2 | 3 | |
\([7, -4, 5]) | \([-3, -1, 0]) | \([1, 6, -2]) |
Display of sets: In a result table, each set displays as «ref». :
Double clicking on «ref» displays the elements of the set in a new result window.
Null values in sets: Set functions, like most other array functions, ignore null
values by default. This makes it possible to pad an array with null
values to hold the elements of a set, when the number of elements is less than the length of the array. However, it also means that set cannot contain the null
value by default. When you want to include null
as an actual element, and not just as a value to be ignored, the various set functions provide an optional boolean parameter to indicate this intent.
SetContains(set, element)
Returns 1 (true
) when element is in set.
Example:
SetContains(\(['a', 'b', 'c', 'd']), 5) → 0
SetContains(\Sequence(1, 100, 7), 85 ) → 1
SetDifference(originalSet, remove1, remove2, ..., resultIndex, keepNull)
Returns the set that results when the elements in the sets remove1, remove2, ..., as well as any duplicate elements, are removed from originalSet. When resultIndex is unspecified or false, the result is a reference to a list of elements. When an index is specified in the resultIndex parameter, the result is a 1-D array indexed by the resultIndex. When keepNull is unspecified or null, then any null
value in originalSet are ignored (and won’t be in the result). When keepNull is true, null
is treated as a legitimate element.
Example:
Var S := \(0 .. 10);
Var S2 := \Sequence(0, 10, 2);
Var S3 := \Sequence(0, 10, 3);
#SetDifference(S, S2, S3) → [1, 5, 7]
Index I := 1 .. 5;
SetDifference(S, S2, S3, resultIndex: I) →
.I ▶ | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
1 | 5 | 7 | «null» | «null» |
Set difference can be used to remove duplicates from a list:
Var L := [Null, 'a', 'b', 'c', 'd', 'b', 'c', 'd'];
#SetDifference(\L) → ['a', 'b', 'c', 'd']
#SetDifference(\L, keepNull: true) → [«null», 'a', 'b', 'c', 'd']
SetIntersection(sets, i, resultIndex, keepNull)
Returns the set intersection of sets. The sets parameter should be an array of sets indexed by i, or i can be omitted and sets specified as a list of sets. When resultIndex is omitted, the result is a set (a reference to a list) containing the elements common to all sets. Null
values are ignored (and not included in the result) unless keepNull is specified as true, in which case null
is treated like any other element.
Example:
Var S1 := \(['a', 'b', 'c', null,'d']);
Var S2 := \(['b', 'c', null, 'e']);
SetIntersection([S1, S2]) → \(['b', 'c'])
SetIntersection([S1, S2], keepNull: true) → \(['b', 'c', «null»])
#SetIntersection([\('a' .. 'p'),\('k' .. 'z')]) → ['k', 'l', 'm', 'n', 'o', 'p']
The following example finds all numbers under 10,000 divisible by the first 5 prime numbers:
Index n := [2, 3, 5, 7,11];
Var sets := (for j := n do \Sequence(j, 10K, j));
#SetIntersection(sets, n) → [2310, 4620, 6930, 9240]
SetsAreEqual(sets, i, ignoreNull)
Tests whether all sets in the parameter sets have the same elements. The parameter sets should be an array indexed by index i, or index i can be omitted and sets can be a list of sets. Null
values are ignored unless ignoreNull is specified as false. The presence of duplicates does not impact equality determination.
Example:
Var L1 := ['a', 'b', 'c', null]; Var L2 := ['b', 'c', 'a']; Var L3 := ['c', 'b', 'a', 'b']; SetsAreEqual([\L1, \L2, \L3]) → 1 SetsAreEqual([\L1, \L2, \L3], ignoreNull: false) → 0
SetUnion(sets, i, resultIndex, keepNull)
Returns a collection of all elements appearing in any set appearing in the parameter sets. The
parameter sets is an array of sets (references to lists or to 1-D arrays) which is indexed by i, or it
may be a list of sets when i is omitted. When resultIndex is omitted, the result is a reference the list of elements. When an index is provided to the resultIndex parameter, the result is a 1-D array indexed by resultIndex. Null
values are ignored (and not included in the result) unless keepNull is specified as true.
Example:
#SetUnion([\('a' .. 'd'), \('c' .. 'f')]) → ['a', 'b', 'c', 'd', 'e', 'f'] Index m := Sequence(1-Jan-2011, 1-May-2011, dateUnit: 'M'); Index d := [0, 14]; #SetUnion(SetUnion(\[d](m + d), d), m) → [1-Jan-2011, 15-Jan-2011, 1-Feb-2011, 15-Feb-2011, 1-Mar-2011, 15-Mar-2011, 1-Apr-2011, 15-Apr-2011, 1-May-2011, 15-May-2011]
Enable comment auto-refresher