def encode_audio(signal): """ Convert a 2D numpy array into a byte sequence for PyAudio Signal should be a numpy array with shape (chunk_size, NUM_CHANNELS) """ interleaved = signal.flatten() out_data = interleaved.astype(np.float32).tostring() return out_data
def parser(signal, signal_ID, measurement_ID, label): parsed_data = { 'signal': signal.flatten(order='C'), #Makes it 50000 float32 1D vector 'signal_ID': signal_ID, 'measurement_ID': measurement_ID, 'label': label } return parsed_data
def encode(signal): """ Convert a 2D numpy array into a byte stream for PyAudio Signal should be a numpy array with shape (chunk_size, channels) """ interleaved = signal.flatten() # TODO: handle data type as parameter, convert between pyaudio/numpy types out_data = interleaved.astype(np.int16).tostring() return out_data
def meda(signal, sizeFilter=30): """ reference : Multipoint Optimal Minimum Entropy Deconvolutionand Convolution Fix: Application to vibration fault detectionGeoff L. McDonalda Qing Zhao MEDA algorithm used to get a signal closer from the original one (1 * n float) signal : waveForm data (int) sizeFilter : size of the filter used to get the original signal back returns : (1 * n float) filtered signal (1 * m float) coefficient of the filter """ sig = signal.flatten() # Step 1 : initialize filter filt = np.zeros((sizeFilter)) filt[sizeFilter // 2] = 1 filt[sizeFilter // 2 + 1] = -1 filtNew = np.ones(sizeFilter) # Step 2 : Calculate X0 and inv(X0 * X0.T) from input signal x nbPts = len(sig) X0 = np.zeros((sizeFilter, nbPts - sizeFilter + 1)) # filling the X0 array with the appropriate values (making it a upper diagonal) for k in range(sizeFilter): X0[k, k:] = sig[np.arange(sizeFilter - 1, nbPts - k)] X0[k, :k] = sig[np.arange(sizeFilter - k - 1, sizeFilter - 1)] while np.sum(np.abs(filtNew - filt)) > 0.001: filt = filtNew k += 1 # Step 3: Calculate y as X0.T * f y = np.dot(X0.T, filt) # Step 4 : finding the new coefficients of the filter coefficient = np.sum(y[:nbPts - sizeFilter + 1]**2) / np.sum( y[:nbPts - sizeFilter + 1]**4) matrix = np.linalg.solve(np.dot(X0, X0.T), X0) filtNew = coefficient * matrix filtNew = np.dot(filtNew, (y[:nbPts - sizeFilter + 1].T)**3) # normalizing the filter result filtNew = filtNew / (np.sum(filtNew**2))**0.5 originalSig = np.dot(X0.T, filtNew) return (originalSig, filtNew)
def compute_fourier_coefficients(signal, window, overlap, frequencies, sampling_rate): """Compuses the complex fourier coefficients for the given frequencies in the given signal with the given sampling rate (windowed using "window" and "overlap"). :param signal: Time domain signal. :type signal: np.ndarray :param window: Vector containing window function. :type window: np.ndarray :param overlap: Overlap given in samples. :type overlap: int :param frequencies: Vector of frequencies to compute of fourier coefficients for (Hz) :type frequencies: np.ndarray :param sampling_rate: Sampling rate of the given signal (Hz) :type sampling_rate: int or float :return: x: complex fourier coefficients, t: time in sec of window positions :rtype: tuple[np.ndarray, np.ndarray] """ signal = signal.flatten() frequencies = (frequencies / 60).flatten() win_len = window.size hopsize = win_len - overlap two_pi_t = 2 * np.pi * (np.arange(0, win_len) / sampling_rate) win_num = int(np.fix((signal.size - overlap) / hopsize)) coefficients = np.zeros((win_num, frequencies.size), dtype=complex) t = np.arange(win_len / 2, signal.size - win_len / 2 + 1, hopsize) / sampling_rate for f0 in range(0, frequencies.size): two_pi_ft = frequencies[f0] * two_pi_t cosine = np.cos(two_pi_ft) sine = np.sin(two_pi_ft) for w in range(0, win_num): start = w * hopsize stop = start + win_len sig = signal[start:stop] * window co = float(np.sum(sig * cosine)) si = float(np.sum(sig * sine)) coefficients[w, f0] = complex(real=co, imag=si) coefficients = coefficients.T return coefficients, t
def plot_signals(signals, names=None, start=0, end=None): """ Plot the signals specified in list <signals> with their names specified in list <names>. Each signal is plotted in its full length unless differently specified. """ # Identify the signals by index if their name is not specified if (names is None): legend = [] count = 0 else: legend = names # Loop over the signals t_max = 0 for signal in signals: signal = signal.flatten() t_max = np.amax([t_max, len(signal)]) plt.plot(signal) # If no name is given use the list index to identify the signals if (names is None): legend.append('Signal [' + str(count) + ']') count += 1 # Plot and format plt.xlabel('Index') plt.ylabel('Value') plt.grid(b=True) plt.legend(legend) if (end is None): plt.xlim(start, t_max) else: plt.xlim(start, end) plt.show()
def test_subbands(rfn, erb_filter_mode='all', verbose=0): print('Testing subband equality for %s' % rfn) # load matlab-generated reference file mlab = sio.loadmat(rfn) # print(list(mlab.keys())) # extract parameters used to generate these filterbanks signal = mlab['wavData'] # everything is in an 2D array # remove 2x zero padding, if necessary if mlab['zp'][0, 0] == 1: signal = signal[:signal.shape[0]/2] print(np.shape(signal)) signal_length = len(signal) sr = mlab['Fs'][0, 0] N = mlab['N_human'][0, 0] low_lim = mlab['low_lim_human'][0, 0] hi_lim = mlab['high_lim_human'][0, 0] sample_factor_list = [1, 2, 4] matlab_api_fx_list = [erb.make_erb_cos_filters_1x, erb.make_erb_cos_filters_2x, erb.make_erb_cos_filters_4x] # use 1x, 2x, and 4x oversampling to test make_erb_cos_filters_nx try: for i in range(len(sample_factor_list)): sample_factor = sample_factor_list[i] # get keynames for looking up matlab reference values freqs_key = 'freqs_%s' % sample_factor hz_cutoffs_key = 'Hz_cutoffs_%s' % sample_factor filts_key = 'fft_filts_%s' % sample_factor fft_sample_key = 'fft_sample_%s' % sample_factor subbands_key = 'subbands_%s' % sample_factor if verbose > 1: print('<Input Params: signal_length: %s, sr: %s, N: %s, low_lim: %s, hi_lim: %s, sample_factor: %s>' % (signal_length, sr, N, low_lim, hi_lim, sample_factor)) if mode == 'all' or mode == 'nx': if verbose > 1: print('making erb filters') # test make_erb_cos_filters_nx filts, hz_cutoffs, freqs = erb.make_erb_cos_filters_nx(signal_length, sr, N, low_lim, hi_lim, sample_factor, pad_factor=1, full_filter=True, strict=False) if verbose > 1: print('performing subband decomposition') start_time = time.time() subbands_dict = sb.generate_subbands(signal.flatten(), filts, 1, debug_ret_all=True) tot_time = time.time() - start_time print('TIME --> %s' % tot_time) # print(subbands.shape, mlab[subbands_key].shape) # subbands = sb.generate_subbands(signal.flatten(), filts, 1, debug_ret_all=False) # print(list(subbands_dict.keys())) if verbose > 1: print('running assertions') # check *full* filterbanks and associated data assert np.allclose(filts, mlab[filts_key].T), 'filts mismatch: make_erb_cos_filters_nx(...)' # transpose because of python assert np.allclose(freqs, mlab[freqs_key]), 'Freqs mismatch: make_erb_cos_filters_nx(...)' assert np.allclose(hz_cutoffs, mlab[hz_cutoffs_key]), 'Hz_cutoff mismatch: make_erb_cos_filters_nx(...)' if verbose > 0: print('PASSED ERB Filters: make_erb_cos_filters_nx(%s)' % sample_factor) # check variables associated with subband decomposition assert np.allclose(subbands_dict['fft_sample'], mlab['fft_sample_1'].flatten()), 'fft sample mismatch: make_erb_cos_filters_nx(...)' assert np.allclose(subbands_dict['subbands'], mlab[subbands_key].T), 'subband mismatch: make_erb_cos_filters_nx(...)' if verbose > 0: print('PASSED Subbands: make_erb_cos_filters_nx(%s)' % sample_factor) # pdb.set_trace() if erb_filter_mode == 'matlab': raise NotImplementedError() if erb_filter_mode == 'literal': raise NotImplementedError() except AssertionError as e: print('FAILED') print('<Input Params: signal_length: %s, sr: %s, N: %s, low_lim: %s, hi_lim: %s, sample_factor: %s>' % (signal_length, sr, N, low_lim, hi_lim, sample_factor)) pdb.set_trace() raise(e)
def test_cochleagram(rfn, erb_filter_mode='all', coch_mode='fast',verbose=0): print('<Testing cochleagrams with coch_mode: %s>' % coch_mode) # load matlab-generated reference file mlab = sio.loadmat(rfn) # print(list(mlab.keys())) # get the function to generate the cochleagrams with coch_fx = _get_coch_function(coch_mode) # extract parameters used to generate these filterbanks signal = mlab['wavData'] # everything is in an 2D array # remove 2x zero padding, if necessary if mlab['zp'][0, 0] == 1: signal = signal[:signal.shape[0]/2] print(np.shape(signal)) signal_length = len(signal) sr = mlab['Fs'][0, 0] N = mlab['N_human'][0, 0] low_lim = mlab['low_lim_human'][0, 0] hi_lim = mlab['high_lim_human'][0, 0] sample_factor_list = [1, 2, 4] # use 1x, 2x, and 4x oversampling to test make_erb_cos_filters_nx try: for i in range(len(sample_factor_list)): sample_factor = sample_factor_list[i] # get keynames for looking up matlab reference values freqs_key = 'freqs_%s' % sample_factor hz_cutoffs_key = 'Hz_cutoffs_%s' % sample_factor filts_key = 'fft_filts_%s' % sample_factor fft_sample_key = 'fft_sample_%s' % sample_factor if verbose > 1: print('<Input Params: signal_length: %s, sr: %s, N: %s, low_lim: %s, hi_lim: %s, sample_factor: %s>' % (signal_length, sr, N, low_lim, hi_lim, sample_factor)) if erb_filter_mode == 'all' or erb_filter_mode == 'nx': if coch_mode == 'coch': sub_envs_key = 'sub_envs_%s' % sample_factor coch = cgram.human_cochleagram(signal, sr, N, low_lim, hi_lim, sample_factor, pad_factor=1, strict=False) assert np.allclose(coch, mlab[sub_envs_key].T), 'subband_env mismatch: make_erb_cos_filters_nx(...)' # transpose for matlab-to-python if verbose > 0: print('\tPASSED Quick Cochleagram: make_erb_cos_filters_nx(%s)' % sample_factor) else: # test make_erb_cos_filters_nx filts, hz_cutoffs, freqs = erb.make_erb_cos_filters_nx(signal_length, sr, N, low_lim, hi_lim, sample_factor, pad_factor=1, full_filter=True, strict=False) # pdb.set_trace() # check *full* filterbanks and associated data assert np.allclose(filts, mlab[filts_key].T), 'filts mismatch: make_erb_cos_filters_nx(...)' # transpose because of python assert np.allclose(freqs, mlab[freqs_key]), 'Freqs mismatch: make_erb_cos_filters_nx(...)' assert np.allclose(hz_cutoffs, mlab[hz_cutoffs_key]), 'Hz_cutoff mismatch: make_erb_cos_filters_nx(...)' if verbose > 0: print('\tPASSED ERB Filters: make_erb_cos_filters_nx(%s)' % sample_factor) # pdb.set_trace() if coch_mode == 'subband': subbands_key = 'subbands_%s' % sample_factor # perform subband decomposition start_time = time.time() subbands_dict = sb.generate_subbands(signal.flatten(), filts, 1, debug_ret_all=True) tot_time = time.time() - start_time print('TIME --> %s' % tot_time) # check variables associated with subband decomposition # assert np.allclose(subbands_dict['fft_sample'], mlab[fft_sample_key].flatten()), 'fft sample mismatch: make_erb_cos_filters_nx(...)' assert np.allclose(subbands_dict['subbands'], mlab[subbands_key].T), 'subband mismatch: make_erb_cos_filters_nx(...)' if verbose > 0: print('\tPASSED Subbands: make_erb_cos_filters_nx(%s)' % sample_factor) elif coch_mode != 'coch': sub_envs_key = 'sub_envs_%s' % sample_factor # generate cochleagrams start_time = time.time() subband_envs = coch_fx(signal,filts, 1) tot_time = time.time() - start_time print('TIME --> %s' % tot_time) # check variables associated with subband envelopes (cochleagrams) assert np.allclose(subband_envs, mlab[sub_envs_key].T), 'subband_env mismatch: make_erb_cos_filters_nx(...)' # transpose for matlab-to-python if verbose > 0: print('\tPASSED Cochleagram: make_erb_cos_filters_nx(%s)' % sample_factor) if erb_filter_mode == 'matlab': raise NotImplementedError('Tests are only implemented for make_erb_cos_filters_nx') if erb_filter_mode == 'literal': raise NotImplementedError('Tests are only implemented for make_erb_cos_filters_nx') except AssertionError as e: print('\tFAILED') print('<Input Params: signal_length: %s, sr: %s, N: %s, low_lim: %s, hi_lim: %s, sample_factor: %s>' % (signal_length, sr, N, low_lim, hi_lim, sample_factor)) pdb.set_trace() raise(e)
def median_filter(signal, size=5): med_signal = medfilt(signal.flatten(), size) return med_signal