summaryrefslogtreecommitdiff
path: root/IQ/IQUtils.swift
diff options
context:
space:
mode:
Diffstat (limited to 'IQ/IQUtils.swift')
-rw-r--r--IQ/IQUtils.swift159
1 files changed, 154 insertions, 5 deletions
diff --git a/IQ/IQUtils.swift b/IQ/IQUtils.swift
index 4a80157..1a3f4b4 100644
--- a/IQ/IQUtils.swift
+++ b/IQ/IQUtils.swift
@@ -6,6 +6,7 @@
//
import Accelerate
+import ComplexModule
/// Convert from UInt8 to Float, naive implementation
/// - Parameters:
@@ -158,9 +159,9 @@ func cnvSC16Q11toFloat32_2(_ arr: [Int16]) -> [Float32] {
//let iq = IQ(size: 8, bits: 8, sign: false, complex: true)
//print("!!!!")
- var out: [Float32] = .init(repeating: 0.0, count: arr.count)
- var cnt_overflow=0
- var cnt_underflow=0
+ let out: [Float32] = .init(repeating: 0.0, count: arr.count)
+ let cnt_overflow=0
+ let cnt_underflow=0
// -32768..32767 -> -0.996 ... 0.996
// -32768..0 -> -0.999 ... 0.0
// 1...32767 -> 0.0 ... 0.999
@@ -255,7 +256,7 @@ Input.Element == UInt8
return AnySequence(s)
}
-/// Conver F32 array to U8
+/// Convert F32 array to U8
/// - Parameters:
/// - arr: array of Float data
/// - Returns: returns array of UInt8
@@ -274,7 +275,32 @@ func cnvFloat32ToUInt8(_ arr: [Float32]) -> [UInt8] {
return ret
}
-/// Conver F32 array to I8
+func cnvFloat32ToCUInt8(_ el: Float32) -> UInt8 {
+ var ret:Float32 = el
+ if el > 1.0 {
+ ret = 1.0
+ } else if el < -1.0 {
+ ret = 1.0
+ }
+ return UInt8(127.0*(ret+1.0))
+}
+
+/// Convert CF32 array to U8
+/// - Parameters:
+/// - arr: array of Compex Float data
+/// - Returns: returns array of IQ UInt8
+///
+func cnvFloat32ToCUInt8(_ arr: [Complex<Float32>]) -> [UInt8] {
+ var ret : [UInt8] = Array(repeating: UInt8(0), count: arr.count*2)
+ for i in 0..<arr.count {
+ let element = arr[i]
+ ret[i*2] = cnvFloat32ToCUInt8(element.real)
+ ret[i*2+1] = cnvFloat32ToCUInt8(element.imaginary)
+ }
+ return ret
+}
+
+/// Convert F32 array to I8
/// - Parameters:
/// - arr: array of Float data
/// - Returns: returns array of Int8
@@ -297,6 +323,30 @@ func cnvFloat32ToInt8(_ arr: [Float32]) -> [Int8] {
return ret
}
+func cnvFloat32ToCInt8(_ el: Float32) -> Int8 {
+ var ret:Float32 = el
+ if ret > 1.0 {
+ ret = 1.0
+ } else if ret < -1.0 {
+ ret = -1.0
+ }
+ if (ret>0.0) {
+ return Int8(127.0*(ret))
+ } else {
+ return Int8(128.0*(ret))
+ }
+}
+
+func cnvFloat32ToCInt8(_ arr: [Complex<Float32>]) -> [Int8] {
+ var ret : [Int8] = Array(repeating: Int8(0), count: arr.count*2)
+ for i in 0..<arr.count {
+ let element = arr[i]
+ ret[i*2] = cnvFloat32ToCInt8(element.real)
+ ret[i*2+1] = cnvFloat32ToCInt8(element.imaginary)
+ }
+ return ret
+}
+
/// Conver F32 array to U16
/// - Parameters:
/// - arr: array of Float data
@@ -316,6 +366,26 @@ func cnvFloat32ToUInt16(_ arr: [Float32]) -> [UInt16] {
return ret
}
+func cnvFloat32ToCUInt16(_ el: Float32) -> UInt16 {
+ var ret:Float32 = el
+ if el > 1.0 {
+ ret = 1.0
+ } else if el < -1.0 {
+ ret = -1.0
+ }
+ return UInt16(32767*(ret+1.0))
+}
+
+func cnvFloat32ToCUInt16(_ arr: [Complex<Float32>]) -> [UInt16] {
+ var ret : [UInt16] = Array(repeating: UInt16(0), count: arr.count*2)
+ for i in 0..<arr.count {
+ let element = arr[i]
+ ret[i*2] = cnvFloat32ToCUInt16(element.real)
+ ret[i*2+1] = cnvFloat32ToCUInt16(element.imaginary)
+ }
+ return ret
+}
+
/// Conver F32 array to I16
/// - Parameters:
/// - arr: array of Float data
@@ -338,3 +408,82 @@ func cnvFloat32ToInt16(_ arr: [Float32]) -> [Int16] {
}
return ret
}
+
+func cnvFloat32ToCInt16(_ el: Float32) -> Int16 {
+ var ret:Float32 = el
+
+ if el > 1.0 {
+ ret = 1.0
+ } else if el < -1.0 {
+ ret = -1.0
+ }
+
+ if (ret>0.0) {
+ return Int16(32767.0*(ret))
+ } else {
+ return Int16(-32768.0*(-ret))
+ }
+}
+
+func cnvFloat32ToCInt16(_ arr: [Complex<Float32>]) -> [Int16] {
+ var ret : [Int16] = Array(repeating: Int16(0), count: arr.count*2)
+ for i in 0..<arr.count {
+ let element = arr[i]
+ ret[i*2] = cnvFloat32ToCInt16(element.real)
+ ret[i*2+1] = cnvFloat32ToCInt16(element.imaginary)
+ }
+ return ret
+}
+
+/// Conver F32 array to S16Q11
+/// - Parameters:
+/// - arr: array of Float data
+/// - Returns: returns array of S16Q11
+///
+func cnvFloat32ToS16Q11(_ arr: [Float32]) -> [Int16] {
+ var ret : [Int16] = Array(repeating: Int16(0), count: arr.count)
+ for i in 0..<arr.count {
+ var element = arr[i]
+ if element > 1.0 {
+ element = 1.0
+ } else if element < -1.0 {
+ element = -1.0
+ }
+ if (element>0.0) {
+ ret[i] = Int16(2048.0*(element))
+ } else {
+ ret[i] = Int16(-2048.0*(-element))
+ }
+ }
+ return ret
+}
+
+
+func cnvFloat32ToS16Q11(_ el: Float32) -> Int16 {
+ var ret:Float32 = el
+ if el > 1.0 {
+ ret = 1.0
+ } else if el < -1.0 {
+ ret = -1.0
+ }
+ if (ret>0.0) {
+ return Int16(2048.0*(ret))
+ }
+ return Int16(2048.0*(ret))
+}
+
+/// Conver F32 array to SC16Q11
+/// - Parameters:
+/// - arr: array of Complex<Float32> data
+/// - Returns: returns array of IQ S16
+///
+func cnvFloat32ToS16Q11(_ arr: [Complex<Float32>]) -> [Int16] {
+ var ret : [Int16] = Array(repeating: Int16(0), count: arr.count*2)
+ for i in 0..<arr.count {
+ let element = arr[i]
+ ret[i*2] = cnvFloat32ToS16Q11(element.real)
+ ret[i*2+1] = cnvFloat32ToS16Q11(element.imaginary)
+ }
+ return ret
+}
+