使用 Accelerate 框架将单精度浮点值转换为 Int 16
Convert single-precision, floating-point value to Int 16 using Accelerate framework
如何借助 Accelerate 框架将单精度浮点数转换为 int 16。
Int16 到浮点数:
import Accelerate
let rr: [Int16] = [ 18, 21, 41, 42, 48, 50, 55, 90]
var float = [Float](repeating: 0, count: rr.count)
vDSP_vflt16(rr, 1, &float, 1, vDSP_Length(rr.count))
现在我要尝试将它从 Float 恢复为 Int16 吗?
你可以使用 vDSP_vfix16 :
var ints = [Int16](repeating: 0, count: rr.count)
vDSP_vfix16(float, 1, &ints, 1, vDSP_Length(float.count))
print(ints)
// [18, 21, 41, 42, 48, 50, 55, 90]
vDSP_vfix16
向 0 舍入。您可以在 "Single-Vector Floating Point to 16-Bit Integer Conversion" 部分 here.[= 中找到使用其他舍入规则的其他函数14=]
vDSP Swift 覆盖层中还有 floatingPointToInteger
:
let floats: [Float] = [ 18, 21, 41, 42, 48, 50, 55, 90]
let ints = vDSP.floatingPointToInteger(floats,
integerType: Int16.self,
rounding: .towardNearestInteger)
print(ints) // Prints "[18, 21, 41, 42, 48, 50, 55, 90]"
如何借助 Accelerate 框架将单精度浮点数转换为 int 16。
Int16 到浮点数:
import Accelerate
let rr: [Int16] = [ 18, 21, 41, 42, 48, 50, 55, 90]
var float = [Float](repeating: 0, count: rr.count)
vDSP_vflt16(rr, 1, &float, 1, vDSP_Length(rr.count))
现在我要尝试将它从 Float 恢复为 Int16 吗?
你可以使用 vDSP_vfix16 :
var ints = [Int16](repeating: 0, count: rr.count)
vDSP_vfix16(float, 1, &ints, 1, vDSP_Length(float.count))
print(ints)
// [18, 21, 41, 42, 48, 50, 55, 90]
vDSP_vfix16
向 0 舍入。您可以在 "Single-Vector Floating Point to 16-Bit Integer Conversion" 部分 here.[= 中找到使用其他舍入规则的其他函数14=]
vDSP Swift 覆盖层中还有 floatingPointToInteger
:
let floats: [Float] = [ 18, 21, 41, 42, 48, 50, 55, 90]
let ints = vDSP.floatingPointToInteger(floats,
integerType: Int16.self,
rounding: .towardNearestInteger)
print(ints) // Prints "[18, 21, 41, 42, 48, 50, 55, 90]"