summaryrefslogtreecommitdiff
path: root/IQ/IQUtils.swift
blob: 1a3f4b498a1cbc0b8e5a8da89c016195e71c3e86 (plain) (blame)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//
//  IQUtils.swift
//  PrySDR
//
//  Created by Jacky Jack on 21/01/2025.
//

import Accelerate
import ComplexModule

/// 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("!!!!")
    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
    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)
}

/// Convert 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
}

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
///
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
}

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
/// - 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
}

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
/// - 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
}

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
}