From c6537b51ae7ff779a0041adecdc073b14fec5ab0 Mon Sep 17 00:00:00 2001 From: Tom Selier Date: Thu, 21 Mar 2024 19:24:50 +0100 Subject: [PATCH 1/3] updated --- .gitignore | 1 + scripts/reader.py | 35 +++++++++++++++++++++++++++++++++++ scripts/reflection.py | 10 ++++++++++ 3 files changed, 46 insertions(+) create mode 100644 scripts/reader.py create mode 100644 scripts/reflection.py diff --git a/.gitignore b/.gitignore index 1a2920d..79a9c70 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ doc/*/*.* !doc/*/*.tex !doc/*/*.bib +*.cpython-311.pyc \ No newline at end of file diff --git a/scripts/reader.py b/scripts/reader.py new file mode 100644 index 0000000..6af7e0f --- /dev/null +++ b/scripts/reader.py @@ -0,0 +1,35 @@ +import csv + +class Reader(): + def __init__(self) -> None: + pass + + def readFile(self, filename) -> dict: + with open(filename, 'r') as file: + data = list(csv.reader(file, delimiter=',')) + + # Delete metadata for now + print("Removing metadata") + for i in range(15): + print(data.pop(0)) + + # {'TIME': [data], 'CH1': [data], 'CH2': [data]} + channels = [] + for i in range(len(data[0])): + channels.append([]) + + for step in data[1:]: + for idx, point in enumerate(step): + channels[idx].append(float(point)) + + result = {} + for idx, label in enumerate(data[0]): + result[label] = channels[idx] + + return result + + +if __name__ == '__main__': + reader = Reader() + data = reader.readFile(r".\data\T0007ALL.csv") + print(data) \ No newline at end of file diff --git a/scripts/reflection.py b/scripts/reflection.py new file mode 100644 index 0000000..78d37a4 --- /dev/null +++ b/scripts/reflection.py @@ -0,0 +1,10 @@ +from matplotlib import pyplot as plt +import reader + +data_reader = reader.Reader() +data = data_reader.readFile(r".\data\T0001CH1.csv") + + + +plt.plot(data['TIME'], data['CH1']) +plt.show() \ No newline at end of file From cde40ce1d9fe4364c20215cd1a65edd3670a2dea Mon Sep 17 00:00:00 2001 From: Tom Selier Date: Fri, 22 Mar 2024 13:55:14 +0100 Subject: [PATCH 2/3] reflections successfully parsed --- requirements.txt | Bin 0 -> 432 bytes scripts/helper.py | 17 +++++++++++++++++ scripts/reader.py | 10 +++++----- scripts/reflection.py | 39 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 requirements.txt create mode 100644 scripts/helper.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..622a25f2a719a2d8438068e905d53dd163d3ee89 GIT binary patch literal 432 zcmZ9IT@HdU5QOL1#G^>~QKJtYMk^{2N&_fzd3AOdB^uI_bfz<3_w{tB;n8uwp+Ufa z9lrz-QmzoA7lq(AKT&;TU-!$nmuXI(| zlO`~w=D(!>plKOh8?H dict: + idx_start = 0 + idx_end = 0 + + for idx, t in enumerate(data['TIME']): + if t > start and idx_start == 0: + idx_start = idx + if t > end and idx_end == 0: + idx_end = idx + if idx_end != 0 and idx_start != 0: + break + + if idx_end != 0 and idx_start != 0: + return {key: value[idx_start:idx_end] for key, value in data.items()} + else: + return None diff --git a/scripts/reader.py b/scripts/reader.py index 6af7e0f..1f9e2bc 100644 --- a/scripts/reader.py +++ b/scripts/reader.py @@ -4,14 +4,13 @@ class Reader(): def __init__(self) -> None: pass - def readFile(self, filename) -> dict: + def read_file(self, filename) -> dict: with open(filename, 'r') as file: data = list(csv.reader(file, delimiter=',')) # Delete metadata for now - print("Removing metadata") for i in range(15): - print(data.pop(0)) + data.pop(0) # {'TIME': [data], 'CH1': [data], 'CH2': [data]} channels = [] @@ -31,5 +30,6 @@ class Reader(): if __name__ == '__main__': reader = Reader() - data = reader.readFile(r".\data\T0007ALL.csv") - print(data) \ No newline at end of file + data = reader.read_file(r".\data\T0007ALL.csv") + print(data) + \ No newline at end of file diff --git a/scripts/reflection.py b/scripts/reflection.py index 78d37a4..c412a0d 100644 --- a/scripts/reflection.py +++ b/scripts/reflection.py @@ -1,10 +1,45 @@ from matplotlib import pyplot as plt +from scipy import signal +import numpy as np import reader +import helper +# load data data_reader = reader.Reader() -data = data_reader.readFile(r".\data\T0001CH1.csv") +data = data_reader.read_file(r".\data\T0004CH1.csv") +# slice empty space, start and end time is in seconds +data = helper.cut_time(data, -1e-7, 5e-7) +# find peaks, use max to find the highest, or a threshold +# adjust width to find the middle of a peak (width is in indices) +# peaks, _ = signal.find_peaks(data['CH1'], max(data['CH1'])) +peaks, _ = signal.find_peaks(data['CH1'], 2, width=10) +# find throughs, comment away it finds too many throughs +# invert = [-x for x in data['CH1']] +# throughs, _ = signal.find_peaks(invert, max(invert)) +# peaks = np.concatenate([peaks, throughs]) + +# store time step +step = data['TIME'][1] - data['TIME'][0] + +# do some calcs +idx_diff = peaks[-1] - peaks[0] +tim_diff = idx_diff*step +print(f"Time between peaks: {tim_diff*1e9:.1f} ns") +c = 299792458 +k = 0.66 +c_coax = c * k +print(f"Measured distance: {(tim_diff/2)*c_coax:.2f} m") + +# Plot data plt.plot(data['TIME'], data['CH1']) -plt.show() \ No newline at end of file +if len(peaks) > 0: + for peak in peaks: + plt.vlines( + peak*step+data['TIME'][0], + min(data['CH1']), + max(data['CH1']), 'red', 'dashed') +plt.grid() +plt.show() From e0236e1a407efe1d53465d127dade6a630a2d0eb Mon Sep 17 00:00:00 2001 From: Tom Selier Date: Fri, 22 Mar 2024 13:59:50 +0100 Subject: [PATCH 3/3] updated readme --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 5e1095b..bcf8a7b 100644 --- a/README.md +++ b/README.md @@ -38,3 +38,15 @@ Description of PCB design choices. Measurement reports for aforementioned experiments. - Arne van Iterson - Tom Selier + +## Scripts +This repository contains scripts to assist in parsing data obtained from a Tektronix oscilloscope + +### `./scripts/reflection.py` +A script containing the neccessary steps to find peaks and measure the peaks and throughs of a 1d signal. + +### `./scripts/reader.py` +A module containing a class to read data from a tektronix oscilloscope. + +### `./scripts/helper.py` +A module containing a function that slices array based on time. \ No newline at end of file