Compare commits

..

No commits in common. "232494365313d7f1617a1c0ee85a4d491e090fd9" and "3c47250fb47b5cfe1bccf422721756548b557fd6" have entirely different histories.

5 changed files with 21 additions and 110 deletions

1
.gitignore vendored
View file

@ -1,3 +1,2 @@
env
.env .env
__pycache__/ __pycache__/

View file

@ -9,7 +9,7 @@ readme = "README.md"
requires-python = ">=3.8" requires-python = ">=3.8"
classifiers = [ classifiers = [
"Programming Language :: Python :: 3", "Programming Language :: Python :: 3",
"Development Status :: 4 - Beta", "Development Status :: 2 - Pre-Alpha",
"Intended Audience :: Science/Research", "Intended Audience :: Science/Research",
"Operating System :: OS Independent", "Operating System :: OS Independent",
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
@ -17,13 +17,8 @@ classifiers = [
] ]
dependencies = [ dependencies = [
"scipy", "scipy",
"numpy",
] ]
[project.urls]
Homepage = "https://git.linarphy.net/linarphy/science_signal"
Issues = "https://git.linarphy.net/linarphy/science_signal/issues"
[build-system] [build-system]
requires = ["hatchling", "hatch-gettext"] requires = ["hatchling", "hatch-gettext"]
build-backend = "hatchling.build" build-backend = "hatchling.build"
@ -36,7 +31,6 @@ show-report = true
[tool.ruff] [tool.ruff]
line-length = 72 line-length = 72
builtins = ["_"]
[tool.basedpyright] [tool.basedpyright]
venvPath = ".env" venvPath = ".env"

View file

@ -2,11 +2,7 @@ from numpy.typing import NDArray
from numpy import arange, float64 from numpy import arange, float64
from gettext import install from gettext import install
from logging import getLogger install("science_signal")
install(__name__)
logger = getLogger(__name__)
def interpolate_abciss(*signals: "Signal") -> NDArray[float64]: 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 Return each signal with a common frequency/time range (smallest
range and highest rate) range and highest rate)
""" """
logger.debug(_("interpolate {n} signals").format(n=len(signals)))
splines = [signal.spline() for signal in signals] splines = [signal.spline() for signal in signals]
x_range = interpolate_abciss(*signals) x_range = interpolate_abciss(*signals)

View file

