Question

I'm using the accelerate framework to optimize my DSP code. There are several times when I want to copy the contents of one array (or portion of an array) to another.

I can't seem to find an appropriate function to do this, so instead I've been doing something kind of silly, which is to multiply the array by 1 (or add 0) and get the copy that way.

float one = 1;

float sourceArray = new float[arrayLength];
/////....sourceArray is filled up with data

float destArray = new float[arrayLength];

vDSP_vsmul(sourceArray, 1, &one, destArray, 1, arrayLength);

there has to be a better way to do this!? Thanks!

Was it helpful?

Solution 2

How about memcpy?

#include <string.h>

memcpy(destArray, sourceArray, arrayLength * sizeof(float));

OTHER TIPS

If you're willing to use the BLAS portion of Accelerate, Jeff Biggus has benchmarked cblas_scopy() as being faster than even memcpy().

I could think of a lot worse ways that vDSP_vsmul(); you could also do vvcopysign().

You could use vDSP_vclr and vDSP_vadd as follow:

int sourceLength = 3;
float* source = (float*)malloc(sourceLength * sizeof(float));
// source is filled with data, let's say [5, 5, 5]

int destinationLength = 10;
float* destination = (float*)malloc(destinationLength * sizeof(float));
// destination is filled with ones so [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

// Prepare the destination array to receive the source array
// by setting its values to 0 in the range [initialIndex, initialIndex + N-1]
int initialIndex = 2;
vDSP_vclr((destination+initialIndex), 1, sourceLength);

// We add source[0, N-1] into destination[initialIndex, initialIndex + N-1]
vDSP_vadd(source, 1, (destination+initialIndex), 1, (destination+initialIndex), 1, sourceLength);

Or more concise you could also use 'cblas_scopy' as Brad Larson said

// Init source and destination
// We copy source[0, sourceLength] into destination[initialIndex, initialIndex + sourceLength]
cblas_scopy(sourceLength, source, 1, (destination+initialIndex), 1);

I think this is the best way to copy.

Copies the contents of a submatrix to another submatrix; single precision. https://developer.apple.com/documentation/accelerate/1449950-vdsp_mmov

func vDSP_mmov(_ __A: UnsafePointer<Float>, 
             _ __C: UnsafeMutablePointer<Float>, 
             _ __M: vDSP_Length, 
             _ __N: vDSP_Length, 
             _ __TA: vDSP_Length, 
             _ __TC: vDSP_Length)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top