import numpy as np import pickle import matplotlib.pyplot as plt import scfb import scfbutils ################################################################################ if __name__ == "__main__": chunks, sig_len_n = pickle.load(open("chunks.pkl", "rb")) freqs = np.logspace(np.log10(50.0), np.log10(4000.0), 100) lims = np.sqrt(freqs[0:-1] * freqs[1:]) lims = np.concatenate([[0], lims, [20000]]) # limits = [] # for k in range(len(freqs)): # limits.append((lims[k], lims[k+1])) limits = [(lims[k], lims[k + 1]) for k in range(len(freqs))] # freqs = np.linspace(50.0, 4000.0, 100) bump_heights = [(1 / k)**0.9 for k in range(1, 7)] bump_heights = np.array(bump_heights) sigma = 0.03 mu = 1 # turn on limits if want to eliminate "butte" behavior temp_array = scfb.TemplateArray(chunks, sig_len_n, freqs, sigma, bump_heights, mu) #, limits=limits) temp_array.adapt(verbose=True) pickle.dump((temp_array.templates, freqs), open('template_data.pkl', 'wb'))
f0 = 205.0 fs = 44100 num_h = 6 mt_h = 0.5 len_t = 0.150 len_n = int(fs * len_t) idcs = np.arange(len_n, dtype=np.int32) chunks = [] for p in range(1, num_h + 1): factor = 1.05 if p == mt_h else 1.0 print(f0 * p * factor) chunks.append((idcs, np.ones(len_n) * f0 * p * factor)) f_vals = np.ones(1) * f0 * 0.95 ta = scfb.TemplateArray(chunks, len_n, f_vals, 8, sigma, mu, scale, beta) ta.adapt(verbose=True) print(ta.templates[0].f_vals[-1]) phi = ta.templates[0].f_vals s = ta.templates[0].strengths fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) ax2 = fig.add_subplot(1, 2, 2) ax1.set_xlim(100.0, 1500.0) ax1.set_ylim(-.5, 1.25) ax2.set_xlim(100.0, 1500.0) ax2.set_ylim(-.5, 1.25) ax1.set_ylabel("Magnitude", fontsize=16)
## process through SCFB peri = scfb.SCFB(peri_f_lo, peri_f_hi, num_peri_units, f_s) peri.process_signal(in_sig, verbose=True) ## process through templates chunks = peri.chunks # probably pickle this freqs = np.logspace(np.log10(temp_f_lo), np.log10(temp_f_hi), num_templates) lims = np.sqrt(freqs[0:-1] * freqs[1:]) lims = np.concatenate([[0], lims, [20000]]) limits = [] for k in range(len(freqs)): limits.append((lims[k], lims[k + 1])) temp_array = scfb.TemplateArray(chunks, sig_len_n, freqs, temp_num_h, sigma, mu, limits=limits) temp_array.adapt(verbose=True) ## process through WTAN templates = temp_array.templates # probably pickle this t = np.arange(len(templates[0].strengths)) * (1. / 44100) strengths = [t.strengths for t in templates] # strengths = np.ascontiguousarray(np.flipud(np.stack(strengths, axis=0))) strengths = np.ascontiguousarray(np.stack(strengths, axis=0)) k = np.ones( (strengths.shape[0], strengths.shape[0])) * 5. # inhibition constant # more elaborate inhibition schemes commented out below # max_val = strengths.shape[0]*strengths.shape[1]
beta = 0.9 pitch_rel = np.zeros_like(mistunings) for q in range(num_h): h_mistuned = q + 1 for k in range(len(mistunings)): # make the input chunks chunks = [] for p in range(1, num_h + 1): if p == h_mistuned: chunks.append( (chunk_idcs, np.ones(sig_len_n) * f0 * p * mistunings[k])) else: chunks.append((chunk_idcs, np.ones(sig_len_n) * f0 * p)) if k == 0: temp_array = scfb.TemplateArray(chunks, sig_len_n, t_freqs, num_h, sigma, mu, scale, beta) else: temp_array.new_data(chunks, sig_len_n) temp_array.adapt() strengths = [t.strengths[-1] for t in temp_array.templates] pitch_idx = np.argmax(strengths) pitch[k] = temp_array.templates[pitch_idx].f_vals[-1] pitch_rel += pitch / f0 pitch_rel /= num_h fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) ax2 = fig.add_subplot(1, 2, 2) ax1.set_ylabel("Perceived pitch shift (percentage)", size=14)