biosppy.synthesizers

This sub-package provides methods to generate artificial (synthesised) physiological signals. (biosignals).

Modules

biosppy.synthesizers.ecg

This module provides methods to synthesize Electrocardiographic (ECG) signals.

copyright:
  1. 2015-2021 by Instituto de Telecomunicacoes

license:

BSD 3-clause, see LICENSE for more details.

biosppy.synthesizers.ecg.B(l, Kb)[source]

Generates the amplitude values of the first isoelectric line (B segment) of the ECG signal.

Follows the approach by Dolinský, Andráš, Michaeli and Grimaldi [Model03].

If the parameter introduced doesn’t make sense in this context, an error will raise. :param l: Inverse of the sampling rate. :type l: float :param Kb: B segment width (miliseconds). :type Kb: int

Returns:

B_segment (array) – B segment amplitude values (milivolts).

References

[Model03]

Pavol DOLINSKÝ, Imrich ANDRÁŠ, Linus MICHAELI, Domenico GRIMALDI, “MODEL FOR GENERATING SIMPLE SYNTHETIC ECG SIGNALS”, Acta Electrotechnica et Informatica, Vol. 18, No. 3, 2018, 3–8

biosppy.synthesizers.ecg.I(i, As, Ks, Kcs, sm, Kst, At, Kt, si, Ki)[source]

Generates the amplitude values of the final isoelectric segment (I segment) in the ECG signal.

Follows the approach by Dolinský, Andráš, Michaeli and Grimaldi [Model03].

If the parameters introduced don’t make sense in this context, an error will raise. :param i: Sampling rate. :type i: int :param As: S wave amplitude (milivolts). :type As: int :param Ks: S wave width (miliseconds). :type Ks: int :param Kcs: Parameter which allows slight adjustment of S wave shape by cutting away a portion at the end. :type Kcs: int :param sm: Slope parameter in the ST segment. :type sm: int :param Kst: ST segment width (miliseconds). :type Kst: int :param At: 1/2 of the T wave amplitude (milivolts). :type At: int :param Kt: T wave width (miliseconds). :type Kt: int :param si: Parameter for setting the transition slope between T wave and isoelectric line. :type si: int :param Ki: I segment width (miliseconds). :type Ki: int

Returns:

I_segment (array) – I segment amplitude values (milivolts).

References

[Model03]

Pavol DOLINSKÝ, Imrich ANDRÁŠ, Linus MICHAELI, Domenico GRIMALDI, “MODEL FOR GENERATING SIMPLE SYNTHETIC ECG SIGNALS”, Acta Electrotechnica et Informatica, Vol. 18, No. 3, 2018, 3–8

biosppy.synthesizers.ecg.P(i, Ap, Kp)[source]

Generates the amplitude values of the P wave in the ECG signal.

Follows the approach by Dolinský, Andráš, Michaeli and Grimaldi [Model03].

If the parameters introduced don’t make sense in this context, an error will raise. :param i: Sampling rate. :type i: int :param Ap: P wave amplitude (milivolts). :type Ap: int :param Kp: P wave width (miliseconds). :type Kp: int

Returns:

P_wave (array) – P wave amplitude values (milivolts).

References

[Model03]

Pavol DOLINSKÝ, Imrich ANDRÁŠ, Linus MICHAELI, Domenico GRIMALDI, “MODEL FOR GENERATING SIMPLE SYNTHETIC ECG SIGNALS”, Acta Electrotechnica et Informatica, Vol. 18, No. 3, 2018, 3–8

biosppy.synthesizers.ecg.Pq(l, Kpq)[source]

Generates the amplitude values of the PQ segment in the ECG signal.

Follows the approach by Dolinský, Andráš, Michaeli and Grimaldi [Model03].

If the parameters introduced don’t make sense in this context, an error will raise. :param l: Inverse of the sampling rate. :type l: float :param Kpq: PQ segment width (miliseconds). :type Kpq: int

Returns:

PQ_segment (array) – PQ segment amplitude values (milivolts).

References

[Model03]

Pavol DOLINSKÝ, Imrich ANDRÁŠ, Linus MICHAELI, Domenico GRIMALDI, “MODEL FOR GENERATING SIMPLE SYNTHETIC ECG SIGNALS”, Acta Electrotechnica et Informatica, Vol. 18, No. 3, 2018, 3–8

