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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
//
// 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 {
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 {
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
}
/// Convert from Int16 to Float, naive implementation for AirSpy
/// - Parameters:
/// - arr: array of Int16
/// - Returns: Return floats -0.996...0.996
///
func cnvI16toFloat32(_ arr: [Int16]) -> [Float32] {
//let iq = IQ(size: 8, bits: 8, sign: false, complex: true)
var out: [Float32] = .init(repeating: 0.0, count: arr.count)
// -32768..32767 -> -0.996 ... 0.996
// -32768..0 -> -0.999 ... 0.0
// 1...32767 -> 0.0 ... 0.999
for i in 0..<arr.count {
out[i] = (Float(arr[i])+0.5)/32768.0
}
return out
}
/// Convert from Int16 to Float, naive implementation for AirSpy
/// - Parameters:
/// - arr: array of Int16
/// - Returns: Return floats -0.996...0.996
///
func cnvU16toFloat32(_ arr: [UInt16]) -> [Float32] {
print("!!!!")
//let iq = IQ(size: 8, bits: 8, sign: false, complex: true)
var out: [Float32] = .init(repeating: 0.0, count: arr.count)
// 0..65535 -> -0.999 ... 0.999
// 0..32767 -> -0.999 ... 0.0
// 32768...65535 -> 0.0 ... 0.999
for i in 0..<arr.count {
out[i] = ((Float(arr[i])+0.5)/32768.0) - 1.0
}
return out
}
/// Convert from Int16 to Float, naive implementation for AirSpy
/// - Parameters:
/// - arr: array of Int16
/// - Returns: Return floats -0.996...0.996
///
func cnvSC16Q11toFloat32(_ 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
// -32768..32767 -> -0.996 ... 0.996
// -32768..0 -> -0.999 ... 0.0
// 1...32767 -> 0.0 ... 0.999
for i in 0..<arr.count {
//out[i] = (Float(arr[i])+0.5)/32768.0
if arr[i] < 0 {
out[i] = Float(arr[i])/2048.0
if out[i] < -1.0 {
cnt_underflow += 1
}
} else {
out[i] = Float(arr[i])/2048.0
if out[i] > 1.0 {
//print("\(arr[i])->\(out[i])")
cnt_overflow += 1
}
}
}
print("overflow: \(cnt_overflow) underflow: \(cnt_underflow)")
return out
}
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
// -32768..32767 -> -0.996 ... 0.996
// -32768..0 -> -0.999 ... 0.0
// 1...32767 -> 0.0 ... 0.999
for i in 0..<arr.count {
let data = arr[i]&0x7FF
let sign = arr[i]&0x800
}
print("overflow: \(cnt_overflow) underflow: \(cnt_underflow)")
return out
}
/// Convert Data stream to Int16 array
/// - Parameters:
/// - arr: array of data
/// - Returns: returns array of Int16
///
func asInt16<Input>(_ input: Input) -> AnySequence<Int16>
where
Input: Sequence,
Input.Element == UInt8
{
let s = sequence(state: input.makeIterator()) { iter -> Int16? in
guard
let b1 = iter.next(),
let b2 = iter.next()
else {
return nil
}
//do we care about endianess at this point?
return Int16(b2) << 8 | Int16(b1)
}
return AnySequence(s)
}
/// Convert Data stream to Unt16 array
/// - Parameters:
/// - arr: array of data
/// - Returns: returns array of Int16
///
func asUInt16<Input>(_ input: Input) -> AnySequence<UInt16>
where
Input: Sequence,
Input.Element == UInt8
{
let s = sequence(state: input.makeIterator()) { iter -> UInt16? in
guard
let b1 = iter.next(),
let b2 = iter.next()
else {
return nil
}
//do we care about endianess at this point?
return UInt16(b2) << 8 | UInt16(b1)
}
return AnySequence(s)
}
/// Convert Data stream to SC16Q11 specific to BladeRF
/// - Parameters:
/// - arr: array of data
/// - Returns: returns array of Int12, -4096...4096
///
func asSC16Q11<Input>(_ input: Input) -> AnySequence<Int16>
where
Input: Sequence,
Input.Element == UInt8
{
let s = sequence(state: input.makeIterator()) { iter -> Int16? in
guard
let b1 = iter.next(),
let b2 = iter.next()
else {
return nil
}
//sign
print(b1)
print(b2)
let sign:UInt16 = (UInt16(b1)&0x8) >> 3
print(sign)
let val:Int16 = Int16(b1&0x7) << 8 | Int16(b2)
//lower bound inclusive [-2048...2047]
if (sign == 1) {
return -1*(val+1)
}
return val
}
return AnySequence(s)
}
/// Conver F32 array to U8
/// - Parameters:
/// - arr: array of Float data
/// - Returns: returns array of UInt8
///
func cnvFloat32ToUInt8(_ arr: [Float32]) -> [UInt8] {
var ret : [UInt8] = Array(repeating: UInt8(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
}
ret[i] = UInt8(127.0*(element+1.0))
}
return ret
}
/// Conver F32 array to I8
/// - Parameters:
/// - arr: array of Float data
/// - Returns: returns array of Int8
///
func cnvFloat32ToInt8(_ arr: [Float32]) -> [Int8] {
var ret : [Int8] = Array(repeating: Int8(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] = Int8(127.0*(element))
} else {
ret[i] = Int8(-128.0*(-element))
}
}
return ret
}
/// Conver F32 array to U16
/// - Parameters:
/// - arr: array of Float data
/// - Returns: returns array of UInt16
///
func cnvFloat32ToUInt16(_ arr: [Float32]) -> [UInt16] {
var ret : [UInt16] = Array(repeating: UInt16(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
}
ret[i] = UInt16(32767*(element+1.0))
}
return ret
}
/// Conver F32 array to I16
/// - Parameters:
/// - arr: array of Float data
/// - Returns: returns array of Int16
///
func cnvFloat32ToInt16(_ 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(32767.0*(element))
} else {
ret[i] = Int16(-32768.0*(-element))
}
}
return ret
}
|