summaryrefslogtreecommitdiff
path: root/Radio/Utils/WaterfallFile/main.swift
blob: cf72d5677d5289251f68ffabceb6da6e6e4e73b4 (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
//
//  main.swift
//  WaterfallFile
//
//  Created by Jacky Jack on 23/12/2024.
//

import Foundation
import Accelerate

print("Read binary file")

let input_filename="rtlsdr_100M_m.cu8"


//get data from u8 file
let dir = getCurrentExecutableDir()
let filemgr = FileManager.default

if !filemgr.fileExists(atPath: input_filename) {
    print("Cant find file \(input_filename)")
    exit(0)
}

let fileHandle: FileHandle? = FileHandle(forReadingAtPath: dir+"/"+input_filename)
var i8_arr:[Int8]? = nil
if let file = fileHandle {
    file.seek(toFileOffset: 0)
    do {
        if let databuf = try file.readToEnd() {
            
            print("read \(databuf.count) \(databuf) bytes")
            //let temp_arr = [Int8](databuf)
            i8_arr = [Int8](repeating: 0, count: databuf.count)
            
            for i in 0..<i8_arr!.count {
                print(String(format:"%02X", databuf[i]),terminator: "")
                //convert from 0..255 to -127..128
                let val = databuf[i]
                if val <= 127 {
                    i8_arr![i] = Int8(val)-127
                } else {
                    i8_arr![i] = Int8(val-128)
                }
                //print(String(format:"%02X", i8_arr![i]),terminator: "")
            }
        }
    } catch {
        print(error)
        exit(0)
    }
    
}
fileHandle?.closeFile()

//all data in buffer lets process data
//convert all u8 data to float's

//is there other ways to do that?
/*for i in 0..<dataFloat.count {
    dataFloat[i] = Float(u8_arr![i])
}*/
//will generate 512x512 image
let sampleCount = 512
let img = SimpleImage(width: sampleCount, height: sampleCount)
let number_of_lines = (i8_arr!.count/sampleCount)
let forwardDCT = vDSP.DCT(count: sampleCount, transformType: .II)!
var frequencyDomain:[Float] = []
var transform_result:[Float] = .init(repeating: 0.0, count: sampleCount)
var dataFloat = [Float](repeating: 0.0, count: sampleCount)
for i in 0..<sampleCount {
    let sample_idx = i*sampleCount
    if i < number_of_lines {
        let processingSlice = i8_arr![sample_idx...sample_idx+512-1]
        let processingArray = Array(processingSlice)
        vDSP.convertElements(of: processingArray, to: &dataFloat)
        //dataFloat = vDSP.add(127.0, dataFloat)
        //print(dataFloat)
        
        //move from -127.0 to 128.0 range -1.0...1.0
        //var adjusted = vDSP.divide(dataFloat, Float(sampleCount))
        var adjusted = dataFloat
        //print(adjusted)
        
        transform_result = forwardDCT.transform(adjusted)
        transform_result = vDSP.absolute(transform_result)
        let max = vDSP.maximum(transform_result)
        //print("max=\(max)")
        //frequencyDomain.append(contentsOf: transform_result)
        img.drawPalletLine(line: i, pixelLine: transform_result)
        //print(transform_result)
    } else {
        img.drawPalletLine(line: i, pixelLine: transform_result)
    }
    
}

img.drawPixel(0, 0, PixelData(a: 255, r: 255, g: 0, b: 0))
img.drawPixel(sampleCount-1, 0, PixelData(a: 255, r: 0, g: 255, b: 0))
img.drawPixel(0, sampleCount-1, PixelData(a: 255, r: 0, g: 255, b: 0))
img.drawPixel(sampleCount-1, sampleCount-1, PixelData(a: 255, r: 0, g: 0, b: 255))


//print(dataFloat)



//output data to image file
img.saveAsJPEG("fft_100m_512.jpeg")



print("All computations are done")