def get_bin_f0_B(bin_initial_estimate, fft_buffers, noise_cutoff, bin_spread_below, bin_spread_above, fs): ### gradually discover/refine estimates of bin_f0 and B bin_f0 = bin_initial_estimate B = 1e-4 limit = defs.TOTAL_HARMONICS for i in range(defs.B_INITIAL_PARTIALS, defs.TOTAL_HARMONICS): #print fft_buffers.shape bin_f0, B, rsquared, ok = align_with_B(i, bin_f0, B, fft_buffers, noise_cutoff, bin_spread_below, bin_spread_above, fs) limit = stiff_ideal_conflict.find_limit(bin_f0, B, bin_spread_below, bin_spread_above) #print B, rsquared, limit if i > limit: #print "warning: exceeding lim?", i, limit ok = False if defs.B_PRINT_INDIVIDUAL_BS: print "%i\t%.4f\t%.5e\t\t%i\t%i" % ( i, bin_f0, B, ok, 0) if ok is False: limit = i break rows, columns = fft_buffers.shape partialss = [] idealss = [] for j in range(rows): fft = fft_buffers[j] #harmonics, ideal_harmonics = partials.get_freqs_mags(defs.TOTAL_HARMONICS, bin_f0, B, harmonics, ideal_harmonics = partials.get_freqs_mags(limit, bin_f0, B, fft, bin_spread_below, bin_spread_above, only_peaks=True, Bsearch=True) harmonics = [ h for h in harmonics if h.mag > noise_cutoff[h.fft_bin] ] ideal_harmonics = [ h for h in ideal_harmonics if h.mag > noise_cutoff[h.fft_bin] ] partialss.extend(harmonics) idealss.extend(ideal_harmonics) #if True: if False: pylab.figure() for j in range(rows): fft = fft_buffers[j] pylab.plot(stft.amplitude2db(fft), color="orange") xs = [ h.fft_bin for h in partialss] #if h.n > defs.B_MIN_HARMONIC_FIT_TO ] ys = [ h.mag for h in partialss] #if h.n > defs.B_MIN_HARMONIC_FIT_TO ] pylab.plot(xs, stft.amplitude2db(ys), 'p', color="blue") pylab.plot(stft.amplitude2db(noise_cutoff), color="black") #ns = [ h.n for h in partialss if h.n > defs.B_MIN_HARMONIC_FIT_TO ] #pylab.plot( ns, # [ h.fft_bin - partials.mode_B2freq(bin_f0, h.n, B) # for h in partialss if h.n > defs.B_MIN_HARMONIC_FIT_TO ], # '.') pylab.show() if defs.B_PLOT_FIT: ns = [ h.n for h in partialss if h.n > defs.B_MIN_HARMONIC_FIT_TO ] ys = [ h.fft_bin / h.n for h in partialss if h.n > defs.B_MIN_HARMONIC_FIT_TO ] omit_ns = [ h.n for h in partialss if h.n <= defs.B_MIN_HARMONIC_FIT_TO ] omit_ys = [ h.fft_bin / h.n for h in partialss if h.n <= defs.B_MIN_HARMONIC_FIT_TO ] plots.plot_B_fit(ns, ys, omit_ns, omit_ys, bin_f0, B, fs, idealss) pylab.show() return bin_f0, B, rsquared, partialss, limit
def get_bin_f0_B(bin_initial_estimate, fft_buffers, noise_cutoff, bin_spread_below, bin_spread_above, fs): ### gradually discover/refine estimates of bin_f0 and B bin_f0 = bin_initial_estimate B = 1e-4 limit = defs.TOTAL_HARMONICS for i in range(defs.B_INITIAL_PARTIALS, defs.TOTAL_HARMONICS): #print fft_buffers.shape bin_f0, B, rsquared, ok = align_with_B(i, bin_f0, B, fft_buffers, noise_cutoff, bin_spread_below, bin_spread_above, fs) limit = stiff_ideal_conflict.find_limit(bin_f0, B, bin_spread_below, bin_spread_above) #print B, rsquared, limit if i > limit: #print "warning: exceeding lim?", i, limit ok = False if defs.B_PRINT_INDIVIDUAL_BS: print "%i\t%.4f\t%.5e\t\t%i\t%i" % (i, bin_f0, B, ok, 0) if ok is False: limit = i break rows, columns = fft_buffers.shape partialss = [] idealss = [] for j in range(rows): fft = fft_buffers[j] #harmonics, ideal_harmonics = partials.get_freqs_mags(defs.TOTAL_HARMONICS, bin_f0, B, harmonics, ideal_harmonics = partials.get_freqs_mags(limit, bin_f0, B, fft, bin_spread_below, bin_spread_above, only_peaks=True, Bsearch=True) harmonics = [h for h in harmonics if h.mag > noise_cutoff[h.fft_bin]] ideal_harmonics = [ h for h in ideal_harmonics if h.mag > noise_cutoff[h.fft_bin] ] partialss.extend(harmonics) idealss.extend(ideal_harmonics) #if True: if False: pylab.figure() for j in range(rows): fft = fft_buffers[j] pylab.plot(stft.amplitude2db(fft), color="orange") xs = [h.fft_bin for h in partialss] #if h.n > defs.B_MIN_HARMONIC_FIT_TO ] ys = [h.mag for h in partialss] #if h.n > defs.B_MIN_HARMONIC_FIT_TO ] pylab.plot(xs, stft.amplitude2db(ys), 'p', color="blue") pylab.plot(stft.amplitude2db(noise_cutoff), color="black") #ns = [ h.n for h in partialss if h.n > defs.B_MIN_HARMONIC_FIT_TO ] #pylab.plot( ns, # [ h.fft_bin - partials.mode_B2freq(bin_f0, h.n, B) # for h in partialss if h.n > defs.B_MIN_HARMONIC_FIT_TO ], # '.') pylab.show() if defs.B_PLOT_FIT: ns = [h.n for h in partialss if h.n > defs.B_MIN_HARMONIC_FIT_TO] ys = [ h.fft_bin / h.n for h in partialss if h.n > defs.B_MIN_HARMONIC_FIT_TO ] omit_ns = [h.n for h in partialss if h.n <= defs.B_MIN_HARMONIC_FIT_TO] omit_ys = [ h.fft_bin / h.n for h in partialss if h.n <= defs.B_MIN_HARMONIC_FIT_TO ] plots.plot_B_fit(ns, ys, omit_ns, omit_ys, bin_f0, B, fs, idealss) pylab.show() return bin_f0, B, rsquared, partialss, limit
def align_with_B(num_harmonics, bin_f0, B, ffts, noise_cutoff, bin_spread_below, bin_spread_above, sample_rate): rows, columns = ffts.shape partialss = [] idealss = [] for j in range(rows): fft = ffts[j] harmonics, ideal_harmonics = partials.get_freqs_mags(num_harmonics, bin_f0, B, fft, bin_spread_below, bin_spread_above, only_peaks=True, Bsearch=True) if harmonics is None: return bin_f0, B, None, False harmonics = [ h for h in harmonics if h.mag > noise_cutoff[h.fft_bin] ] ideal_harmonics = [ h for h in ideal_harmonics if h.mag > noise_cutoff[h.fft_bin] ] partialss.extend(harmonics) idealss.extend(ideal_harmonics) #ns = [ h.n for h in partialss if h.n > 1 ] #ys = [ h.fft_bin / h.n for h in partialss if h.n > 1 ] ns = [ h.n for h in partialss if h.n > defs.B_MIN_HARMONIC_FIT_TO ] ys = [ h.fft_bin / h.n for h in partialss if h.n > defs.B_MIN_HARMONIC_FIT_TO ] ns_orig = list(ns) ys_orig = list(ys) ns = [] ys = [] weights = [] #for i, h in enumerate(partialss): # if h.n <= defs.B_MIN_HARMONIC_FIT_TO: # continue #if stiff_ideal_conflict.does_confict(bin_f0, B, # bin_spread_below, bin_spread_above, h.n): # #print "bail from conflict" # continue # ns.append( h.n ) # ys.append( h.fft_bin / h.n) # weights.append( h.mag - noise_cutoff[h.fft_bin] ) ns = ns_orig ys = ys_orig bin_f0, B, rsquared = estimate_B(ns, ys, weights, initial_bin_f0=bin_f0, initial_B=B) if defs.B_PLOT_FIT_INDIVIDUAL: omit_ns = [ h.n for h in partialss if h.n <= defs.B_MIN_HARMONIC_FIT_TO ] omit_ys = [ h.fft_bin / h.n for h in partialss if h.n <= defs.B_MIN_HARMONIC_FIT_TO ] plots.plot_B_fit(ns, ys, omit_ns, omit_ys, bin_f0, B, sample_rate, idealss) pylab.show() ok = True nums_per = [] #print '----' for n in range(1,num_harmonics+1): nums = len( [h.n for h in partialss if h.n == n]) nums_per.append(nums) #print nums, if nums_per[num_harmonics-1] < 4: #ok = False #print "bail", num_harmonics pass return bin_f0, B, rsquared, ok
def align_with_B(num_harmonics, bin_f0, B, ffts, noise_cutoff, bin_spread_below, bin_spread_above, sample_rate): rows, columns = ffts.shape partialss = [] idealss = [] for j in range(rows): fft = ffts[j] harmonics, ideal_harmonics = partials.get_freqs_mags(num_harmonics, bin_f0, B, fft, bin_spread_below, bin_spread_above, only_peaks=True, Bsearch=True) if harmonics is None: return bin_f0, B, None, False harmonics = [h for h in harmonics if h.mag > noise_cutoff[h.fft_bin]] ideal_harmonics = [ h for h in ideal_harmonics if h.mag > noise_cutoff[h.fft_bin] ] partialss.extend(harmonics) idealss.extend(ideal_harmonics) #ns = [ h.n for h in partialss if h.n > 1 ] #ys = [ h.fft_bin / h.n for h in partialss if h.n > 1 ] ns = [h.n for h in partialss if h.n > defs.B_MIN_HARMONIC_FIT_TO] ys = [ h.fft_bin / h.n for h in partialss if h.n > defs.B_MIN_HARMONIC_FIT_TO ] ns_orig = list(ns) ys_orig = list(ys) ns = [] ys = [] weights = [] #for i, h in enumerate(partialss): # if h.n <= defs.B_MIN_HARMONIC_FIT_TO: # continue #if stiff_ideal_conflict.does_confict(bin_f0, B, # bin_spread_below, bin_spread_above, h.n): # #print "bail from conflict" # continue # ns.append( h.n ) # ys.append( h.fft_bin / h.n) # weights.append( h.mag - noise_cutoff[h.fft_bin] ) ns = ns_orig ys = ys_orig bin_f0, B, rsquared = estimate_B(ns, ys, weights, initial_bin_f0=bin_f0, initial_B=B) if defs.B_PLOT_FIT_INDIVIDUAL: omit_ns = [h.n for h in partialss if h.n <= defs.B_MIN_HARMONIC_FIT_TO] omit_ys = [ h.fft_bin / h.n for h in partialss if h.n <= defs.B_MIN_HARMONIC_FIT_TO ] plots.plot_B_fit(ns, ys, omit_ns, omit_ys, bin_f0, B, sample_rate, idealss) pylab.show() ok = True nums_per = [] #print '----' for n in range(1, num_harmonics + 1): nums = len([h.n for h in partialss if h.n == n]) nums_per.append(nums) #print nums, if nums_per[num_harmonics - 1] < 4: #ok = False #print "bail", num_harmonics pass return bin_f0, B, rsquared, ok