Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
dhomm
Bachelor Thesis - Sensor Localization
Commits
327b30bb
Commit
327b30bb
authored
Jan 15, 2022
by
dhomm
Browse files
add preparation files to main branch
parent
dbc4eab5
Changes
64
Expand all
Hide whitespace changes
Inline
Side-by-side
Sensor localization/preparation/Automatic-Approach/__pycache__/transformations.cpython-39.pyc
0 → 100644
View file @
327b30bb
File added
Sensor localization/preparation/Automatic-Approach/left-right-distinction.ipynb
0 → 100644
View file @
327b30bb
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "729808fa",
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from numpy import mean, sqrt, square, var, corrcoef"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a94bc3d",
"metadata": {},
"outputs": [],
"source": [
"def lr_distinction(body_part, first_component, second_component):\n",
" \"\"\"\n",
" This function distinguishes if the sensor is placed on the left or right\n",
" body part\n",
" \n",
" :args:\n",
" body_part: the body_part the sensor was assigned to\n",
" first_component, second_component: the components which will be compared\n",
" \n",
" The components should be as following for differen bps\n",
" upper arm:\n",
" first = sternum orientation (theta_x), second = upper arm z-accelerometer \n",
" upper leg: \n",
" first = pelvis orientation (theta_x), second upper leg z-accelerometer\n",
" shoulder:\n",
" first = shoulder, second = upper arm : z-accelerometer\n",
" forearm:\n",
" first = forearm, second = upper arm : x-accelerometer\n",
" hands:\n",
" first = hands, second = forearms : y-accelerometer\n",
" lower legs:\n",
" first = lower legs, second = upper legs : x-accelerometer\n",
" feet:\n",
" first = feet, second = lower legs : x-accelerometer\n",
" \n",
" :return:\n",
" left or right as string\n",
" \"\"\"\n",
" \n",
" cc = corrcoef(first_component, second_component)\n",
" if body_part == \"upper arm\":\n",
" if cc <= 1:\n",
" return \"left\"\n",
" else:\n",
" return \"right\"\n",
" if body_part == \"upper leg\":\n",
" if cc <= 1:\n",
" return \"left\"\n",
" else:\n",
" return \"right\"\n",
" if body_part == \"shoulder\":\n",
" if cc <= xxx:\n",
" return \"left\"\n",
" else:\n",
" return \"right\"\n",
" if body_part == \"forearm\":\n",
" if cc <= xxx:\n",
" return \"left\"\n",
" else:\n",
" return \"right\"\n",
" if body_part == \"hands\":\n",
" if cc <= xxx:\n",
" return \"left\"\n",
" else:\n",
" return \"right\"\n",
" if body_part == \"lower legs\":\n",
" if cc <= xxx:\n",
" return \"left\"\n",
" else:\n",
" return \"right\"\n",
" if body_part == \"feet\":\n",
" if cc <= xxx:\n",
" return \"left\"\n",
" else:\n",
" return \"right\"\n",
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:code id:729808fa tags:
```
python
import
matplotlib.pyplot
as
plt
import
numpy
as
np
from
numpy
import
mean
,
sqrt
,
square
,
var
,
corrcoef
```
%% Cell type:code id:1a94bc3d tags:
```
python
def
lr_distinction
(
body_part
,
first_component
,
second_component
):
"""
This function distinguishes if the sensor is placed on the left or right
body part
:args:
body_part: the body_part the sensor was assigned to
first_component, second_component: the components which will be compared
The components should be as following for differen bps
upper arm:
first = sternum orientation (theta_x), second = upper arm z-accelerometer
upper leg:
first = pelvis orientation (theta_x), second upper leg z-accelerometer
shoulder:
first = shoulder, second = upper arm : z-accelerometer
forearm:
first = forearm, second = upper arm : x-accelerometer
hands:
first = hands, second = forearms : y-accelerometer
lower legs:
first = lower legs, second = upper legs : x-accelerometer
feet:
first = feet, second = lower legs : x-accelerometer
:return:
left or right as string
"""
cc
=
corrcoef
(
first_component
,
second_component
)
if
body_part
==
"upper arm"
:
if
cc
<=
1
:
return
"left"
else
:
return
"right"
if
body_part
==
"upper leg"
:
if
cc
<=
1
:
return
"left"
else
:
return
"right"
if
body_part
==
"shoulder"
:
if
cc
<=
xxx
:
return
"left"
else
:
return
"right"
if
body_part
==
"forearm"
:
if
cc
<=
xxx
:
return
"left"
else
:
return
"right"
if
body_part
==
"hands"
:
if
cc
<=
xxx
:
return
"left"
else
:
return
"right"
if
body_part
==
"lower legs"
:
if
cc
<=
xxx
:
return
"left"
else
:
return
"right"
if
body_part
==
"feet"
:
if
cc
<=
xxx
:
return
"left"
else
:
return
"right"
```
Sensor localization/preparation/Automatic-Approach/load_data_sets.ipynb
0 → 100644
View file @
327b30bb
This diff is collapsed.
Click to expand it.
Sensor localization/preparation/Automatic-Approach/on-body_localization.ipynb
0 → 100644
View file @
327b30bb
This source diff could not be displayed because it is too large. You can
view the blob
instead.
Sensor localization/preparation/Automatic-Approach/preprocessing.ipynb
0 → 100644
View file @
327b30bb
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "3f0b605f",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from scipy.constants import g\n",
"from ahrs.filters import Madgwick\n",
"\n",
"# import ../../../used_scripts/geometry/tf/src/tf/transformations\n",
"import transformations"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e2f84fd9",
"metadata": {},
"outputs": [],
"source": [
"def get_inital_quaternions(acc_data, gyro_data, magnetic_data=0, frequency=100):\n",
" \"\"\"\n",
" See build quaternions, the sensor data however should only include static standing\n",
" data, for estimating the initial orientation of the sensors.\n",
" \"\"\"\n",
" return build_quaternions(acc_data, gyro_data, magnetic_data=magnetic_data, q0=0, frequency=frequency)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2cd34a63",
"metadata": {},
"outputs": [],
"source": [
"def calc_q0(initial_madgwick):\n",
" \"\"\"\n",
" This function calculates the mean of every value in the inital madgwick object\n",
" \"\"\"\n",
" \n",
" q0 = [[], [], [], []]\n",
" for q in initial_madgwick.Q:\n",
" for i in range(len(q0)):\n",
" q0[i].append(q[i])\n",
" for i in range(len(q0)):\n",
" q0[i] = sum(q0[i])/len(q0[i])\n",
" \n",
" return q0"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "bbbd93d6",
"metadata": {},
"outputs": [],
"source": [
"def build_quaternions(acc_data, gyro_data, magnetic_data=0, q0=0, frequency=100):\n",
" \"\"\"\n",
" This function creates quaternions based on the given data\n",
" \n",
" :args: \n",
" accel_data: a set of accelerometer data (Nx3 array)\n",
" gyro_data: a set of gyroscope data (Nx3 array)\n",
" magnetic_data: if given a set of magnetic data (Nx3 array)\n",
" q_init: an initial quaterion showing the initial rotation\n",
" \n",
" :return:\n",
" if every given data set has the same length N: N quaternions showing the rotation of N samples\n",
" else:\n",
" raising Value Error\n",
" \"\"\"\n",
" if magnetic_data == 0 and q0 == 0:\n",
" madgwick = Madgwick(acc=acc_data, gyr=gyro_data, frequency=frequency)\n",
" elif magnetic_data == 0 and q0 != 0:\n",
" madgwick = Madgwick(acc=acc_data, gyr=gyro_data, q0=q0, frequency=frequency)\n",
" elif magnetic_data != 0 and q0 == 0:\n",
" madgwick = Madgwick(acc=acc_data, gyr=gyro_data, mag=mag_data, frequency=frequency)\n",
" elif magnetic_data != 0 and q0 != 0:\n",
" madgwick = Madgwick(acc=acc_data, gyr=gyro_data, mag=mag_data, q0=q0, frequency=frequency)\n",
" \n",
" return madgwick\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "043e84f4",
"metadata": {},
"outputs": [],
"source": [
"# rotate vector v1 by quaternion q1 \n",
"# quelle: https://answers.ros.org/question/196149/how-to-rotate-vector-by-quaternion-in-python/\n",
"def qv_mult(q1, v1):\n",
" # v1 = transformations.unit_vector(v1)\n",
" q2 = list(v1)\n",
" q2.append(0.0)\n",
" return transformations.quaternion_multiply(\n",
" transformations.quaternion_multiply(q1, q2), \n",
" transformations.quaternion_conjugate(q1)\n",
" )[:3]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "91ddebcd",
"metadata": {},
"outputs": [],
"source": [
"def rotate_samples(quaternions, data_set):\n",
" \"\"\"\n",
" This function rotates the samples given in this data_set\n",
" with the help of the quaternions\n",
" \n",
" :args:\n",
" quaternions:\n",
" data_set:\n",
" \n",
" :return:\n",
" rotated samples\n",
" \"\"\"\n",
" if len(quaternions) != len(data_set):\n",
" raise Exception(\"Length of quaternion does not fit length of data set\")\n",
" rotated_data = []\n",
" for index in range(len(data_set)):\n",
" rotated_sample = qv_mult(quaternions[index], data_set[index])\n",
" rotated_data.append(rotated_sample)\n",
" \n",
" return np.array(rotated_data)\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c90fc793",
"metadata": {},
"outputs": [],
"source": [
"def preprocess_data(standing_acc, standing_gyro, walking_acc, walking_gyro, freq):\n",
" \"\"\"\n",
" This function combines the preprocessing steps to be able to compare samples from different \n",
" body_parts and subjects.\n",
" \n",
" :args:\n",
" acc_standing:\n",
" gyro_standing:\n",
" acc_walking:\n",
" gyro_walking:\n",
" freq:\n",
" \n",
" :return:\n",
" rotated accerlation and gyro data from their walking samples\n",
" \"\"\"\n",
" \n",
" initial_madgwick = get_inital_quaternions(standing_acc, standing_gyro, magnetic_data=0, frequency=freq)\n",
" q0 = calc_q0(initial_madgwick)\n",
" quaternions = build_quaternions(walking_acc, walking_gyro, magnetic_data=0, q0=q0, frequency=freq)\n",
" \n",
" rotated_acc = rotate_samples(quaternions.Q, walking_acc)\n",
" rotated_gyro = rotate_samples(quaternions.Q, walking_gyro)\n",
" \n",
" return rotated_acc, rotated_gyro\n",
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:code id:3f0b605f tags:
```
python
import
numpy
as
np
from
scipy.constants
import
g
from
ahrs.filters
import
Madgwick
# import ../../../used_scripts/geometry/tf/src/tf/transformations
import
transformations
```
%% Cell type:code id:e2f84fd9 tags:
```
python
def
get_inital_quaternions
(
acc_data
,
gyro_data
,
magnetic_data
=
0
,
frequency
=
100
):
"""
See build quaternions, the sensor data however should only include static standing
data, for estimating the initial orientation of the sensors.
"""
return
build_quaternions
(
acc_data
,
gyro_data
,
magnetic_data
=
magnetic_data
,
q0
=
0
,
frequency
=
frequency
)
```
%% Cell type:code id:2cd34a63 tags:
```
python
def
calc_q0
(
initial_madgwick
):
"""
This function calculates the mean of every value in the inital madgwick object
"""
q0
=
[[],
[],
[],
[]]
for
q
in
initial_madgwick
.
Q
:
for
i
in
range
(
len
(
q0
)):
q0
[
i
].
append
(
q
[
i
])
for
i
in
range
(
len
(
q0
)):
q0
[
i
]
=
sum
(
q0
[
i
])
/
len
(
q0
[
i
])
return
q0
```
%% Cell type:code id:bbbd93d6 tags:
```
python
def
build_quaternions
(
acc_data
,
gyro_data
,
magnetic_data
=
0
,
q0
=
0
,
frequency
=
100
):
"""
This function creates quaternions based on the given data
:args:
accel_data: a set of accelerometer data (Nx3 array)
gyro_data: a set of gyroscope data (Nx3 array)
magnetic_data: if given a set of magnetic data (Nx3 array)
q_init: an initial quaterion showing the initial rotation
:return:
if every given data set has the same length N: N quaternions showing the rotation of N samples
else:
raising Value Error
"""
if
magnetic_data
==
0
and
q0
==
0
:
madgwick
=
Madgwick
(
acc
=
acc_data
,
gyr
=
gyro_data
,
frequency
=
frequency
)
elif
magnetic_data
==
0
and
q0
!=
0
:
madgwick
=
Madgwick
(
acc
=
acc_data
,
gyr
=
gyro_data
,
q0
=
q0
,
frequency
=
frequency
)
elif
magnetic_data
!=
0
and
q0
==
0
:
madgwick
=
Madgwick
(
acc
=
acc_data
,
gyr
=
gyro_data
,
mag
=
mag_data
,
frequency
=
frequency
)
elif
magnetic_data
!=
0
and
q0
!=
0
:
madgwick
=
Madgwick
(
acc
=
acc_data
,
gyr
=
gyro_data
,
mag
=
mag_data
,
q0
=
q0
,
frequency
=
frequency
)
return
madgwick
```
%% Cell type:code id:043e84f4 tags:
```
python
# rotate vector v1 by quaternion q1
# quelle: https://answers.ros.org/question/196149/how-to-rotate-vector-by-quaternion-in-python/
def
qv_mult
(
q1
,
v1
):
# v1 = transformations.unit_vector(v1)
q2
=
list
(
v1
)
q2
.
append
(
0.0
)
return
transformations
.
quaternion_multiply
(
transformations
.
quaternion_multiply
(
q1
,
q2
),
transformations
.
quaternion_conjugate
(
q1
)
)[:
3
]
```
%% Cell type:code id:91ddebcd tags:
```
python
def
rotate_samples
(
quaternions
,
data_set
):
"""
This function rotates the samples given in this data_set
with the help of the quaternions
:args:
quaternions:
data_set:
:return:
rotated samples
"""
if
len
(
quaternions
)
!=
len
(
data_set
):
raise
Exception
(
"Length of quaternion does not fit length of data set"
)
rotated_data
=
[]
for
index
in
range
(
len
(
data_set
)):
rotated_sample
=
qv_mult
(
quaternions
[
index
],
data_set
[
index
])
rotated_data
.
append
(
rotated_sample
)
return
np
.
array
(
rotated_data
)
```
%% Cell type:code id:c90fc793 tags:
```
python
def
preprocess_data
(
standing_acc
,
standing_gyro
,
walking_acc
,
walking_gyro
,
freq
):
"""
This function combines the preprocessing steps to be able to compare samples from different
body_parts and subjects.
:args:
acc_standing:
gyro_standing:
acc_walking:
gyro_walking:
freq:
:return:
rotated accerlation and gyro data from their walking samples
"""
initial_madgwick
=
get_inital_quaternions
(
standing_acc
,
standing_gyro
,
magnetic_data
=
0
,
frequency
=
freq
)
q0
=
calc_q0
(
initial_madgwick
)
quaternions
=
build_quaternions
(
walking_acc
,
walking_gyro
,
magnetic_data
=
0
,
q0
=
q0
,
frequency
=
freq
)
rotated_acc
=
rotate_samples
(
quaternions
.
Q
,
walking_acc
)
rotated_gyro
=
rotate_samples
(
quaternions
.
Q
,
walking_gyro
)
return
rotated_acc
,
rotated_gyro
```
Sensor localization/preparation/Automatic-Approach/transformations.py
0 → 100644
View file @
327b30bb
This diff is collapsed.
Click to expand it.
Sensor localization/preparation/Data-Sets/PAMAP2/.ipynb_checkpoints/load_pamap-checkpoint.ipynb
0 → 100644
View file @
327b30bb
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "69b70970",
"metadata": {},
"outputs": [],
"source": [
"%run ../base_functions.ipynb"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d23d288",
"metadata": {},
"outputs": [],
"source": [
"# reading 2 proband files between 1-15\n",
"pamap_p1 = \"../../../../PycharmProjects/BP/PAMAP2/PAMAP2-final/Subject1.mkv\"\n",
"pamap_p2 = \"../../../../PycharmProjects/BP/PAMAP2/PAMAP2-final/Subject2.mkv\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b42ca66",
"metadata": {},
"outputs": [],
"source": [
"# reading the audio forefarm files for now\n",
"pamap_audio_p1 = read(\"a:\", file=pamap_p1)\n",
"pamap_sub_p1 = read(\"s:\", file=pamap_p1)\n",
"print(pamap_sub_p1)\n",
"pamap_accel_p1 = 0\n",
"pamap_gyro_p1 = 0\n",
"\n",
"\n",
"pamap_audio_p2 = read(\"a:\", file=pamap_p2)\n",
"pamap_sub_p2 = read(\"s:\", file=pamap_p2)\n",
"print(pamap_sub_p2)\n",
"pamap_accel_p2 = 0\n",
"pamap_gyro_p2 = 0\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9c6a0f3",
"metadata": {},
"outputs": [],
"source": [
"# get walking ranges\n",
"activity = \"walking\"\n",
"for comp in pamap_sub_p1[0]:\n",
" if activity in comp:\n",
" pamap_range_p1 = [comp[0], comp[1]]\n",
" \n",
"for comp in pamap_sub_p2[0]:\n",
" if activity in comp:\n",
" pamap_range_p2 = [comp[0], comp[1]]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf479c7a",
"metadata": {},
"outputs": [],
"source": [
"# get accel and gyroscope\n",