def spectral_roll_off(sign, fs): """Compute the spectral roll-off of the signal, i.e., the frequency where 95% of the signal energy is contained below of this value. Parameters ---------- sign: ndarray signal from which spectral slope is computed. fs: int sampling frequency of the signal Returns ------- roll_off: float spectral roll-off """ output = None f, ff = ni.plotfft(sign, fs) cum_ff = np.cumsum(ff) value = 0.95 * (sum(ff)) for i in range(len(ff)): if cum_ff[i] >= value: output = f[i] break return output
def curve_distance(sign, fs): f, ff = ni.plotfft(sign, fs) cum_ff = np.cumsum(ff) points_y = np.linspace(0, cum_ff[-1], len(cum_ff)) return np.sum(points_y - cum_ff)
def filtro(nome, load, freq, directory_to_save): """function to show signals (original and filtrated) in time or as FFT""" freqs = FA picture = plab.figure() #filters emg = load[:, 6] freqs = float(freqs)/2.0 if (conf.FILTRO50): filt = ni.bandstop(emg, conf.FREQ1, conf.FREQ2, 4, fs = freqs) else: filt = emg bfilt, afilt = signal.butter(4, freq / freqs, btype = 'highpass') filtrado = signal.lfilter(bfilt, afilt, filt) #plots: Signals and time - original and filtrated plab.subplot(2, 2, 1) plab.plot(filtrado) plab.title('EMG Filtrado', fontsize=10) plab.xlabel('Tempo', fontsize=8) plab.ylabel('Amplitude', fontsize=8) plab.subplot(2, 2, 2) plab.plot(emg) plab.title('EMG Original', fontsize=10) plab.xlabel('Tempo', fontsize=8) plab.ylabel('Amplitude', fontsize=8) #plots: FFTsignals - original and filtrated plab.subplot(2, 2, 3) ni.plotfft(emg, FA) plab.title('FFT do Emg original', fontsize=10) plab.xlabel('Frequencia', fontsize=8) plab.ylabel('Amplitude', fontsize=8) plab.subplot(2, 2, 4) ni.plotfft(filtrado, FA) plab.title('FFT do Emg Filtrado', fontsize=10) plab.xlabel('Frquencia', fontsize=8) plab.ylabel('Amplitude', fontsize=8) plab.subplots_adjust(SL, SB, SR, ST, SW, SH) picture.savefig(directory_to_save + nome + '.pdf')
def test_spect_variation(): f1 = 1 sample = 1000 x = np.arange(0, sample, 0.01) Fs = len(x) / x[-1] y = np.sin(2 * np.pi * f1 * x / Fs) f, ff = ni.plotfft(y, Fs) np.testing.assert_almost_equal(spect_variation(y, Fs), 0.9999999999957343, decimal=5)
def test_spectral_curve_distance(): f1 = 1 sample = 1000 x = np.arange(0, sample, 0.01) Fs = len(x) / x[-1] y = np.sin(2 * np.pi * f1 * x / Fs) f, ff = ni.plotfft(y, Fs) np.testing.assert_almost_equal(curve_distance(y, Fs), -1251684201.9742942, decimal=5)
def test_spectral_roll_off(): f1 = 1 sample = 1000 x = np.arange(0, sample, 0.01) Fs = len(x) / x[-1] y = np.sin(2 * np.pi * f1 * x / Fs) f, ff = ni.plotfft(y, Fs) #roll_off(ff, f) np.testing.assert_almost_equal(spectral_roll_off(y, Fs), 0.010000300007000151, decimal=5)
def test_spectral_centroid(): f1 = 0.1 sample = 1000 x = np.arange(0, sample, 0.01) Fs = len(x) / x[-1] y = np.sin(2 * np.pi * f1 * x / Fs) f, ff = ni.plotfft(y, Fs) # centroid(ff, f) np.testing.assert_almost_equal(spectral_centroid(y, Fs), 0.0021658004162115937, decimal=2)
def test_spectral_slope(): f1 = 1 sample = 1000 x = np.arange(0, sample, 0.01) Fs = len(x) / x[-1] y = np.sin(2 * np.pi * f1 * x / Fs) f, ff = ni.plotfft(y, Fs) # slope(ff, f) np.testing.assert_almost_equal(spectral_slope(y, Fs), -0.1201628830466239, decimal=5)
def test_spectral_spread(): f1 = 1 sample = 1000 x = np.arange(0, sample, 0.01) Fs = len(x) / x[-1] y = np.sin(2 * np.pi * f1 * x / Fs) f, ff = ni.plotfft(y, Fs) p = ff / np.sum(ff) # np.dot(((f-np.mean(centroid(ff, f)))**2),p) np.testing.assert_almost_equal(spectral_spread(y, Fs), 0.31264360946306424, decimal=5)
def spectral_roll_on(sign, fs): output = None f, ff = ni.plotfft(sign, fs) cum_ff = np.cumsum(ff) value = 0.05 * (sum(ff)) for i in range(len(ff)): if cum_ff[i] >= value: output = f[i] break return output
def spectral_decrease(sign, fs): f, ff = ni.plotfft(sign, fs) k = len(ff) soma_num = 0 for a in range(2, k): soma_num = soma_num + ((ff[a] - ff[1]) / (a - 1)) ff2 = ff[2:] soma_den = 1 / np.sum(ff2) if soma_den == 0: return 0 else: return soma_den * soma_num
def test_spectral_kurtosis(): # np.random.seed(seed=1) # x = np.random.normal(0, 2, 10000) # np.testing.assert_almost_equal(spectral_kurtosis(x, 100), 0, decimal=3) # mu, sigma = 20, 0.1 # x = mu + sigma * np.random.randn(100) #_kurtosis(ff,f,sp,c) f1 = 1 sample = 1000 x = np.arange(0, sample, 0.01) Fs = len(x) / x[-1] y = np.sin(2 * np.pi * f1 * x / Fs) f, ff = ni.plotfft(y, Fs) sp = spectral_spread(y, Fs) c = spectral_centroid(y, Fs) np.testing.assert_almost_equal(spectral_kurtosis(y, Fs), 4293.9932884381524, decimal=0)
def spectral_slope(sign, fs): """Computes the constants m and b of the function aFFT = mf + b, obtained by linear regression of the spectral amplitude. Parameters ---------- sign: ndarray signal from which spectral slope is computed. fs: int sampling frequency of the signal Returns ------- m: float slope b: float y-intercept """ f, ff = ni.plotfft(sign, fs) return (len(f) * np.dot(f, ff) - np.sum(f) * np.sum(ff)) / (len(f) * np.dot(f, f) - np.sum(f)**2)
def spectral_maxpeaks(sign, FS): """Compute number of peaks along the specified axes. Parameters ---------- sig: ndarray input from histogram is computed. type: string can be 'all', 'max', and 'min', and expresses which peaks are going to be accounted Returns ------- num_p: float total number of peaks """ f, ff = ni.plotfft(sign, FS) diff_sig = np.diff(ff) return np.sum([ 1 for nd in range(len(diff_sig[:-1])) if (diff_sig[nd + 1] < 0 and diff_sig[nd] > 0) ])
def spect_variation(sign, fs): ''' returns the temporal variation ''' f, ff = ni.plotfft(sign, fs) energy, freq = signal_energy(ff, f) sum1 = 0 sum2 = 0 sum3 = 0 for a in range(len(energy) - 1): sum1 = sum1 + (energy[a - 1] * energy[a]) sum2 = sum2 + (energy[a - 1]**2) sum3 = sum3 + (energy[a]**2) if (sum2 == 0 or sum3 == 0): variation = 1 else: variation = 1 - ((sum1) / ((sum2**0.5) * (sum3**0.5))) return variation
def spectral_centroid(sign, fs): #enter the portion of the signal f, ff = ni.plotfft(sign, fs) return np.dot(f, ff / np.sum(ff))
def spectral_spread(sign, fs): f, ff = ni.plotfft(sign, fs) spect_centr = spectral_centroid(sign, fs) return np.dot(((f - spect_centr)**2), (ff / np.sum(ff)))
def spectral_kurtosis(sign, fs): f, ff = ni.plotfft(sign, fs) spect_kurt = ((f - spectral_centroid(sign, fs))**4) * (ff / np.sum(ff)) skew = np.sum(spect_kurt) return skew / (spectral_spread(sign, fs)**2)
def spectral_skewness(sign, fs): f, ff = ni.plotfft(sign, fs) spect_centr = spectral_centroid(sign, fs) skew = ((f - spect_centr)**3) * (ff / np.sum(ff)) spect_skew = np.sum(skew) return spect_skew / (spectral_spread(sign, fs)**(3 / 2))