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/Graph_Smooth.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
70 lines (61 sloc)
1.68 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
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('<file_name> <box_pts> <times>') | |
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() |