Last active
February 27, 2023 07:59
-
-
Save TheRealBanana/c9ea0700e3c24184c59e8185678e9108 to your computer and use it in GitHub Desktop.
Correct rtl_power output data due to frequency shift from an upconverter (like Spyverter or Ham-it-up)
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
# This script takes in a csv file created by rtl_power and shifts the frequencies by CONVERTER_SHIFT Hz. | |
# This is needed when using rtl_power through an upconverter to correct the output frequency range in the data before being put through a heatmap generator. | |
import sys, os | |
CONVERTER_SHIFT = -125000000 #ham-it-up | |
#CONVERTER_SHIFT = -120000000 #spyverter | |
OUT_EXTENSION = "shifted" #What extension to prepend to the actual file extension e.g. outdata.shifted.csv | |
#The newfilename is what we wanted to use but it already exists. Just add a number and try again. | |
def get_new_filename(newfilename): | |
while os.access(newfilename, os.F_OK) is True: | |
#Does it have a number already? Would be like filename.shifted.x.csv | |
if newfilename[-5:][0].isnumeric(): | |
addnum = int(newfilename[-5:][0]) + 1 | |
n = -5 | |
else: | |
addnum = 1 | |
n = -3 | |
newfilename = newfilename[:n] + str(addnum) + ".csv" | |
return newfilename | |
# Go line-by-line and write each new line as its made. If we run into a line that is nonconforming we will probably crash or do something dumb. | |
#Format for the csv file is: | |
# date, time, Hz low, Hz high, Hz step, samples, dB, dB, dB, ... | |
def create_new_shifted_csv(origfilename): | |
print("Shifting frequencies in file '%s'" % origfilename) | |
newfilename = origfilename[:-3] + "shifted.csv" | |
#Make sure we're not about to overwrite an existing file and change the name if we are. | |
if os.access(newfilename, os.F_OK): | |
newfilename = get_new_filename(newfilename) | |
with open(newfilename, "w") as newcsvout, open(origfilename, "r") as oldcsvin: | |
curline = oldcsvin.readline() | |
while curline != '': | |
splitline = curline.split(", ") | |
#Add frequency shift to index 2 and 3 (Hz low, Hz high) | |
splitline[2] = str(CONVERTER_SHIFT + int(splitline[2])) | |
splitline[3] = str(CONVERTER_SHIFT + int(splitline[3])) | |
newline = ", ".join(splitline) | |
newcsvout.write(newline) | |
curline = oldcsvin.readline() | |
print("Finished shifting and wrote to new file: '%s'\n" % newfilename) | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
print("Syntax: shift_frequency.py <file1.csv> [file2.csv] ...") | |
print("Adjust frequency shift amount by editing CONVERTER_SHIFT variable on line 6.") | |
#Only accept csv files. | |
filelist = [f for f in sys.argv[1:] if f[-3:].lower() == "csv"] | |
for f in filelist: | |
if not os.access(f, os.R_OK): | |
print("Error trying to read: '%s', skipping file..." % f) | |
continue | |
create_new_shifted_csv(f) | |
print("Done converting file list") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In a gist this time instead of its own repo, will see how well google indexes it.