Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
NFC-MITM/NFC_Measurement.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
108 lines (92 sloc)
2.78 KB
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
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.") |