biosppy.synthesizers.ecg.Q1(i, Aq, Kq1)[source]

Generates the amplitude values of the first 5/6 of the Q wave in the ECG signal.

Follows the approach by Dolinský, Andráš, Michaeli and Grimaldi [Model03].

If the parameters introduced don’t make sense in this context, an error will raise. :param i: Sampling rate. :type i: int :param Aq: Q wave amplitude (milivolts). :type Aq: int :param Kq1: First 5/6 of the Q wave width (miliseconds). :type Kq1: int

Returns:

Q1_wave (array) – First 5/6 of the Q wave amplitude values (milivolts).

References

[Model03]

Pavol DOLINSKÝ, Imrich ANDRÁŠ, Linus MICHAELI, Domenico GRIMALDI, “MODEL FOR GENERATING SIMPLE SYNTHETIC ECG SIGNALS”, Acta Electrotechnica et Informatica, Vol. 18, No. 3, 2018, 3–8

biosppy.synthesizers.ecg.Q2(i, Aq, Kq2)[source]

Generates the amplitude values of the last 1/6 of the Q wave in the ECG signal.

Follows the approach by Dolinský, Andráš, Michaeli and Grimaldi [Model03].

If the parameters introduced don’t make sense in this context, an error will raise. :param i: Sampling rate. :type i: int :param Aq: Q wave amplitude (milivolts). :type Aq: int :param Kq2: Last 1/6 of the Q wave width (miliseconds). :type Kq2: int

Returns:

Q2_wave (array) – Last 1/6 of the Q wave amplitude values (milivolts).

References

[Model03]

Pavol DOLINSKÝ, Imrich ANDRÁŠ, Linus MICHAELI, Domenico GRIMALDI, “MODEL FOR GENERATING SIMPLE SYNTHETIC ECG SIGNALS”, Acta Electrotechnica et Informatica, Vol. 18, No. 3, 2018, 3–8

biosppy.synthesizers.ecg.R(i, Ar, Kr)[source]

Generates the amplitude values of the R wave in the ECG signal.

Follows the approach by Dolinský, Andráš, Michaeli and Grimaldi [Model03].

If the parameters introduced don’t make sense in this context, an error will raise. :param i: Sampling rate. :type i: int :param Ar: R wave amplitude (milivolts). :type Ar: int :param Kr: R wave width (miliseconds). :type Kr: int

Returns:

R_wave (array) – R wave amplitude values (milivolts).

References

[Model03]

Pavol DOLINSKÝ, Imrich ANDRÁŠ, Linus MICHAELI, Domenico GRIMALDI, “MODEL FOR GENERATING SIMPLE SYNTHETIC ECG SIGNALS”, Acta Electrotechnica et Informatica, Vol. 18, No. 3, 2018, 3–8

biosppy.synthesizers.ecg.S(i, As, Ks, Kcs, k=0)[source]

Generates the amplitude values of the S wave in the ECG signal.

Follows the approach by Dolinský, Andráš, Michaeli and Grimaldi [Model03].

If the parameters introduced don’t make sense in this context, an error will raise. :param i: Sampling rate. :type i: int :param As: S wave amplitude (milivolts). :type As: int :param Ks: S wave width (miliseconds). :type Ks: int :param Kcs: Parameter which allows slight adjustment of S wave shape by cutting away a portion at the end. :type Kcs: int :param k: :type k: int, optional

Returns:

  • S (array) – If k = 0, S wave amplitude values (milivolts).

  • S (int) – If k != 0, value obtained by using the S wave expression for the given k value.

References

[Model03]

Pavol DOLINSKÝ, Imrich ANDRÁŠ, Linus MICHAELI, Domenico GRIMALDI, “MODEL FOR GENERATING SIMPLE SYNTHETIC ECG SIGNALS”, Acta Electrotechnica et Informatica, Vol. 18, No. 3, 2018, 3–8

biosppy.synthesizers.ecg.St(i, As, Ks, Kcs, sm, Kst, k=0)[source]

Generates the amplitude values of the ST segment in the ECG signal.

Follows the approach by Dolinský, Andráš, Michaeli and Grimaldi [Model03].

