summaryrefslogtreecommitdiff
path: root/Radio/Utils/WaterfallFile/main.swift
diff options
context:
space:
mode:
authorArturs Artamonovs <arturs.artamonovs@protonmail.com>2025-01-05 10:07:35 +0000
committerArturs Artamonovs <arturs.artamonovs@protonmail.com>2025-01-05 10:07:35 +0000
commitf923a3824561c6cf200c638e3d44d1cbf4adf7d8 (patch)
treecdf4d935e8be653a7d296d4cd48e06cffd3b376d /Radio/Utils/WaterfallFile/main.swift
parentb999f85d83728bd7034e85f2e038bb9a6454b16a (diff)
downloadPrySDR-f923a3824561c6cf200c638e3d44d1cbf4adf7d8.tar.gz
PrySDR-f923a3824561c6cf200c638e3d44d1cbf4adf7d8.zip
Waterfall: drawing from file initial implementatiom, visual version concept is ready
Diffstat (limited to 'Radio/Utils/WaterfallFile/main.swift')
-rw-r--r--Radio/Utils/WaterfallFile/main.swift113
1 files changed, 113 insertions, 0 deletions
diff --git a/Radio/Utils/WaterfallFile/main.swift b/Radio/Utils/WaterfallFile/main.swift
new file mode 100644
index 0000000..cf72d56
--- /dev/null
+++ b/Radio/Utils/WaterfallFile/main.swift
@@ -0,0 +1,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")