blob: 7d882de422d9527fa3ca7a041f72109cc698f858 [file] [log] [blame]
James Kuszmaul7c8aad62018-09-08 18:16:18 -07001#!/usr/bin/python3
2import sys
3import numpy as np
4
5# Note on associated data files:
6# calib_data_60*.csv has each output channel set at a constant value of 60.
Brian Silverman37a95d62018-11-09 16:08:32 -08007# calib_data_6030*.csv actuates two channels.
James Kuszmaul7c8aad62018-09-08 18:16:18 -07008
9def calibrate(fnames):
10 """Do fitting to calibrate ADC data given csv files.
11
12 CSVs should be of format:
Brian Silverman37a95d62018-11-09 16:08:32 -080013 command_a, command_b, command_c, reading0, reading1, reading2
14 The command columns are the on-time for each timer in FTM ticks.
15 The reading columns in this case are the 3 samples taken from the
16 ADC (with each pair corresponding to the same measurement pre-averaged). We
17 only care about the averaged samples because otherwise the solution matrix
18 can't be solved for in a stable manner.
James Kuszmaul7c8aad62018-09-08 18:16:18 -070019 """
Brian Silverman37a95d62018-11-09 16:08:32 -080020 data = np.zeros((1, 6))
James Kuszmaul7c8aad62018-09-08 18:16:18 -070021 for fname in fnames:
22 data = np.vstack((data, np.genfromtxt(fname, delimiter=',')))
23 data = data[1:, :]
24
James Kuszmaul7c8aad62018-09-08 18:16:18 -070025 data = data[:, :6]
26
27 b = data[:, 0:3]
28 b = b - np.tile(np.mean(b, axis=1), (3, 1)).T
29 # Vcc / 3000 / R
Brian Silverman37a95d62018-11-09 16:08:32 -080030 # 3000 converts duty cycle in FTM ticks to fraction of full.
31 b *= 20.9 / 3000.0 / 0.0079
James Kuszmaul7c8aad62018-09-08 18:16:18 -070032 A = data[:, 3:]
33
34 return np.linalg.lstsq(A, b[:])[0].T
35
36if __name__ == "__main__":
37 if len(sys.argv) < 2:
38 print("Need filenames for data")
39 sys.exit(1)
40 print(calibrate(sys.argv[1:]))