4 interactively select accel and gyro data for FFT analysis 6 from __future__
import print_function
11 import matplotlib.pyplot
as pyplot
13 from argparse
import ArgumentParser
14 parser = ArgumentParser(description=__doc__)
15 parser.add_argument(
"--condition", default=
None, help=
"select packets by condition")
16 parser.add_argument(
"logs", metavar=
"LOG", nargs=
"+")
18 args = parser.parse_args()
20 from pymavlink
import mavutil
28 preview = pylab.figure()
29 preview.set_size_inches(12, 3, forward=
True)
30 for axis
in [
'X',
'Y',
'Z']:
31 field = msg +
'.' + prefix + axis
32 d = numpy.array(data[field][start:end])
33 pylab.plot( d, marker=
'.', label=field )
34 pylab.legend(loc=
'upper right')
36 pylab.subplots_adjust(left=0.06, right=0.95, top=0.95, bottom=0.16)
37 preview.canvas.set_window_title(
'FFT input: ' + msg)
41 ts = 1e-6 * numpy.array(data[msg +
'.TimeUS'])
42 seqcnt = numpy.array(data[msg +
'.SampleC'])
44 deltas = numpy.diff(seqcnt[start:end])
46 duration = ts[end] - ts[start]
47 print(msg +
' duration: {0:.3f} seconds'.format(duration))
48 avg_rate =
float(end - start - 1) / duration
49 print(
'average logging rate: {0:.0f} Hz'.format(avg_rate))
50 ts_mean = numpy.mean(deltas)
51 dmin = numpy.min(deltas)
52 dmax = numpy.max(deltas)
53 print(
'sample count delta min: {0}, max: {1}'.format(dmin, dmax))
55 print(
'sample count delta mean: ',
'{0:.2f}, std: {0:.2f}'.format(ts_mean, numpy.std(deltas)))
56 print(
'sensor sample rate: {0:.0f} Hz'.format(ts_mean * avg_rate))
61 for i
in range(0, len(deltas)):
62 if (deltas[i] > 1.5 * ts_mean):
63 drop_lens.append(deltas[i])
64 drop_times.append(ts[start+i])
65 print(
'dropout at sample {0}: length {1}'.format(start+i, deltas[i]))
67 print(
'{0:d} sample intervals > {1:.3f}'.format(len(drop_lens), 1.5 * ts_mean))
71 '''display fft for raw ACC data in logfile''' 73 print(
"Processing log %s" % filename)
74 mlog = mavutil.mavlink_connection(filename)
76 data = {
'ACC1.rate' : 1000,
82 for acc
in [
'ACC1',
'ACC2',
'ACC3']:
83 for ax
in [
'AccX',
'AccY',
'AccZ',
'SampleC',
'TimeUS']:
85 for gyr
in [
'GYR1',
'GYR2',
'GYR3']:
86 for ax
in [
'GyrX',
'GyrY',
'GyrZ',
'SampleC',
'TimeUS']:
91 m = mlog.recv_match(condition=args.condition)
95 if type.startswith(
"ACC"):
96 data[type+
'.AccX'].append(m.AccX)
97 data[type+
'.AccY'].append(m.AccY)
98 data[type+
'.AccZ'].append(m.AccZ)
99 data[type+
'.SampleC'].append(m.SampleC)
100 data[type+
'.TimeUS'].append(m.TimeUS)
101 if type.startswith(
"GYR"):
102 data[type+
'.GyrX'].append(m.GyrX)
103 data[type+
'.GyrY'].append(m.GyrY)
104 data[type+
'.GyrZ'].append(m.GyrZ)
105 data[type+
'.SampleC'].append(m.SampleC)
106 data[type+
'.TimeUS'].append(m.TimeUS)
109 ts = 1e-6 * numpy.array(data[
'ACC1.TimeUS'])
110 seqcnt = numpy.array(data[
'ACC1.SampleC'])
112 print(
"Extracted %u data points" % len(data[
'ACC1.AccX']))
115 preview = pylab.figure()
116 preview.set_size_inches(12, 3, forward=
True)
118 for axis
in [
'X',
'Y',
'Z']:
119 field = msg +
'.Acc' + axis
120 d = numpy.array(data[field])
121 pylab.plot( d, marker=
'.', label=field )
122 pylab.legend(loc=
'upper right')
123 pylab.ylabel(
'm/sec/sec')
124 pylab.subplots_adjust(left=0.06, right=0.95, top=0.95, bottom=0.16)
126 currentAxes = preview.gca()
129 n_samp = s_end - s_start
130 currentAxes.set_xlim(s_start, s_end)
136 print(
'select sample range for fft analysis')
137 preview.canvas.set_window_title(
'select sample range')
139 s_start =
input(
'start sample: ')
140 s_end =
input(
'end sample: ')
141 currentAxes.set_xlim(s_start, s_end)
146 s_start =
int(currentAxes.get_xlim()[0])
147 s_end =
int(currentAxes.get_xlim()[1])
148 n_samp = s_end - s_start
149 print(
'sample range: ', s_start, s_end)
150 print(
'N samples: ', n_samp)
153 avg_rate =
check_drops(data,
'ACC1', s_start, s_end)
155 title =
'FFT input: {0:s} ACC1[{1:d}:{2:d}], {3:d} samples'.format(logfile, s_start, s_end, n_samp)
156 currentAxes.set_xlabel(
'sample index : nsamples: {0:d}, avg rate: {1:.0f} Hz'.format(n_samp, avg_rate))
157 preview.canvas.set_window_title(title)
158 preview.savefig(
'acc1z.png')
160 for msg
in [
'ACC1',
'GYR1',
'ACC2',
'GYR2']:
161 if msg.startswith(
'ACC'):
163 title =
'{2} FFT [{0:d}:{1:d}]'.format(s_start, s_end, msg)
166 title =
'{2} FFT [{0:d}:{1:d}]'.format(s_start, s_end, msg)
169 data[msg+
'.rate'] =
check_drops(data, msg, s_start, s_end)
172 fftwin = pylab.figure()
173 fftwin.set_size_inches(12, 3, forward=
True)
174 f_res =
float(data[msg+
'.rate']) / n_samp
179 avg = {
'X':0,
'Y':0,
'Z':0}
180 for axis
in [
'X',
'Y',
'Z']:
181 field = msg +
'.' + prefix + axis
182 d = data[field][s_start:s_end]
187 freq = numpy.fft.rfftfreq(len(d), 1.0 / data[msg+
'.rate'])
189 avg[axis] = numpy.mean(d)
191 print(
'{1} DC component: {0:.3f}'.format(avg[axis], field))
193 d_fft = numpy.fft.rfft(d)
194 abs_fft[axis] = numpy.abs(d_fft)
196 thismax = numpy.max(abs_fft[axis])
197 if (max_fft < thismax):
201 for axis
in [
'X',
'Y',
'Z']:
203 field = msg +
'.' + prefix + axis
204 db_fft = 20 * numpy.log10(abs_fft[axis] / max_fft)
205 pylab.plot( freq, db_fft, label=field )
207 fftwin.canvas.set_window_title(title)
208 fftwin.gca().set_ylim(-90, 0)
209 pylab.legend(loc=
'upper right')
210 pylab.xlabel(
'Hz : res: ' +
'{0:.3f}'.format(f_res))
211 pylab.ylabel(
'dB X {0:.3f} Y {1:.3f} Z {2:.3f}\n'.format(avg[
'X'], avg[
'Y'], avg[
'Z']))
212 pylab.subplots_adjust(left=0.1, right=0.95, top=0.95, bottom=0.16)
213 fftwin.savefig(msg +
'_fft.png')
216 selection =
raw_input(
'q to proceed to next file, anything else to select a new range: ')
221 if (selection ==
'q'):
225 for filename
in args.logs:
228 print(
'type ctrl-c to close windows and exit')