def num_above_noise(signal, noise_top): begin_above = None for i, hm in enumerate(signal): noise_top_extra = stft.db2amplitude( stft.amplitude2db(noise_top) + defs.HARMONIC_DB_ABOVE_NOISE_TOP) if hm < noise_top_extra: begin_above = i break return begin_above
def estimate_f0_B(filenames): ### ASSUME: all filenames are of the same instrument-string wav_filename = filenames[0] basename='-'.join(os.path.basename(wav_filename).split('-')[0:3]) if basename.startswith("test-440f"): return 440.0, 0, 1, 1, 1, 1 ### get initial f0 estimate base_frequency_estimate = expected_frequencies.get_freq_from_filename( wav_filename) ### get noise initial_noise_floor, initial_noise_freqs, _, _, _ = calc_noise.get_noise(wav_filename) noise_cutoff = stft.db2amplitude( stft.amplitude2db(initial_noise_floor)+defs.B_MINIMUM_HARMONIC_SNR) #### get FFT frames from audio files sample_rate = None freqs = None estimate_B_buffers_list = [] for wav_i, wav_filename in enumerate(filenames): #print wav_filename #window_buffer, sample_rate = stft.get_long_buffer_from_file(wav_filename, window_buffers, sample_rate = stft.get_buffers_from_file(wav_filename, (defs.B_NUM_BUFFERS_ESTIMATE)) if freqs is None: freqs = [ stft.bin2hertz(i, sample_rate) for i in range(stft.WINDOWSIZE/2+1) ] estimate_B_buffers_this_list = [] #fft_amplitude = stft.fft_amplitude(window_buffer, sample_rate) #estimate_B_buffers_this_list.append(fft_amplitude) for window_number in range(defs.B_NUM_BUFFERS_ESTIMATE): window_buffer = window_buffers[window_number] fft_amplitude = stft.stft_amplitude(window_buffer) estimate_B_buffers_this_list.append(fft_amplitude) estimate_B_buffers_list.extend(estimate_B_buffers_this_list) estimate_B_buffers = numpy.array(estimate_B_buffers_list) ### radius of search area for peaks # used with STFT only bin_initial_estimate = stft.hertz2bin(base_frequency_estimate, sample_rate) #bin_initial_estimate = (base_frequency_estimate # * fft_amplitude.shape[0] / (sample_rate/2) # ) #print bin_initial_estimate bin_spread_below = int(math.ceil(abs( stft.hertz2bin( (1.0-defs.B_PEAK_SPREAD_BELOW_HERTZ)*base_frequency_estimate, sample_rate) - bin_initial_estimate))) bin_spread_above = int(math.ceil( stft.hertz2bin( (1.0+defs.B_PEAK_SPREAD_ABOVE_HERTZ)*base_frequency_estimate, sample_rate) - bin_initial_estimate)) #bin_spread_below = int(round(bin_initial_estimate * # defs.B_PEAK_SPREAD_BELOW_HERTZ)) #bin_spread_above = int(round(bin_initial_estimate * # defs.B_PEAK_SPREAD_BELOW_HERTZ)) #bin_spread_below_main = int( # stft.hertz2bin(defs.STFT_PEAK_SPREAD_BELOW_HERTZ*base_frequency_estimate, # sample_rate)) #bin_spread_above_main = int( # stft.hertz2bin(defs.STFT_PEAK_SPREAD_ABOVE_HERTZ*base_frequency_estimate, # sample_rate)) ### actual estimate bin_f0, B, rsquared, harmonics, limit = get_bin_f0_B( bin_initial_estimate, estimate_B_buffers, noise_cutoff, #estimate_B_buffers, numpy.zeros(defs.LONG_WINDOWSIZE+1), bin_spread_below, bin_spread_above, sample_rate) highest_harmonic = 0 for h in harmonics: if highest_harmonic < h.n: highest_harmonic = h.n limit = min(limit, highest_harmonic) # HACK: remove limit #limit = defs.TOTAL_HARMONICS #print "limit to:", limit #harmonics_enable = [True]*defs.TOTAL_HARMONICS harmonics_enable = [True]*limit bins_estimate = [ partials.mode_B2freq(bin_f0, i, B) for i in range(1,len(harmonics_enable)+1) ] bins_naive = [ i*bin_f0 for i in range(1,len(harmonics_enable)+1) ] if defs.B_PLOT: pylab.figure() pylab.plot(initial_noise_freqs, stft.amplitude2db(initial_noise_floor), color='black') #pylab.plot(initial_noise_freqs, # stft.amplitude2db(initial_noise_floor)+defs.B_MINIMUM_HARMONIC_SNR, # color='black') pylab.xlabel("Frequency (seconds)") pylab.ylabel("Power (/ dB)") for i in range(estimate_B_buffers.shape[0]): #color = matplotlib.cm.spring(float(wav_i)/len(filenames)) #color = matplotlib.cm.RdYlGn( #color = matplotlib.cm.spring( # float(i)/len(estimate_B_buffers_this_list)) pylab.plot(freqs, stft.amplitude2db(estimate_B_buffers[i,:]), #color=color, color="orange", alpha=0.5, label=basename, ) for est in bins_estimate: low = stft.bin2hertz(est - bin_spread_below, sample_rate) high = stft.bin2hertz(est + bin_spread_above, sample_rate) if True: pylab.axvspan(low, high, color='c', alpha=0.3) else: pylab.axvline(stft.bin2hertz(est, sample_rate), color='cyan', alpha=0.3, #linewidth=2.0 ) for naive in bins_naive: freq = stft.bin2hertz(naive, sample_rate) pylab.axvline(freq, color='grey', alpha=0.2, #linewidth=2.0 ) for j, harm in enumerate(harmonics): if harm.mag == 0: continue fn = stft.bin2hertz(harm.fft_bin, sample_rate) mag = stft.amplitude2db(harm.mag) #pylab.plot(fn, mag, 'o', # color='green' # ) pylab.xlabel("Frequency") pylab.ylabel("Decibels") if defs.B_DUMP_HARMS: t_fns = [] t_mags = [] for j, harm in enumerate(harmonics): if harm.mag == 0: continue fn = stft.bin2hertz(harm.fft_bin, sample_rate) mag = stft.amplitude2db(harm.mag) t_fns.append(fn) t_mags.append(mag) data = numpy.vstack((t_fns, t_mags)).transpose() numpy.savetxt("B-harms.txt", data) if defs.B_PLOT: pylab.show() f0 = stft.bin2hertz(bin_f0, sample_rate) stiff_ideal_limit = stiff_ideal_conflict.find_limit(bin_f0, B, bin_spread_below, bin_spread_above) lim = min(stiff_ideal_limit, limit) detected_freqs = StringFreqsB(f0, B, lim) stats = StringFreqsB_stats() stats.num_files = len(filenames) stats.rsquared = rsquared stats.highest_mode_detected = limit stats.highest_mode_stiff_ideal = stiff_ideal_limit stats.basename = basename adjusted_B, delta_fn = adjust_B.adjust(basename, limit, f0, B) if adjusted_B is not None: stiff_ideal_lim_adjusted = stiff_ideal_conflict.find_limit( bin_f0, adjusted_B, bin_spread_below, bin_spread_above) lim = min(stiff_ideal_lim_adjusted, limit) adjusted_freqs = StringFreqsB(f0, adjusted_B, lim) adjusted_freqs.delta_fn = delta_fn stats.highest_mode_stiff_ideal_adjusted = stiff_ideal_lim_adjusted stats.delta_fn = delta_fn final = StringFreqsB(f0, adjusted_B, min(stats.highest_mode_detected, stats.highest_mode_stiff_ideal, stiff_ideal_lim_adjusted)) else: adjusted_freqs = None final = StringFreqsB(f0, B, min(stats.highest_mode_detected, stats.highest_mode_stiff_ideal)) return detected_freqs, adjusted_freqs, stats, final
def generate_data(self, dirname, basename, plot_harms=False): #png_dirname = os.path.join(dirname, 'png') #if not os.path.exists(png_dirname): # os.makedirs(png_dirname) if defs.ONLY_FILES_CONTAINING: search_filename = '%s*%s*wav' % ( basename, defs.ONLY_FILES_CONTAINING) else: search_filename = basename + '*.wav' filenames = glob.glob( os.path.join(dirname, search_filename)) filenames = filter(lambda x: "noise" not in x, filenames) filenames.sort() if defs.ONLY_N_FILES > 0: filenames = filenames[:defs.ONLY_N_FILES] _, _, _, final = estimate_f0_B.estimate_f0_B(filenames) f0 = final.f0 B = final.B limit = final.highest_mode stats = HarmonicsStats() stats.num_files = len(filenames) decays = [] for wav_filename_count, wav_filename in enumerate(filenames): basename = os.path.basename(wav_filename) #print "Processing", wav_filename pickle_filename = wav_filename+".stft.pickle" if os.path.exists(pickle_filename): pickle_file = open(pickle_filename, 'rb') harmonics, hop_rate = pickle.load(pickle_file) pickle_file.close() #print "... read pickle" else: #print "... calculating new" #frequency = expected_frequencies.get_freq_from_filename( # wav_filename, f0, B) harmonics, hop_rate = stft_interface.get_harmonics( wav_filename, f0, B, limit) pickle_file = open(pickle_filename, 'wb') pickle.dump( (harmonics, hop_rate), pickle_file, -1) pickle_file.close() #print "... wrote pickle" nums = tables.save_partials(os.path.splitext(basename)[0]) if nums: dest_dir = "out/" for num in nums: h = harmonics[num] #print h.n data = numpy.vstack( ( h.frame_numbers*hop_rate, stft.amplitude2db(h.mags) )).transpose() filename = dest_dir + '/partials-%s-%i.txt' % ( basename, num) numpy.savetxt( filename, data) print "Wrote to %s" % filename for i, h in enumerate(harmonics): stats.num_harms_original += 1 if len(h.mags) < 2: stats.num_harms_max_no_above_noise += 1 continue #if h.n > 0: # pylab.figure() # pylab.semilogy(h.mags, '.') # pylab.title("mode %i" % h.n) # pylab.show() #N = 16 #b, a = scipy.signal.butter(N, 0.25) #b = scipy.signal.firwin(N, 0.25) #a = 1.0 #zi = scipy.signal.lfiltic(b, a, h.mags[0:N], # h.mags[0:N]) #h.mags, zf = scipy.signal.lfilter(b, a, h.mags, # zi=zi) #pylab.semilogy(h.mags) #pylab.show() #if defs.HARMONICS_PRINT_SUMMARY: # print "n: %i\tbegin" %(h.n) noise_mean = get_noise_mean(h.mags, 0.9) #noise_top = get_noise_top(h.mags, 0.9) #frames_above = num_above_noise(h.mags, noise_top) frames_above = num_above_noise(h.mags, noise_mean) #print h.n, "above:", frames_above noise_top_extra_min = stft.db2amplitude( stft.amplitude2db(noise_mean) + defs.HARMONIC_MAX_DB_ABOVE_NOISE_TOP) if max(h.mags) < noise_top_extra_min: # print "bail noise_top_extra" # # FIXME: special stats.num_harms_max_no_above_noise += 1 continue #print h.n, frames_above if frames_above < defs.HARMONIC_MIN_HOPS_ABOVE_NOISE: stats.num_harms_num_no_above_noise += 1 #print "not enough above noise top", frames_above continue ### experiment: only take beginning #h.frame_numbers = h.frame_numbers[:frames_above] #h.mags = h.mags[:frames_above] ### experiment: test the derivative #dh_mags = numpy.zeros(len(h.mags)-1) #for i in range(0, len(h.mags)-1): # subtraction on log scale #dh_mags[i] = (h.mags[i+1] / h.mags[i]) * ( # 1.0 + h.mags[i]) # dh_mags[i] = (h.mags[i+1] - h.mags[i]) #ddh_mags = numpy.zeros(len(dh_mags)) #for i in range(0, len(dh_mags)-1): # ddh_mags[i] = dh_mags[i+1] - dh_mags[i] #dh_mags = (h.mags[1:] - h.mags[:-1]) #sub = (dh_mags > 0) ##print dh_mags * sub #num_below_zero = (dh_mags * sub).sum() #print "bad: %.3g" % (float(num_below_zero) / len(dh_mags) ) ##pylab.plot(dh_mags) ##pylab.show() #print "%.3g\t%.3g\t%.3g\t%.3g" % ( # scipy.std(dh_mags), scipy.median(dh_mags), # scipy.std(ddh_mags), scipy.median(ddh_mags)) #if h.n in defs.HARMONICS_FIT_PLOT_N: #if False: # #pylab.plot(h.mags, '-o') # pylab.plot(dh_mags, '-') # pylab.plot(ddh_mags, '-*') # #pylab.xlim([0, 30]) # # pylab.show() #exit(1) #num_harms_above_noise += 1 ts = hop_rate * h.frame_numbers if h.n == defs.HARMONICS_FIT_PLOT_N: show=True plot=False plot_last=True else: show=False plot=False plot_last=defs.HARMONICS_FIT_PLOT fit, rsquared, variance = decay_exponential.fit_best_exponential( ts, h.mags, noise_mean=noise_mean, show=show, plot=plot, plot_last=plot_last) if fit is None: stats.num_harms_no_fit += 1 print "bail from no fit" continue #alpha = fit[2] alpha = fit[1] #drop_amplitude = fit[0] / noise_mean drop_amplitude = max(h.mags) / noise_mean drop_db = stft.amplitude2db(drop_amplitude) #print drop_db #if drop_db < defs.HARMONIC_MIN_DROP_DB: # stats.num_harms_no_drop += 1 # continue if rsquared < defs.HARMONIC_FIT_MIN_RSQUARED: stats.num_harms_no_rsquared += 1 continue #if variance > defs.HARMONIC_FIT_MAX_VARIANCE: # stats.num_harms_no_variance += 1 # continue #if variance > 1.0: # continue freq = partials.mode_B2freq(f0, h.n, B) w = 2*numpy.pi*freq Q = w / (2*alpha) decay = classes.Decay(freq, w, h.n, alpha, Q, rsquared, variance, drop_db) decays.append(decay) stats.num_harms_end += 1 if defs.HARMONICS_PRINT_SUMMARY: print "n: %i\t%.1f\tdecay: %.2f\tr-squared: %.2f\tvariance: %.2f\tdrop: %.2f db" % ( h.n, freq, alpha, rsquared, variance, drop_db) #print "%s\t%i\t%i\t%i\t%i\t%i" % ( #print "%s\t%i | \t%i\t%i\t%i\t| %i" % ( # basename, # num_harms_original, # num_harms_no_above_noise, # num_harms_no_fit, # #num_harms_no_rsquared, # num_harms_no_drop, # num_harms_end, # ) print "dropped:", stats.num_harms_max_no_above_noise, stats.num_harms_num_no_above_noise, def dot_color(d): #rs = 1.0/d.variance #if rs > 10.: # rs = 10. #rs = 10*d.rsquared rs = d.drop/10.0 rss = rs/10.0 dot = '.' markersize = 5 + 5.0*(rss) color = matplotlib.cm.winter(1.-rss) return dot, color, markersize if defs.HARMONICS_PLOT_DECAYS or plot_harms: pylab.figure() for d in decays: #dot, color, markersize = dot_color(d.rsquared) dot, color, markersize = dot_color(d) pylab.plot(d.n, d.alpha, dot, color=color, markersize=markersize, linewidth=0, ) pylab.xlabel("mode") pylab.ylabel("decay rates") pylab.xlim([0, max([d.n for d in decays])+1]) #pylab.legend() if defs.HARMONICS_PLOT_Q: pylab.figure() #print "# n, loss factor, weight" for d in decays: #print "%i, %.2e, %.2e" %(d.n, 1./d.Q, d.rsquared) #dot, color, markersize = dot_color(1.0/d.variance) dot, color, markersize = dot_color(d) #if d.variance > 10 or d.rsquared < 0.25: #if d.rsquared < 0.3: # dot = 'x' # color = 'red' #else: # print d.variance pylab.plot(d.n, d.Q, dot, color=color, markersize=markersize, linewidth=0, ) pylab.xlabel("mode") pylab.ylabel("Q") pylab.xlim([0, max([d.n for d in decays])+1]) #pylab.legend() if defs.HARMONICS_PLOT_LOSS: pylab.figure() #print "# n, loss factor, weight" for d in decays: #print "%i, %.2e, %.2e" %(d.n, 1./d.Q, d.rsquared) #dot, color, markersize = dot_color(1.0/d.variance) dot, color, markersize = dot_color(d) #if d.variance > 10 or d.rsquared < 0.25: #if d.rsquared < 0.3: # dot = 'x' # color = 'red' #else: # print d.variance pylab.plot(d.n, 1.0/d.Q, dot, color=color, markersize=markersize, linewidth=0, ) pylab.xlabel("mode") pylab.ylabel("loss") pylab.xlim([0, max([d.n for d in decays])+1]) #pylab.legend() ns = [ h.n for h in decays ] stats.highest_harm = max(ns) if (defs.HARMONICS_PLOT_DECAYS or defs.HARMONICS_PLOT_Q or defs.HARMONICS_PLOT_LOSS or plot_harms): pylab.show() return decays, f0, B, stats
def calc_harmonics(wav_filename, f0=None, B=None, limit=defs.TOTAL_HARMONICS): if f0 is None: raise Exception("need f0 and B; run another program") # eliminate $HOME ~ and symlinks wav_filename = os.path.realpath(os.path.expanduser(wav_filename)) basename = os.path.splitext(os.path.basename(wav_filename))[0] shared_dirname = os.path.abspath( os.path.join(os.path.dirname(wav_filename), '..')) dest_dir = os.path.join(shared_dirname, "spectrum", basename) if not os.path.exists(dest_dir): os.makedirs(dest_dir) window_buffers, sample_rate = stft.get_buffers_from_file(wav_filename) freqs = [ stft.bin2hertz(i, sample_rate) for i in range(stft.WINDOWSIZE/2+1) ] ### get noise for tuning off low harmonics initial_noise_floor, initial_noise_freqs, _, _, _ = calc_noise.get_noise(wav_filename) noise_cutoff = stft.db2amplitude( stft.amplitude2db(initial_noise_floor) +defs.STFT_MIN_DB_ABOVE_NOISE) bin_f0 = stft.hertz2bin(f0, sample_rate) # radius of search area for peaks bin_spread_below = int( stft.hertz2bin(defs.STFT_PEAK_SPREAD_BELOW_HERTZ*f0, sample_rate)) bin_spread_above = int( stft.hertz2bin(defs.STFT_PEAK_SPREAD_ABOVE_HERTZ*f0, sample_rate)) bin_spread_below = 3 bin_spread_above = 3 if defs.STFT_DUMP_TEXT: write_data.write_Bs(dest_dir, sample_rate, f0, B, limit, bin_spread_below, bin_spread_above) write_data.write_ideals(dest_dir, f0, limit) # store the peaks harmonics = [None]*limit spectrums = [] table_info = tables.save_fft(basename) if table_info: harms_freqs = [] harms_mags = [] if defs.ONLY_N_WINDOWS: window_buffers = window_buffers[:defs.ONLY_N_WINDOWS] for window_number, window_buffer in enumerate(window_buffers): #print '-------- window --- %i' % window_number #fft_amplitude = stft.stft(window_buffer) fft_amplitude = stft.stft_amplitude(window_buffer) if window_number == 0: write_data.write_spectrum(dest_dir, window_number, freqs, stft.amplitude2db(fft_amplitude)) if defs.STFT_DUMP_TEXT: write_data.write_spectrum(dest_dir, window_number, freqs, stft.amplitude2db(fft_amplitude)) spectrums.append(fft_amplitude) # get harmonic peaks, and disable harmonics if can't do harms, _ = partials.get_freqs_mags( limit, bin_f0, B, fft_amplitude, bin_spread_below, bin_spread_above, only_peaks=False) if defs.STFT_PLOT_PARTIALS: plots.plot_partials(fft_amplitude, sample_rate, harms, bin_f0, B, bin_spread_below, bin_spread_above ) if defs.STFT_DUMP_TEXT: dump_freqs = numpy.zeros(limit) dump_mags = numpy.zeros(limit) if table_info: harm_freqs = [] harm_mags = [] for h in harms: i = h.n-1 if harmonics[i] is None: #print stft.bin2hertz(h.fft_bin, sample_rate), h.mag, noise_cutoff[h.fft_bin] harmonics[i] = classes.HarmonicSignal(h.n) #if use_harmonic(h, noise_cutoff, fft_amplitude, # bin_f0, B, # bin_spread_below, bin_spread_above, # ): # harmonics[i] = classes.HarmonicSignal(h.n) #else: # #print "disable harmonic ", n # harmonics[i] = False if harmonics[i] is not False: if h.mag == 0: continue if defs.STFT_DUMP_TEXT: dump_freqs[i] = stft.bin2hertz(h.fft_bin, sample_rate) dump_mags[i] = h.mag if table_info: harm_freqs.append(stft.bin2hertz(h.fft_bin, sample_rate)) harm_mags.append(h.mag) harmonics[i].mags.append(h.mag) harmonics[i].frame_numbers.append(window_number) #print harmonics[i] if table_info: harms_freqs.append(harm_freqs) harms_mags.append(harm_mags) if defs.STFT_DUMP_TEXT: #print dump_mags write_data.write_harms(dest_dir, window_number, dump_freqs, dump_mags, harmonics) if (defs.STFT_PLOT_FIRST_N > 0) and (window_number < defs.STFT_PLOT_FIRST_N): plots.plot_stft_first_n(window_number, defs.STFT_PLOT_FIRST_N, fft_amplitude, sample_rate, harms, wav_filename, bin_f0, B, bin_spread_below, bin_spread_above ) if window_number >= defs.STFT_PLOT_FIRST_N - 1: pylab.show() dh = float(defs.HOPSIZE) / sample_rate if defs.STFT_DUMP_ALL: write_data.write_stft_all(dest_dir, spectrums, freqs, dh) table_info = tables.save_fft(basename) if table_info: for ti in table_info: write_data.write_stft_3d(basename, spectrums, freqs, dh, ti, harms_freqs, harms_mags, sample_rate) # clean up harmonics harmonics = filter(lambda x: x is not False, harmonics) for h in harmonics: h.mags = numpy.array(h.mags) h.frame_numbers = numpy.array(h.frame_numbers) #pylab.plot(stft.amplitude2db(h.mags)) #pylab.show() return harmonics, dh
def estimate_f0_B(filenames): ### ASSUME: all filenames are of the same instrument-string wav_filename = filenames[0] basename = '-'.join(os.path.basename(wav_filename).split('-')[0:3]) if basename.startswith("test-440f"): return 440.0, 0, 1, 1, 1, 1 ### get initial f0 estimate base_frequency_estimate = expected_frequencies.get_freq_from_filename( wav_filename) ### get noise initial_noise_floor, initial_noise_freqs, _, _, _ = calc_noise.get_noise( wav_filename) noise_cutoff = stft.db2amplitude( stft.amplitude2db(initial_noise_floor) + defs.B_MINIMUM_HARMONIC_SNR) #### get FFT frames from audio files sample_rate = None freqs = None estimate_B_buffers_list = [] for wav_i, wav_filename in enumerate(filenames): #print wav_filename #window_buffer, sample_rate = stft.get_long_buffer_from_file(wav_filename, window_buffers, sample_rate = stft.get_buffers_from_file( wav_filename, (defs.B_NUM_BUFFERS_ESTIMATE)) if freqs is None: freqs = [ stft.bin2hertz(i, sample_rate) for i in range(stft.WINDOWSIZE / 2 + 1) ] estimate_B_buffers_this_list = [] #fft_amplitude = stft.fft_amplitude(window_buffer, sample_rate) #estimate_B_buffers_this_list.append(fft_amplitude) for window_number in range(defs.B_NUM_BUFFERS_ESTIMATE): window_buffer = window_buffers[window_number] fft_amplitude = stft.stft_amplitude(window_buffer) estimate_B_buffers_this_list.append(fft_amplitude) estimate_B_buffers_list.extend(estimate_B_buffers_this_list) estimate_B_buffers = numpy.array(estimate_B_buffers_list) ### radius of search area for peaks # used with STFT only bin_initial_estimate = stft.hertz2bin(base_frequency_estimate, sample_rate) #bin_initial_estimate = (base_frequency_estimate # * fft_amplitude.shape[0] / (sample_rate/2) # ) #print bin_initial_estimate bin_spread_below = int( math.ceil( abs( stft.hertz2bin((1.0 - defs.B_PEAK_SPREAD_BELOW_HERTZ) * base_frequency_estimate, sample_rate) - bin_initial_estimate))) bin_spread_above = int( math.ceil( stft.hertz2bin((1.0 + defs.B_PEAK_SPREAD_ABOVE_HERTZ) * base_frequency_estimate, sample_rate) - bin_initial_estimate)) #bin_spread_below = int(round(bin_initial_estimate * # defs.B_PEAK_SPREAD_BELOW_HERTZ)) #bin_spread_above = int(round(bin_initial_estimate * # defs.B_PEAK_SPREAD_BELOW_HERTZ)) #bin_spread_below_main = int( # stft.hertz2bin(defs.STFT_PEAK_SPREAD_BELOW_HERTZ*base_frequency_estimate, # sample_rate)) #bin_spread_above_main = int( # stft.hertz2bin(defs.STFT_PEAK_SPREAD_ABOVE_HERTZ*base_frequency_estimate, # sample_rate)) ### actual estimate bin_f0, B, rsquared, harmonics, limit = get_bin_f0_B( bin_initial_estimate, estimate_B_buffers, noise_cutoff, #estimate_B_buffers, numpy.zeros(defs.LONG_WINDOWSIZE+1), bin_spread_below, bin_spread_above, sample_rate) highest_harmonic = 0 for h in harmonics: if highest_harmonic < h.n: highest_harmonic = h.n limit = min(limit, highest_harmonic) # HACK: remove limit #limit = defs.TOTAL_HARMONICS #print "limit to:", limit #harmonics_enable = [True]*defs.TOTAL_HARMONICS harmonics_enable = [True] * limit bins_estimate = [ partials.mode_B2freq(bin_f0, i, B) for i in range(1, len(harmonics_enable) + 1) ] bins_naive = [i * bin_f0 for i in range(1, len(harmonics_enable) + 1)] if defs.B_PLOT: pylab.figure() pylab.plot(initial_noise_freqs, stft.amplitude2db(initial_noise_floor), color='black') #pylab.plot(initial_noise_freqs, # stft.amplitude2db(initial_noise_floor)+defs.B_MINIMUM_HARMONIC_SNR, # color='black') pylab.xlabel("Frequency (seconds)") pylab.ylabel("Power (/ dB)") for i in range(estimate_B_buffers.shape[0]): #color = matplotlib.cm.spring(float(wav_i)/len(filenames)) #color = matplotlib.cm.RdYlGn( #color = matplotlib.cm.spring( # float(i)/len(estimate_B_buffers_this_list)) pylab.plot( freqs, stft.amplitude2db(estimate_B_buffers[i, :]), #color=color, color="orange", alpha=0.5, label=basename, ) for est in bins_estimate: low = stft.bin2hertz(est - bin_spread_below, sample_rate) high = stft.bin2hertz(est + bin_spread_above, sample_rate) if True: pylab.axvspan(low, high, color='c', alpha=0.3) else: pylab.axvline( stft.bin2hertz(est, sample_rate), color='cyan', alpha=0.3, #linewidth=2.0 ) for naive in bins_naive: freq = stft.bin2hertz(naive, sample_rate) pylab.axvline( freq, color='grey', alpha=0.2, #linewidth=2.0 ) for j, harm in enumerate(harmonics): if harm.mag == 0: continue fn = stft.bin2hertz(harm.fft_bin, sample_rate) mag = stft.amplitude2db(harm.mag) #pylab.plot(fn, mag, 'o', # color='green' # ) pylab.xlabel("Frequency") pylab.ylabel("Decibels") if defs.B_DUMP_HARMS: t_fns = [] t_mags = [] for j, harm in enumerate(harmonics): if harm.mag == 0: continue fn = stft.bin2hertz(harm.fft_bin, sample_rate) mag = stft.amplitude2db(harm.mag) t_fns.append(fn) t_mags.append(mag) data = numpy.vstack((t_fns, t_mags)).transpose() numpy.savetxt("B-harms.txt", data) if defs.B_PLOT: pylab.show() f0 = stft.bin2hertz(bin_f0, sample_rate) stiff_ideal_limit = stiff_ideal_conflict.find_limit( bin_f0, B, bin_spread_below, bin_spread_above) lim = min(stiff_ideal_limit, limit) detected_freqs = StringFreqsB(f0, B, lim) stats = StringFreqsB_stats() stats.num_files = len(filenames) stats.rsquared = rsquared stats.highest_mode_detected = limit stats.highest_mode_stiff_ideal = stiff_ideal_limit stats.basename = basename adjusted_B, delta_fn = adjust_B.adjust(basename, limit, f0, B) if adjusted_B is not None: stiff_ideal_lim_adjusted = stiff_ideal_conflict.find_limit( bin_f0, adjusted_B, bin_spread_below, bin_spread_above) lim = min(stiff_ideal_lim_adjusted, limit) adjusted_freqs = StringFreqsB(f0, adjusted_B, lim) adjusted_freqs.delta_fn = delta_fn stats.highest_mode_stiff_ideal_adjusted = stiff_ideal_lim_adjusted stats.delta_fn = delta_fn final = StringFreqsB( f0, adjusted_B, min(stats.highest_mode_detected, stats.highest_mode_stiff_ideal, stiff_ideal_lim_adjusted)) else: adjusted_freqs = None final = StringFreqsB( f0, B, min(stats.highest_mode_detected, stats.highest_mode_stiff_ideal)) return detected_freqs, adjusted_freqs, stats, final