Investment Studio > Expressions > Functions > DSP > CONVOLVE
| Short fom: | float array[*][data_columns] convolve(float array[1][*]
window, float amplification, float array[*][data_columns] data) Returns the columnwise convolution of the data array with the window vector, multiplied by the amplification factor. Each data column is convolved separately with window. |
| Range form: | float array[*][data_columns] convolve(float array[1][*]
window, float amplification, float array[*][data_columns] data, integer first_column, integer last_column = data_columns) Returns the data array, with columns first_column through last_column replaced by their convolution with the window vector, multiplied by the amplification factor. Each data column in the specified range is convolved separately with window. If last_column is omitted, it defaults to first_column (i.e. only first_column is convolved). |
All arguments (including all array elements) are converted to float. If conversion fails, the default value 0.0 is used.
Convolution is the operation used to apply FIR (Finite Impulse Response) filters to data. Mathematically, it's defined as
¥ |
||
y[n] = |
å |
x[k] * h[n - k] |
k = -¥ |
where the convention is for x to denote the input data and for h to denote the window (also known as the filter's unit impulse response, since it equals the filter's output when x is a unit impulse, i.e. a single 1 followed by zeros).
You can visualize this as the window sliding over the input sequence: each point of the output sequence is the sum of the window elements multiplied with the corresponding input elements. In practice, the infinities in the sum are handled by assuming all array elements outside the array bounds to be zero.
The filter window can be created using one of the standard window functions (blackman, hamming, hanning, rectangle, triangle) or it can be tailor-made, e.g. by computing the inverse Fourier transform of the filter's desired frequency response. Conversely, since the window is the filter's unit impulse response, obtaining the filter's frequency response is just a matter of taking the Fourier transform of the window (see function fftp).
Strictly performed, the convolution operation produces length(data) + length(window) - 1 non-zero values. The convolve function only returns the first length(data) values. The last, length(window) - 1 "overhang" (created when the window is partially in "the future"), is discarded. If you want that part, pad the data with length(window) - 1 zeros before running it through the convolve function.
Example
The convolution of the 100-point sine wave with wavelength 100
_data = mop("sin()", makevector(100, 0, 2 * PI / 100))
(a column vector) and the 50-point triangular window
_window = triangle(50)
(a row vector) is
=convolve(array(_window), 1 / 25, array(_data))
where the inverse of the triangle's area has been used as the amplification factor.
Used as graph sources and plotted together, the above expressions look like this:

The black dots are the original sine wave, the green crosses are the triangular window and the red line is their convolution.