来自Apple Accelerate Framework的conv
我需要执行一个数组的自相关(矢量),但我很难找到正确的方法来做到这一点。 我相信我需要加速框架中的“vDSP_conv”方法,但我无法按照如何成功设置它。 抛出我最多的东西是需要2个输入。 也许我有错误的功能,但我找不到在单个矢量上运行的功能。
文档可以在这里找到
从网站复制
vDSP_conv
对两个向量执行相关或卷积; 单精度。
void vDSP_conv(const float __vDSP_signal [],vDSP_Stride __vDSP_signalStride,const float __vDSP_filter [],vDSP_Stride __vDSP_strideFilter,float __vDSP_result [],vDSP_Stride __vDSP_strideResult,vDSP_Length __vDSP_lenResult,vDSP_Length __vDSP_lenFilter);
参数
__vDSP_signal
Input vector A. The length of this vector must be at least __vDSP_lenResult + __vDSP_lenFilter - 1.
__vDSP_signalStride
The stride through __vDSP_signal.
__vDSP_filter
Input vector B.
__vDSP_strideFilter
The stride through __vDSP_filter.
__vDSP_result
Output vector C.
__vDSP_strideResult
The stride through __vDSP_result.
__vDSP_lenResult
The length of __vDSP_result.
__vDSP_lenFilter
The length of __vDSP_filter.
举个例子,假设你有一个float x = [1.0, 2.0, 3.0, 4.0, 5.0]
的数组。 我将如何采取这种自相关?
输出应该类似于float y = [5.0, 14.0, 26.0, 40.0, 55.0, 40.0, 26.0, 14.0, 5.0] //generated using Matlab's xcorr(x) function
执行自相关只是意味着你将一个向量与自身进行互相关。 没有什么奇特的。
所以在你的情况下,做:
vDSP_conv(x, 1, x, 1, result, 1, 2*len_X-1, len_X);
查看示例代码以获取更多详细信息:(它进行卷积)
http://disanji.net/iOS_Doc/#documentation/Performance/Conceptual/vDSP_Programming_Guide/SampleCode/SampleCode.html
编辑:这边界荒谬,但你需要通过特定数量的零抵消x值,这只是疯狂。
以下是一个工作代码,只需将过滤器设置为您希望的x的值,然后将其余部分放在正确的位置:
float *signal, *filter, *result;
int32_t signalStride, filterStride, resultStride;
uint32_t lenSignal, filterLength, resultLength;
uint32_t i;
filterLength = 5;
resultLength = filterLength*2 -1;
lenSignal = ((filterLength + 3) & 0xFFFFFFFC) + resultLength;
signalStride = filterStride = resultStride = 1;
printf("nConvolution ( resultLength = %d, "
"filterLength = %d )nn", resultLength, filterLength);
/* Allocate memory for the input operands and check its availability. */
signal = (float *) malloc(lenSignal * sizeof(float));
filter = (float *) malloc(filterLength * sizeof(float));
result = (float *) malloc(resultLength * sizeof(float));
for (i = 0; i < filterLength; i++)
filter[i] = (float)(i+1);
for (i = 0; i < resultLength; i++)
if (i >=resultLength- filterLength)
signal[i] = filter[i - filterLength+1];
/* Correlation. */
vDSP_conv(signal, signalStride, filter, filterStride,
result, resultStride, resultLength, filterLength);
printf("signal: ");
for (i = 0; i < lenSignal; i++)
printf("%2.1f ", signal[i]);
printf("n filter: ");
for (i = 0; i < filterLength; i++)
printf("%2.1f ", filter[i]);
printf("n result: ");
for (i = 0; i < resultLength; i++)
printf("%2.1f ", result[i]);
/* Free allocated memory. */
free(signal);
free(filter);
free(result);
链接地址: http://www.djcxy.com/p/60353.html