If the parameters introduced don’t make sense in this context, an error will raise. :param i: Sampling rate. :type i: int :param As: S wave amplitude (milivolts). :type As: int :param Ks: S wave width (miliseconds). :type Ks: int :param Kcs: Parameter which allows slight adjustment of S wave shape by cutting away a portion at the end. :type Kcs: int :param sm: Slope parameter in the ST segment. :type sm: int :param Kst: ST segment width (miliseconds). :type Kst: int :param k: :type k: int, optional

Returns:

  • ST (array) – If k = 0, ST segment amplitude values (milivolts).

  • ST (int) – If k != 0, value obtained by using the ST segment expression for the given k value.

References

[Model03]

Pavol DOLINSKÝ, Imrich ANDRÁŠ, Linus MICHAELI, Domenico GRIMALDI, “MODEL FOR GENERATING SIMPLE SYNTHETIC ECG SIGNALS”, Acta Electrotechnica et Informatica, Vol. 18, No. 3, 2018, 3–8

biosppy.synthesizers.ecg.T(i, As, Ks, Kcs, sm, Kst, At, Kt, k=0)[source]

Generates the amplitude values of the T wave in the ECG signal.

Follows the approach by Dolinský, Andráš, Michaeli and Grimaldi [Model03].

If the parameters introduced don’t make sense in this context, an error will raise. :param i: Sampling rate. :type i: int :param As: S wave amplitude (milivolts). :type As: int :param Ks: S wave width (miliseconds). :type Ks: int :param Kcs: Parameter which allows slight adjustment of S wave shape by cutting away a portion at the end. :type Kcs: int :param sm: Slope parameter in the ST segment. :type sm: int :param Kst: ST segment width (miliseconds). :type Kst: int :param At: 1/2 of the T wave amplitude (milivolts). :type At: int :param Kt: T wave width (miliseconds). :type Kt: int :param k: :type k: int, optional

Returns:

  • T (array) – If k = 0, T wave amplitude values (milivolts).

  • T (int) – If k != 0, value obtained by using the T wave expression for the given k value.

References

[Model03]

Pavol DOLINSKÝ, Imrich ANDRÁŠ, Linus MICHAELI, Domenico GRIMALDI, “MODEL FOR GENERATING SIMPLE SYNTHETIC ECG SIGNALS”, Acta Electrotechnica et Informatica, Vol. 18, No. 3, 2018, 3–8

biosppy.synthesizers.ecg.ecg(Kb=130, Ap=0.2, Kp=100, Kpq=40, Aq=0.1, Kq1=25, Kq2=5, Ar=0.7, Kr=40, As=0.2, Ks=30, Kcs=5, sm=96, Kst=100, At=0.15, Kt=220, si=2, Ki=200, var=0.01, sampling_rate=10000)[source]

Concatenates the segments and waves to make an ECG signal. The default values are physiological.

Follows the approach by Dolinský, Andráš, Michaeli and Grimaldi [Model03].

If the parameters introduced aren’t within physiological values (limits based on the website [ECGwaves]), a warning will raise.

Parameters:
  • Kb (int, optional) – B segment width (miliseconds).

  • Ap (float, optional) – P wave amplitude (milivolts).

  • Kp (int, optional) – P wave width (miliseconds).

  • Kpq (int, optional) – PQ segment width (miliseconds).

  • Aq (float, optional) – Q wave amplitude (milivolts).

  • Kq1 (int, optional) – First 5/6 of the Q wave width (miliseconds).

  • Kq2 (int, optional) – Last 1/6 of the Q wave width (miliseconds).

  • Ar (float, optional) – R wave amplitude (milivolts).

  • Kr (int, optional) – R wave width (miliseconds).

  • As (float, optional) – S wave amplitude (milivolts).

  • Ks (int, optional) – S wave width (miliseconds).

  • Kcs (int, optional) – Parameter which allows slight adjustment of S wave shape by cutting away a portion at the end.

  • sm (int, optional) – Slope parameter in the ST segment.

  • Kst (int, optional) – ST segment width (miliseconds).

  • At (float, optional) – 1/2 of the T wave amplitude (milivolts).

  • Kt (int, optional) – T wave width (miliseconds).

  • si (int, optional) – Parameter for setting the transition slope between T wave and isoelectric line.

  • Ki (int, optional) – I segment width (miliseconds).

  • var (float, optional) – Value between 0.0 and 1.0 that adds variability to the obtained signal, by changing each parameter following a normal distribution with mean value parameter_value and std var * parameter_value.

  • sampling_rate (int, optional) – Sampling frequency (Hz).

