Skip to content

Commit b16ded8

Browse files
committed
symbol update
1 parent 2014a0e commit b16ded8

10 files changed

+155
-173
lines changed

Showcase.ipynb

Lines changed: 57 additions & 65 deletions
Large diffs are not rendered by default.

example/example_nonstationarity.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
# define frequency vector and one-sided flat-shaped PSD
1818
M = N // 2 + 1 # number of data points of frequency vector
19-
f = np.arange(0, M, 1) * fs / N # frequency vector
20-
f_min = 50 # PSD upper frequency limit [Hz]
21-
f_max = 100 # PSD lower frequency limit [Hz]
22-
PSD = es.get_psd(f, f_min, f_max) # one-sided flat-shaped PSD
23-
plt.plot(f, PSD)
19+
freq = np.arange(0, M, 1) * fs / N # frequency vector
20+
freq_lower = 50 # PSD upper frequency limit [Hz]
21+
freq_upper = 100 # PSD lower frequency limit [Hz]
22+
PSD = es.get_psd(freq, freq_lower, freq_upper) # one-sided flat-shaped PSD
23+
plt.plot(freq, PSD)
2424
plt.xlabel('Frequency [Hz]')
2525
plt.ylabel('PSD [Unit**2/Hz]')
2626
plt.xlim(0, 200)
@@ -38,17 +38,16 @@
3838

3939
# get non-gaussian stationary signal, with kurtosis k_u=10
4040
k_u_target = 10
41-
rng = np.random.default_rng(seed)
41+
rg = np.random.default_rng(seed)
4242
nongaussian_signal = es.stationary_nongaussian_signal(N, PSD, fs, k_u=k_u_target, rg=rg)
4343
# calculate kurtosis
4444
k_u_stationary_nongaussian = es.get_kurtosis(nongaussian_signal)
4545

4646
# get non-gaussian non-stationary signal, with kurtosis k_u=10
4747
# a) amplitude modulation, modulating signal defined by PSD
48-
rng = np.random.default_rng(seed)
49-
PSD_modulating = es.get_psd(f, f_low=1, f_high=k_u_target)
50-
plt.plot(f, PSD, label='PSD, carrier signal')
51-
plt.plot(f, PSD_modulating, label='PSD, modulating signal')
48+
PSD_modulating = es.get_psd(freq, freq_lower=1, freq_upper=10)
49+
plt.plot(freq, PSD, label='PSD, carrier signal')
50+
plt.plot(freq, PSD_modulating, label='PSD, modulating signal')
5251
plt.xlabel('Frequency [Hz]')
5352
plt.ylabel('PSD [Unit**2/Hz]')
5453
plt.xlim(0, 200)
@@ -119,4 +118,4 @@
119118
)
120119
print(
121120
f'kurtosis of non-stationary non-Gaussian signal (beta):{k_u_nonstationary_nongaussian_beta:.3f}'
122-
)
121+
)

example/example_signals.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
N = 2 * width
1313
n_start = 90
1414
amplitude = 3
15-
# pulse_sine = es.impulse(N=N, n_start=n_start, width=width, amplitude=amplitude, window='sine')
1615
pulse_sine = es.impulse(
1716
N=N, n_start=n_start, width=width, amplitude=amplitude, window='sine'
1817
)
@@ -76,7 +75,7 @@
7675

7776
# sweep
7877
t = np.linspace(0, 10, 1000)
79-
sweep = es.sine_sweep(time=t, f_start=0, f_stop=5)
78+
sweep = es.sine_sweep(time=t, freq_start=0, freq_stop=5)
8079
plt.plot(t, sweep)
8180
plt.xlabel('Time [s]')
8281
plt.ylabel('Sine sweep [Unit]')

