-----
    Brief
    -----
    Function for extracting EMG parameters from time and frequency domains.

    -----------
    Description
    -----------
    EMG signals have specific properties that are different from other biosignals. For example, it is not periodic,
    contrary to ECG signals.
    This type of biosignals are composed of activation and inactivation periods that are different from one another.
    Specifically, there differences in the amplitude domain, in which the activation periods are characterised by an
    increase of amplitude.

    This function allows the extraction of a wide range of features specific of EMG signals.

    ----------
    Parameters
    ----------
    data : list
        EMG signal.

    sample_rate : int
        Sampling frequency.

    raw_to_mv : boolean
        If True then it is assumed that the input samples are in a raw format and the output
        results will be in mV.
        When True "device" and "resolution" inputs became mandatory.

    device : str
        Plux device label:
        - "bioplux"
        - "bioplux_exp"
        - "biosignalsplux"
        - "rachimeter"
        - "channeller"
        - "swifter"
        - "ddme_openbanplux"

    resolution : int
        Resolution selected during acquisition.

    Returns
    -------
    out : dict
        Dictionary with EMG parameters values, with keys:
            Maximum Burst Duration : Duration of the longest activation in the EMG signal
            Minimum Burst Duration : Duration of the shortest activation in the EMG signal
            Average Burst Duration : Average duration of the activations in the EMG signal
            Standard Deviation of Burst Duration : Standard Deviation duration of the activations in the EMG signal
            Maximum Sample Value : Maximum value of the EMG signal
            Minimum Sample Value : Minimum value of the EMG signal
            Average Sample Value : Average value of the EMG signal
            Standard Deviation Sample Value : Standard deviation value of the EMG signal
            RMS : Root mean square of the EMG signal
            Area : Area under the curve of the EMG signal
            Total Power Spect : Total power of the power spectrum of the EMG signal
            Median Frequency : Median frequency of the EMG signal calculated using the power spectrum of the EMG signal
            Maximum Power Frequency : Frequency correspondent to the maximum amplitude in the power spectrum of the EMG
                                      signal

def emg_parameters(data, sample_rate, raw_to_mv=True, device="biosignalsplux", resolution=16):
    """
    -----
    Brief
    -----
    Function for extracting EMG parameters from time and frequency domains.

    -----------
    Description
    -----------
    EMG signals have specific properties that are different from other biosignals. For example, it is not periodic,
    contrary to ECG signals.
    This type of biosignals are composed of activation and inactivation periods that are different from one another.
    Specifically, there differences in the amplitude domain, in which the activation periods are characterised by an
    increase of amplitude.

    This function allows the extraction of a wide range of features specific of EMG signals.

    ----------
    Parameters
    ----------
    data : list
        EMG signal.

    sample_rate : int
        Sampling frequency.

    raw_to_mv : boolean
        If True then it is assumed that the input samples are in a raw format and the output
        results will be in mV.
        When True "device" and "resolution" inputs became mandatory.

    device : str
        Plux device label:
        - "bioplux"
        - "bioplux_exp"
        - "biosignalsplux"
        - "rachimeter"
        - "channeller"
        - "swifter"
        - "ddme_openbanplux"

    resolution : int
        Resolution selected during acquisition.

    Returns
    -------
    out : dict
        Dictionary with EMG parameters values, with keys:
            Maximum Burst Duration : Duration of the longest activation in the EMG signal
            Minimum Burst Duration : Duration of the shortest activation in the EMG signal
            Average Burst Duration : Average duration of the activations in the EMG signal
            Standard Deviation of Burst Duration : Standard Deviation duration of the activations in the EMG signal
            Maximum Sample Value : Maximum value of the EMG signal
            Minimum Sample Value : Minimum value of the EMG signal
            Average Sample Value : Average value of the EMG signal
            Standard Deviation Sample Value : Standard deviation value of the EMG signal
            RMS : Root mean square of the EMG signal
            Area : Area under the curve of the EMG signal
            Total Power Spect : Total power of the power spectrum of the EMG signal
            Median Frequency : Median frequency of the EMG signal calculated using the power spectrum of the EMG signal
            Maximum Power Frequency : Frequency correspondent to the maximum amplitude in the power spectrum of the EMG
                                      signal
    """

    out_dict = {}

    # Conversion of data samples to mV if requested by raw_to_mv input.
    if raw_to_mv is True:
        data = raw_to_phy("EMG", device, data, resolution, option="mV")

    # Detection of muscular activation periods.
    burst_begin, burst_end = detect_emg_activations(data, sample_rate, smooth_level=20,
                                                    threshold_level=10, time_units=True)[:2]

    # --------------------------- Number of activation periods. -----------------------------------
    out_dict["Number of Bursts"] = len(burst_begin)

    # ----------- Maximum, Minimum and Average duration of muscular activations. ------------------
    # Bursts Duration.
    bursts_time = burst_end - burst_begin

    # Parameter extraction.
    out_dict["Maximum Burst Duration"] = numpy.max(bursts_time)
    out_dict["Minimum Burst Duration"] = numpy.min(bursts_time)
    out_dict["Average Burst Duration"] = numpy.average(bursts_time)
    out_dict["Standard Deviation of Burst Duration"] = numpy.std(bursts_time)

    # --------- Maximum, Minimum, Average and Standard Deviation of EMG sample values -------------
    # Maximum.
    out_dict["Maximum Sample Value"] = numpy.max(data)

    # Minimum.
    out_dict["Minimum Sample Value"] = numpy.min(data)

    # Average and Standard Deviation.
    out_dict["Average Sample Value"] = numpy.average(data)
    out_dict["Standard Deviation Sample Value"] = numpy.std(data)

    # ---------- Root Mean Square and Area under the curve (Signal Intensity Estimators) ----------
    # Root Mean Square.
    out_dict["RMS"] = numpy.sqrt(numpy.sum(numpy.array(data) * numpy.array(data)) / len(data))

    # Area under the curve.
    out_dict["Area"] = integr.cumtrapz(data)[-1]

    # -------------------- Total power and frequency reference points -----------------------------
    # Signal Power Spectrum.
    freq_axis, power_axis = scisignal.welch(data, fs=sample_rate, window='hanning', noverlap=0,
                                            nfft=int(256.))

    # Total Power and Median Frequency (Frequency that divides the spectrum into two regions with
    # equal power).
    area_power_spect = integr.cumtrapz(power_axis, freq_axis, initial=0)
    out_dict["Total Power Spect"] = area_power_spect[-1]
    out_dict["Median Frequency"] = freq_axis[numpy.where(area_power_spect >=
                                                         out_dict["Total Power Spect"] / 2)[0][0]]
    out_dict["Maximum Power Frequency"] = freq_axis[numpy.argmax(power_axis)]

    return out_dict