def find_admissible_controls(output_file=None, alphamin=-1.25, alphamax=0.25, alphastep=0.01, betamin=-1.50, betamax=1.50, betastep=0.01): no = NormalOscillator() stime = time.time() alpharng = np.arange(alphamin, alphamax, alphastep) betarng = np.arange(betamin, betamax, betastep) nrows = len(alpharng) ncols = len(betarng) print '# of (alpha,beta) pairs: %d' % (nrows * ncols) all_pairs = np.zeros([nrows, ncols, 2]) all_dv_rms = np.zeros([nrows, ncols]) * np.nan all_ff = np.zeros([nrows, ncols]) * np.nan sim_duration = 0.010 step_size = 1e-6 steady_state_point = 0.005 steady_state_index = int(steady_state_point / step_size) for i, alpha in enumerate(alpharng): for j, beta in enumerate(betarng): all_pairs[i, j, :] = [alpha, beta] output = no.simulate(0.0, 0.0, duration=sim_duration, dt=step_size, alpha=alpha, beta=beta) dv = np.diff(output[:, 1]) dv_rms = dv[steady_state_index:].std(ddof=1) all_dv_rms[i, j] = dv_rms #compute power spectrum fftx = fft(output[:, 0]) ps_f = fftfreq(len(output[:, 0]), d=step_size) findx = (ps_f > 100.0) & (ps_f < 8000.0) #estimate fundamental frequency from log power spectrum in the simplest way possible ps = np.abs(fftx[findx]) peak_index = ps.argmax() all_ff[i, j] = ps_f[findx][peak_index] etime = time.time() - stime print 'Elapsed Time: %0.2f s' % etime if output_file is not None: hf = h5py.File(output_file, 'w') hf['all_pairs'] = all_pairs hf['all_dv_rms'] = all_dv_rms hf['all_ff'] = all_ff hf.close()
class Motogram(object): def __init__(self): self.oscillator = NormalOscillator() self.sample_rate = 1000.0 self.waveform_sample_rate = 1e5 self.alpha = list() self.beta = list() self.mu = list() self.sigma1 = list() self.sigma2 = list() def simulate(self): nsteps = len(self.alpha) if nsteps == 0: print 'Nothing to simulate!' total_duration = nsteps / self.sample_rate step_duration = 1.0 / self.sample_rate dt = 1.0 / self.waveform_sample_rate Nwave = int(np.ceil(total_duration * self.waveform_sample_rate)) waveform = np.zeros(Nwave) print 'nsteps=%d, total_duration=%0.6f, step_duration=%0.6f, dt=%0.6f, Nwave=%d' % ( nsteps, total_duration, step_duration, dt, Nwave) #extend alpha and beta by one time step for interpolation alpha_ext = np.zeros(nsteps + 1) alpha_ext[:-1] = self.alpha alpha_ext[-1] = alpha_ext[-2] beta_ext = np.zeros(nsteps + 1) beta_ext[:-1] = self.beta beta_ext[-1] = beta_ext[-2] #upsample alpha and beta to a finer timescale for simulation t_alpha = np.arange(nsteps + 1) * (1.0 / self.sample_rate) f_alpha = interp1d(t_alpha, alpha_ext) f_beta = interp1d(t_alpha, beta_ext) #simulate the oscillator to produce the sound pressure waveform last_x = 0.0 last_v = 0.0 for k in range(Nwave): t = k * dt a = f_alpha(t) b = f_beta(t) states = self.oscillator.simulate(last_x, last_v, dt, dt, alpha=a, beta=b) #print 'k=%d, last_x=%f, last_v=%f, step_duration=%0.6f, dt=%0.6f, alpha=%0.6f, beta=%0.6f' % (k, last_x, last_v, step_duration, dt, a, b) waveform[k] = states[0, 0] last_x = states[0, 0] last_v = states[0, 1] if np.isnan(last_x): last_x = 0.0 if np.isnan(last_v): last_v = 0.0 #transform the waveform into a spectrogram nstd = 6.0 freq_spacing = 125.0 increment = 1.0 / 1000.0 window_length = nstd / (2.0 * np.pi * freq_spacing) spec_t, spec_f, spec, rms = gaussian_stft(waveform, self.waveform_sample_rate, window_length, increment, nstd=nstd, min_freq=0.0, max_freq=10000.0) spec = np.abs(spec) #do vocal tract filtering on the spectrogram spec_filt = np.zeros(spec.shape) for k, t in enumerate(spec_t): mu_t = self.mu[k] sigma1_t = self.sigma1[k] sigma2_t = self.sigma2[k] print 'k=%d, mu_t=%0.6f, sigma1_t=%0.6f, sigma2_t=%0.6f' % ( k, mu_t, sigma1_t, sigma2_t) #create gaussian based on sigma2 mean2 = (mu_t - spec_f) mean2[mean2 < 0.0] = 0.0 gauss2 = mean2**2 / (2 * sigma2_t**2) #create gaussian based on sigma1 mean1 = (spec_f - mu_t) mean1[mean1 < 0.0] = 0.0 gauss1 = mean1**2 / (2 * sigma1_t**2) #create filter as combination of two gaussians filt = np.exp(-(gauss1 + gauss2)) #filter the spectrogram sfilt = spec[:, k] * filt #compute normalization factors spi = spec[:, k].sum() ssi = sfilt.sum() #normalize the filtered spectrogram spec_filt[:, k] = sfilt / (spi * ssi) return waveform, spec_t, spec_f, spec_filt def plot(self): waveform, spec_t, spec_f, spec = self.simulate() nsp = 4 if hasattr(self, 'wav_file'): nsp += 1 plt.figure() plt.subplots_adjust(bottom=0.05, right=0.99, top=0.99, left=0.08, wspace=0.0, hspace=0.25) plt.subplot(nsp, 1, 1) wt = np.arange(len(waveform)) * (1.0 / self.waveform_sample_rate) plt.plot(wt, waveform, 'k-') plt.legend(['x(t)']) plt.axis('tight') plt.subplot(nsp, 1, 2) plt.title('Model Spectrogram') plot_spectrogram(spec_t, spec_f, spec, fmin=0.0, fmax=8000.0) plt.axis('tight') plt.subplot(nsp, 1, 3) mt = np.arange(len(self.alpha)) * (1.0 / self.sample_rate) plt.plot(mt, self.alpha, 'r-') #plt.plot(self.beta, 'b-') plt.legend(['alpha']) plt.axis('tight') plt.subplot(nsp, 1, 4) plt.plot(mt, self.sigma1, 'g-') plt.plot(mt, self.sigma2, 'b-') plt.plot(mt, self.mu, 'k-', linewidth=2) plt.legend(['sigma1', 'sigma2', 'mu']) plt.axis('tight') if hasattr(self, 'wav_file'): plt.subplot(nsp, 1, 5) print 'start_time=%f, end_time=%f' % (self.start_time, self.end_time) si = int(self.start_time * self.sample_rate) ei = int(self.end_time * self.sample_rate) plot_spectrogram(self.wav_file.spectrogram_t[si:ei], self.wav_file.spectrogram_f, self.wav_file.spectrogram[:, si:ei], fmin=0.0, fmax=8000.0) plt.title('Actual Spectrogram') """
class Motogram(object): def __init__(self): self.oscillator = NormalOscillator() self.sample_rate = 1000.0 self.waveform_sample_rate = 1e5 self.alpha = list() self.beta = list() self.mu = list() self.sigma1 = list() self.sigma2 = list() def simulate(self): nsteps = len(self.alpha) if nsteps == 0: print 'Nothing to simulate!' total_duration = nsteps / self.sample_rate step_duration = 1.0 / self.sample_rate dt = 1.0 / self.waveform_sample_rate Nwave = int(np.ceil(total_duration * self.waveform_sample_rate)) waveform = np.zeros(Nwave) print 'nsteps=%d, total_duration=%0.6f, step_duration=%0.6f, dt=%0.6f, Nwave=%d' % (nsteps, total_duration, step_duration, dt, Nwave) #extend alpha and beta by one time step for interpolation alpha_ext = np.zeros(nsteps+1) alpha_ext[:-1] = self.alpha alpha_ext[-1] = alpha_ext[-2] beta_ext = np.zeros(nsteps+1) beta_ext[:-1] = self.beta beta_ext[-1] = beta_ext[-2] #upsample alpha and beta to a finer timescale for simulation t_alpha = np.arange(nsteps+1)*(1.0 / self.sample_rate) f_alpha = interp1d(t_alpha, alpha_ext) f_beta = interp1d(t_alpha, beta_ext) #simulate the oscillator to produce the sound pressure waveform last_x = 0.0 last_v = 0.0 for k in range(Nwave): t = k*dt a = f_alpha(t) b = f_beta(t) states = self.oscillator.simulate(last_x, last_v, dt, dt, alpha=a, beta=b) #print 'k=%d, last_x=%f, last_v=%f, step_duration=%0.6f, dt=%0.6f, alpha=%0.6f, beta=%0.6f' % (k, last_x, last_v, step_duration, dt, a, b) waveform[k] = states[0, 0] last_x = states[0, 0] last_v = states[0, 1] if np.isnan(last_x): last_x = 0.0 if np.isnan(last_v): last_v = 0.0 #transform the waveform into a spectrogram nstd = 6.0 freq_spacing = 125.0 increment = 1.0 / 1000.0 window_length = nstd / (2.0*np.pi*freq_spacing) spec_t,spec_f,spec,rms = gaussian_stft(waveform, self.waveform_sample_rate, window_length, increment, nstd=nstd, min_freq=0.0, max_freq=10000.0) spec = np.abs(spec) #do vocal tract filtering on the spectrogram spec_filt = np.zeros(spec.shape) for k,t in enumerate(spec_t): mu_t = self.mu[k] sigma1_t = self.sigma1[k] sigma2_t = self.sigma2[k] print 'k=%d, mu_t=%0.6f, sigma1_t=%0.6f, sigma2_t=%0.6f' % (k, mu_t, sigma1_t, sigma2_t) #create gaussian based on sigma2 mean2 = (mu_t - spec_f) mean2[mean2 < 0.0] = 0.0 gauss2 = mean2**2 / (2*sigma2_t**2) #create gaussian based on sigma1 mean1 = (spec_f - mu_t) mean1[mean1 < 0.0] = 0.0 gauss1 = mean1**2 / (2*sigma1_t**2) #create filter as combination of two gaussians filt = np.exp(-(gauss1 + gauss2)) #filter the spectrogram sfilt = spec[:, k]*filt #compute normalization factors spi = spec[:, k].sum() ssi = sfilt.sum() #normalize the filtered spectrogram spec_filt[:, k] = sfilt / (spi*ssi) return waveform,spec_t,spec_f,spec_filt def plot(self): waveform,spec_t,spec_f,spec = self.simulate() nsp = 4 if hasattr(self, 'wav_file'): nsp += 1 plt.figure() plt.subplots_adjust(bottom=0.05, right=0.99, top=0.99, left=0.08, wspace=0.0, hspace=0.25) plt.subplot(nsp, 1, 1) wt = np.arange(len(waveform))*(1.0 / self.waveform_sample_rate) plt.plot(wt, waveform, 'k-') plt.legend(['x(t)']) plt.axis('tight') plt.subplot(nsp, 1, 2) plt.title('Model Spectrogram') plot_spectrogram(spec_t, spec_f, spec, fmin=0.0, fmax=8000.0) plt.axis('tight') plt.subplot(nsp, 1, 3) mt = np.arange(len(self.alpha))*(1.0 / self.sample_rate) plt.plot(mt, self.alpha, 'r-') #plt.plot(self.beta, 'b-') plt.legend(['alpha']) plt.axis('tight') plt.subplot(nsp, 1, 4) plt.plot(mt, self.sigma1, 'g-') plt.plot(mt, self.sigma2, 'b-') plt.plot(mt, self.mu, 'k-', linewidth=2) plt.legend(['sigma1', 'sigma2', 'mu']) plt.axis('tight') if hasattr(self, 'wav_file'): plt.subplot(nsp, 1, 5) print 'start_time=%f, end_time=%f' % (self.start_time, self.end_time) si = int(self.start_time * self.sample_rate) ei = int(self.end_time * self.sample_rate) plot_spectrogram(self.wav_file.spectrogram_t[si:ei], self.wav_file.spectrogram_f, self.wav_file.spectrogram[:, si:ei], fmin=0.0, fmax=8000.0) plt.title('Actual Spectrogram') """