g2ltk 1.1.9__py3-none-any.whl
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.
- g2ltk/__init__.py +39 -0
- g2ltk/fourier/FFT1D.py +385 -0
- g2ltk/fourier/FFT2D.py +639 -0
- g2ltk/fourier/__init__.py +50 -0
- g2ltk/logging.py +87 -0
- g2ltk/peakfinder.py +199 -0
- g2ltk/rivulets/__init__.py +0 -0
- g2ltk/rivulets/datasaving.py +299 -0
- g2ltk/rivulets/fringeswork.py +274 -0
- g2ltk/rivulets/rivuletfinding.py +1036 -0
- g2ltk/rivulets/utility/__init__.py +330 -0
- g2ltk/rivulets/utility/fourier.py +1001 -0
- g2ltk/rivulets/utility/genfig.py +581 -0
- g2ltk/rivulets/utility/plotting.py +115 -0
- g2ltk/scripts/jupytopy.py +97 -0
- g2ltk/scripts/pytojupy.py +98 -0
- g2ltk/videoreading/__init__.py +643 -0
- g2ltk/videoreading/reading_datasheets.py +192 -0
- g2ltk/videoreading/reading_gcv.py +445 -0
- g2ltk/videoreading/reading_lcv.py +94 -0
- g2ltk/videoreading/reading_mov.py +108 -0
- g2ltk/videoreading/reading_mp4.py +121 -0
- g2ltk/videoreading/reading_tiffs.py +257 -0
- g2ltk/videoreading/saving_videos.py +186 -0
- g2ltk-1.1.9.dist-info/METADATA +31 -0
- g2ltk-1.1.9.dist-info/RECORD +29 -0
- g2ltk-1.1.9.dist-info/WHEEL +4 -0
- g2ltk-1.1.9.dist-info/entry_points.txt +3 -0
- g2ltk-1.1.9.dist-info/licenses/LICENSE +674 -0
g2ltk/__init__.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
from colorama import Fore # change display text color
|
|
4
|
+
|
|
5
|
+
__DISPLAYSIZE__:int = 80
|
|
6
|
+
|
|
7
|
+
def force_print(text:str, flush:bool=True, end:str='\n', padding:bool=True, displaytype:str=""):
|
|
8
|
+
text = str(text)
|
|
9
|
+
text = '\r' + text + max(__DISPLAYSIZE__ - len(text), 0) * ' ' * padding
|
|
10
|
+
print(text, flush=flush, end=end)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
VIOLET = '#4f0e51'
|
|
14
|
+
JAUNE = '#eaac3f'
|
|
15
|
+
|
|
16
|
+
__version__ = '1.1.9'
|
|
17
|
+
|
|
18
|
+
import warnings
|
|
19
|
+
_warn_skips = (os.path.dirname(__file__),)
|
|
20
|
+
|
|
21
|
+
class G2LWarning(UserWarning):
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
def throw_G2L_warning(text:str):
|
|
25
|
+
warnings.warn(Fore.LIGHTYELLOW_EX + 'Warning: ' + text + Fore.RESET, category=G2LWarning, stacklevel=3
|
|
26
|
+
# skip_file_prefixes=_warn_skips # THIS ONLY WORKS FOR PYTHON >= 3.12
|
|
27
|
+
)
|
|
28
|
+
sys.stderr.flush() ; force_print('') # force to display warning at runtime
|
|
29
|
+
|
|
30
|
+
from .logging import *
|
|
31
|
+
|
|
32
|
+
log_info('Loading g2ltk version '+__version__)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def display(text:str, flush:bool=True, end:str='\n', padding:bool=True, displaytype:str=""):
|
|
36
|
+
log_warning('The use of g2ltk.display function is deprecated !')
|
|
37
|
+
force_print(text, flush=flush, end=end, padding=padding, displaytype=displaytype)
|
|
38
|
+
|
|
39
|
+
|
g2ltk/fourier/FFT1D.py
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
from typing import Optional, Any, Tuple, Dict, List, Union
|
|
2
|
+
import numpy as np
|
|
3
|
+
import math
|
|
4
|
+
from scipy.signal.windows import get_window # FFT windowing
|
|
5
|
+
from skimage import filters # filters.window for 2D FFT windowing
|
|
6
|
+
from scipy import fft
|
|
7
|
+
|
|
8
|
+
from .. import log_error, log_warning, log_info, log_debug, log_trace, log_subtrace
|
|
9
|
+
from g2ltk.peakfinder import step, span, interp_roots, find_global_max
|
|
10
|
+
from . import floatarray1D, complexarray1D, attenuate_power
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Dual: changing from real space to frequency space
|
|
14
|
+
def dual1d(arr: floatarray1D, zero_pad: Optional[int] = None, zero_pad_factor: Optional[int] = None) -> floatarray1D:
|
|
15
|
+
"""
|
|
16
|
+
Returns the dual, i.e. the frequencies.
|
|
17
|
+
|
|
18
|
+
The unit is the inverse, e.g. time (s)-> frequency (Hz).
|
|
19
|
+
|
|
20
|
+
Parameters
|
|
21
|
+
----------
|
|
22
|
+
arr
|
|
23
|
+
zero_pad
|
|
24
|
+
zero_pad_factor
|
|
25
|
+
|
|
26
|
+
Returns
|
|
27
|
+
-------
|
|
28
|
+
|
|
29
|
+
"""
|
|
30
|
+
N = arr.shape[0]
|
|
31
|
+
log_trace(f'dual: Called with {arr.shape} array, zp = {zero_pad}, zpf = {zero_pad_factor}')
|
|
32
|
+
|
|
33
|
+
pad_width = None
|
|
34
|
+
if zero_pad_factor is not None:
|
|
35
|
+
log_subtrace(f'rft2d: | zero_pad_factor={zero_pad_factor}')
|
|
36
|
+
try:
|
|
37
|
+
pad = np.rint(N * (zero_pad_factor - 1)).astype(int)
|
|
38
|
+
pad_width = (pad // 2, pad // 2 + pad % 2)
|
|
39
|
+
except:
|
|
40
|
+
log_warning(f'rft2d: What is this zero-padding factor "{zero_pad_factor}" ? I made it None')
|
|
41
|
+
pad_width = None
|
|
42
|
+
elif zero_pad is not None:
|
|
43
|
+
log_subtrace(f'rft2d: | zero_pad={zero_pad}')
|
|
44
|
+
try:
|
|
45
|
+
pad_width = (0, int(zero_pad))
|
|
46
|
+
except:
|
|
47
|
+
log_warning(f'rft2d: What is this zero-padding "{zero_pad}" ? I made it None')
|
|
48
|
+
pad_width = None
|
|
49
|
+
|
|
50
|
+
log_subtrace(f'rft2d: Padding (artificially better resolution) | pad={pad_width}')
|
|
51
|
+
if pad_width is None:
|
|
52
|
+
pad_width = (0, 0)
|
|
53
|
+
|
|
54
|
+
n = N + pad_width[0] + pad_width[1]
|
|
55
|
+
|
|
56
|
+
log_debug(f'dual: {N} -> {n}')
|
|
57
|
+
|
|
58
|
+
return np.fft.fftshift(np.fft.fftfreq(n, step(arr)))
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def rdual1d(arr: floatarray1D, zero_pad: Optional[int] = None, zero_pad_factor: Optional[int] = None) -> floatarray1D:
|
|
62
|
+
"""Returns the dual, i.e. the frequencies, in a numpy.fft.rfft-compatible style.
|
|
63
|
+
|
|
64
|
+
The unit is the inverse, e.g. time (s)-> frequency (Hz).
|
|
65
|
+
|
|
66
|
+
Parameters
|
|
67
|
+
----------
|
|
68
|
+
arr
|
|
69
|
+
zero_pad
|
|
70
|
+
zero_pad_factor
|
|
71
|
+
|
|
72
|
+
Returns
|
|
73
|
+
-------
|
|
74
|
+
|
|
75
|
+
"""
|
|
76
|
+
N = arr.shape[0]
|
|
77
|
+
log_trace(f'rdual: Called with {arr.shape} array, zp = {zero_pad}, zpf = {zero_pad_factor}')
|
|
78
|
+
|
|
79
|
+
pad_width = None
|
|
80
|
+
if zero_pad_factor is not None:
|
|
81
|
+
log_subtrace(f'rft2d: | zero_pad_factor={zero_pad_factor}')
|
|
82
|
+
try:
|
|
83
|
+
pad = np.rint(N * (zero_pad_factor - 1)).astype(int)
|
|
84
|
+
pad_width = (pad // 2, pad // 2 + pad % 2)
|
|
85
|
+
except:
|
|
86
|
+
log_warning(f'rft2d: What is this zero-padding factor "{zero_pad_factor}" ? I made it None')
|
|
87
|
+
pad_width = None
|
|
88
|
+
elif zero_pad is not None:
|
|
89
|
+
log_subtrace(f'rft2d: | zero_pad={zero_pad}')
|
|
90
|
+
try:
|
|
91
|
+
pad_width = (0, int(zero_pad))
|
|
92
|
+
except:
|
|
93
|
+
log_warning(f'rft2d: What is this zero-padding "{zero_pad}" ? I made it None')
|
|
94
|
+
pad_width = None
|
|
95
|
+
|
|
96
|
+
log_subtrace(f'rft2d: Padding (artificially better resolution) | pad={pad_width}')
|
|
97
|
+
if pad_width is None:
|
|
98
|
+
pad_width = (0, 0)
|
|
99
|
+
|
|
100
|
+
n = N + pad_width[0] + pad_width[1]
|
|
101
|
+
|
|
102
|
+
log_debug(f'rdual: {N} -> {n}')
|
|
103
|
+
|
|
104
|
+
return np.fft.rfftfreq(n, step(arr))
|
|
105
|
+
|
|
106
|
+
### FT: computing the Fourier Transform
|
|
107
|
+
def prepare_signal_for_ft1d(arr: complexarray1D, window: str = 'hann',
|
|
108
|
+
zero_pad: Optional[int] = None, zero_pad_factor: Optional[int] = None) -> complexarray1D:
|
|
109
|
+
N = arr.shape[0]
|
|
110
|
+
|
|
111
|
+
# Removing mean
|
|
112
|
+
z_nozero = arr - np.mean(arr)
|
|
113
|
+
|
|
114
|
+
# windowing
|
|
115
|
+
z_win = z_nozero * get_window(window, N)
|
|
116
|
+
|
|
117
|
+
pad_width = None
|
|
118
|
+
if zero_pad_factor is not None:
|
|
119
|
+
log_subtrace(f'rft2d: | zero_pad_factor={zero_pad_factor}')
|
|
120
|
+
try:
|
|
121
|
+
pad = np.rint(N * (zero_pad_factor - 1)).astype(int)
|
|
122
|
+
pad_width = (pad // 2, pad // 2 + pad % 2)
|
|
123
|
+
except:
|
|
124
|
+
log_warning(f'rft2d: What is this zero-padding factor "{zero_pad_factor}" ? I made it None')
|
|
125
|
+
pad_width = None
|
|
126
|
+
elif zero_pad is not None:
|
|
127
|
+
log_subtrace(f'rft2d: | zero_pad={zero_pad}')
|
|
128
|
+
try:
|
|
129
|
+
pad_width = (0, int(zero_pad))
|
|
130
|
+
except:
|
|
131
|
+
log_warning(f'rft2d: What is this zero-padding "{zero_pad}" ? I made it None')
|
|
132
|
+
pad_width = None
|
|
133
|
+
|
|
134
|
+
log_subtrace(f'rft2d: Padding (artificially better resolution) | pad={pad_width}')
|
|
135
|
+
if pad_width is None:
|
|
136
|
+
pad_width = (0, 0)
|
|
137
|
+
z_pad = np.pad(z_win, pad_width=pad_width, mode='constant', constant_values=0)
|
|
138
|
+
|
|
139
|
+
# z_treated -= np.mean(z_treated) * (1-1e-12) # this is to avoid having zero amplitude and problems when taking the log
|
|
140
|
+
z_clean = z_pad - np.mean(z_pad)
|
|
141
|
+
|
|
142
|
+
log_subtrace(f'rft2d: Rolling (restoring phase) | pad={pad_width}')
|
|
143
|
+
z_roll = np.roll(z_clean, pad_width[0] + N // 2)
|
|
144
|
+
|
|
145
|
+
return z_roll
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def rft1d(arr: floatarray1D, x: Optional[floatarray1D] = None, window: str = 'hann', norm=None,
|
|
149
|
+
zero_pad: Optional[int] = None, zero_pad_factor: Optional[int] = None) -> complexarray1D:
|
|
150
|
+
""" Returns the 1-D Fourier transform of the input array using the given windowing.
|
|
151
|
+
|
|
152
|
+
The unit is in amplitude/(inverse period), e.g. V(s) -> V/Hz(Hz)
|
|
153
|
+
|
|
154
|
+
Parameters
|
|
155
|
+
----------
|
|
156
|
+
arr
|
|
157
|
+
x
|
|
158
|
+
window
|
|
159
|
+
norm
|
|
160
|
+
zero_pad
|
|
161
|
+
zero_pad_factor
|
|
162
|
+
|
|
163
|
+
Returns
|
|
164
|
+
-------
|
|
165
|
+
|
|
166
|
+
"""
|
|
167
|
+
log_trace(f'rft2d: Computing 2-D FFT of array of shape {arr.shape}')
|
|
168
|
+
|
|
169
|
+
z_roll = prepare_signal_for_ft1d(arr, window=window, zero_pad=zero_pad, zero_pad_factor=zero_pad_factor)
|
|
170
|
+
|
|
171
|
+
z_hat = fft.rfft(z_roll, norm=norm, n=z_roll.shape[0])
|
|
172
|
+
|
|
173
|
+
return z_hat * step(x)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def ft1d(arr: complexarray1D, x: Optional[floatarray1D] = None, window: str = 'hann', norm=None,
|
|
177
|
+
zero_pad: Optional[int] = None, zero_pad_factor: Optional[int] = None) -> complexarray1D:
|
|
178
|
+
""" Returns the 1-D Fourier transform of the input array using the given windowing.
|
|
179
|
+
|
|
180
|
+
The unit is in amplitude/(inverse period), e.g. V(s) -> V/Hz(Hz)
|
|
181
|
+
|
|
182
|
+
Parameters
|
|
183
|
+
----------
|
|
184
|
+
arr
|
|
185
|
+
x
|
|
186
|
+
window
|
|
187
|
+
norm
|
|
188
|
+
zero_pad
|
|
189
|
+
zero_pad_factor
|
|
190
|
+
|
|
191
|
+
Returns
|
|
192
|
+
-------
|
|
193
|
+
|
|
194
|
+
"""
|
|
195
|
+
log_trace(f'rft2d: Computing 2-D FFT of array of shape {arr.shape}')
|
|
196
|
+
|
|
197
|
+
arr_prepared = prepare_signal_for_ft1d(arr, window=window, zero_pad=zero_pad, zero_pad_factor=zero_pad_factor)
|
|
198
|
+
|
|
199
|
+
z_hat = fft.fft(arr_prepared, norm=norm, n=arr_prepared.shape[0])
|
|
200
|
+
|
|
201
|
+
return fft.fftshift(z_hat) * step(x)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def ifft1d(zhat: complexarray1D, xdual: Optional[np.ndarray] = None):
|
|
205
|
+
return fft.fftshift(fft.ifft(np.fft.ifftshift(zhat))) * span(xdual)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
### Power:
|
|
209
|
+
|
|
210
|
+
def window_factor1d(window: str):
|
|
211
|
+
"""
|
|
212
|
+
Returns the factor by which the energy is multiplied when the signal is windowed.
|
|
213
|
+
|
|
214
|
+
Parameters
|
|
215
|
+
----------
|
|
216
|
+
window
|
|
217
|
+
|
|
218
|
+
Returns
|
|
219
|
+
-------
|
|
220
|
+
|
|
221
|
+
"""
|
|
222
|
+
if window is None:
|
|
223
|
+
return 1.
|
|
224
|
+
elif window == 'boxcar':
|
|
225
|
+
return 1.
|
|
226
|
+
elif window == 'hann':
|
|
227
|
+
return 8 / 3
|
|
228
|
+
else:
|
|
229
|
+
N = 10000
|
|
230
|
+
return 1 / ((get_window(window, N) ** 2).sum() / N)
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def psd1d(z: np.ndarray, x: Optional[np.ndarray] = None, window: str = 'hann', zero_pad: Optional[int] = None,
|
|
235
|
+
zero_pad_factor: Optional[float] = None) -> floatarray1D:
|
|
236
|
+
### Step 1 : do the dimensional Fourier transform
|
|
237
|
+
# if the unit of z(t) is [V(s)], then the unit of $\hat{z}$ is [V/Hz(Hz)]
|
|
238
|
+
z_ft = rft1d(z, x=x, window=window, norm="backward", zero_pad=zero_pad, zero_pad_factor=zero_pad_factor)
|
|
239
|
+
### Step 2 : compute the ESD (Energy Spectral Density)
|
|
240
|
+
# Rigorously, this is the only thing we can really measure with discretized inputs and FFT
|
|
241
|
+
# It is the total energy (i.e., during all the sampling time) of the signal at this frequency
|
|
242
|
+
# It is useful in itself for time-limited signals (impulsions)
|
|
243
|
+
# if the unit of z(t) is [V(s)], then the unit of $ESD(z)$ is [V^2/Hz^2(Hz)]
|
|
244
|
+
esd = np.abs(z_ft) ** 2 * window_factor1d(window)
|
|
245
|
+
### Step 3 : compute the PSD (Power Spectral Density)
|
|
246
|
+
# Assuming that the signal is periodic, then PSD = ESD / duration
|
|
247
|
+
# Thus if the unit of z(t) is [V(s)], then the unit of PSD(z)$ is [V^2/Hz(Hz)]
|
|
248
|
+
return esd / span(x)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def rpsd1d(z: floatarray1D, x: Optional[np.ndarray] = None, window: str = 'hann', zero_pad: Optional[int] = None,
|
|
252
|
+
zero_pad_factor: Optional[float] = None) -> floatarray1D:
|
|
253
|
+
### Step 1 : do the dimensional Fourier transform
|
|
254
|
+
# if the unit of z(t) is [V(s)], then the unit of $\hat{z}$ is [V/Hz(Hz)]
|
|
255
|
+
z_ft = rft1d(z, x=x, window=window, norm="backward", zero_pad=zero_pad, zero_pad_factor=zero_pad_factor)
|
|
256
|
+
### Step 2 : compute the ESD (Energy Spectral Density)
|
|
257
|
+
# Rigorously, this is the only thing we can really measure with discretized inputs and FFT
|
|
258
|
+
# It is the total energy (i.e., during all the sampling time) of the signal at this frequency
|
|
259
|
+
# It is useful in itself for time-limited signals (impulsions)
|
|
260
|
+
# if the unit of z(t) is [V(s)], then the unit of $ESD(z)$ is [V^2/Hz^2(Hz)]
|
|
261
|
+
esd = np.abs(z_ft) ** 2 * window_factor1d(window)
|
|
262
|
+
esd[1:] *= 2 # x 2 because of rfft which truncates the spectrum (except the 0 harmonic)
|
|
263
|
+
### Step 3 : compute the PSD (Power Spectral Density)
|
|
264
|
+
# Assuming that the signal is periodic, then PSD = ESD / duration
|
|
265
|
+
# Thus if the unit of z(t) is [V(s)], then the unit of PSD(z)$ is [V^2/Hz(Hz)]
|
|
266
|
+
return esd / span(x)
|
|
267
|
+
|
|
268
|
+
### QOL functions
|
|
269
|
+
|
|
270
|
+
def estimatesignalfrequency(z: floatarray1D, x: Optional[floatarray1D] = None,
|
|
271
|
+
window: str = 'boxcar', zero_pad_factor: Optional[int] = 4,
|
|
272
|
+
bounds=None) -> float:
|
|
273
|
+
"""Estimates the frequency of a signal
|
|
274
|
+
|
|
275
|
+
Parameters
|
|
276
|
+
----------
|
|
277
|
+
z
|
|
278
|
+
x
|
|
279
|
+
window
|
|
280
|
+
By default we use a rectangular ('boxcar') window since it is the most precise for single-frequency finding. But it is very sensitive to noise.
|
|
281
|
+
zero_pad_factor
|
|
282
|
+
By default we use zero-padding to have a 'more precise' frequency measurement.
|
|
283
|
+
bounds
|
|
284
|
+
|
|
285
|
+
Returns
|
|
286
|
+
-------
|
|
287
|
+
|
|
288
|
+
"""
|
|
289
|
+
if x is None:
|
|
290
|
+
x = np.arange(len(z))
|
|
291
|
+
fx: floatarray1D = rdual1d(x, zero_pad_factor=zero_pad_factor)
|
|
292
|
+
pw: floatarray1D = rpsd1d(z, x, window=window, zero_pad_factor=zero_pad_factor)
|
|
293
|
+
if bounds is not None:
|
|
294
|
+
pw = pw[(fx > bounds[0]) & (fx < bounds[1])]
|
|
295
|
+
fx = fx[(fx > bounds[0]) & (fx < bounds[1])]
|
|
296
|
+
return find_global_max(pw, x=fx)
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
# find the edges of the peak (1D, everything is easy)
|
|
300
|
+
def peak_contour1d(peak_x, z: floatarray1D, peak_depth_dB: Optional[int] = 40, x: Optional[floatarray1D]=None,
|
|
301
|
+
peak_max_length: Optional[float] = None):
|
|
302
|
+
if x is None:
|
|
303
|
+
x = np.arange(z.shape[0])
|
|
304
|
+
|
|
305
|
+
peak_index = np.argmin((x - peak_x) ** 2)
|
|
306
|
+
|
|
307
|
+
min_peak_depth_dB = 10
|
|
308
|
+
|
|
309
|
+
while peak_depth_dB > 0: # infinite loop, return is in it
|
|
310
|
+
zintercept = attenuate_power(z[peak_index], peak_depth_dB)
|
|
311
|
+
x1_intercept = interp_roots(z - zintercept, x=x)
|
|
312
|
+
x1_before = peak_x
|
|
313
|
+
x1_after = peak_x
|
|
314
|
+
if len(x1_intercept[x1_intercept < peak_x] > 0):
|
|
315
|
+
x1_before = x1_intercept[x1_intercept < peak_x].max()
|
|
316
|
+
if len(x1_intercept[x1_intercept > peak_x] > 0):
|
|
317
|
+
x1_after = x1_intercept[x1_intercept > peak_x].min()
|
|
318
|
+
|
|
319
|
+
length = x1_after - x1_before
|
|
320
|
+
isnottoobig = True if (peak_max_length is None) else length < peak_max_length
|
|
321
|
+
|
|
322
|
+
contour_is_valid = isnottoobig
|
|
323
|
+
|
|
324
|
+
if peak_max_length is not None:
|
|
325
|
+
log_trace(f'contour length: {length} | Not too big (< {peak_max_length}): {isnottoobig}')
|
|
326
|
+
log_trace(f'Valid contour: {contour_is_valid}')
|
|
327
|
+
|
|
328
|
+
# find the contour that contains the point
|
|
329
|
+
if contour_is_valid or peak_depth_dB == min_peak_depth_dB:
|
|
330
|
+
return x1_before, x1_after
|
|
331
|
+
else:
|
|
332
|
+
peak_depth_dB -= 10
|
|
333
|
+
if peak_depth_dB < min_peak_depth_dB: peak_depth_dB = min_peak_depth_dB
|
|
334
|
+
log_debug(f"Couldn't find any valid contour: Trying peak_depth={peak_depth_dB} dB")
|
|
335
|
+
if peak_depth_dB != min_peak_depth_dB:
|
|
336
|
+
peak_depth_dB = min_peak_depth_dB
|
|
337
|
+
log_debug(f"Couldn't find any valid contour: Trying peak_depth={peak_depth_dB} dB")
|
|
338
|
+
|
|
339
|
+
# peak_index = np.argmin((x - peak_x) ** 2)
|
|
340
|
+
# zintercept = attenuate_power(z[peak_index], peak_depth_dB)
|
|
341
|
+
# x1_intercept = find_roots(x, z - zintercept)
|
|
342
|
+
# x1_before = peak_x
|
|
343
|
+
# x1_after = peak_x
|
|
344
|
+
# if len(x1_intercept[x1_intercept < peak_x] > 0):
|
|
345
|
+
# x1_before = x1_intercept[x1_intercept < peak_x].max()
|
|
346
|
+
# if len(x1_intercept[x1_intercept > peak_x] > 0):
|
|
347
|
+
# x1_after = x1_intercept[x1_intercept > peak_x].min()
|
|
348
|
+
|
|
349
|
+
log_debug(f'peak_contour1d: Around {peak_x} ({min_peak_depth_dB} dB) : {(x1_before, x1_after)}')
|
|
350
|
+
return x1_before, x1_after
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def peak_vicinity1d(peak_x, z: floatarray1D, peak_depth_dB: Optional[int] = 40, x: Optional[floatarray1D]=None,
|
|
354
|
+
peak_contour: Optional[List] = None):
|
|
355
|
+
if x is None:
|
|
356
|
+
x = np.arange(z.shape[0])
|
|
357
|
+
|
|
358
|
+
if peak_contour is None:
|
|
359
|
+
peak_contour = peak_contour1d(peak_x=peak_x, z=z, peak_depth_dB=peak_depth_dB, x=x)
|
|
360
|
+
x1_before, x1_after = peak_contour
|
|
361
|
+
|
|
362
|
+
log_trace(f'peak_vicinity1d: Vicinity of {(x1_before, x1_after)} (around {peak_x})')
|
|
363
|
+
|
|
364
|
+
vicinity = (x >= x1_before) & (x <= x1_after)
|
|
365
|
+
|
|
366
|
+
log_debug(f'peak_vicinity1d: Found {np.sum(vicinity)} points in zone {(x1_before, x1_after)} (around {peak_x})')
|
|
367
|
+
|
|
368
|
+
return vicinity
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def power_near_peak1d(peak_x, z: floatarray1D, peak_depth_dB: Optional[int] = 40, x: Optional[floatarray1D]=None,
|
|
372
|
+
peak_vicinity: Optional[np.ndarray] = None,
|
|
373
|
+
peak_contour: Optional[List] = None):
|
|
374
|
+
# integrate the PSD along the peak
|
|
375
|
+
log_debug(f'Measuring the power around ({round(peak_x, 3)})')
|
|
376
|
+
if peak_vicinity is None:
|
|
377
|
+
peak_vicinity = peak_vicinity1d(peak_x=peak_x, z=z, peak_depth_dB=peak_depth_dB, x=x, peak_contour=peak_contour)
|
|
378
|
+
# ### (abandoned) do a trapezoid integration
|
|
379
|
+
# x_f = np.concatenate(([freqpre], freqs[(freqpre < freqs)*(freqs < freqpost)], [freqpost]))
|
|
380
|
+
# y_f = np.concatenate(([np.interp(freqpre, freqs, zmeanx_psd)], zmeanx_psd[(freqpre < freqs)*(freqs < freqpost)], [np.interp(freqpost, freqs, zmeanx_psd)]))
|
|
381
|
+
# p_ft_peak = trapezoid(y_f, x_f)
|
|
382
|
+
### Go bourrin (we are in log we do not care) : rectangular integration
|
|
383
|
+
pw = np.sum(z[peak_vicinity]) * step(x)
|
|
384
|
+
log_debug(f'Power: {pw} (amplitude: {np.sqrt(pw * 2)})')
|
|
385
|
+
return pw
|