7

我需要执行数组(向量)的自相关,但我无法找到正确的方法。我相信我需要来自 Accelerate Framework 的方法“vDSP_conv”,但我不知道如何成功设置它。最让我失望的是需要 2 个输入。也许我有错误的功能,但我找不到对单个向量进行操作的功能。

文档可以在这里找到

从网站复制

vDSP_conv

对两个向量执行相关或卷积;单精度。

无效vDSP_conv(常量浮动__vDSP_signal[],vDSP_Stride __vDSP_signalStride,常量浮动__vDSP_filter[],vDSP_Stride __vDSP_strideFilter,浮动__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

4

1 回答 1

4

执行自相关仅仅意味着您将一个向量与其自身进行互相关。它没有什么花哨的。

所以在你的情况下,做:

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 值偏移特定数量的零,这太疯狂了。

以下是一个工作代码,只需将 filter 设置为您想要的 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);
于 2012-06-06T18:31:13.710 回答