pyExSi/signals.py

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,19 @@ def burst_random(
183183

184184

185185
def sine_sweep(
186-
time, phi=0, f_start=1, sweep_rate=None, f_stop=None, mode='linear', phi_end=False
186+
time, phi=0, freq_start=1, sweep_rate=None, freq_stop=None, mode='linear', phi_end=False
187187
):
188188
"""
189189
Generate a sine sweep signal time series.
190190
191191
:param time: array of shape (N,), time vector.
192192
:param phi: float, initial phase of the sine signal in radians.
193193
Defaults to 0.
194-
:param f_start: float, initial frequency in Hz.
194+
:param freq_start: float, initial frequency in Hz.
195195
:param sweep_rate: float, the rate of sweep. In Hz/s for a linear sweep,
196196
in octaves/minute for a logarithmic sweep. If not given it is
197-
calculated from `time`, `f_start` and `f_stop`.
198-
:param f_stop: float, final frequency in Hz.
197+
calculated from `time`, `freq_start` and `freq_stop`.
198+
:param freq_stop: float, final frequency in Hz.
199199
:param mode: 'linear' or 'logarithmic', type of sweep, optional.
200200
Defaults to 'linear'.
201201
:param phi_end: If True, return (`sweep_sine`, `phi_end`), where
@@ -211,16 +211,16 @@ def sine_sweep(
211211
>>> import pyExSi as es
212212
213213
>>> t = np.linspace(0,10,1000)
214-
>>> x = es.sine_sweep(time=t, f_start=0, f_stop=5)
214+
>>> x = es.sine_sweep(time=t, freq_start=0, freq_stop=5)
215215
>>> plt.plot(t, x)
216216
>>> plt.show()
217217
"""
218218
if sweep_rate is None:
219-
if not f_stop is None:
219+
if not freq_stop is None:
220220
T = time[-1] - time[0]
221-
sweep_rate = _sweep_rate(T, f_start, f_stop, mode)
221+
sweep_rate = _sweep_rate(T, freq_start, freq_stop, mode)
222222
else:
223-
raise ValueError('`sweep_rate` is not given, please supply `f_stop`.')
223+
raise ValueError('`sweep_rate` is not given, please supply `freq_stop`.')
224224
if phi_end:
225225
# prepare time
226226
time_ = np.zeros(len(time) + 1)
@@ -230,13 +230,13 @@ def sine_sweep(
230230
time_ = time
231231

232232
if mode == 'linear':
233-
phase_t = 2 * np.pi * (sweep_rate * 0.5 * time_ ** 2 + f_start * time_)
233+
phase_t = 2 * np.pi * (sweep_rate * 0.5 * time_ ** 2 + freq_start * time_)
234234
elif mode == 'logarithmic':
235235
phase_t = (
236236
2
237237
* np.pi
238238
* 60
239-
* f_start
239+
* freq_start
240240
/ (sweep_rate * np.log(2))
241241
* (2 ** (sweep_rate * time_ / 60) - 1)
242242
)
@@ -250,15 +250,15 @@ def sine_sweep(
250250
return s
251251

252252

253-
def _sweep_rate(T, f_start, f_stop, mode='linear'):
253+
def _sweep_rate(T, freq_start, freq_stop, mode='linear'):
254254
"""
255255
Calculate the sweep rate given the time difference, initial and end
256256
frequency values and sweep mode. For internal use by `sweep`.
257257
"""
258258
if mode == 'linear':
259-
sweep_rate = (f_stop - f_start) / T # Hz/s
259+
sweep_rate = (freq_stop - freq_start) / T # Hz/s
260260
elif mode == 'logarithmic':
261-
sweep_rate = np.log((f_stop / f_start) ** (60 / T / np.log(2))) # octaves/min
261+
sweep_rate = np.log((freq_stop / freq_start) ** (60 / T / np.log(2))) # octaves/min
262262
else:
263263
raise ValueError('Invalid sweep mode `{mode}`.')
264264
return sweep_rate
@@ -323,16 +323,16 @@ def impulse(N, n_start=0, width=None, amplitude=1.0, window='sine'):
323323
return pulse
324324

325325

326-
def get_psd(f, f_low, f_high, variance=1):
326+
def get_psd(freq, freq_lower, freq_upper, variance=1):
327327
"""
328328
One-sided flat-shaped power spectral density (PSD).
329329
330-
:param f: Frequency vector [Hz]
331-
:type f: array
332-
:param f_low: Lower frequency of PSD [Hz]
333-
:type f_low: float
334-
:param f_high: Higher frequency of PSD [Hz]
335-
:type f_high: float
330+
:param freq: Frequency vector [Hz]
331+
:type freq: array
332+
:param freq_lower: Lower frequency of PSD [Hz]
333+
:type freq_lower: float
334+
:param freq_upper: Upper frequency of PSD [Hz]
335+
:type freq_upper: float
336336
:param variance: Variance of random process, described by PSD [unit^2]
337337
:type variance: float
338338
:returns: one-sided flat-shaped PSD [unit^2/Hz]
@@ -347,19 +347,19 @@ def get_psd(f, f_low, f_high, variance=1):
347347
>>> fs = 100 # sampling frequency [Hz]
348348
>>> t = np.arange(0,N)/fs # time vector
349349
>>> M = N // 2 + 1 # number of data points of frequency vector
350-
>>> f = np.arange(0, M, 1) * fs / N # frequency vector
351-
>>> f_min = 10 # PSD upper frequency limit [Hz]
352-
>>> f_max = 20 # PSD lower frequency limit [Hz]
350+
>>> freq = np.arange(0, M, 1) * fs / N # frequency vector
351+
>>> freq_lower = 10 # PSD lower frequency limit [Hz]
352+
>>> freq_upper = 20 # PSD upper frequency limit [Hz]
353353
354-
>>> PSD = es.get_psd(f, f_min, f_max) # one-sided flat-shaped PSD
355-
>>> plt.plot(f,PSD)
354+
>>> PSD = es.get_psd(freq, freq_lower, freq_upper) # one-sided flat-shaped PSD
355+
>>> plt.plot(freq,PSD)
356356
>>> plt.xlabel(f [Hz])
357357
>>> plt.ylabel(PSD [unit^2/Hz])
358358
>>> plt.show()
359359
"""
360-
PSD = np.zeros(len(f))
361-
indx = np.logical_and(f >= f_low, f <= f_high)
362-
PSD_width = f[indx][-1] - f[indx][0]
360+
PSD = np.zeros(len(freq))
361+
indx = np.logical_and(freq >= freq_lower, freq <= freq_upper)
362+
PSD_width = freq[indx][-1] - freq[indx][0]
363363
PSD[indx] = variance / PSD_width # area under PSD is variance
364364
return PSD
365365

@@ -395,11 +395,11 @@ def random_gaussian(N, PSD, fs, rg=None):
395395
>>> fs = 100 # sampling frequency [Hz]
396396
>>> t = np.arange(0,N)/fs # time vector
397397
>>> M = N // 2 + 1 # number of data points in frequency vector
398-
>>> f = np.arange(0, M, 1) * fs / N # frequency vector
399-
>>> f_min = 10 # PSD upper frequency limit [Hz]
400-
>>> f_max = 20 # PSD lower frequency limit [Hz]
398+
>>> freq = np.arange(0, M, 1) * fs / N # frequency vector
399+
>>> freq_lower = 10 # PSD lower frequency limit [Hz]
400+
>>> freq_upper = 20 # PSD upper frequency limit [Hz]
401401
402-
>>> PSD = es.get_psd(f, f_min, f_max) # one-sided flat-shaped PSD
402+
>>> PSD = es.get_psd(freq, freq_lower, freq_upper) # one-sided flat-shaped PSD
403403
>>> x = es.random_gaussian(N, PSD, fs)
404404
>>> plt.plot(t,x)
405405
>>> plt.xlabel(t [s])
@@ -463,11 +463,11 @@ def stationary_nongaussian_signal(N, PSD, fs, s_k=0, k_u=3, mean=0, rg=None):
463463
>>> fs = 100 # sampling frequency [Hz]
464464
>>> t = np.arange(0,N)/fs # time vector
465465
>>> M = N // 2 + 1 # number of data points of frequency vector
466-
>>> f = np.arange(0, M, 1) * fs / N # frequency vector
467-
>>> f_min = 10 # PSD upper frequency limit [Hz]
468-
>>> f_max = 20 # PSD lower frequency limit [Hz]
466+
>>> freq = np.arange(0, M, 1) * fs / N # frequency vector
467+
>>> freq_lower = 10 # PSD lower frequency limit [Hz]
468+
>>> freq_upper = 20 # PSD upper frequency limit [Hz]
469469
470-
>>> PSD = es.get_psd(f, f_min, f_max) # one-sided flat-shaped PSD
470+
>>> PSD = es.get_psd(freq, freq_lower, freq_upper) # one-sided flat-shaped PSD
471471
>>> x_gauss = es.random_gaussian(N, PSD, fs)
472472
>>> x_ngauss = es.stationary_nongaussian_signal(N, PSD, fs, k_u = 5)
473473
>>> plt.plot(t, x_gauss, label='gaussian')
@@ -705,16 +705,16 @@ def nonstationary_signal(
705705
>>> fs = 100 # sampling frequency [Hz]
706706
>>> t = np.arange(0,N)/fs # time vector
707707
>>> M = N // 2 + 1 # number of data points of frequency vector
708-
>>> f = np.arange(0, M, 1) * fs / N # frequency vector
709-
>>> f_min = 10 # signals's PSD upper frequency limit [Hz]
710-
>>> f_max = 20 # signals' PSD lower frequency limit [Hz]
711-
>>> f_min_mod = 1 # modulating signals's PSD upper frequency limit [Hz]
712-
>>> f_max_mod = 2 # modulating signals's PSD lower frequency limit [Hz]
708+
>>> freq = np.arange(0, M, 1) * fs / N # frequency vector
709+
>>> freq_lower = 10 # PSD lower frequency limit [Hz]
710+
>>> freq_upper = 20 # PSD upper frequency limit [Hz]
711+
>>> freq_lower_mod = 1 # modulating signals's PSD lower frequency limit [Hz]
712+
>>> freq_upper_mod = 2 # modulating signals's PSD upper frequency limit [Hz]
713713
714714
PSD of stationary and modulating signal
715715
716-
>>> PSD = es.get_psd(f, f_low = f_min, f_high = f_max) # one-sided flat-shaped PSD
717-
>>> PSD_modulating = es.get_psd(f, f_low = f_min_mod, f_high = f_max_mod) # one-sided flat-shaped PSD
716+
>>> PSD = es.get_psd(freq, freq_lower, freq_upper) # one-sided flat-shaped PSD
717+
>>> PSD_modulating = es.get_psd(freq, freq_lower_mod, freq_upper_mod) # one-sided flat-shaped PSD
718718
719719
Specify kurtosis and return non-stationary signal
720720

readme.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ A simple example on how to generate random signals on PSD basis:
3636
3737
# define frequency vector and one-sided flat-shaped PSD
3838
M = N//2 + 1 # number of data points of frequency vector
39-
f = np.arange(0, M, 1) * fs / N # frequency vector
40-
f_min = 50 # PSD upper frequency limit [Hz]
41-
f_max = 100 # PSD lower frequency limit [Hz]
42-
PSD = es.get_psd(f, f_min, f_max) # one-sided flat-shaped PSD
39+
freq = np.arange(0, M, 1) * fs / N # frequency vector
40+
freq_lower = 50 # PSD lower frequency limit [Hz]
41+
freq_upper = 100 # PSD upper frequency limit [Hz]
42+
PSD = es.get_psd(freq, freq_lower, freq_upper) # one-sided flat-shaped PSD
4343
4444
#get gaussian stationary signal
4545
gausian_signal = es.random_gaussian((N, PSD, fs)
4646
4747
#get non-gaussian non-stationary signal, with kurtosis k_u=10
4848
#amplitude modulation, modulating signal defined by PSD
49-
PSD_modulating = es.get_psd(f, f_low=1, f_high=10)
49+
PSD_modulating = es.get_psd(freq, freq_lower=1, freq_upper=10)
5050
#define array of parameters delta_m and p
5151
delta_m_list = np.arange(.1,2.1,.5)
5252
p_list = np.arange(.1,2.1,.5)

0 commit comments

Comments
 (0)