emg-tools 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- emg_tools-2.0.0/CITATION.cff +11 -0
- emg_tools-2.0.0/LICENSE +21 -0
- emg_tools-2.0.0/MANIFEST.in +4 -0
- emg_tools-2.0.0/PKG-INFO +131 -0
- emg_tools-2.0.0/README.md +94 -0
- emg_tools-2.0.0/emg_tools/__init__.py +1 -0
- emg_tools-2.0.0/emg_tools/feats.py +68 -0
- emg_tools-2.0.0/emg_tools/freq.py +71 -0
- emg_tools-2.0.0/emg_tools/plots.py +362 -0
- emg_tools-2.0.0/emg_tools/prepro.py +153 -0
- emg_tools-2.0.0/emg_tools/tools.py +159 -0
- emg_tools-2.0.0/emg_tools.egg-info/PKG-INFO +131 -0
- emg_tools-2.0.0/emg_tools.egg-info/SOURCES.txt +22 -0
- emg_tools-2.0.0/emg_tools.egg-info/dependency_links.txt +1 -0
- emg_tools-2.0.0/emg_tools.egg-info/requires.txt +18 -0
- emg_tools-2.0.0/emg_tools.egg-info/top_level.txt +1 -0
- emg_tools-2.0.0/pyproject.toml +123 -0
- emg_tools-2.0.0/setup.cfg +4 -0
- emg_tools-2.0.0/tests/conftest.py +58 -0
- emg_tools-2.0.0/tests/test_feats.py +32 -0
- emg_tools-2.0.0/tests/test_freq.py +33 -0
- emg_tools-2.0.0/tests/test_plots.py +67 -0
- emg_tools-2.0.0/tests/test_prepro.py +40 -0
- emg_tools-2.0.0/tests/test_tools.py +48 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
cff-version: 1.2.0
|
|
2
|
+
message: "If you use this software, please cite it as below."
|
|
3
|
+
authors:
|
|
4
|
+
- family-names: "Mendez Guerra"
|
|
5
|
+
given-names: "Irene"
|
|
6
|
+
orcid: "https://orcid.org/0000-0001-7361-4618"
|
|
7
|
+
title: "EMG Toolbox"
|
|
8
|
+
version: 2.0.0
|
|
9
|
+
# doi: tbd
|
|
10
|
+
date-released: 2026-10-30
|
|
11
|
+
url: "https://github.com/imendezguerra/emg-tools"
|
emg_tools-2.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Irene Mendez Guerra
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
emg_tools-2.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: emg-tools
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Preprocess, analyse and plot EMG signals
|
|
5
|
+
Author: Irene Mendez Guerra
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://imendezguerra.github.io/emg-tools/
|
|
8
|
+
Project-URL: Documentation, https://imendezguerra.github.io/emg-tools/
|
|
9
|
+
Project-URL: Source, https://github.com/imendezguerra/emg-tools
|
|
10
|
+
Project-URL: Issues, https://github.com/imendezguerra/emg-tools/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/imendezguerra/emg-tools/releases
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: numpy>=1.22
|
|
21
|
+
Requires-Dist: scipy>=1.8
|
|
22
|
+
Requires-Dist: matplotlib>=3.5
|
|
23
|
+
Requires-Dist: seaborn>=0.11
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
26
|
+
Requires-Dist: pytest-cov>=4; extra == "dev"
|
|
27
|
+
Requires-Dist: ruff>=0.6; extra == "dev"
|
|
28
|
+
Requires-Dist: pre-commit>=3; extra == "dev"
|
|
29
|
+
Requires-Dist: build>=1; extra == "dev"
|
|
30
|
+
Requires-Dist: twine>=5; extra == "dev"
|
|
31
|
+
Provides-Extra: docs
|
|
32
|
+
Requires-Dist: mkdocs<2,>=1.6; extra == "docs"
|
|
33
|
+
Requires-Dist: mkdocs-material>=9.5; extra == "docs"
|
|
34
|
+
Requires-Dist: mkdocstrings[python]>=0.26; extra == "docs"
|
|
35
|
+
Requires-Dist: markdown-exec>=1.9; extra == "docs"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# EMG Toolbox
|
|
39
|
+
|
|
40
|
+
[](https://pypi.org/project/emg-tools/)
|
|
41
|
+
[](https://pypi.org/project/emg-tools/)
|
|
42
|
+
[](https://github.com/imendezguerra/emg-tools/actions/workflows/ci.yml)
|
|
43
|
+
[](https://imendezguerra.github.io/emg-tools/)
|
|
44
|
+
|
|
45
|
+
## Overview
|
|
46
|
+
<!-- --8<-- [start:overview] -->
|
|
47
|
+
EMG Toolbox is a Python package to preprocess, analyse and plot electromyography (EMG) signals, including high-density recordings from electrode grids.
|
|
48
|
+
|
|
49
|
+
The package is composed of the following modules:
|
|
50
|
+
|
|
51
|
+
- `tools`: rearrange channels between `(samples, channels)` and grid `(rows, cols, samples)` formats, and replace bad channels with the mean of their neighbours.
|
|
52
|
+
- `prepro`: Butterworth bandpass, highpass, lowpass and powerline (bandstop) filters.
|
|
53
|
+
- `feats`: amplitude features such as the moving-window root mean square (RMS).
|
|
54
|
+
- `freq`: amplitude spectrum and power spectral density (periodogram or Welch).
|
|
55
|
+
- `plots`: plot channels over time, PSDs, PSD maps across channels, and spectrograms.
|
|
56
|
+
<!-- --8<-- [end:overview] -->
|
|
57
|
+
|
|
58
|
+
## Table of Contents
|
|
59
|
+
- [Installation](#installation)
|
|
60
|
+
- [Quick start](#quick-start)
|
|
61
|
+
- [Documentation](#documentation)
|
|
62
|
+
- [Contributing](#contributing)
|
|
63
|
+
- [License](#license)
|
|
64
|
+
- [Citation](#citation)
|
|
65
|
+
- [Contact](#contact)
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
<!-- --8<-- [start:install] -->
|
|
69
|
+
Install the latest release from PyPI (Python 3.10 or newer):
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
pip install emg-tools
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The package is imported as `emg_tools`.
|
|
76
|
+
<!-- --8<-- [end:install] -->
|
|
77
|
+
|
|
78
|
+
To work on the code itself, see the development setup in [CONTRIBUTING.md](https://github.com/imendezguerra/emg-tools/blob/main/CONTRIBUTING.md#development-setup).
|
|
79
|
+
|
|
80
|
+
## Quick start
|
|
81
|
+
<!-- --8<-- [start:quickstart] -->
|
|
82
|
+
EMG data is an array of shape `(samples, channels)`. The example below filters a synthetic 16-channel recording and computes its RMS envelope:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
import numpy as np
|
|
86
|
+
|
|
87
|
+
from emg_tools import feats, prepro
|
|
88
|
+
|
|
89
|
+
fs = 2048 # sampling frequency (Hz)
|
|
90
|
+
timestamps = np.arange(5 * fs) / fs # 5 s recording
|
|
91
|
+
rng = np.random.default_rng(0)
|
|
92
|
+
emg = rng.standard_normal((timestamps.size, 16)) # (samples, channels)
|
|
93
|
+
emg += 0.5 * np.sin(2 * np.pi * 50 * timestamps)[:, None] # powerline noise
|
|
94
|
+
|
|
95
|
+
emg_filt = prepro.remove_powerline(emg, fs=fs, cutoff=50)
|
|
96
|
+
emg_filt = prepro.bandpass_filter(emg_filt, fs=fs, cutoff=[20, 500])
|
|
97
|
+
|
|
98
|
+
rms, t_win = feats.compute_rms(emg_filt, timestamps, win_len_s=0.25, win_step_s=0.125, fs=fs)
|
|
99
|
+
rms.shape # (10240, 16): one RMS value per sample and channel
|
|
100
|
+
```
|
|
101
|
+
<!-- --8<-- [end:quickstart] -->
|
|
102
|
+
|
|
103
|
+
## Documentation
|
|
104
|
+
The full documentation, with a worked example and the API reference, is at
|
|
105
|
+
[imendezguerra.github.io/emg-tools](https://imendezguerra.github.io/emg-tools/).
|
|
106
|
+
|
|
107
|
+
## Contributing
|
|
108
|
+
Contributions are welcome! [CONTRIBUTING.md](https://github.com/imendezguerra/emg-tools/blob/main/CONTRIBUTING.md) explains how to set up a development environment, run the tests, preview the docs, and what the automated checks (pre-commit and CI) do.
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
This project is licensed under the [MIT License](https://github.com/imendezguerra/emg-tools/blob/main/LICENSE).
|
|
112
|
+
|
|
113
|
+
## Citation
|
|
114
|
+
|
|
115
|
+
If you use this code in your research, please cite this repository:
|
|
116
|
+
|
|
117
|
+
```sh
|
|
118
|
+
@software{Mendez_Guerra_EMG_Toolbox,
|
|
119
|
+
author = {Mendez Guerra, Irene},
|
|
120
|
+
title = {{EMG Toolbox}},
|
|
121
|
+
url = {https://github.com/imendezguerra/emg-tools},
|
|
122
|
+
version = {2.0.0}
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
## Contact
|
|
126
|
+
|
|
127
|
+
For any questions or inquiries, please contact us at:
|
|
128
|
+
```sh
|
|
129
|
+
Irene Mendez Guerra
|
|
130
|
+
irene.mendez17@imperial.ac.uk
|
|
131
|
+
```
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# EMG Toolbox
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/emg-tools/)
|
|
4
|
+
[](https://pypi.org/project/emg-tools/)
|
|
5
|
+
[](https://github.com/imendezguerra/emg-tools/actions/workflows/ci.yml)
|
|
6
|
+
[](https://imendezguerra.github.io/emg-tools/)
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
<!-- --8<-- [start:overview] -->
|
|
10
|
+
EMG Toolbox is a Python package to preprocess, analyse and plot electromyography (EMG) signals, including high-density recordings from electrode grids.
|
|
11
|
+
|
|
12
|
+
The package is composed of the following modules:
|
|
13
|
+
|
|
14
|
+
- `tools`: rearrange channels between `(samples, channels)` and grid `(rows, cols, samples)` formats, and replace bad channels with the mean of their neighbours.
|
|
15
|
+
- `prepro`: Butterworth bandpass, highpass, lowpass and powerline (bandstop) filters.
|
|
16
|
+
- `feats`: amplitude features such as the moving-window root mean square (RMS).
|
|
17
|
+
- `freq`: amplitude spectrum and power spectral density (periodogram or Welch).
|
|
18
|
+
- `plots`: plot channels over time, PSDs, PSD maps across channels, and spectrograms.
|
|
19
|
+
<!-- --8<-- [end:overview] -->
|
|
20
|
+
|
|
21
|
+
## Table of Contents
|
|
22
|
+
- [Installation](#installation)
|
|
23
|
+
- [Quick start](#quick-start)
|
|
24
|
+
- [Documentation](#documentation)
|
|
25
|
+
- [Contributing](#contributing)
|
|
26
|
+
- [License](#license)
|
|
27
|
+
- [Citation](#citation)
|
|
28
|
+
- [Contact](#contact)
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
<!-- --8<-- [start:install] -->
|
|
32
|
+
Install the latest release from PyPI (Python 3.10 or newer):
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
pip install emg-tools
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The package is imported as `emg_tools`.
|
|
39
|
+
<!-- --8<-- [end:install] -->
|
|
40
|
+
|
|
41
|
+
To work on the code itself, see the development setup in [CONTRIBUTING.md](https://github.com/imendezguerra/emg-tools/blob/main/CONTRIBUTING.md#development-setup).
|
|
42
|
+
|
|
43
|
+
## Quick start
|
|
44
|
+
<!-- --8<-- [start:quickstart] -->
|
|
45
|
+
EMG data is an array of shape `(samples, channels)`. The example below filters a synthetic 16-channel recording and computes its RMS envelope:
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
import numpy as np
|
|
49
|
+
|
|
50
|
+
from emg_tools import feats, prepro
|
|
51
|
+
|
|
52
|
+
fs = 2048 # sampling frequency (Hz)
|
|
53
|
+
timestamps = np.arange(5 * fs) / fs # 5 s recording
|
|
54
|
+
rng = np.random.default_rng(0)
|
|
55
|
+
emg = rng.standard_normal((timestamps.size, 16)) # (samples, channels)
|
|
56
|
+
emg += 0.5 * np.sin(2 * np.pi * 50 * timestamps)[:, None] # powerline noise
|
|
57
|
+
|
|
58
|
+
emg_filt = prepro.remove_powerline(emg, fs=fs, cutoff=50)
|
|
59
|
+
emg_filt = prepro.bandpass_filter(emg_filt, fs=fs, cutoff=[20, 500])
|
|
60
|
+
|
|
61
|
+
rms, t_win = feats.compute_rms(emg_filt, timestamps, win_len_s=0.25, win_step_s=0.125, fs=fs)
|
|
62
|
+
rms.shape # (10240, 16): one RMS value per sample and channel
|
|
63
|
+
```
|
|
64
|
+
<!-- --8<-- [end:quickstart] -->
|
|
65
|
+
|
|
66
|
+
## Documentation
|
|
67
|
+
The full documentation, with a worked example and the API reference, is at
|
|
68
|
+
[imendezguerra.github.io/emg-tools](https://imendezguerra.github.io/emg-tools/).
|
|
69
|
+
|
|
70
|
+
## Contributing
|
|
71
|
+
Contributions are welcome! [CONTRIBUTING.md](https://github.com/imendezguerra/emg-tools/blob/main/CONTRIBUTING.md) explains how to set up a development environment, run the tests, preview the docs, and what the automated checks (pre-commit and CI) do.
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
This project is licensed under the [MIT License](https://github.com/imendezguerra/emg-tools/blob/main/LICENSE).
|
|
75
|
+
|
|
76
|
+
## Citation
|
|
77
|
+
|
|
78
|
+
If you use this code in your research, please cite this repository:
|
|
79
|
+
|
|
80
|
+
```sh
|
|
81
|
+
@software{Mendez_Guerra_EMG_Toolbox,
|
|
82
|
+
author = {Mendez Guerra, Irene},
|
|
83
|
+
title = {{EMG Toolbox}},
|
|
84
|
+
url = {https://github.com/imendezguerra/emg-tools},
|
|
85
|
+
version = {2.0.0}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
## Contact
|
|
89
|
+
|
|
90
|
+
For any questions or inquiries, please contact us at:
|
|
91
|
+
```sh
|
|
92
|
+
Irene Mendez Guerra
|
|
93
|
+
irene.mendez17@imperial.ac.uk
|
|
94
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "2.0.0"
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""Amplitude features of EMG signals."""
|
|
2
|
+
|
|
3
|
+
from typing import Optional, Tuple
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from scipy.interpolate import Akima1DInterpolator
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def compute_rms(
|
|
10
|
+
data: np.ndarray,
|
|
11
|
+
timestamps: np.ndarray,
|
|
12
|
+
win_len_s: Optional[float] = 0.1,
|
|
13
|
+
win_step_s: Optional[float] = 0.1,
|
|
14
|
+
fs: Optional[int] = 2048
|
|
15
|
+
) -> Tuple[np.ndarray, np.ndarray]:
|
|
16
|
+
|
|
17
|
+
"""Compute the moving-window Root Mean Square (RMS) of the data.
|
|
18
|
+
|
|
19
|
+
The RMS is computed per window and interpolated (Akima) back to the input
|
|
20
|
+
timestamps. Before the first and after the last window centre the edge values
|
|
21
|
+
are held.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
data (np.ndarray): Input data with shape (samples, chs).
|
|
25
|
+
timestamps (np.ndarray): Time of each sample in seconds, with shape (samples,).
|
|
26
|
+
win_len_s (float, optional): Window length in seconds. Default is 0.1.
|
|
27
|
+
win_step_s (float, optional): Window step in seconds. Default is 0.1.
|
|
28
|
+
fs (int, optional): Sampling frequency in Hz. Default is 2048.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
rms (np.ndarray): RMS interpolated to the input timestamps, with shape
|
|
32
|
+
(samples, chs).
|
|
33
|
+
timestamps_win (np.ndarray): Time of each window centre in seconds, with
|
|
34
|
+
shape (windows,).
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
# Initialise variables
|
|
38
|
+
samples, chs = data.shape
|
|
39
|
+
win_len = min(np.round( win_len_s * fs ).astype(int), samples)
|
|
40
|
+
win_step = np.round( win_step_s * fs ).astype(int)
|
|
41
|
+
|
|
42
|
+
win_num = np.round( (samples - win_len)/win_step ).astype(int) + 1
|
|
43
|
+
|
|
44
|
+
timestamps_aux = np.zeros(win_num)
|
|
45
|
+
rms_aux = np.zeros((win_num, chs))
|
|
46
|
+
|
|
47
|
+
# Compute RMS
|
|
48
|
+
for win in range(win_num):
|
|
49
|
+
mask = np.arange(
|
|
50
|
+
0 + win_step * win,
|
|
51
|
+
np.amin([win_len + win_step * win, samples])
|
|
52
|
+
).astype(int)
|
|
53
|
+
rms_aux[win] = np.sqrt(np.mean(data[mask]**2, axis=0))
|
|
54
|
+
timestamps_aux[win] = np.mean(timestamps[mask])
|
|
55
|
+
|
|
56
|
+
# Single window, no interpolation possible
|
|
57
|
+
if win_num < 2:
|
|
58
|
+
rms = np.tile(rms_aux, (samples, 1))
|
|
59
|
+
return rms, timestamps_aux
|
|
60
|
+
|
|
61
|
+
# Interpolate RMS to match data signals, holding the edge values before
|
|
62
|
+
# the first and after the last window centre
|
|
63
|
+
timestamps_interp = np.clip(timestamps, timestamps_aux[0], timestamps_aux[-1])
|
|
64
|
+
rms = np.zeros_like(data, dtype=float)
|
|
65
|
+
for ch in range(chs):
|
|
66
|
+
rms[:, ch] = Akima1DInterpolator(timestamps_aux, rms_aux[:, ch])(timestamps_interp)
|
|
67
|
+
|
|
68
|
+
return rms, timestamps_aux
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Frequency-domain analysis of EMG signals."""
|
|
2
|
+
|
|
3
|
+
from typing import Optional, Tuple
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from scipy import fft, signal
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def get_spectrum(
|
|
10
|
+
data: np.ndarray,
|
|
11
|
+
fs: Optional[int] = 2048,
|
|
12
|
+
) -> Tuple[np.ndarray, np.ndarray]:
|
|
13
|
+
|
|
14
|
+
"""Compute the single-sided amplitude spectrum of the input data.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
data (np.ndarray): Input data with shape (samples, channels).
|
|
18
|
+
fs (int, optional): Sampling frequency in Hz. Default is 2048.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
spectrum (np.ndarray): Amplitude spectrum of the input data with shape
|
|
22
|
+
(samples//2, channels), in the same units as the data, where the
|
|
23
|
+
first dimension corresponds to the positive frequencies.
|
|
24
|
+
xf (np.ndarray): Frequency axis in Hz, with shape (samples//2,).
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
# Initialise variables
|
|
28
|
+
samples, chs = data.shape
|
|
29
|
+
|
|
30
|
+
# Compute amplitude spectrum
|
|
31
|
+
yf = fft.fft(data, axis=0)
|
|
32
|
+
xf = fft.fftfreq(samples, 1/fs)[:samples//2]
|
|
33
|
+
spectrum = 2/samples * np.abs(yf[0:samples//2])
|
|
34
|
+
spectrum[0] /= 2 # DC component is not mirrored
|
|
35
|
+
|
|
36
|
+
return spectrum, xf
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def get_psd(
|
|
40
|
+
data: np.ndarray,
|
|
41
|
+
fs: Optional[int] = 2048,
|
|
42
|
+
*,
|
|
43
|
+
welch: Optional[bool] = False,
|
|
44
|
+
nperseg: Optional[int] = None,
|
|
45
|
+
) -> Tuple[np.ndarray, np.ndarray]:
|
|
46
|
+
|
|
47
|
+
"""Compute the one-sided Power Spectral Density (PSD) of the input data.
|
|
48
|
+
|
|
49
|
+
By default the periodogram is used. Welch's method can be requested instead.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
data (np.ndarray): Input data with shape (samples, channels).
|
|
53
|
+
fs (int, optional): Sampling frequency in Hz. Default is 2048.
|
|
54
|
+
welch (bool, optional): If True, the PSD is estimated with Welch's
|
|
55
|
+
method. Default is False (periodogram).
|
|
56
|
+
nperseg (int, optional): Length of each segment in samples, only used
|
|
57
|
+
if welch is True. Default is None, which uses scipy's default (256).
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
psd (np.ndarray): One-sided PSD of the input data with shape
|
|
61
|
+
(freqs, channels), in units of data^2/Hz.
|
|
62
|
+
xf (np.ndarray): Frequency axis in Hz, with shape (freqs,).
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
# Compute PSD
|
|
66
|
+
if welch:
|
|
67
|
+
xf, psd = signal.welch(data, fs=fs, nperseg=nperseg, axis=0)
|
|
68
|
+
else:
|
|
69
|
+
xf, psd = signal.periodogram(data, fs=fs, axis=0)
|
|
70
|
+
|
|
71
|
+
return psd, xf
|