I am running into some issues trying to use the Accelerate framework with vDSP API from Swift. Obviously I am doing something wrong although the compiler gives me all sorts of warnings
var srcAsFloat:CConstPointer<CFloat> = CFloat[](count: Int(width*height), repeatedValue: 0)
var dstAsFloat = CFloat[](count: Int(width*height), repeatedValue: 0)
if shouldClip {
var min:CFloat = 0.0
var max:CFloat = 255.0
var l:vDSP_Stride = Int(width*height)
vDSP_vclip(CConstPointer<CFloat>(&dstAsFloat), vDSP_Stride(1), CConstPointer<CFloat>(&min), CConstPointer<CFloat>(&max), CMutablePointer<CFloat>(&dstAsFloat), vDSP_Stride(1), l)
}
The error:
error: could not find an overload for 'init' that accepts the supplied arguments
vDSP_vclip(CConstPointer<CFloat>(&dstAsFloat),
vDSP_Stride(1),
CConstPointer<CFloat>(&min),
CConstPointer<CFloat>(&max),
CMutablePointer<CFloat>(&dstAsFloat),
vDSP_Stride(1),
l) –
I've tried to cast the heck out of it but so far, no luck.
Okay, I figured out a (probably sub-optimal) solution. I decided to bridge to Objective-C (and used a slightly different function).
import Foundation
func abs(x: matrix) -> matrix{
let N = x.count
var arg1 = NSArray(array: x)
var yy = abs_objc(arg1, CInt(N))
var y = zeros(N)
for i in 0..N{
y[i] = Double(yy[i])
}
return y
#import <Accelerate/Accelerate.h>
double* abs_objc(NSArray * x, int N){
// converting input to double *
double * xx = (double *)malloc(sizeof(double) * N);
for (int i=0; i<[x count]; i++) {
xx[i] = [[x objectAtIndex:i] doubleValue];
}
// init'ing output
double * y = (double *)malloc(sizeof(double) * N);
for (int i=0; i<N; i++){
y[i] = 0;
}
vDSP_vabsD(xx,1,y,1,N);
return y;
}
#import <Foundation/Foundation.h>
double* abs_objc(NSArray * x, int N);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With