Returns:

  • ecg (array) – Amplitude values of the ECG wave.

  • t (array) – Time values accoring to the provided sampling rate.

  • params (dict) – Input parameters of the function

Example

sampling_rate = 10000 beats = 3 noise_amplitude = 0.05

ECGtotal = np.array([]) for i in range(beats):

ECGwave, _, _ = ecg(sampling_rate=sampling_rate, var=0.1) ECGtotal = np.concatenate((ECGtotal, ECGwave))

t = np.arange(0, len(ECGtotal)) / sampling_rate

# add powerline noise (50 Hz) noise = noise_amplitude * np.sin(50 * (2 * pi) * t) ECGtotal += noise

plt.plot(t, ECGtotal) plt.xlabel(“Time (ms)”) plt.ylabel(“Amplitude (mV)”) plt.grid() plt.title(“ECG”)

plt.show()

References

[Model03]

Pavol DOLINSKÝ, Imrich ANDRÁŠ, Linus MICHAELI, Domenico GRIMALDI, “MODEL FOR GENERATING SIMPLE SYNTHETIC ECG SIGNALS”, Acta Electrotechnica et Informatica, Vol. 18, No. 3, 2018, 3–8

biosppy.synthesizers.emg

This module provides methods to synthesize Electromyographic (EMG) signals. :copyright: (c) 2015-2021 by Instituto de Telecomunicacoes :license: BSD 3-clause, see LICENSE for more details.

biosppy.synthesizers.emg.synth_gaussian(duration=10, sampling_rate=1000, length=None, SNR=30, sigma=0.1, alpha=2.5, baseline=None, burst_number=1, burst_location=None, random_state=None)[source]

Generates an artificial (synthetic) EMG signal of a given duration and sampling rate.

Follows the approach by by Ghislieri, Cerone, Knaflitz and Agostini [ModelEMG2], where muscle activity bursts are modeled as a zero-mean Gaussian process with standard deviation equal to 10**(SNR/20) mV, and the background noise is modeled as a zero-mean Gaussian process with standard deviation equal to 1 mV. All muscle activity bursts have the same duration, which is equal to 2*sigma*alpha (in seconds).

If the parameters introduced lead to superimposed burst locations, an error will be raised, and if they lead to consecutive bursts, a warning will raise. Warnings will also be raised, if the precision derived from the selected sampling rate is not compatible with each burst’s location or duration, or the duration of quiet periods.

Parameters:
  • duration (int, optional) – Desired recording length in seconds.

  • sampling_rate (int, optional) – The desired sampling rate (in Hz, i.e., samples/second).

  • length (int, optional, optional) – The desired length of the signal (in samples).

  • SNR (float, optional) – Desired signal-to-noise ratio of the signal (in dB).

  • sigma (float, optional) – Standard deviation of the truncated Gaussian process with zero mean used to simulate muscle activity.

  • alpha (float, optional) – Multiplier that multiplied with the ‘sigma’ value defines the time support of the truncated Gaussian process, which will be the duration of the bursts (burst_duration = 2*sigma*alpha, with default values, this duration is 0.5s).

  • baseline (float, optional) – Signal offset from zero. If no value is given, it is assumed that the baseline has already been removed.

  • burst_number (int, optional) – Desired number of bursts of activity (active muscle periods).

  • burst_location (list, optional) – Location of the bursts (in seconds).

  • random_state (None, int, numpy.random.RandomState or numpy.random.Generator) – Seed for the random number generator.

Returns:

  • emg (array) – Vector containing the EMG signal.

  • t (array) – Time values accoring to the provided sampling rate.

  • params (dict) – Input parameters of the function, clean EMG and noise signals, and SNR

Examples

