Last active
April 20, 2019 22:05
-
-
Save asterix24/99e612279dce06879e214e5ae99f4bf7 to your computer and use it in GitHub Desktop.
Simple utility to plot raw data, gaussian and fft.
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
#!/usr/bin/env python3 | |
from optparse import OptionParser | |
import pylab | |
import sys | |
import numpy | |
import scipy.stats as stats | |
from scipy.fftpack import fft, fftfreq | |
parser = OptionParser() | |
parser.add_option("--ylimit-up", dest="y_up", | |
default=None, help="Y axis upper limts") | |
parser.add_option("--ylimit-down", dest="y_down", | |
default=None, help="Y axis lower limits") | |
parser.add_option("--xlimit-up", dest="x_up", | |
default=None, help="X axis upper limts") | |
parser.add_option("--xlimit-down", dest="x_down", | |
default=None, help="X axis lower limits") | |
parser.add_option("-f", "--data-file", dest="data_file", | |
default=None, help="Data file") | |
parser.add_option("-n", "--module-avg", dest="module_avg", action="store_true", | |
default=False, help="Plot normalizided samples.") | |
parser.add_option("-i", "--histogram-graph", dest="histogram_graph", | |
action="store_true", default=False, | |
help="Plot histogram graph.") | |
parser.add_option("-b", "--histogram-bins", dest="histogram_bins", | |
action="store_true", default=False, | |
help="Histogram graph bins.") | |
parser.add_option("-g", "--gaussian-curve", dest="gaussian_curve", | |
action="store_true", default=False, | |
help="Plot standard devision of given samples.") | |
parser.add_option("-s", "--show-limits", dest="show_limits", | |
action="store_true", default=False, | |
help="Show avg and sigma limtis on gaussian curve") | |
parser.add_option("-t", "--fft-graph", dest="fft_graph", | |
action="store_true", default=False, | |
help="Plot Fast Fourier Transformar of given samples.") | |
parser.add_option("-p", "--fft-sampling-rate", dest="fft_sampling_rate", | |
default="96000.0", help="Sample rate frequency for fft [Hz]") | |
parser.add_option("-x", "--fft-test", dest="fft_test", | |
action="store_true", default=False, | |
help="Similate sample data to test fft graph plot.") | |
(options, args) = parser.parse_args() | |
if options.data_file is None: | |
print("%s -f <file.csv> [colum]" % sys.argv[0]) | |
print(parser.print_help()) | |
sys.exit(1) | |
select = False | |
if args: | |
select = True | |
data = [[] for i in range(30)] | |
with open(options.data_file, "r") as f: | |
for j in f: | |
l = j.split(";") | |
for k in range(len(l)): | |
try: | |
data[k].append(float(l[k])) | |
except ValueError: | |
# print ">>>", l[k], | |
continue | |
t = "" | |
if options.module_avg: | |
for m, col in enumerate(data): | |
if len(col) > 0: | |
avg = sum(col)/float(len(col)) | |
print(len(col), avg) | |
if avg > 0: | |
data[m] = map(lambda x: x/avg, col) | |
t += "Column=%s, Avg=%s, Sample=%s\n" % (m, avg, len(col)) | |
plot_col = range(len(data)) | |
if select: | |
plot_col = map(int, args) | |
label_hand = [] | |
for m in plot_col: | |
avg = numpy.mean(data[m]) | |
sigma = numpy.std(data[m], axis=0) | |
s2 = sigma*sigma | |
s_min = min(data[m]) | |
s_max = max(data[m]) | |
rms = numpy.sqrt(sum([x*x for x in data[m]]) / len(data[m])) | |
print("min[%s] max[%s] sg[%s] rms[%s] avg[%s]" % | |
(s_min, s_max, sigma, rms, avg)) | |
if options.gaussian_curve: | |
if len(data[m]) == 0: | |
continue | |
x = numpy.linspace(avg - 4*sigma, avg + 4*sigma, 1000) | |
print("sigma=%s, sigma^2=%s, avg=%s" % (sigma, s2, avg)) | |
print("Min=%s Max=%s" % (s_min, s_max)) | |
print("min=%s " % round(s_min, 3)) | |
print("max=%s " % round(s_max, 3)) | |
print("diff=%s " % (round(s_max, 3)-round(s_min, 3))) | |
print("RMS=%s " % round(rms, 3)) | |
print("Campioni %s" % len(data[m])) | |
y_max = stats.norm.pdf(x, avg, sigma) | |
pylab.plot(x, y_max, label=r'c%s,$\mu:%s\,\sigma:%s\,6\sigma:%s$ ' % | |
(m, round(avg, 2), round(sigma, 2), round(6*sigma, 2))) | |
if options.show_limits: | |
pylab.plot(numpy.linspace(avg + sigma, avg + sigma, 100), | |
numpy.linspace(0, max(y_max), 100), linewidth=1.5, | |
label='$+\sigma:%s$' % m) | |
pylab.plot(numpy.linspace(avg - sigma, avg - sigma, 100), | |
numpy.linspace(0, max(y_max), 100), linewidth=1.5, | |
label='$-\sigma:%s$' % m) | |
pylab.plot(numpy.linspace(avg, avg, 100), | |
numpy.linspace(0, max(y_max), 100), linewidth=2, | |
color="green", label='$\mu:%s$' % m) | |
pylab.title("Gaussian") | |
elif options.fft_graph: | |
T = 1.0 / float(options.fft_sampling_rate) | |
N = len(data[m]) | |
y = data[m] | |
if options.fft_test: | |
print("FFT Test mode.") | |
N = 1000 | |
T = 1.0/1000.0 | |
x = numpy.linspace(0.0, N*T, N) | |
y = numpy.sin(50.0 * 2.0*numpy.pi*x) + 0.5 * \ | |
numpy.sin(80.0 * 2.0*numpy.pi*x) | |
print("Sample Freq %s" % options.fft_sampling_rate) | |
print("Sample Number %s" % N) | |
yf = fft(y) | |
xf = numpy.linspace(0.0, 1.0/(2.0*T), N/2) | |
pylab.plot(xf[1:], 2.0/N * numpy.abs(yf[1:int(N/2)]), label='f%s' % m) | |
pylab.title("FFT") | |
elif options.histogram_graph: | |
bins = None | |
if options.histogram_bins is not None: | |
bins = sorted(data[options.histogram_bins]) | |
n, bins, patches = pylab.hist(data[m], bins=bins, density=True, facecolor='g', alpha=0.75) | |
pylab.xlabel('Smarts') | |
pylab.ylabel('Probability') | |
pylab.title('Histogram') | |
pylab.text(60, .025, r'$\mu=%s,\ \sigma=%s$' % (avg, sigma)) | |
else: | |
# Plot raw data | |
pylab.title("Plot graph") | |
l, = pylab.plot(data[m], label='C%s' % m) | |
label_hand.append(l) | |
pylab.legend() | |
if len(label_hand) != 0: | |
pylab.legend(label_hand) | |
if options.x_up is not None: | |
pylab.xlim(xmax=float(options.x_up)) | |
if options.x_down is not None: | |
pylab.xlim(xmin=float(options.x_down)) | |
if options.y_up is not None: | |
pylab.ylim(ymax=float(options.y_up)) | |
if options.y_down is not None: | |
pylab.ylim(ymin=float(options.y_down)) | |
pylab.grid() | |
pylab.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment