From d1b5eec84ad7c2fb2172bd831b211fc7839b3d24 Mon Sep 17 00:00:00 2001 From: David Given Date: Sun, 10 Jan 2021 00:51:43 +0100 Subject: [PATCH] Add the analysis tool and Python script for drawing the results. --- scripts/analysedriveresponse.py | 77 +++++++++++++++++++++++++++++++++ src/fe-analysedriveresponse.cc | 69 +++++++++++++++-------------- 2 files changed, 114 insertions(+), 32 deletions(-) create mode 100644 scripts/analysedriveresponse.py diff --git a/scripts/analysedriveresponse.py b/scripts/analysedriveresponse.py new file mode 100644 index 00000000..b3d4367a --- /dev/null +++ b/scripts/analysedriveresponse.py @@ -0,0 +1,77 @@ +import numpy +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +TICK_FREQUENCY = 12e6 +TICKS_PER_US = TICK_FREQUENCY / 1e6 +print(TICKS_PER_US) + +# Load data. +data = numpy.loadtxt(open("analysis.csv", "rb"), delimiter=",", skiprows=1) + +labels = data[:, 0] +frequencies = data[:, 1:] + +# Scale the frequencies. +def scaled(row): + m = row.mean() + if m != 0: + return row / m + else: + return row + +scaledfreq = numpy.array([scaled(row) for row in frequencies]) + +# Create new Figure with black background +fig = plt.figure(figsize=(8, 8), facecolor='#aaa') + +plt.imshow(scaledfreq, extent=[0, 512/TICKS_PER_US, labels[0], labels[-1]], cmap='jet', + vmin=0, vmax=1, origin='lower', aspect='auto') +plt.colorbar() +plt.ylabel("Interval period (us)") +plt.xlabel("Response (us)") +plt.show() + +## Add a subplot with no frame +#ax = plt.subplot(111, frameon=False) +# +## Generate random data +#X = numpy.linspace(-1, 1, frequencies.shape[-1]) +#G = 1.5 * numpy.exp(-4 * X ** 2) +# +# +## Generate line plots +#lines = [] +#for i in range(len(frequencies)): +# # Small reduction of the X extents to get a cheap perspective effect +# xscale = 1 - i / 300. +# +# # Same for linewidth (thicker strokes on bottom) +# lw = 1.5 - i / 200.0 +# +# # Scale. +# row = frequencies[i] +# max = numpy.amax(row) +# if max == 0: +# Y = 1 +# else: +# Y = 5 / max +# line, = ax.plot(xscale*X, i + Y*frequencies[i], color="w", lw=lw) +# lines.append(line) +# +## Set y limit (or first line is cropped because of thickness) +##ax.set_ylim(-1, 100) +# +## No ticks +##ax.set_xticks([]) +##ax.set_yticks([]) +# +## 2 part titles to get different font weights +##ax.text(0.5, 1.0, "MATPLOTLIB ", transform=ax.transAxes, +##ha="right", va="bottom", color="w", +##family="sans-serif", fontweight="light", fontsize=16) +##ax.text(0.5, 1.0, "UNCHAINED", transform=ax.transAxes, +##ha="left", va="bottom", color="w", +##family="sans-serif", fontweight="bold", fontsize=16) + +plt.show() diff --git a/src/fe-analysedriveresponse.cc b/src/fe-analysedriveresponse.cc index baf74f9f..090b7425 100644 --- a/src/fe-analysedriveresponse.cc +++ b/src/fe-analysedriveresponse.cc @@ -36,7 +36,7 @@ static DoubleFlag intervalStep( static StringFlag writeCsv( { "--write-csv" }, "Write detailed CSV data", - ""); + "driveresponse.csv"); int mainAnalyseDriveResponse(int argc, const char* argv[]) { @@ -60,43 +60,47 @@ int mainAnalyseDriveResponse(int argc, const char* argv[]) for (double interval = minInterval; interval frequencies(512); - Fluxmap outFluxmap; - while (outFluxmap.duration() < period) + if (interval >= 2.0) { - outFluxmap.appendInterval(ticksPerInterval); - outFluxmap.appendPulse(); - } - usbWrite(spec.locations[0].side, outFluxmap.rawBytes(), 0); + /* Write the test pattern. */ - /* Read the test pattern in again. */ + Fluxmap outFluxmap; + while (outFluxmap.duration() < period) + { + outFluxmap.appendInterval(ticks); + outFluxmap.appendPulse(); + } + usbWrite(spec.locations[0].side, outFluxmap.rawBytes(), 0); - Fluxmap inFluxmap; - inFluxmap.appendBytes(usbRead(spec.locations[0].side, true, period, 0)); + /* Read the test pattern in again. */ - /* Compute histogram. */ + Fluxmap inFluxmap; + inFluxmap.appendBytes(usbRead(spec.locations[0].side, true, period, 0)); - FluxmapReader fmr(inFluxmap); - fmr.seek((double)period*0.1); /* skip first 10% and last 10% as contains junk */ - fmr.findEvent(F_BIT_PULSE); - int frequencies[256] = {}; - while (fmr.tell().ns() < ((double)period*0.9)) - { - unsigned ticks = fmr.findEvent(F_BIT_PULSE); - if (ticks < 256) - frequencies[ticks]++; + /* Compute histogram. */ + + FluxmapReader fmr(inFluxmap); + fmr.seek((double)period*0.1); /* skip first 10% and last 10% as contains junk */ + fmr.findEvent(F_BIT_PULSE); + while (fmr.tell().ns() < ((double)period*0.9)) + { + unsigned ticks = fmr.findEvent(F_BIT_PULSE); + if (ticks < frequencies.size()) + frequencies[ticks]++; + } } /* Compute standard deviation. */ int sum = 0; int prod = 0; - for (int i=0; i<256; i++) + for (int i=0; i