-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
293 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from time import sleep | ||
|
||
import picoquake | ||
|
||
if __name__ == "__main__": | ||
# Create a PicoQuake device | ||
device = picoquake.PicoQuake("c6e3") | ||
|
||
# Configure acquisition | ||
device.configure( | ||
sample_rate=picoquake.SampleRate.hz_100, | ||
filter_hz=picoquake.Filter.hz_42, | ||
acc_range=picoquake.AccRange.g_16, | ||
gyro_range=picoquake.GyroRange.dps_2000, | ||
) | ||
|
||
# Acquire data from the device | ||
data, exception = device.trigger(rms_threshold=2, | ||
pre_seconds=2, | ||
post_seconds=5, | ||
source="accel", | ||
axis="xyz",) | ||
|
||
# Stop the device | ||
device.stop() | ||
|
||
# Check if an exception occurred | ||
if exception is not None: | ||
raise exception | ||
|
||
# Print the acquired data | ||
print(f"Data: {data}") | ||
|
||
# Save to a CSV file | ||
data.to_csv("acquisition_triggered.csv") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
from .data import AcquisitionData, IMUSample | ||
from .plot import * | ||
|
||
__version__ = "1.0.4" | ||
__version__ = "1.1.0-beta" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
This module implements various data analysis functions. | ||
""" | ||
|
||
from typing import List, Tuple | ||
|
||
from .data import IMUSample | ||
|
||
|
||
# def rms(samples: List[IMUSample]) -> Tuple[float, float, float, float, float, float]: | ||
# """ | ||
# Calculate the root mean square of the acceleration and angular velocity components. | ||
|
||
# Args: | ||
# samples: List of IMU samples. | ||
|
||
# Returns: | ||
# Tuple of the root mean square of the acceleration and angular velocity components in the order: | ||
# (rms_ax, rms_ay, rms_az, rms_gx, rms_gy, rms_gz) | ||
# """ | ||
# rms_ax = (sum([sample.acc_x ** 2 for sample in samples]) / len(samples)) ** 0.5 | ||
# rms_ay = (sum([sample.acc_y ** 2 for sample in samples]) / len(samples)) ** 0.5 | ||
# rms_az = (sum([sample.acc_z ** 2 for sample in samples]) / len(samples)) ** 0.5 | ||
# rms_gx = (sum([sample.gyro_x ** 2 for sample in samples]) / len(samples)) ** 0.5 | ||
# rms_gy = (sum([sample.gyro_y ** 2 for sample in samples]) / len(samples)) ** 0.5 | ||
# rms_gz = (sum([sample.gyro_z ** 2 for sample in samples]) / len(samples)) ** 0.5 | ||
# return (rms_ax, rms_ay, rms_az, rms_gx, rms_gy, rms_gz) | ||
|
||
# def rms_combined(samples: List[IMUSample]) -> Tuple[float, float]: | ||
# """ | ||
# Calculate the root mean square of the combined acceleration and angular velocity components. | ||
|
||
# Args: | ||
# samples: List of IMU samples. | ||
|
||
# Returns: | ||
# Tuple of the root mean square of the combined acceleration and angular velocity components in the order: | ||
# (rms_a, rms_g) | ||
# """ | ||
# rms_a = (sum([sample.acc_x ** 2 + sample.acc_y ** 2 + sample.acc_z ** 2 for sample in samples]) / len(samples)) ** 0.5 | ||
# rms_g = (sum([sample.gyro_x ** 2 + sample.gyro_y ** 2 + sample.gyro_z ** 2 for sample in samples]) / len(samples)) ** 0.5 | ||
# return (rms_a, rms_g) | ||
|
||
def rms(samples: List[IMUSample], axes: str) -> Tuple[float, float]: | ||
""" | ||
Calculate the root mean square of the acceleration and angular velocity components for the specified axes. | ||
Args: | ||
samples: List of IMU samples. | ||
axes: String with the axes to calculate the RMS values. Must be a combination of 'x', 'y', and 'z'. | ||
Returns: | ||
Tuple of the root mean square of the acceleration and angular velocity. | ||
""" | ||
|
||
x = int('x' in axes) | ||
y = int('y' in axes) | ||
z = int('z' in axes) | ||
|
||
rms_acc = (sum([sample.acc_x ** 2 * x + sample.acc_y ** 2 * y + sample.acc_z ** 2 * z for sample in samples]) / len(samples)) ** 0.5 | ||
rms_gyro = (sum([sample.gyro_x ** 2 * x + sample.gyro_y ** 2 * y + sample.gyro_z ** 2 * z for sample in samples]) / len(samples)) ** 0.5 | ||
return (rms_acc, rms_gyro) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from itertools import permutations | ||
from collections import deque | ||
from typing import List, Any, Optional | ||
|
||
|
||
def get_axis_combinations(axis: str) -> set: | ||
return set(''.join(p) for i in range(1, len(axis) + 1) for p in permutations(axis, i)) | ||
|
||
|
||
def deque_get_last_n(data: deque, n: int) -> List[Any]: | ||
""" | ||
Get the last n elements from a deque. | ||
Args: | ||
data: Deque with data. | ||
n: Number of elements to get. | ||
Returns: | ||
List with the last n elements from the deque. If n is greater than the length of the deque, all elements are returned. | ||
""" | ||
start_idx = max(0, len(data) - n) | ||
return [data[i] for i in range(start_idx, len(data))] | ||
|
||
def deque_slice(dq: deque, start: Optional[int], end: Optional[int] = None) -> List[Any]: | ||
""" | ||
Return a slice from the deque. | ||
Args: | ||
dq: The deque to slice. | ||
start: The starting index of the slice. | ||
end : The ending index of the slice. | ||
Returns: | ||
deque: A deque containing the specified slice. | ||
""" | ||
if start is None: | ||
start = 0 | ||
if end is None: | ||
end = len(dq) | ||
if start < 0: | ||
start = len(dq) + start | ||
if end < 0: | ||
end = len(dq) + end | ||
if start < 0: | ||
start = 0 | ||
if end > len(dq): | ||
end = len(dq) | ||
return [dq[i] for i in range(start, end)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from pytest import approx | ||
|
||
from picoquake.data import IMUSample | ||
from picoquake.analisys import * | ||
|
||
def test_rms(): | ||
samples = [ | ||
IMUSample(0, 1, 2, 3, 4, 5, 6), | ||
IMUSample(1, 1, 2, 3, 4, 5, 6), | ||
IMUSample(2, 1, 2, 3, 4, 5, 6), | ||
] | ||
|
||
# Test for all axes | ||
acc_rms, gyro_rms = rms(samples, 'xyz') | ||
assert approx(acc_rms, 1e-3) == 3.7417 | ||
assert approx(gyro_rms, 1e-3) == 8.775 | ||
|
||
# Test for x axis only | ||
acc_rms, gyro_rms = rms(samples, 'x') | ||
assert approx(acc_rms, 1e-3) == 1.0 | ||
assert approx(gyro_rms, 1e-3) == 4.0 | ||
|
||
# Test for y axis only | ||
acc_rms, gyro_rms = rms(samples, 'y') | ||
assert approx(acc_rms, 1e-3) == 2.0 | ||
assert approx(gyro_rms, 1e-3) == 5.0 | ||
|
||
# Test for z axis only | ||
acc_rms, gyro_rms = rms(samples, 'z') | ||
assert approx(acc_rms, 1e-3) == 3.0 | ||
assert approx(gyro_rms, 1e-3) == 6.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from picoquake.utils import * | ||
|
||
|
||
def test_get_axis_combinations(): | ||
assert get_axis_combinations("x") == {"x"} | ||
assert get_axis_combinations("xyz") == {"x", "y", "z", | ||
"xy", "xz", "yx", "yz", "zx", "zy", | ||
"xyz", "xzy", "yxz", "yzx", "zxy", "zyx"} | ||
assert get_axis_combinations("abcd") == {"a", "b", "c", "d", | ||
"ab", "ac", "ad", "ba", "bc", "bd", "ca", "cb", "cd", "da", "db", "dc", | ||
"abc", "abd", "acb", "acd", "adb", "adc", "bac", "bad", "bca", "bcd", "bda", "bdc", | ||
"cab", "cad", "cba", "cbd", "cda", "cdb", "dab", "dac", "dba", "dbc", "dca", "dcb", | ||
"abcd", "abdc", "acbd", "acdb", "adbc", "adcb", "bacd", "badc", "bcad", "bcda", "bdac", "bdca", | ||
"cabd", "cadb", "cbad", "cbda", "cdab", "cdba", "dabc", "dacb", "dbac", "dbca", "dcab", "dcba"} | ||
|
||
|
||
def test_deque_get_last_n(): | ||
d = deque([1, 2, 3, 4, 5]) | ||
assert deque_get_last_n(d, 3) == [3, 4, 5] | ||
assert deque_get_last_n(d, 5) == [1, 2, 3, 4, 5] | ||
assert deque_get_last_n(d, 6) == [1, 2, 3, 4, 5] | ||
assert deque_get_last_n(d, 0) == [] | ||
assert deque_get_last_n(d, -1) == [] | ||
d = deque() | ||
assert deque_get_last_n(d, 3) == [] | ||
assert deque_get_last_n(d, 0) == [] | ||
assert deque_get_last_n(d, -1) == [] | ||
|
||
|
||
def test_deque_slice(): | ||
dq = deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) | ||
test_slices = [(None, None), (None, 3), (0, 3), (0, 10), (3, 7), | ||
(None, -1), (-5, None), (-5, -3), (0, -3), (3, -3), (0, -10)] | ||
for start, end in test_slices: | ||
assert deque_slice(dq, start, end) == list(dq)[start:end] | ||
|