vDSP_conv
Performs either correlation or convolution on two real single-precision vectors.
Declaration
extern void vDSP_conv(const float *__A, vDSP_Stride __IA, const float *__F, vDSP_Stride __IF, float *__C, vDSP_Stride __IC, vDSP_Length __N, vDSP_Length __P);Parameters
- __A:
The real single-precision input signal vector. The length of this vector must be at least
N + P - 1. - __IA:
The stride through the input signal vector,
A. - __F:
The real single-precision filter vector.
- __IF:
The stride through the filter vector,
F. - __C:
The real single-precision output signal vector.
- __IC:
The stride through the output signal vector,
C. - __N:
The number of elements in the output signal vector,
C. - __P:
The number of elements in the filter vector,
F.
Mentioned in
Discussion
Use this function to compute either the convolution or the correlation of an input signal and a filter vector. Both operations compute the sliding dot product of the filter and the section of the input signal that the filter is over. Specify a positive stride through the filter to perform correlation and a negative stride through the filter to perform convolution. When you perform convolution, the __F parameter must point to the last element in the filter.
The function can run in place, but C can’t be in place with F. For example, the following code defines an input signal, a filter, and the number of output elements:
let signal: [Float] = [1, 2, 3, 4, 5, 6, 7, 8]
let filter: [Float] = [10, 20, 30]
let outputCount = signal.count - filter.count + 1Perform Correlation
The following code performs correlation. In this example, the stride through the filter is 1.
var correlationResult = [Float](repeating: 0,
count: outputCount)
vDSP_conv(signal,
1,
filter,
1, // The stride through the filter vector.
&correlationResult,
1,
vDSP_Length(outputCount),
vDSP_Length(filter.count))On return, correlationResult contains the values [140.0, 200.0, 260.0, 320.0, 380.0, 440.0]. The figure below shows how the function calculates each element in the result:
[Image]
Perform Convolution
The following code performs convolution. In this example, the stride through the filter is -1 and the filter parameter points to the last element in the filter.
var convolutionResult = [Float](repeating: 0,
count: outputCount)
filter.withUnsafeBufferPointer { filterPtr in
vDSP_conv(signal,
1,
filterPtr.baseAddress!.advanced(by: filter.count - 1),
-1, // The stride through the filter vector.
&convolutionResult,
1,
vDSP_Length(outputCount),
vDSP_Length(filter.count))
}On return, convolutionResult contains the values [100.0, 160.0, 220.0, 280.0, 340.0, 400.0]. The figure below shows how the function calculates each element in the result:
[Image]