def sliding_window(self, window_size, hop_size, function=None, window_name=None): """ Use a sliding window to aggregate the data from the time series by applying ``function`` to each analysis window. The content of each window will be passed as the first argument to the function. Return the aggregated data in an array. Args ---- window_size: int Size of the window. hop_size : int Number of samples to be skipped between two successive windowing operations. function : function Function to be applied to each window. If no function is specified, each window will contain an unaltered excerpt of the time series. window_name : str Name of the window function to be used. Options are: {"boxcar", "triang", "blackman", "hamming", "hann", "bartlett", "flattop", "parzen", "bohman", "blackmanharris", "nuttall", "barthann", "no_window", None}. """ ts = self.copy() ts = sliding_window(ts, window_size, hop_size, function=function, window_name=window_name) return ts
def fft(time_series, window_size, hop_size, fft_len=4096): """ Calculate the Fast Fourier Transform for the ``time_series`` Args ---- time_series : TimeSeries Time series to be used in the FFT. window_size : int hop_size : int fftlen : int Length of the FFT. The signal will be zero-padded if ``fftlen`` > ``rolling_window.window_size``. """ def calculate(x): return np.fft.fft(x, n=2 * fft_len)[:int(fft_len)] fft_time_series = sliding_window(time_series, window_size, hop_size, calculate, window_name='hann') fft_time_series.max_frequency = time_series.nyquist fft_time_series.frequencies = np.linspace(0, fft_time_series.max_frequency, fft_len, dtype=np.float_) fft_time_series.label = 'FFT' fft_time_series.unit = 'Magnitude' return fft_time_series
def zcr(time_series, window_size, hop_size): """ Calculate the zero-crossing rate of a time series, i.e., the number of times the signal crosses the zero axis, per second. The zero-crossing rate gives some insight on the noisiness character of a sound. In noisy / unvoiced signals, the zero-crossing rate tends to reach higher values than in periodic / voiced signals. .. math:: \\operatorname{ZC} = \\frac{1}{2 L} \\sum_{n=1}^{L}\\left|\\operatorname{sgn}\\left[x(n)\\right]-\\operatorname{sgn}\\left[x(n-1)\\right]\\right| Where .. math:: \\operatorname{sgn}\\left[x(n)\\right]=\\left\\{\\begin{array}{c}{1, x(n) \\geq 0} \\\\ {-1, x(n)<0}\\end{array}\\right. And `x(n)` is the n-th sample of a window of length `L`. Args ---- time_series : iracema.core.timeseries.TimeSeries A time-series object. It is usually applied on Audio objects. window_size : int hop_size : int """ # count the number of times the signal changes between successive samples def function(x): return np.sum(x[1:] * x[:-1] < 0) / window_size * time_series.fs time_series = sliding_window(time_series, window_size, hop_size, function) time_series.label = 'ZCR' time_series.unit = 'Hz' return time_series
def rms(time_series, window_size, hop_size): """ Calculate the root mean square of a time series The RMS envelope consists in the root mean square of the amplitude, calculated within the aggregation window. .. math:: RMS = \\sqrt{ \\frac{1}{L} \\sum_{n=1}^{L} x(n)^2 } Where `x(n)` is the n-th sample of a window of length `L`. Args ---- time_series : iracema.core.timeseries.TimeSeries A time-series object. It is usually applied on Audio objects. window_size : int hop_size : int """ def function(x): return np.sqrt(np.mean(x**2)) time_series = sliding_window(time_series, window_size, hop_size, function) time_series.label = 'RMS' time_series.unit = 'amplitude' return time_series
def peak_envelope(time_series, window_size, hop_size): """ Calculate the peak envelope of a time series The peak envelope consists in the peak absolute values of the amplitude within the aggregation window. .. math:: \\operatorname{PE} = max(|x(n)|), 1 <= n <= L Where `x(n)` is the n-th sample of a window of length `L`. Args ---- time_series : iracema.core.timeseries.TimeSeries An audio time-series object. window_size : int hop_size : int """ def function(x): return np.max(np.abs(x)) time_series = sliding_window(time_series, window_size, hop_size, function) time_series.label = 'PeakEnvelope' time_series.unit = 'amplitude' return time_series
def pitch_mode(pitch_time_series, window=9): """ Apply a windowed mode to the pitch curve to remove noise. Arguments --------- window: int Length of the window for the calculation of the mode. Return ------ pitch: TimeSeries A smoothed pitch time series. """ def mode(x): values, counts = np.unique(x, return_counts=True) return values[np.argmax(counts)] return sliding_window(pitch_time_series, window, 1, mode)