James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | import sys |
| 3 | import 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. |
| 7 | # calib_data_6030.csv actuates two channels. |
| 8 | |
| 9 | def calibrate(fnames): |
| 10 | """Do fitting to calibrate ADC data given csv files. |
| 11 | |
| 12 | CSVs should be of format: |
| 13 | command_a, command_b, command_c, adc0, adc0, adc1, adc2, adc1, adc2 |
| 14 | Where The adc columns in this case are the 6 samples taken from the |
| 15 | ADC where each pair of columns with the same name correspond with |
| 16 | the same measurement (we average samples that are of the same value |
| 17 | because otherwise the solution matrix can't be solved for in a stable |
| 18 | manner). |
| 19 | """ |
| 20 | data = np.zeros((1, 9)) |
| 21 | for fname in fnames: |
| 22 | data = np.vstack((data, np.genfromtxt(fname, delimiter=','))) |
| 23 | data = data[1:, :] |
| 24 | |
| 25 | if data.shape[1] == 9: |
| 26 | data[:, 3] = (data[:, 3] + data[:, 4]) / 2.0 |
| 27 | data[:, 4] = (data[:, 5] + data[:, 7]) / 2.0 |
| 28 | data[:, 5] = (data[:, 6] + data[:, 8]) / 2.0 |
| 29 | data = data[:, :6] |
| 30 | |
| 31 | b = data[:, 0:3] |
| 32 | b = b - np.tile(np.mean(b, axis=1), (3, 1)).T |
| 33 | # Vcc / 3000 / R |
| 34 | b *= 30.8 / 3000.0 / 0.0084 |
| 35 | A = data[:, 3:] |
| 36 | |
| 37 | return np.linalg.lstsq(A, b[:])[0].T |
| 38 | |
| 39 | if __name__ == "__main__": |
| 40 | if len(sys.argv) < 2: |
| 41 | print("Need filenames for data") |
| 42 | sys.exit(1) |
| 43 | print(calibrate(sys.argv[1:])) |