shaper_calibrate: Reworked multi-file shaper autocalibration

Signed-off-by: Dmitry Butyugin <dmbutyugin@google.com>
This commit is contained in:
Dmitry Butyugin authored and KevinOConnor committed 2025-11-21 17:48:56 -05:00
1 parent b4c7cf4a33
commit 2aff840f68
4 files changed
+265 -171

No files matched your search

+77 -72
View File
@@ -2,10 +2,10 @@
# Generate adxl345 accelerometer graphs
#
# Copyright (C) 2020 Kevin O'Connor <kevin@koconnor.net>
# Copyright (C) 2020 Dmitry Butyugin <dmbutyugin@google.com>
# Copyright (C) 2020-2025 Dmitry Butyugin <dmbutyugin@google.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import importlib, optparse, os, sys
import csv, importlib, optparse, os, sys
from textwrap import wrap
import numpy as np, matplotlib
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),
@@ -19,32 +19,49 @@ def parse_log(logname, opts):
for header in f:
if header.startswith('#'):
continue
if header.startswith('freq,psd_x,psd_y,psd_z,psd_xyz'):
if header.startswith('freq,'):
# Processed power spectral density file
break
# Raw accelerometer data
return np.loadtxt(logname, comments='#', delimiter=',')
# Parse power spectral density data
data = np.loadtxt(logname, skiprows=1, comments='#', delimiter=',')
calibration_data = shaper_calibrate.CalibrationData(
freq_bins=data[:,0], psd_sum=data[:,4],
psd_x=data[:,1], psd_y=data[:,2], psd_z=data[:,3])
calibration_data.set_numpy(np)
data = np.genfromtxt(logname, dtype=np.float64, skip_header=1,
comments='#', delimiter=',', filling_values=0.)
if header.startswith('freq,psd_x,psd_y,psd_z,psd_xyz'):
calibration_data = shaper_calibrate.CalibrationData(
name=logname, freq_bins=data[:,0], psd_sum=data[:,4],
psd_x=data[:,1], psd_y=data[:,2], psd_z=data[:,3])
calibration_data.set_numpy(np)
else:
parsed_header = next(csv.reader([header], delimiter=','))
calibration_data = None
for i, dataset_name in enumerate(parsed_header[1:]):
if dataset_name == 'shapers:':
break
cdata = shaper_calibrate.CalibrationData(
name=dataset_name, freq_bins=data[:,0], psd_sum=data[:,i+1],
# Individual axes data is not stored
psd_x=None, psd_y=None, psd_z=None)
cdata.set_numpy(np)
if calibration_data is None:
calibration_data = cdata
else:
calibration_data.add_data(cdata)
return calibration_data
######################################################################
# Raw accelerometer graphing
######################################################################
def plot_accel(datas, lognames):
def plot_accel(opts, datas, lognames):
fig, axes = matplotlib.pyplot.subplots(nrows=3, sharex=True)
axes[0].set_title("\n".join(wrap(
"Accelerometer data (%s)" % (', '.join(lognames)), MAX_TITLE_LENGTH)))
axis_names = ['x', 'y', 'z']
for data, logname in zip(datas, lognames):
if isinstance(data, shaper_calibrate.CalibrationData):
raise error("Cannot plot raw accelerometer data using the processed"
" resonances, raw_data input is required")
opts.error("Cannot plot raw accelerometer data using the processed"
" resonances, raw_data input is required")
first_time = data[0, 0]
times = data[:,0] - first_time
for i in range(len(axis_names)):
@@ -70,16 +87,13 @@ def plot_accel(datas, lognames):
######################################################################
# Calculate estimated "power spectral density"
def calc_freq_response(data, max_freq):
def calc_freq_response(name, data, max_freq):
if isinstance(data, shaper_calibrate.CalibrationData):
return data
helper = shaper_calibrate.ShaperCalibrate(printer=None)
return helper.process_accelerometer_data(data)
return helper.process_accelerometer_data(name, data)
def calc_specgram(data, axis):
if isinstance(data, shaper_calibrate.CalibrationData):
raise error("Cannot calculate the spectrogram using the processed"
" resonances, raw_data input is required")
N = data.shape[0]
Fs = N / (data[-1,0] - data[0,0])
# Round up to a power of 2 for faster FFT
@@ -99,28 +113,45 @@ def calc_specgram(data, axis):
pdata += _specgram(d[ax])[0]
return pdata, bins, t
def plot_frequency(datas, lognames, max_freq):
calibration_data = calc_freq_response(datas[0], max_freq)
for data in datas[1:]:
calibration_data.add_data(calc_freq_response(data, max_freq))
freqs = calibration_data.freq_bins
psd = calibration_data.psd_sum[freqs <= max_freq]
px = calibration_data.psd_x[freqs <= max_freq]
py = calibration_data.psd_y[freqs <= max_freq]
pz = calibration_data.psd_z[freqs <= max_freq]
freqs = freqs[freqs <= max_freq]
def plot_frequency(opts, datas, lognames, max_freq, axis):
calibration_data = calc_freq_response(lognames[0], datas[0], max_freq)
for logname, data in zip(lognames[1:], datas[1:]):
calibration_data.add_data(calc_freq_response(logname, data, max_freq))
fig, ax = matplotlib.pyplot.subplots()
ax.set_title("\n".join(wrap(
"Frequency response (%s)" % (', '.join(lognames)), MAX_TITLE_LENGTH)))
ax.set_xlabel('Frequency (Hz)')
ax.set_ylabel('Power spectral density')
ax.plot(freqs, psd, label='X+Y+Z', alpha=0.6)
ax.plot(freqs, px, label='X', alpha=0.6)
ax.plot(freqs, py, label='Y', alpha=0.6)
ax.plot(freqs, pz, label='Z', alpha=0.6)
datasets = calibration_data.get_datasets()
if len(datasets) == 1:
freqs = calibration_data.freq_bins
if axis == 'all':
psd = calibration_data.psd_sum[freqs <= max_freq]
px = calibration_data.psd_x[freqs <= max_freq]
py = calibration_data.psd_y[freqs <= max_freq]
pz = calibration_data.psd_z[freqs <= max_freq]
freqs = freqs[freqs <= max_freq]
ax.plot(freqs, psd, label='X+Y+Z', alpha=0.6)
ax.plot(freqs, px, label='X', alpha=0.6)
ax.plot(freqs, py, label='Y', alpha=0.6)
ax.plot(freqs, pz, label='Z', alpha=0.6)
else:
psd = calibration_data.get_psd(axis)[freqs <= max_freq]
freqs = freqs[freqs <= max_freq]
ax.plot(freqs, psd, label=axis.upper(), alpha=0.6)
title = "Frequency response (%s)" % calibration_data.name
else:
for data in datasets:
freqs = data.freq_bins
psd = data.get_psd(axis)
if psd is None:
opts.error("Per-axis data is not present in the input file(s)")
psd = psd[freqs <= max_freq]
freqs = freqs[freqs <= max_freq]
ax.plot(freqs, psd, label=data.name)
title = "Frequency responses comparison"
ax.set_title("\n".join(wrap(title, MAX_TITLE_LENGTH)))
ax.xaxis.set_minor_locator(matplotlib.ticker.AutoMinorLocator())
ax.yaxis.set_minor_locator(matplotlib.ticker.AutoMinorLocator())
ax.grid(which='major', color='grey')
@@ -133,31 +164,11 @@ def plot_frequency(datas, lognames, max_freq):
fig.tight_layout()
return fig
def plot_compare_frequency(datas, lognames, max_freq, axis):
fig, ax = matplotlib.pyplot.subplots()
ax.set_title('Frequency responses comparison')
ax.set_xlabel('Frequency (Hz)')
ax.set_ylabel('Power spectral density')
for data, logname in zip(datas, lognames):
calibration_data = calc_freq_response(data, max_freq)
freqs = calibration_data.freq_bins
psd = calibration_data.get_psd(axis)[freqs <= max_freq]
freqs = freqs[freqs <= max_freq]
ax.plot(freqs, psd, label="\n".join(wrap(logname, 60)), alpha=0.6)
ax.xaxis.set_minor_locator(matplotlib.ticker.AutoMinorLocator())
ax.yaxis.set_minor_locator(matplotlib.ticker.AutoMinorLocator())
ax.grid(which='major', color='grey')
ax.grid(which='minor', color='lightgrey')
fontP = matplotlib.font_manager.FontProperties()
fontP.set_size('x-small')
ax.legend(loc='best', prop=fontP)
fig.tight_layout()
return fig
# Plot data in a "spectrogram colormap"
def plot_specgram(data, logname, max_freq, axis):
def plot_specgram(opts, data, logname, max_freq, axis):
if isinstance(data, shaper_calibrate.CalibrationData):
opts.error("Cannot calculate the spectrogram using the processed"
" resonances, raw_data input is required")
pdata, bins, t = calc_specgram(data, axis)
fig, ax = matplotlib.pyplot.subplots()
@@ -174,11 +185,12 @@ def plot_specgram(data, logname, max_freq, axis):
# CSV output
######################################################################
def write_frequency_response(datas, output):
def write_frequency_response(lognames, datas, output):
helper = shaper_calibrate.ShaperCalibrate(printer=None)
calibration_data = helper.process_accelerometer_data(datas[0])
for data in datas[1:]:
calibration_data.add_data(helper.process_accelerometer_data(data))
calibration_data = helper.process_accelerometer_data(lognames[0], datas[0])
for logname, data in zip(lognames[1:], datas[1:]):
calibration_data.add_data(
helper.process_accelerometer_data(logname, data))
helper.save_calibration_data(output, calibration_data)
def write_specgram(psd, freq_bins, time, output):
@@ -223,9 +235,6 @@ def main():
help="maximum frequency to graph")
opts.add_option("-r", "--raw", action="store_true",
help="graph raw accelerometer data")
opts.add_option("-c", "--compare", action="store_true",
help="graph comparison of power spectral density "
"between different accelerometer data files")
opts.add_option("-s", "--specgram", action="store_true",
help="graph spectrogram of accelerometer data")
opts.add_option("-a", type="string", dest="axis", default="all",
@@ -242,29 +251,25 @@ def main():
if is_csv_output(options.output):
if options.raw:
opts.error("raw mode is not supported with csv output")
if options.compare:
opts.error("comparison mode is not supported with csv output")
if options.specgram:
if len(args) > 1:
opts.error("Only 1 input is supported in specgram mode")
pdata, bins, t = calc_specgram(datas[0], options.axis)
write_specgram(pdata, bins, t, options.output)
else:
write_frequency_response(datas, options.output)
write_frequency_response(args, datas, options.output)
return
# Draw graph
if options.raw:
fig = plot_accel(datas, args)
fig = plot_accel(opts, datas, args)
elif options.specgram:
if len(args) > 1:
opts.error("Only 1 input is supported in specgram mode")
fig = plot_specgram(datas[0], args[0], options.max_freq, options.axis)
elif options.compare:
fig = plot_compare_frequency(datas, args, options.max_freq,
options.axis)
fig = plot_specgram(opts, datas[0], args[0],
options.max_freq, options.axis)
else:
fig = plot_frequency(datas, args, options.max_freq)
fig = plot_frequency(opts, datas, args, options.max_freq, options.axis)
# Show graph
if options.output is None: