fix fft x-axis
This commit is contained in:
@@ -36,7 +36,7 @@ method to coped with the corresponding problem:
|
||||
import os.path
|
||||
import sys
|
||||
from collections import Counter
|
||||
from typing import List, Optional, Dict, Set, Any, Union
|
||||
from typing import List, Optional, Dict, Set, Any
|
||||
|
||||
|
||||
class Options:
|
||||
@@ -44,8 +44,9 @@ class Options:
|
||||
self.skip = False
|
||||
self.round = 2
|
||||
self.overflow_pow: Optional[int] = None
|
||||
self.graph: Union[bool, str] = False
|
||||
self.graph: Optional[str] = None
|
||||
self.graph_log_scale = False
|
||||
self.graph_save_path: Optional[str] = None
|
||||
|
||||
|
||||
class Result:
|
||||
@@ -61,8 +62,8 @@ class Result:
|
||||
|
||||
# analysis data
|
||||
self.data_count = 0
|
||||
self.time_delta_counter: Dict[float, int] = Counter()
|
||||
self.value_delta_counter: Dict[int, int] = Counter()
|
||||
self.time_delta_counter = Counter()
|
||||
self.value_delta_counter = Counter()
|
||||
|
||||
# analysis options
|
||||
self.round = options.round
|
||||
@@ -105,6 +106,7 @@ class Result:
|
||||
self._prev_time = time
|
||||
self.channel.add(channel)
|
||||
self._prev_value = value
|
||||
self.total_duration = time
|
||||
|
||||
def close(self):
|
||||
self._close = True
|
||||
@@ -221,14 +223,73 @@ def print_result(result: Result, options: Options):
|
||||
|
||||
print('time_delta')
|
||||
print_table(result.time_delta_counter, '%10.2f')
|
||||
print()
|
||||
|
||||
print('value_delta')
|
||||
print_table(result.value_delta_counter)
|
||||
|
||||
print()
|
||||
|
||||
# packet miss rate
|
||||
|
||||
def paint_result(result: Result, options: Options):
|
||||
#
|
||||
if options.graph == 'ramp':
|
||||
delta_value = list(filter(lambda it: it > 0, result.value_delta_counter))
|
||||
total = sum(map(lambda it: it * result.value_delta_counter[it], delta_value))
|
||||
miss = sum(map(lambda it: (it - 1) * result.value_delta_counter[it], delta_value))
|
||||
print('value_miss (ramp)', '%d/%d' % (miss, total), '%.2f%%' % (100 * miss / total))
|
||||
print()
|
||||
|
||||
paint_ramp_result(result, options)
|
||||
|
||||
elif options.graph == 'fft':
|
||||
paint_fft_result(result, options)
|
||||
|
||||
|
||||
def paint_fft_result(result: Result, options: Options):
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from scipy.interpolate import interp1d
|
||||
from scipy.fftpack import fft
|
||||
|
||||
time_seq = []
|
||||
value_seq = []
|
||||
|
||||
with open(result.data_file) as _file:
|
||||
for line in _file:
|
||||
line = line.strip()
|
||||
|
||||
if len(line) == 0 or line.startswith('#'):
|
||||
continue
|
||||
|
||||
else:
|
||||
part = line.split(' ', maxsplit=3)
|
||||
time_seq.append(float(part[0]))
|
||||
value_seq.append(int(part[2]))
|
||||
|
||||
sample_rate = result.sample_rate
|
||||
time_delta = 1000 / sample_rate
|
||||
total_duration = result.total_duration
|
||||
total_time_step = int(total_duration / time_delta)
|
||||
|
||||
f = interp1d(time_seq, value_seq, copy=False)
|
||||
|
||||
t = np.linspace(0, total_duration, total_time_step)
|
||||
|
||||
fft_result = fft(f(t))
|
||||
fft_result = fft_result[:len(fft_result) // 2]
|
||||
fft_x = np.linspace(0, 1000 * total_time_step / (2 * total_duration), len(fft_result))
|
||||
fft_y = abs(fft_result)
|
||||
fft_sum = sum(fft_y)
|
||||
|
||||
plt.plot(np.log10(fft_x), fft_y / fft_sum)
|
||||
|
||||
if options.graph_save_path is not None:
|
||||
plt.savefig(options.graph_save_path, dpi=600)
|
||||
else:
|
||||
plt.show()
|
||||
|
||||
|
||||
def paint_ramp_result(result: Result, options: Options):
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
fig, (at, av) = plt.subplots(2, 1)
|
||||
@@ -254,8 +315,8 @@ def paint_result(result: Result, options: Options):
|
||||
|
||||
fig.tight_layout()
|
||||
|
||||
if isinstance(options.graph, str):
|
||||
plt.savefig(options.graph, dpi=600)
|
||||
if options.graph_save_path is not None:
|
||||
plt.savefig(options.graph_save_path, dpi=600)
|
||||
else:
|
||||
plt.show()
|
||||
|
||||
@@ -270,8 +331,11 @@ def print_help():
|
||||
print(' --skip : skip if FILE not found')
|
||||
print(' --round VALUE : floating number round')
|
||||
print(' --overflow VALUE : overflow number. 2^VALUE')
|
||||
print(' --graph[=FILE] : use matplotlib to generate graph')
|
||||
print(' --graph[=TYPE] : use matplotlib to generate graph. could be:')
|
||||
print(' ramp : ramp data, calculate value miss')
|
||||
print(' fft : sin wave data, calculate fft')
|
||||
print(' --graph-log-scale :')
|
||||
print(' --graph-save PATH : save graph file path')
|
||||
print()
|
||||
print('ARGUMENTS:')
|
||||
print(' FILE : txt data file')
|
||||
@@ -325,8 +389,8 @@ def main(argv: List[str]):
|
||||
i += 1
|
||||
|
||||
elif arg == '--graph':
|
||||
o.graph = True
|
||||
i += 1
|
||||
o.graph = argv[i + 1]
|
||||
i += 2
|
||||
|
||||
elif arg.startswith('--graph='):
|
||||
o.graph = arg[len('--graph='):]
|
||||
@@ -336,6 +400,14 @@ def main(argv: List[str]):
|
||||
o.graph_log_scale = True
|
||||
i += 1
|
||||
|
||||
elif arg == '--graph-save':
|
||||
o.graph_save_path = argv[i + 1]
|
||||
i += 2
|
||||
|
||||
elif arg.startswith('--graph-save='):
|
||||
o.graph = arg[len('--graph-save='):]
|
||||
i += 1
|
||||
|
||||
elif arg.startswith('-'):
|
||||
raise ValueError('unknown options : ' + arg)
|
||||
|
||||
@@ -352,9 +424,6 @@ def main(argv: List[str]):
|
||||
else:
|
||||
print_result(result, o)
|
||||
|
||||
if o.graph:
|
||||
paint_result(result, o)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv[1:])
|
||||
|
||||
Reference in New Issue
Block a user