-
-
Save akshmakov/04e300434ad8bd4c31903ed7f8add523 to your computer and use it in GitHub Desktop.
Convert RAW iq samples to SDRAngel "sdriq" format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/env python3 | |
""" | |
Convert raw IQ samples to SDRIQ with header, suitable for batch processing | |
for use with RTL_SDR convert from unsigned 8 bit to signed 16 bit samples. using rtl_433 | |
.cu8 - RTL_SDR Raw samples (collected via rtl_tcp) | |
.cs16 - converted samples for this script (or raw data from a 16 bit SDR) | |
Converting formats: | |
rtl_433 -w output.cs16 input.cu8 | |
## License | |
Changes Copyright 2022 Andrey K. Shmakov | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
## Changelog | |
V3 - Added buffered copy of 128MB for large files where file.read() fails | |
V2 - Changed interface to consume list of files, automatically calculate fout name | |
Added safety and with statements for file operations | |
Added filename extraction for frequency and sample rate | |
ala https://triq.org/pdv/ and recomendations per https://triq.org/rtl_433/IQ_FORMATS.html | |
V1 - https://gist.github.com/kbeckmann/6db25501ff02c46e79143c0ed1c47d88, Unknown License | |
""" | |
import sys | |
import struct | |
import binascii | |
import os | |
COPY_BLOCK_SIZE=128000000 | |
def get_sr(fname): | |
loc = fname.lower().find('sps') | |
if loc != -1: | |
prefix = fname[loc-1] | |
mult = 1 | |
if prefix in ['k','K']: | |
mult = 1000 | |
elif prefix in['m','M']: | |
mult = 1000000 | |
elif not(prefix.isnumeric()): | |
print("Unknown prefix in sample rate: ", prefix) | |
return False | |
i = 1 if prefix.isnumeric() else 2 | |
while fname[loc-i].isnumeric(): | |
i = i+1 | |
num = fname[loc - i + 1: loc if prefix.isnumeric() else loc-1] | |
return int(num)*mult | |
else: | |
return False | |
def get_f(fname): | |
loc = fname.lower().find('hz') | |
if loc != -1: | |
prefix = fname[loc-1] | |
mult = 1 | |
if prefix in ['k','K']: | |
mult = 1000 | |
elif prefix in['m','M']: | |
mult = 1000000 | |
elif not(prefix.isnumeric()): | |
print("Unknown prefix in frequency: ", prefix) | |
return False | |
i = 1 if prefix.isnumeric() else 2 | |
while fname[loc-i].isnumeric() or fname[loc-i] == '.': | |
i = i+1 | |
num = fname[loc - i + 1: loc if prefix.isnumeric() else loc-1] | |
return int(float(num)*mult) | |
else: | |
return False | |
# See https://github.com/f4exb/sdrangel/tree/master/plugins/samplesource/filesource | |
for ifname in sys.argv[1:]: | |
ofname = os.path.splitext(ifname)[0] + ".sdriq" | |
if not(os.path.exists(ifname)) or not(os.path.isfile(ifname)): | |
print (ifname, " either doesn't exist or is not a file") | |
continue | |
with open(ifname, 'rb') as fin: | |
print ("Reading: ", ifname) | |
data = fin.read(COPY_BLOCK_SIZE) | |
w = bytearray() | |
sr = get_sr(ifname) | |
if not(sr): | |
print("Invalid SR for :", ifname) | |
continue | |
else: | |
print("SR is ", sr) | |
fr = get_f(ifname) | |
if not(fr): | |
print("Invalid frequency for:", ifname) | |
continue | |
else: | |
print("Frequency is ", fr) | |
w += struct.pack("<I", sr) | |
w += struct.pack("<Q", fr) # Center freq Hz | |
w += struct.pack("<Q", 0) # Timestamp | |
w += struct.pack("<I", 16) # Sample size 16 or 24 ? | |
w += struct.pack("<I", 0) # Padding | |
with open(ofname, 'wb') as fout: | |
print ("Writing :", ofname) | |
fout.write(w) | |
fout.write(struct.pack("<I", binascii.crc32(w))) | |
cntr = len(data) | |
while(data): | |
fout.write(data) | |
data = fin.read(COPY_BLOCK_SIZE) | |
cntr = cntr + len(data) | |
print("Data Bytes Copied: ", cntr) | |
# Original Code - Konrad Beckmann https://gist.github.com/kbeckmann/6db25501ff02c46e79143c0ed1c47d88 | |
#data = open(sys.argv[1], "rb").read() | |
#out = open(sys.argv[2], "wb") | |
#w = bytearray() | |
#w += struct.pack("<I", 3200000) # Sample rate | |
#w += struct.pack("<Q", 1280000000) # Center freq Hz | |
#w += struct.pack("<Q", 0) # Timestamp | |
#w += struct.pack("<I", 16) # Sample size 16 or 24 ? | |
#w += struct.pack("<I", 0) # Padding | |
#out.write(w) | |
#out.write(struct.pack("<I", binascii.crc32(w))) # CRC32 | |
#out.write(data) |
for i in *.cu8; do rtl_433 -w "${i%.cu8}.cs16" $i; done
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use with RTL SDR Samples, convert from unnsigned 8 bits to signed 16 bits using rtl_433
Script is intended to be used with rtl_433 filename based metadata(https://triq.org/rtl_433/IQ_FORMATS.html#file-name-meta-data)
rtl_433 -w output_300ksps_100MHZ.cs16 input_300ksps_100MHZ.cu8
convert with this script
./ConvertRawToSDRAngel.py output_300ksps_100MHZ.cs16
open
output_300ksps_100MHZ.sdriq
in SDRAngel and confirm the metadata.