sampling_rate = 1000 duration = 10 SNR = 30 sigma = 0.2 alpha = 1.25 bursts = 4 burst_location = [2,4,6,8] output = synth_gaussian(duration=duration, sampling_rate=sampling_rate, SNR=SNR,

sigma=sigma, alpha=alpha, burst_number=bursts, burst_location=burst_location, random_state=0)

emg_synth, t, params = output[“emg”], output[“t”], output[“params”]

# Get muscle activity state activity = params[“activity”]

plt.figure() plt.plot(t,emg_synth,label=”EMG”) plt.plot(t,activity,label=”Muscle activity”) plt.xlabel(“Time (s)”) plt.ylabel(“Amplitude (mV)”) plt.grid() plt.title(“EMG”) plt.legend()

plt.show()

References

[ModelEMG2]

Marco GHISLIERI, Giacinto Luigi CERONE, Marco KNAFLITZ & Valentina AGOSTINI “LONG SHORT-TERM MEMORY (LSTM) RECURRENT NEURAL NETWORK FOR MUSCLE ACTIVITY DETECTION” Journal of NeuroEngineering and Rehabilitation, Vol. 18, No. 1, 2021, 3–4

biosppy.synthesizers.emg.synth_uniform(duration=10, length=None, sampling_rate=1000, noise=0.01, baseline=None, burst_number=1, burst_duration=1.0, burst_location=None, amplitude_mult=None, random_state=None)[source]

Generates an artificial (synthetic) EMG signal of a given duration and sampling rate, with muscle activity bursts modeled as an uniform distribution and background noise modeled as a zero-mean Gaussian process with adjustable standard deviation.

Follows the approach by Diong, Joanna [ModelEMG1], but, additionally, this function also allows to manually choose the muscle activity burst locations, and add amplitude multipliers for each burst.

If the parameters introduced lead to superimposed burst locations, an error will be raised, and if they lead to consecutive bursts, a warning will be raised. Warnings will also be raised, if the precision derived from the selected sampling rate is not compatible with each burst’s location or duration, or the duration of quiet periods.

Parameters:
  • duration (int, optional) – Desired recording length in seconds.

  • sampling_rate (int, optional) – The desired sampling rate (in Hz, i.e., samples/second).

  • length (int, optional) – The desired length of the signal (in samples).

  • noise (float, optional) – Noise level (standard deviation of the gaussian distribution from which values are sampled).

  • baseline (float, optional) – Signal offset from zero. If no value is given, it is assumed that the baseline has already been removed.

  • burst_number (int, optional) – Desired number of bursts of activity (active muscle periods).

  • burst_duration (float or list, optional) – Duration of the bursts. Can be a float (each burst will have the same duration) or a list of durations for each burst.

  • burst_location (list, optional) – Location of the bursts (in seconds).

  • amplitude_mult (float or list, optional) – Amplitude multiplier for the bursts. Can be a float (each burst will have the same amplitude range) or a list of multipliers for each bursts.

  • random_state (None, int, numpy.random.RandomState or numpy.random.Generator, optional) – Seed for the random number generator.

Returns:

  • emg (array) – Vector containing the EMG signal.

  • t (array) – Time values accoring to the provided sampling rate.

  • params (dict) – Input parameters of the function, clean EMG and noise signals, and SNR

Examples

sampling_rate = 1000 duration = 10 noise_amplitude = 0.05 bursts = 7 burst_duration = [0.5,1,0.5,0.6,1,0.5,0.5] burst_location = [0.1,2.5,4,5.5,7,8.5,9.4] amplitude_mult = [1,1,0.5,1.5,1,0.75,1] emg_synth, t, params = synth_uniform(duration=duration, sampling_rate=sampling_rate, noise=noise_amplitude,

burst_number=bursts, burst_duration=burst_duration, burst_location=burst_location, amplitude_mult=amplitude_mult)

# Get muscle activity state activity = params[“activity”]

plt.plot(t,emg_synth,label=”EMG”) plt.plot(t,activity,label=”Muscle activity”) plt.xlabel(“Time (s)”) plt.ylabel(“Amplitude (mV)”) plt.grid() plt.title(“EMG”) plt.legend()

plt.show()

References

[ModelEMG1]

Joanna DIONG,

“PYTHON: ANALYSING EMG SIGNALS”, https://scientificallysound.org/2016/08/11/python-analysing-emg-signals-part-1/