diff options
Diffstat (limited to 'Radio')
| -rw-r--r-- | Radio/Utils/GenSin/main.swift | 194 | ||||
| -rw-r--r-- | Radio/Utils/WaterfallFile/main.swift | 2 | ||||
| -rw-r--r-- | Radio/Utils/iqconvert/main.swift | 117 |
3 files changed, 292 insertions, 21 deletions
diff --git a/Radio/Utils/GenSin/main.swift b/Radio/Utils/GenSin/main.swift new file mode 100644 index 0000000..9f624e2 --- /dev/null +++ b/Radio/Utils/GenSin/main.swift @@ -0,0 +1,194 @@ +// +// main.swift +// GenSin +// +// Created by Jacky Jack on 21/02/2025. +// + +import Foundation +import ArgumentParser +import ComplexModule + +/* +Takes as input +*/ + + +enum OutputFormat { + case FMT_NONE + case FMT_U8 + case FMT_S8 + case FMT_U16 + case FMT_S16 + case FMT_SC16Q11 + case FMT_F32 + case FMT_WAV_INT16 + case FMT_WAV_F32 +} +var complex_type:Bool = false + +print("gen sin") + +//set the command line arguments +struct CommandLineArgs: ParsableCommand { + @Argument var outputFile:String = "" + @Flag(help:"Version \(software_version)") var version: Bool = false + @Flag(name: .shortAndLong) var verbose: Bool = false + @Option(name:.shortAndLong) var samplerate: Int = 1000 + @Option(name:.shortAndLong) var amplitude: Float = 1.0 + @Option(name:.shortAndLong) var freq: Int = 10 + @Option(name:.shortAndLong) var num: Int = 100000 + //@Option(name:.shortAndLong) var type: String = "sine" + @Flag(name: .shortAndLong) var listFormats: Bool = false +} + +let args = CommandLineArgs.parseOrExit() + +if args.version { + print("Software version \(software_version)") + exit(0) +} + +if args.listFormats { + print("Supported output:") + print(" s8,u8 : [DONE]") + print(" cs8,cu8 : [TEST]") + print(" s16,u16 : [DONE]") + print(" cs16,cu16: [TEST]") + print(" s16q11 : [TEST]") + print(" cs16q11 : [TEST]") + print(" f32 : not ") + print(" cf32 : not ") + print(" wav i16, fc32: not") +} + +//outputfile Format +var outputFormat = OutputFormat.FMT_NONE +if let dotIndex = args.outputFile.lastIndex(of: ".") { + let index = args.outputFile.index(dotIndex, offsetBy: 1) + let fileExtension = String(args.outputFile[index..<args.outputFile.endIndex]) + print("\(args.outputFile) extension \(fileExtension)") + let supportedExtensionList = ["s8","cs8","u8","cu8","s16","cs16","u16","cu16","s16q11","cs16q11"] + if supportedExtensionList.contains(fileExtension) { + if ["s8"].contains(fileExtension) { + outputFormat = OutputFormat.FMT_S8 + } else if ["cs8"].contains(fileExtension){ + outputFormat = OutputFormat.FMT_S8 + complex_type = true + } else if ["u8"].contains(fileExtension) { + outputFormat = OutputFormat.FMT_U8 + } else if ["cu8"].contains(fileExtension) { + outputFormat = OutputFormat.FMT_U8 + complex_type = true + } else if ["s16"].contains(fileExtension) { + outputFormat = OutputFormat.FMT_S16 + } else if ["cs16"].contains(fileExtension) { + outputFormat = OutputFormat.FMT_S16 + complex_type = true + } else if ["u16"].contains(fileExtension) { + outputFormat = OutputFormat.FMT_U16 + } else if ["cu16"].contains(fileExtension) { + outputFormat = OutputFormat.FMT_U16 + complex_type = true + } else if ["s16q11"].contains(fileExtension) { + outputFormat = OutputFormat.FMT_SC16Q11 + } else if ["cs16q11"].contains(fileExtension) { + outputFormat = OutputFormat.FMT_SC16Q11 + complex_type = true + } else if ["wav"].contains(fileExtension) { + outputFormat = OutputFormat.FMT_WAV_F32 + } + else { + print("Unknown output format") + } + } +} + +var outdata_s8:[Int8] = [] +var outdata_u8:[UInt8] = [] +var outdata_s16:[Int16] = [] +var outdata_u16:[UInt16] = [] +var outdata_f32:[Float32] = [] +var outdata_cf32:[Complex<Float32>] = [] + +//var outdata_cs8:[Complex<Int8>] = [] +//var outdata_cu8:[UInt8] = [] +//var outdata_cs16:[Int16] = [] +//var outdata_cu16:[UInt16] = [] +//var outdata_cs16q11:[Complex<Int16>] = [] +//var outdata_cf32:[Float32] = [] + +//generate signal +outdata_f32 = genSin(args.samplerate, args.freq, args.amplitude, 0.0, args.num) +outdata_cf32 = genSin(args.samplerate, args.freq, args.amplitude, 0.0, args.num) +if complex_type == false { + if outputFormat == OutputFormat.FMT_S8 { + outdata_s8 = cnvFloat32ToInt8(outdata_f32) + } else if outputFormat == OutputFormat.FMT_U8 { + outdata_u8 = cnvFloat32ToUInt8(outdata_f32) + } else if outputFormat == OutputFormat.FMT_S16 { + outdata_s16 = cnvFloat32ToInt16(outdata_f32) + } else if outputFormat == OutputFormat.FMT_U16 { + outdata_u16 = cnvFloat32ToUInt16(outdata_f32) + } else if outputFormat == OutputFormat.FMT_SC16Q11 { + outdata_s16 = cnvFloat32ToS16Q11(outdata_f32) + } else { + print("Unimplemented output format converter") + } +//complex numbers +} else { + if outputFormat == OutputFormat.FMT_S8 { + outdata_s8 = cnvFloat32ToCInt8(outdata_cf32) + } else if outputFormat == OutputFormat.FMT_U8 { + outdata_u8 = cnvFloat32ToCUInt8(outdata_cf32) + } else if outputFormat == OutputFormat.FMT_S16 { + outdata_s16 = cnvFloat32ToCInt16(outdata_cf32) + } else if outputFormat == OutputFormat.FMT_U16 { + outdata_u16 = cnvFloat32ToCUInt16(outdata_cf32) + } else if outputFormat == OutputFormat.FMT_SC16Q11 { + outdata_s16 = cnvFloat32ToS16Q11(outdata_cf32) + } else { + print("Unimplemented complex output format converter") + } +} + +// write to file +let fileWrite = FileWriter() +var file_ok = false +if (checkIfFileExists(args.outputFile)) { + do { + print("try to delet file for write") + try fileWrite.deleteForWrite(filename: args.outputFile) + } catch { + print("cant delete file \(error)") + exit(1) + } +} else { + do { + print("try to creating file") + try fileWrite.createForWrite(filename: args.outputFile) + } catch { + print("cant create file \(error)") + exit(1) + } +} + +do { + if outputFormat == OutputFormat.FMT_S8 { + try fileWrite.writeAll(outdata_s8) + } else if outputFormat == OutputFormat.FMT_U8 { + try fileWrite.writeAll(outdata_u8) + } else if (outputFormat == OutputFormat.FMT_S16) + || (outputFormat == OutputFormat.FMT_SC16Q11) { + try fileWrite.writeAll(outdata_s16) + } else if outputFormat == OutputFormat.FMT_U16 { + try fileWrite.writeAll(outdata_u16) + } else { + print("Not supported write type") + } +} catch { + print("Cant write to file \(error)") + exit(1) +} + +fileWrite.close() diff --git a/Radio/Utils/WaterfallFile/main.swift b/Radio/Utils/WaterfallFile/main.swift index 7717f1c..dffe0fe 100644 --- a/Radio/Utils/WaterfallFile/main.swift +++ b/Radio/Utils/WaterfallFile/main.swift @@ -34,7 +34,7 @@ do { } var i8_arr:[Int8]? = nil do { - try i8_arr = fileReader.readAll() + try i8_arr = fileReader.readAllInt8() } catch { print("Got error \(error)") } diff --git a/Radio/Utils/iqconvert/main.swift b/Radio/Utils/iqconvert/main.swift index 49db280..b8b5f3b 100644 --- a/Radio/Utils/iqconvert/main.swift +++ b/Radio/Utils/iqconvert/main.swift @@ -11,7 +11,11 @@ import ArgumentParser enum SDRInputFormat { case FMT_NONE case FMT_U8 + case FMT_I16 + case FMT_U16 + case FMT_SC16Q11 case FMT_F32 + case FMT_WAV } //set the command line arguments @@ -27,16 +31,17 @@ let args = CommandLineArgs.parseOrExit() if args.listFormats { print("Supported input:") - print(" u8: RTLSDR [not supported]") - print(" u16: AirSpyHF [not supported]") + print(" u8: RTLSDR ") + print(" u16: AirSpyHF ") print(" u16r: AirSpy [not supported]") - print(" i16: AirSpy [not supported]") + print(" i16: AirSpy ") print(" i16r: AirSpy [not supported]") //int16 real 1 * 16bit per sample - print(" sc16q11: BladeRF [not supported]") - print(" fc32r: AirSpy [not supported]") //float32 real 1 * 32bit per sampl + print(" sc16q11: BladeRF ") + print(" fc32r: AirSpy [not supported]") //float32 real 1 * 32bit per sample + print(" fc32: AirSpy [not supported]") print("Supported output:") - print(" fc32: inspectrum, sdr++ ") - print(" wav: SDR# [not suppoprted]") + print(" fc32: inspectrum ") + print(" wav: SDR#,sdr++ [not suppoprted]") } //var inputFilePath:String @@ -60,10 +65,16 @@ if let dotIndex = args.inputFile.lastIndex(of: ".") { let index = args.inputFile.index(dotIndex, offsetBy: 1) let fileExtension = String(args.inputFile[index..<args.inputFile.endIndex]) print("\(args.inputFile) extension \(fileExtension)") - let supportedExtensionList = ["u8","cu8"] + let supportedExtensionList = ["u8","cu8","i16","ci16","u16","cu16","sc16q11"] if supportedExtensionList.contains(fileExtension) { if ["u8","cu8"].contains(fileExtension) { inputFormat = SDRInputFormat.FMT_U8 + } else if ["i16","ci16"].contains(fileExtension) { + inputFormat = SDRInputFormat.FMT_I16 + } else if ["u16","cu16"].contains(fileExtension) { + inputFormat = SDRInputFormat.FMT_U16 + } else if ["sc16q11"].contains(fileExtension) { + inputFormat = SDRInputFormat.FMT_SC16Q11 } } } @@ -78,29 +89,95 @@ if let dotIndex = args.outputFile.lastIndex(of: ".") { if supportedExtensionList.contains(fileExtension) { if ["f32","fc32"].contains(fileExtension) { outputFormat = SDRInputFormat.FMT_F32 + } else if ["wav"].contains(fileExtension) { + outputFormat = SDRInputFormat.FMT_WAV } } } print("Input file format:\(inputFormat) output file format:\(outputFormat)") -//read from file -//if let data_u8:[UInt8] = fileReader.readAll() { +//read from file and convert to file format + + var data_u8:[UInt8] = [] -do { - data_u8 = try fileReader.readAll() -} catch { - print("Reading file: \(error)") - exit(1) +var data_i16:[Int16] = [] +var data_u16:[UInt16] = [] + +var outdata_f32:[Float32] = [] + +switch inputFormat { +case .FMT_U8: + do { + data_u8 = try fileReader.readAllUInt8() + } catch { + print("Reading file: \(error)") + exit(1) + } + break +case .FMT_U16: + do { + data_u16 = try fileReader.readAllUInt16() + } catch { + print("Reading file: \(error)") + exit(1) + } + break +case .FMT_I16: + do { + data_i16 = try fileReader.readAllInt16() + } catch { + print("Reading file: \(error)") + exit(1) + } + break +case .FMT_SC16Q11: + do { + print("FMT_SC16Q11") + data_i16 = try fileReader.readAllInt16() + } catch { + print("Reading file: \(error)") + exit(1) + } + break +default: + print("Unknow input file format") } //convert to output format -var outdata_f32:[Float32] = [] -if data_u8.count > 0 { - outdata_f32 = cnvU8toFloat32(data_u8) -} else { - print("No data in buffer") +switch (inputFormat) { +case .FMT_U8: + if data_u8.count > 0 { + outdata_f32 = cnvU8toFloat32(data_u8) + } else { + print("No data in buffer") + } + break +case .FMT_U16: + if data_u16.count > 0 { + outdata_f32 = cnvU16toFloat32(data_u16) + } else { + print("No data in buffer") + } + break +case .FMT_I16: + if data_i16.count > 0 { + outdata_f32 = cnvI16toFloat32(data_i16) + } else { + print("No data in buffer") + } + break +case .FMT_SC16Q11: + if data_i16.count > 0 { + print("convert to FMT_SC16Q11") + outdata_f32 = cnvSC16Q11toFloat32(data_i16) + } else { + print("No data in buffer") + } + break +default: + print("Cant convert to unknown format") } // write to file |
