使用Apple Accelerate Framework中的vDSP_conv执行自相关
我需要执行数组(向量)的自相关,但我无法找到正确的方法来执行此操作。 我相信我需要Accelerate Framework中的方法“vDSP_conv”,但我无法按照如何成功设置它。 让我失望最多的是需要2个输入。 也许我有错误的function,但我找不到一个在单个矢量上运行的function。
文档可以在这里找到
从网站复制
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);
检查示例代码以获取更多详细信息:(进行卷积)
编辑:这边界很荒谬,但你需要将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 )\n\n", 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);