def spectral_entropy(data, sampling_freq, bands=None):
    psd = (np.abs(np.fft.rfft(data)) ** 2)
    psd /= np.sum(psd)
    if (bands is None):
        power_per_band = psd[(psd > 0)]
    else:
        freqs = np.fft.rfftfreq(data.size, (1 / float(sampling_freq)))
        bands = np.asarray(bands)
        freq_limits_low = np.concatenate([[0.0], bands])
        freq_limits_up = np.concatenate([bands, [np.Inf]])
        power_per_band = [np.sum(psd[np.bitwise_and((freqs >= low), (freqs < up))]) for (low, up) in zip(freq_limits_low, freq_limits_up)]
        power_per_band = power_per_band[(power_per_band > 0)]
    return (- np.sum((power_per_band * np.log2(power_per_band))))
def spectral_entropy(data, sampling_freq, bands=None):
    psd = (np.abs(np.fft.rfft(data)) ** 2)
    psd /= np.sum(psd)
    if (bands is None):
        power_per_band = psd[(psd > 0)]
    else:
        freqs = np.fft.rfftfreq(data.size, (1 / float(sampling_freq)))
        bands = np.asarray(bands)
        freq_limits_low = np.concatenate([[0.0], bands])
        freq_limits_up = np.concatenate([bands, [np.Inf]])
        power_per_band = [np.sum(psd[np.bitwise_and((freqs >= low), (freqs < up))]) for (low, up) in zip(freq_limits_low, freq_limits_up)]
        power_per_band = power_per_band[(power_per_band > 0)]
    return (- np.sum((power_per_band * np.log2(power_per_band))))