diff --git a/Graph_Smooth.py b/Graph_Smooth.py new file mode 100644 index 0000000..55b5026 --- /dev/null +++ b/Graph_Smooth.py @@ -0,0 +1,70 @@ +import numpy as np +import matplotlib.pyplot as plt +import sys + +def get_args(): + # Get arguments or exit on error + try: + file_name = sys.argv[1] + box_pts = int(sys.argv[2]) + times = int(sys.argv[3]) + except: + print('Not enouth arguments supplied!') + print(' ') + quit() + return file_name, box_pts, times + +def get_data(file_name): + global x, y + # Open file and get data + f = open(file_name, 'r') + file_lines = f.readlines()[1:-1] + + # Get data lists from file's lines + for line in file_lines: + line = str(line) + x.append(float(line.split(';')[0])) + y.append(float(line.split(';')[1].rsplit()[0])) + +def smooth(data, box_pts, times): + # Smoothen the y axis data + for _ in range(times): + box = np.ones(box_pts)/box_pts + data = np.convolve(data, box, mode='same') + return data + +def generate_graph(x,y,y_smooth): + # Set up the graph + plt.clf() + plt.grid(True) + plt.xlabel("Time [s]") + plt.ylabel("Power [dB]") + # Draw the graph + plt.plot(x, y, lw=1, label="Original") + plt.plot(x, y_smooth, 'r-', lw=2, label="Smoothened") + plt.legend(loc="upper left") + # Show the graph + plt.show() + +if __name__ == "__main__": + # Initiate data lists + x = [] + y = [] + + # Get arguments + file_name, box_pts, times = get_args() + # Check if file name is supplied in argument + try: + file_name = sys.argv[1] + except: + print('No file was specified!') + quit() + + # Generate data + get_data(file_name) + # Smoothen the data + y_smooth = smooth(y, box_pts, times) + # Generate a graph + generate_graph(x,y,y_smooth) + + quit() \ No newline at end of file diff --git a/NFC_Measurement.py b/NFC_Measurement.py new file mode 100644 index 0000000..218e936 --- /dev/null +++ b/NFC_Measurement.py @@ -0,0 +1,108 @@ +from matplotlib import pyplot as plt +import matplotlib.animation as animation +import time +from rtlsdr import RtlSdr +import numpy as np +import datetime +from multiprocessing import Process, freeze_support + + +def find_nearest(array, value): + # Finding nearest value + array = np.asarray(array) + idx = (np.abs(array - value)).argmin() + return idx + +def create_file(graph_x, graph_y): + # Create a file with scan data + filename = "output/Scan-" + datetime.datetime.now().strftime("%y%m%d%H%M%S") + f = open(filename+".csv", "w+") + f.write("Timestamp;Power\n") + for i in range(len(graph_x)): + f.write(str(graph_x[i]-graph_x[0]) + ";" + str(graph_y[i]) + "\n") + f.close() + print("\nSaved scan as " + filename + ".csv") + +def generate_graph(graph_x, graph_y): + # SHow a graph with supplied data + create_file(graph_x, graph_y) + plt.clf() + plt.grid(True) + plt.xlabel("Time [s]") + plt.ylabel("Power [dB]") + plt.plot(graph_x,graph_y) + plt.show() + quit() + +def gather_data(): + # Initiate global variables + global x, y + global counter + global graph_x, graph_y + global temp_x, graph_y + + # Keeping length of arrays below 2000 + if len(x) > 500: + x = x[1:] + y = y[1:] + + # Gathering samples, editing them and appending them to main data arrays + samples = sdr.read_samples(1024) + data = plt.psd(samples, NFFT=1024, Fs=sdr.sample_rate/1e6, Fc=sdr.center_freq/1e6) + sample_index = find_nearest(data[1], frequency) + data_x = time.perf_counter() + data_y = data[0][sample_index] + x.append(data_x) + y.append(data_y) + + # Start gathering graph data if power threshold is passed + if counter == 0 and data_y > 2.25: + counter += 1 + + # Gather 200 data samples after initial scan + if 0 < counter < 200: + graph_x.append(data_x) + graph_y.append(data_y) + counter += 1 + + # Get last 400 samples and start a process to generate graph + if counter == 200: + graph_x = x[-400:] + graph_y = y[-400:] + counter = -400 + th = Process(target=generate_graph, args=(graph_x,graph_y)) + th.start() + + # Delaying start of data gathering for reader to stabilize + if counter < 0: + counter += 1 + print(str(counter)) + +if __name__ == "__main__": + # Initiate line data + x = [] + y = [] + + # Initiate variables + counter = -200 + graph_x = [] + graph_y = [] + freeze_support() + + # Initiate frequencies variables + frequency = 27.118070 + center_freq = 27118070 + + # Tuner configuration + sdr = RtlSdr() + sdr.sample_rate = 2.4e6 + sdr.center_freq = center_freq + sdr.freq_correction = 1 + sdr.gain = 17.9 + + # Main loop + try: + while(1): + gather_data() + except KeyboardInterrupt: + print("Terminating.") \ No newline at end of file