1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
}
|