summaryrefslogtreecommitdiff
path: root/IQ
diff options
context:
space:
mode:
authorArturs Artamonovs <arturs.artamonovs@protonmail.com>2025-02-03 22:31:45 +0000
committerArturs Artamonovs <arturs.artamonovs@protonmail.com>2025-02-03 22:31:45 +0000
commit972a6d3e4d3b684fbadeb5cd046a8634ec24eb8c (patch)
treee96c4ce2d649558cc0b7b452d7a64fbe980b65c0 /IQ
parent1e096e55ca30dc80c3faa8d1ea36de13bc90cc6a (diff)
downloadPrySDR-972a6d3e4d3b684fbadeb5cd046a8634ec24eb8c.tar.gz
PrySDR-972a6d3e4d3b684fbadeb5cd046a8634ec24eb8c.zip
iqconvert: converts from u8 to f32 without issuesmain
Diffstat (limited to 'IQ')
-rw-r--r--IQ/IQUtils.swift88
1 files changed, 88 insertions, 0 deletions
diff --git a/IQ/IQUtils.swift b/IQ/IQUtils.swift
new file mode 100644
index 0000000..9d1b048
--- /dev/null
+++ b/IQ/IQUtils.swift
@@ -0,0 +1,88 @@
+//
+// IQUtils.swift
+// PrySDR
+//
+// Created by Jacky Jack on 21/01/2025.
+//
+
+import Accelerate
+
+/// Convert from UInt8 to Float, naive implementation
+/// - Parameters:
+/// - arr: array of UInt8
+/// - Returns: Return floats -0.996...0.996
+///
+func cnvU8toFloat32(_ arr: [UInt8]) -> [Float32] {
+
+ //let iq = IQ(size: 8, bits: 8, sign: false, complex: true)
+
+ var out: [Float32] = .init(repeating: 0.0, count: arr.count)
+
+ // 0..255 -> -0.996 ... 0.996
+ // 0..127 -> -0.996 ... 0.0039
+ // 128...255 -> 0.0039 ... 0.996
+ for i in 0..<arr.count {
+ /*
+ if arr[i] >= 128 {
+ out[i] = Float(arr[i]-128)/128.0
+ } else { //
+ out[i] = Float(Int(arr[i])-128)/128.0
+ }*/
+ out[i] = (Float(arr[i])-127.5)/128.0
+ }
+
+ return out
+}
+
+/// Convert from UInt8 to Float, naive implementation rtlsdr specfic
+/// https://cgit.osmocom.org/gr-osmosdr/tree/lib/rtl/rtl_source_c.cc#n179
+/// - Parameters:
+/// - arr: array of UInt8
+/// - Returns: Return floats -0.996...0.996
+///
+func cnvU8toFloat32_rtlsdr(_ arr: [UInt8]) -> [Float32] {
+
+ //let iq = IQ(size: 8, bits: 8, sign: false, complex: true)
+
+ var out: [Float32] = .init(repeating: 0.0, count: arr.count)
+
+ // 0..255 -> -1.0 ... 1.0
+ for i in 0..<arr.count {
+ /*
+ if arr[i] >= 128 {
+ out[i] = Float(arr[i]-128)/128.0
+ } else { //
+ out[i] = Float(Int(arr[i])-128)/128.0
+ }*/
+ out[i] = (Float(arr[i])-127.4)/128.0
+ }
+
+ return out
+}
+
+
+/// Convert from UInt8 to Float, using Accel framework
+/// - Parameters:
+/// - arr: array of UInt8
+/// - Returns: Return floats -1.0...1.0
+///
+func cnvU8toFloat32_accel(_ arr: [UInt8]) -> [Float32] {
+ var out: [Float32] = .init(repeating: 0.0, count: arr.count)
+ vDSP.convertElements(of: arr, to: &out)
+ out = vDSP.add(-127.5, out)
+ out = vDSP.divide(out, 128)
+ return out
+}
+
+/// Convert from UInt8 to Float, using Accel framework specific to rtlsdr
+/// - Parameters:
+/// - arr: array of UInt8
+/// - Returns: Return floats -1.0...1.0
+///
+func cnvU8toFloat32_accel_rtlsdr(_ arr: [UInt8]) -> [Float32] {
+ var out: [Float32] = .init(repeating: 0.0, count: arr.count)
+ vDSP.convertElements(of: arr, to: &out)
+ out = vDSP.add(-127.4, out)
+ out = vDSP.divide(out, 128)
+ return out
+}