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

import Foundation
import Accelerate
import ArgumentParser

//set the command line arguments
struct CommandLineArgs: ParsableCommand {
    @Argument var inputFile:String = ""
    @Argument var outputFile:String = ""
}

let args = CommandLineArgs.parseOrExit()

print("Read binary file")

let input_filename=args.inputFile


//get data from u8 file
let fileReader = FileReader()
do {
    try fileReader.open(filename: input_filename)
} catch {
    print("Cant open file \(input_filename)")
    exit(0)
}
var i8_arr:[Int8]? = nil
do {
    try i8_arr = fileReader.readAll()
} catch {
    print("Got error \(error)")
}

fileReader.close()

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

//is there other ways to do that?
//will generate 512x512 image
let sampleCount = 512
let img = SimpleImage(width: sampleCount, height: sampleCount)
let number_of_lines = (i8_arr!.count/sampleCount)
var frequencyDomain:[Float] = []
var transform_result:[Float] = .init(repeating: 0.0, count: sampleCount)
let fft512 = NaiveFFT512()

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)
        transform_result = fft512.computeLine(processingArray)
        
        //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(args.outputFile)

print("All computations are done")