使用Apple Accelerate Framework中的vDSP_conv执行自相关

时间:2012-06-06 16:07:06

标签: iphone c accelerate-framework cross-correlation

我需要执行数组(向量)的自相关,但我找不到正确的方法。我相信我需要Accelerate Framework中的方法“vDSP_conv”,但我无法按照如何成功设置它。最让我失望的是需要2个输入。也许我有错误的功能,但我找不到一个在单个载体上运行的功能。

可以找到文档here

从网站上复制

  

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

1 个答案:

答案 0 :(得分: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值偏移一定数量的零,这真是太疯狂了。

以下是一个工作代码,只需将滤波器设置为您想要的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);