Compare commits
No commits in common. "232494365313d7f1617a1c0ee85a4d491e090fd9" and "3c47250fb47b5cfe1bccf422721756548b557fd6" have entirely different histories.
2324943653
...
3c47250fb4
5 changed files with 21 additions and 110 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,2 @@
|
|||
env
|
||||
.env
|
||||
__pycache__/
|
||||
|
|
|
@ -9,7 +9,7 @@ readme = "README.md"
|
|||
requires-python = ">=3.8"
|
||||
classifiers = [
|
||||
"Programming Language :: Python :: 3",
|
||||
"Development Status :: 4 - Beta",
|
||||
"Development Status :: 2 - Pre-Alpha",
|
||||
"Intended Audience :: Science/Research",
|
||||
"Operating System :: OS Independent",
|
||||
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
|
||||
|
@ -17,13 +17,8 @@ classifiers = [
|
|||
]
|
||||
dependencies = [
|
||||
"scipy",
|
||||
"numpy",
|
||||
]
|
||||
|
||||
[project.urls]
|
||||
Homepage = "https://git.linarphy.net/linarphy/science_signal"
|
||||
Issues = "https://git.linarphy.net/linarphy/science_signal/issues"
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling", "hatch-gettext"]
|
||||
build-backend = "hatchling.build"
|
||||
|
@ -36,7 +31,6 @@ show-report = true
|
|||
|
||||
[tool.ruff]
|
||||
line-length = 72
|
||||
builtins = ["_"]
|
||||
|
||||
[tool.basedpyright]
|
||||
venvPath = ".env"
|
||||
|
|
|
@ -2,11 +2,7 @@ from numpy.typing import NDArray
|
|||
from numpy import arange, float64
|
||||
from gettext import install
|
||||
|
||||
from logging import getLogger
|
||||
|
||||
install(__name__)
|
||||
|
||||
logger = getLogger(__name__)
|
||||
install("science_signal")
|
||||
|
||||
|
||||
def interpolate_abciss(*signals: "Signal") -> NDArray[float64]:
|
||||
|
@ -25,7 +21,6 @@ def interpolate(*signals: "Signal") -> tuple["Signal", ...]:
|
|||
Return each signal with a common frequency/time range (smallest
|
||||
range and highest rate)
|
||||
"""
|
||||
logger.debug(_("interpolate {n} signals").format(n=len(signals)))
|
||||
splines = [signal.spline() for signal in signals]
|
||||
x_range = interpolate_abciss(*signals)
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from numpy import arange, pi, sign, sin as np_sin
|
||||
from numpy import arange, pi, sin as np_sin
|
||||
from numpy.random import default_rng
|
||||
from science_signal.signal import Signal
|
||||
|
||||
|
@ -8,7 +8,6 @@ def sin(
|
|||
rate: float = 10,
|
||||
frequency: float = 1,
|
||||
amplitude: float = 1,
|
||||
offset: float = 0,
|
||||
phase: float = 0,
|
||||
) -> Signal:
|
||||
"""
|
||||
|
@ -17,11 +16,10 @@ def sin(
|
|||
duration: duration of the signal in second
|
||||
rate: rate of the signal in Hz (s-1)
|
||||
frequency: frequency of the wanted sinus
|
||||
offset: mean of the wanted sinus
|
||||
phase: between 0 and 2 pi (if more, congruate)
|
||||
"""
|
||||
x = arange(0, duration, 1 / rate)
|
||||
y = offset + amplitude * np_sin(2 * pi * frequency * x + phase)
|
||||
y = amplitude * np_sin(2 * pi * frequency * x + phase)
|
||||
|
||||
return Signal(
|
||||
x,
|
||||
|
@ -34,7 +32,6 @@ def cos(
|
|||
rate: float = 10,
|
||||
frequency: float = 1,
|
||||
amplitude: float = 1,
|
||||
offset: float = 0,
|
||||
phase: float = 0,
|
||||
) -> Signal:
|
||||
"""
|
||||
|
@ -45,36 +42,7 @@ def cos(
|
|||
frequency: frequency of the wanted sinus
|
||||
phase: between 0 and 2 pi (if more, congruate)
|
||||
"""
|
||||
return sin(
|
||||
duration, rate, frequency, amplitude, offset, phase + pi / 2
|
||||
)
|
||||
|
||||
|
||||
def square(
|
||||
duration: float = 10,
|
||||
rate: float = 10,
|
||||
frequency: float = 1,
|
||||
amplitude: float = 1,
|
||||
offset: float = 0,
|
||||
phase: float = 0,
|
||||
) -> Signal:
|
||||
"""
|
||||
Generate square signal
|
||||
|
||||
duration: duration of the signal in second
|
||||
rate: rate of the signal in Hz (s-1)
|
||||
frequency: frequency of the wanted square signal
|
||||
phase: between 0 and 2 pi (if more, congruate)
|
||||
"""
|
||||
x = arange(0, duration, 1 / rate)
|
||||
y = offset + amplitude * sign(
|
||||
np_sin(2 * pi * frequency * x + phase)
|
||||
)
|
||||
|
||||
return Signal(
|
||||
x,
|
||||
y,
|
||||
)
|
||||
return sin(duration, rate, frequency, amplitude, phase + pi / 2)
|
||||
|
||||
|
||||
def gaussian_noise(
|
||||
|
@ -88,36 +56,12 @@ def gaussian_noise(
|
|||
Generate a gaussian noise signal
|
||||
|
||||
duration: duration of the signal in second
|
||||
rate: rate of the signal in Hz (s-1)
|
||||
rate: rate of the signal in hz (s-1)
|
||||
sigma: standard deviation of the wanted distribution
|
||||
mu: mean of the distribution
|
||||
seed: optional, allowd to specify a seed for testing purpose
|
||||
"""
|
||||
x = arange(0, duration, 1 / rate)
|
||||
return Signal(
|
||||
x,
|
||||
default_rng(seed).normal(mu, sigma, len(x)),
|
||||
)
|
||||
|
||||
|
||||
def white_noise(
|
||||
duration: float = 10,
|
||||
rate: float = 10,
|
||||
minimum: float = -1,
|
||||
maximum: float = 1,
|
||||
seed: None | int = None,
|
||||
) -> Signal:
|
||||
"""
|
||||
Generate a white noise signal
|
||||
|
||||
duration: duration of the signal in seconds
|
||||
rate: rate of the signal in hz (s-1)
|
||||
minimum: minimum value of the noise
|
||||
maximum: maximum value of the noise
|
||||
seed: optional, allowd to specify a seed for testing purpose
|
||||
"""
|
||||
x = arange(0, duration, 1 / rate)
|
||||
return Signal(
|
||||
x,
|
||||
default_rng(seed).uniform(minimum, maximum, len(x)),
|
||||
)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from scipy.interpolate import CubicSpline # type: ignore[reportMissingTypeStubs]
|
||||
from scipy.interpolate import CubicSpline # pyright: ignore[reportMissingTypeStubs]
|
||||
from numpy.typing import ArrayLike, NDArray
|
||||
from numpy import (
|
||||
append,
|
||||
|
@ -6,14 +6,14 @@ from numpy import (
|
|||
float64,
|
||||
linspace,
|
||||
array,
|
||||
logical_and,
|
||||
logical_or,
|
||||
sin,
|
||||
where,
|
||||
arange,
|
||||
zeros,
|
||||
)
|
||||
from scipy.signal import detrend, welch # type: ignore[reportMissingTypeStubs]
|
||||
from scipy.fft import rfftfreq, rfft, irfft # type: ignore[reportUnknownVariableType]
|
||||
from scipy.signal import detrend, welch
|
||||
from scipy.fft import rfftfreq, rfft, irfft
|
||||
from science_signal import interpolate
|
||||
|
||||
|
||||
|
@ -66,9 +66,7 @@ class Signal:
|
|||
cos(self.y),
|
||||
)
|
||||
|
||||
def cut(
|
||||
self, start: None | float = None, end: None | float = None
|
||||
) -> "Signal":
|
||||
def cut(self, start: None | float = None, end: None | float = None) -> "Signal":
|
||||
"""
|
||||
Cut signal from a start x to an end x
|
||||
"""
|
||||
|
@ -76,7 +74,7 @@ class Signal:
|
|||
start = min(self.x)
|
||||
if end is None:
|
||||
end = max(self.x)
|
||||
indexes = where(logical_and(self.x >= start, self.x <= end)) # type: ignore[reportOperatorIssue]
|
||||
indexes = where(logical_and(self.x >= start, self.x <= end))
|
||||
return Signal(
|
||||
self.x[indexes],
|
||||
self.y[indexes],
|
||||
|
@ -113,52 +111,33 @@ class Signal:
|
|||
def filter(
|
||||
self, start: None | float = None, end: None | float = None
|
||||
) -> "Signal":
|
||||
freq_x = rfftfreq(len(self), self.sampling) # type: ignore[reportUnknownVariableType]
|
||||
freq_x = rfftfreq(len(self), self.sampling)
|
||||
freq_y = rfft(self.y)
|
||||
|
||||
rate = 1 / (freq_x[1] - freq_x[0]) # type: ignore[reportUnknownVariableType]
|
||||
|
||||
if start is None:
|
||||
start = min(abs(freq_x))
|
||||
if end is None:
|
||||
end = max(abs(freq_x))
|
||||
|
||||
index_start = where(abs(freq_x) < start) # type: ignore[reportOperatorIssue]
|
||||
index_end = where(abs(freq_x) > end) # type: ignore[reportOperatorIssue]
|
||||
|
||||
if len(index_start) != 0:
|
||||
"""
|
||||
damping_start = 10 ** -( # type: ignore[reportUnknownVariableType]
|
||||
arange(1, len(index_start[0]) + 1, 1, dtype=float64)
|
||||
/ rate
|
||||
index_to_remove = where(
|
||||
logical_or(abs(freq_x) <= start, abs(freq_x) >= end)
|
||||
)
|
||||
freq_y[index_start] = freq_y[index_start] * damping_start # type: ignore[reportIndexIssue]
|
||||
"""
|
||||
freq_y[index_start] = 0
|
||||
if len(index_end) != 0:
|
||||
"""
|
||||
damping_end = 10 ** -( # type: ignore[reportUnknownVariableType]
|
||||
arange(1, len(index_end[0]) + 1, 1, dtype=float64)
|
||||
/ rate
|
||||
)
|
||||
freq_y[index_end] = freq_y[index_end] * damping_end # type: ignore[reportAny]
|
||||
"""
|
||||
freq_y[index_end] = 0
|
||||
|
||||
freq_y[index_to_remove] = 0
|
||||
y = irfft(freq_y)
|
||||
return Signal(
|
||||
self.x[: len(y)],
|
||||
y[: len(self.x)], # type: ignore[reportArgumentType]
|
||||
y[: len(self.x)],
|
||||
)
|
||||
|
||||
def psd(self, fft_length: int = 10) -> "Signal":
|
||||
"""
|
||||
Compute psd of a given signal
|
||||
"""
|
||||
freq, psd = welch( # type: ignore[reportUnknownVariableType]
|
||||
freq, psd = welch(
|
||||
self.y,
|
||||
self.rate,
|
||||
nperseg=fft_length * self.rate,
|
||||
detrend="linear",
|
||||
)
|
||||
|
||||
return Signal(freq, psd)
|
||||
|
@ -210,7 +189,7 @@ class Signal:
|
|||
# suppose same range but different rates
|
||||
return self.operator_signal(
|
||||
Signal(
|
||||
linspace(self.x[0], self.x[-1], len(other)),
|
||||
linspace(self.x[0], self.x[-1], len(others)),
|
||||
array(other),
|
||||
),
|
||||
operator,
|
||||
|
|
Loading…
Reference in a new issue