# Embdedded player from: https://github.com/Carreau/posts import StringIO import base64 import struct from IPython.core.display import HTML def wavPlayer(data, rate): """ will display html 5 player for compatible browser The browser need to know how to play wav through html5. there is no autoplay to prevent file playing when the browser opens Adapted from SciPy.io. """ buffer = StringIO.StringIO() buffer.write(b'RIFF') buffer.write(b'\x00\x00\x00\x00') buffer.write(b'WAVE') buffer.write(b'fmt ') if data.ndim == 1: noc = 1 else: noc = data.shape[1] bits = data.dtype.itemsize * 8 sbytes = rate*(bits // 8)*noc ba = noc * (bits // 8) buffer.write(struct.pack('' or (data.dtype.byteorder == '=' and sys.byteorder == 'big'): data = data.byteswap() buffer.write(data.tostring()) size = buffer.tell() buffer.seek(4) buffer.write(struct.pack(' """.format(base64=base64.encodestring(val)) display(HTML(src)) from scipy.io import wavfile sr, src = wavfile.read('sources/THX.wav') wavPlayer(src , sr) src.shape, src.dtype rcParams['figure.figsize'] = (16, 4) src_mono = sum(src/2, axis=1) src_mono.shape src_mono = sum(src.astype(float), axis=1)/src.shape[1] src_mono.shape src_mono = sum(src.astype(float)/src.shape[1], axis=1).astype(src.dtype) src_mono.shape, src_mono.dtype plot(src_mono) figure() plot(src); src.max(), src_mono.max(), src.min(), src_mono.min() abs_max = max(abs(src_mono.min()), abs(src_mono.max())) print abs_max abs(src_mono.min()) -src_mono.min(),src_mono.min() abs(src_mono.min().astype(int64)) abs_max = max(abs(src_mono.min().astype(float)), abs(src_mono.max().astype(float))) print abs_max iinfo(int16), iinfo(uint8), finfo(float32), finfo(float) src_mono.dtype iinfo(src_mono.dtype) src_mono_norm = src_mono.astype(float) * -iinfo(src_mono.dtype).min / abs_max src_mono_norm.dtype src_mono.dtype src_mono_norm = src_mono.astype(float) / abs_max plot(src_mono_norm) from scipy.signal import decimate src_mono_norm_dec = decimate(src_mono_norm, 4) plot(src_mono_norm_dec) src_mono_norm_dec.max() len(src_mono_norm_dec)/float(len(src_mono_norm)) from scipy.signal import resample src_mono_norm_rsmpl = resample(src_mono_norm, len(src_mono_norm) * 1.5) plot(src_mono_norm_rsmpl) src_mono_norm_dec.max(), src_mono_norm_dec.min() src_mono_norm_dec.max() - src_mono_norm_dec.min() src_mono_norm_dec.mean() abs(src_mono_norm_dec).mean() dur = len(src_mono_norm_dec)/(sr*1.5) print dur, 'seconds' N = float(len(src_mono_norm_dec)) ms = sum(src_mono_norm_dec**2)/N # or use the mean() function rms = sqrt(ms) print rms, 'RMS' from scipy.signal import argrelextrema maxima = argrelextrema(src_mono_norm_dec, np.greater) minima = argrelextrema(src_mono_norm_dec, np.less) minima plot(src_mono_norm_dec) plot(maxima[0], zeros_like(maxima[0]),'x') plot(minima[0], zeros_like(minima[0]),'x') xlim((50000, 50200)) ylim((-0.25, 0.25)) grid() maxima = argrelextrema(abs(src_mono_norm_dec), np.greater) plot(abs(src_mono_norm_dec)) plot(maxima[0], zeros_like(maxima[0]),'x') xlim((50000, 50200)) ylim((-0.25, 0.25)) grid() gcf().set_figwidth(16) maxima = argrelextrema(abs(src_mono_norm_dec), np.greater) plot(abs(src_mono_norm_dec)) plot(maxima[0], abs(src_mono_norm_dec[maxima[0]]),'o-') xlim((50000, 50200)) ylim((0, 0.25)) grid() gcf().set_figwidth(16) plot(maxima[0], abs(src_mono_norm_dec[maxima[0]]),'g') plot(maxima[0], abs(src_mono_norm_dec[maxima[0]]),'g') xlim((150000,160000)) from scipy.interpolate import interp1d fi = interp1d(maxima[0], abs(src_mono_norm_dec[maxima[0]])) plot(fi(linspace(0, len(src_mono_norm_dec), 1600))) fi = interp1d(maxima[0], abs(src_mono_norm_dec[maxima[0]])) plot(fi(linspace(100, len(src_mono_norm_dec) - 100, 1600))) fi = interp1d(maxima[0], abs(src_mono_norm_dec[maxima[0]])) plot(fi(linspace(maxima[0][0], maxima[0][-1], 1600))) plot(src_mono_norm_dec) fi = interp1d(maxima[0], abs(src_mono_norm_dec[maxima[0]])) interp_out = fi(linspace(maxima[0][0], maxima[0][-1], 100)) plot(linspace(0, len(src_mono_norm_dec), len(interp_out)), interp_out, lw=3, color='w') fi = interp1d(maxima[0], abs(src_mono_norm_dec[maxima[0]])) peak_interp = fi( linspace(maxima[0][0], maxima[0][-1], len(src_mono_norm_dec) - (maxima[0][0] - maxima[0][-1]))) plot(linspace(maxima[0][0], maxima[0][-1], len(peak_interp)), peak_interp, 'x') plot(maxima[0], abs(src_mono_norm_dec[maxima[0]]),'o-') xlim((50000, 50200)) ylim((0, 0.25)) grid() peak_interp_dec = decimate(peak_interp,99) plot(peak_interp_dec) plot(src_mono_norm_dec) twinx() plot(linspace(0, len(src_mono_norm_dec), len(peak_interp_dec)),peak_interp_dec, color='r', lw='3') ylim((-0.0020, 0.0020)) xlim((50000, 65000)) plot(src_mono_norm_dec) twinx() plot(linspace(0, len(src_mono_norm_dec), len(peak_interp_dec)),peak_interp_dec, color='r', lw='3') ylim((-0.0020, 0.0020)) xlim((50000, 225000)) # Moving average (smoothing filter) w = ones(32) plot(convolve(w/w.sum(),peak_interp_dec,mode='valid')) figure() plot(src_mono_norm_dec) sr2, src2 = wavfile.read('sources/passport.wav') wavPlayer(src2 , sr2) maxima = argrelextrema(abs(src2), np.greater) plot(abs(src2)) plot(maxima[0], abs(src2[maxima[0]]),'o-') fi = interp1d(maxima[0], abs(src2[maxima[0]])) peak_interp = fi(linspace(100, len(src2)-100, 1600)) plot(peak_interp) peak_interp_dec = decimate(peak_interp,99) plot(peak_interp_dec) peak_interp_dec = decimate(peak_interp,5) plot(peak_interp_dec) w = ones(5) plot(convolve(w/w.sum(),peak_interp_dec,mode='valid')) figure() plot(src2) win_size = 2048 hop = 1024 window_start = arange(0, len(src_mono_norm), hop) window_start rms = [] for start in window_start: w = src_mono_norm[start: start+win_size].astype(float) rms_inst = sqrt(mean(w**2)) rms.append(rms_inst) plot(rms) win_sizes = [512, 1024, 2048, 4096] rms_windows = [] for win_size in win_sizes: hop = win_size/2 window_start = arange(0, len(src_mono_norm), hop) rms = [] for start in window_start: w = src_mono_norm[start: start+win_size].astype(float) rms_inst = sqrt(mean(w**2)) rms.append(rms_inst) rms_windows.append(rms) for rms_plot in rms_windows: plot(rms_plot) for rms_plot in rms_windows: plot(linspace(0, len(src_mono_norm_dec), len(rms_plot)), rms_plot) plot(linspace(0, len(src_mono_norm_dec), len(rms_windows[-1])), rms_windows[-1], lw=3, color='w') for rms_plot in rms_windows: plot(linspace(0, len(src_mono_norm_dec), len(rms_plot)), rms_plot, 'x-') xlim((100000, 105000)) ylim((0.1, 0.3)); legend(win_sizes) def windowed_rms(input_sig, win_sizes = [512, 1024, 2048, 4096], hop=None): rms_windows = [] for win_size in win_sizes: if not hop: hop = win_size/2 window_start = arange(0, len(input_sig) - win_size, hop) rms = [] for start in window_start: w = input_sig[start: start+win_size].astype(float) rms_inst = sqrt(mean(w**2)) rms.append(rms_inst) rms_windows.append(rms) return rms_windows, win_sizes w_rms, win_sizes = windowed_rms(src2) for rms_plot in w_rms: plot(linspace(0, len(src2), len(rms_plot)), rms_plot) plot(linspace(0, len(src2), len(w_rms[-1])), w_rms[-1], lw=3, color='k') sr = 44100 L = 0.1 win_len = sr * L print win_len w_rms, win_lens = windowed_rms(src2, win_sizes=[win_len, 10000]) for rms_plot in w_rms: plot(linspace(0, len(src2), len(rms_plot)), rms_plot) legend(win_lens) gcf().set_figwidth(16)