-
Notifications
You must be signed in to change notification settings - Fork 9
/
optimize.py
79 lines (66 loc) · 2.85 KB
/
optimize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Find the best high-gain and low-gain value for a FD bitstream data
#
# This program uses Monte-Carlo optimization method to find the optimal value
# You can visualize the result in 3D scatter plot with 'result_viewer.py'
import argparse
import random
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from floppylib import *
def main(args):
high_gain_range = eval(args.high_gain_range)
low_gain_range = eval(args.low_gain_range)
print('High gain = {}, Low gain = {}'.format(high_gain_range, low_gain_range))
bs = bitstream(args.input)
x = []
y = []
z = []
best_sector = 0
best_error = 99999999
with open(args.output, 'a') as f:
str = 'High Low Read Error'
print(str)
f.write(str+'\n')
f.flush()
for i in range(args.iter):
total_sector = 0
total_error = 0
hg = random.uniform(0, 0.08)
lg = random.uniform(0, hg)
for track_id in bs.disk:
track_data = bs.disk[track_id] # pulse interval buffer
track, sec_read, sec_err = read_all_sectors(track_data, clk_spd=4e6, high_gain=hg, low_gain=lg, log_level=0)
trk, sid = track_id.split('-')
total_sector += sec_read
total_error += sec_err
x.append(hg)
y.append(lg)
z.append(total_error)
updated = False
if total_sector > best_sector:
best_sector = total_sector
updated = True
if total_error < best_error:
best_error = total_error
updated = True
str = '{:10.8f} {:10.8f} {:04} {:04} {}'.format(hg, lg, total_sector, total_error, '*' if updated else ' ')
print(str)
f.write(str+'\n')
f.flush()
"""
fig = plt.figure()
ax = Axes3D(fig)
ax.set_xlabel('high gain')
ax.set_ylabel('low_gain')
ax.plot(x, y, z, marker='o', linestyle='None')
plt.show()
"""
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', type=str, required=True, help='input bitstream file path')
parser.add_argument('-o', '--output', type=str, required=False, default='log.txt', help='optimizer output log file path. output will be appended if the log file is already existing (default=log.txt)')
parser.add_argument('--high_gain_range', type=str, required=False, default='(0., 0.4)', help='high gain range (default = (0, 0.4))')
parser.add_argument('--low_gain_range', type=str, required=False, default='(0., 0.4)', help='low gain range (default = (0, 0.4))')
parser.add_argument('--iter', type=int, default=1000, required=False, help='number of iteration (default=1000)')
args = parser.parse_args()
main(args)