blob: 16c0ef772e6c3311750adef3c52c09ab18caf26e [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
Ravago Jones5127ccc2022-07-31 16:32:45 -07009
James Kuszmaul7c8aad62018-09-08 18:16:18 -070010def calibrate(fnames):
Ravago Jones5127ccc2022-07-31 16:32:45 -070011 """Do fitting to calibrate ADC data given csv files.
James Kuszmaul7c8aad62018-09-08 18:16:18 -070012
13 CSVs should be of format:
Brian Silverman37a95d62018-11-09 16:08:32 -080014 command_a, command_b, command_c, reading0, reading1, reading2
15 The command columns are the on-time for each timer in FTM ticks.
16 The reading columns in this case are the 3 samples taken from the
17 ADC (with each pair corresponding to the same measurement pre-averaged). We
18 only care about the averaged samples because otherwise the solution matrix
19 can't be solved for in a stable manner.
James Kuszmaul7c8aad62018-09-08 18:16:18 -070020 """
Ravago Jones5127ccc2022-07-31 16:32:45 -070021 data = np.zeros((1, 6))
22 for fname in fnames:
23 data = np.vstack((data, np.genfromtxt(fname, delimiter=',')))
24 data = data[1:, :]
James Kuszmaul7c8aad62018-09-08 18:16:18 -070025
Ravago Jones5127ccc2022-07-31 16:32:45 -070026 data = data[:, :6]
James Kuszmaul7c8aad62018-09-08 18:16:18 -070027
Ravago Jones5127ccc2022-07-31 16:32:45 -070028 b = data[:, 0:3]
29 b = b - np.tile(np.mean(b, axis=1), (3, 1)).T
30 # Vcc / 3000 / R
31 # 3000 converts duty cycle in FTM ticks to fraction of full.
32 b *= 20.9 / 3000.0 / 0.0079
33 A = data[:, 3:]
James Kuszmaul7c8aad62018-09-08 18:16:18 -070034
Ravago Jones5127ccc2022-07-31 16:32:45 -070035 return np.linalg.lstsq(A, b[:])[0].T
36
James Kuszmaul7c8aad62018-09-08 18:16:18 -070037
38if __name__ == "__main__":
Ravago Jones5127ccc2022-07-31 16:32:45 -070039 if len(sys.argv) < 2:
40 print("Need filenames for data")
41 sys.exit(1)
42 print(calibrate(sys.argv[1:]))