Pregunta

I am currently in the process of converting a MATLAB algorithm to C in order to use it in an iOS application.

I've been struggling with the MATLAB's xcorr function. Here is the relevant MATLAB code.

xcr = xcorr(A,A,maxlags);

This , according to MATLAB documentation

returns the cross-correlation sequence over the lag range [-maxlags:maxlags]. Output c has length 2*maxlags+1.

The Apple Accelerate.Framework provides a convolution/correlation function named vDSP_conv but I fail to see how to use it in a way that it produces the same output as xcorr. Is this possible ? If yes could anyone help me.

Best Regards,

Acacio

¿Fue útil?

Solución

To duplicate the results of MATLAB’s xcorr, you need to pad the vector with zeroes before and after:

#include <stdio.h>

#include <Accelerate/Accelerate.h>


int main(void)
{
    #define NF  3
    #define NC  (2*NF+1)

    float A[3*NF] = {0, 0, 0, 1, 2, 3, 0, 0, 0};
    float C[NC];

    vDSP_conv(A, 1, A+NF, 1, C, 1, NC, NF);

    for (vDSP_Length i = 0; i < NC; ++i)
        printf("C[%u] = %g.\n", (unsigned) i, C[i]);

    return 0;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top