@ -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 numpy.random import default_rng
from science_signal.signal import Signal from science_signal.signal import Signal
@ -8,7 +8,6 @@ def sin(
rate: float = 10, rate: float = 10,
frequency: float = 1, frequency: float = 1,
amplitude: float = 1, amplitude: float = 1,
offset: float = 0,
phase: float = 0, phase: float = 0,
) -> Signal: ) -> Signal:
""" """
@ -17,11 +16,10 @@ def sin(
duration: duration of the signal in second 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)
frequency: frequency of the wanted sinus frequency: frequency of the wanted sinus
offset: mean of the wanted sinus
phase: between 0 and 2 pi (if more, congruate) phase: between 0 and 2 pi (if more, congruate)
""" """
x = arange(0, duration, 1 / rate) 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( return Signal(
x, x,
@ -34,7 +32,6 @@ def cos(
rate: float = 10, rate: float = 10,
frequency: float = 1, frequency: float = 1,
amplitude: float = 1, amplitude: float = 1,
offset: float = 0,
phase: float = 0, phase: float = 0,
) -> Signal: ) -> Signal:
""" """
@ -45,36 +42,7 @@ def cos(
frequency: frequency of the wanted sinus frequency: frequency of the wanted sinus
phase: between 0 and 2 pi (if more, congruate) phase: between 0 and 2 pi (if more, congruate)
""" """
return sin( return sin(duration, rate, frequency, amplitude, phase + pi / 2)
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,
)
def gaussian_noise( def gaussian_noise(
@ -88,36 +56,12 @@ def gaussian_noise(
Generate a gaussian noise signal Generate a gaussian noise signal
duration: duration of the signal in second 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 sigma: standard deviation of the wanted distribution
mu: mean of the distribution mu: mean of the distribution
seed: optional, allowd to specify a seed for testing purpose
""" """
x = arange(0, duration, 1 / rate) x = arange(0, duration, 1 / rate)
return Signal( return Signal(
x, x,
default_rng(seed).normal(mu, sigma, len(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)),
)

View file

@ -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.typing import ArrayLike, NDArray
from numpy import ( from numpy import (
append, append,
@ -6,14 +6,14 @@ from numpy import (
float64, float64,
linspace, linspace,
array, array,
logical_and, logical_or,
sin, sin,
where, where,
arange, arange,
zeros, zeros,
) )
from scipy.signal import detrend, welch # type: ignore[reportMissingTypeStubs] from scipy.signal import detrend, welch
from scipy.fft import rfftfreq, rfft, irfft # type: ignore[reportUnknownVariableType] from scipy.fft import rfftfreq, rfft, irfft
from science_signal import interpolate from science_signal import interpolate
@ -66,9 +66,7 @@ class Signal:
cos(self.y), cos(self.y),
) )
def cut( def cut(self, start: None | float = None, end: None | float = None) -> "Signal":
self, start: None | float = None, end: None | float = None
) -> "Signal":
""" """
Cut signal from a start x to an end x Cut signal from a start x to an end x
""" """
@ -76,7 +74,7 @@ class Signal:
start = min(self.x) start = min(self.x)
if end is None: if end is None:
end = max(self.x) 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( return Signal(
self.x[indexes], self.x[indexes],
self.y[indexes], self.y[indexes],
@ -113,52 +111,33 @@ class Signal:
def filter( def filter(
self, start: None | float = None, end: None | float = None self, start: None | float = None, end: None | float = None
) -> "Signal": ) -> "Signal":
freq_x = rfftfreq(len(self), self.sampling) # type: ignore[reportUnknownVariableType] freq_x = rfftfreq(len(self), self.sampling)
freq_y = rfft(self.y) freq_y = rfft(self.y)
rate = 1 / (freq_x[1] - freq_x[0]) # type: ignore[reportUnknownVariableType]
if start is None: if start is None:
start = min(abs(freq_x)) start = min(abs(freq_x))
if end is None: if end is None:
end = max(abs(freq_x)) end = max(abs(freq_x))
index_start = where(abs(freq_x) < start) # type: ignore[reportOperatorIssue] index_to_remove = where(
index_end = where(abs(freq_x) > end) # type: ignore[reportOperatorIssue] logical_or(abs(freq_x) <= start, abs(freq_x) >= end)
)
if len(index_start) != 0: freq_y[index_to_remove] = 0
"""
damping_start = 10 ** -( # type: ignore[reportUnknownVariableType]
arange(1, len(index_start[0]) + 1, 1, dtype=float64)
/ rate
)
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
y = irfft(freq_y) y = irfft(freq_y)
return Signal( return Signal(
self.x[: len(y)], self.x[: len(y)],
y[: len(self.x)], # type: ignore[reportArgumentType] y[: len(self.x)],
) )
def psd(self, fft_length: int = 10) -> "Signal": def psd(self, fft_length: int = 10) -> "Signal":
""" """
Compute psd of a given signal Compute psd of a given signal
""" """
freq, psd = welch( # type: ignore[reportUnknownVariableType] freq, psd = welch(
self.y, self.y,
self.rate, self.rate,
nperseg=fft_length * self.rate, nperseg=fft_length * self.rate,
detrend="linear",
) )
return Signal(freq, psd) return Signal(freq, psd)
@ -210,7 +189,7 @@ class Signal:
# suppose same range but different rates # suppose same range but different rates
return self.operator_signal( return self.operator_signal(
Signal( Signal(
linspace(self.x[0], self.x[-1], len(other)), linspace(self.x[0], self.x[-1], len(others)),
array(other), array(other),
), ),
operator, operator,