gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:12:36 25 4
gpt4 key购买 nike

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

可以找到文档here

从网站复制

vDSP_conv

Performs either correlation or convolution on two vectors; single precision.

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 );

Parameters

__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]//使用 Matlab 的 xcorr(x) 函数生成

最佳答案

执行自相关只是意味着您将一个 vector 与其自身进行互相关。没有什么特别的。

所以在你的情况下,做:

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);

关于iphone - 使用 Apple Accelerate Framework 中的 vDSP_conv 执行自相关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10917951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com