def plot_stft_first_n(window_number, n_windows, fft_amplitude, sample_rate, harms, wav_filename, bin_f0, B, bin_spread_below, bin_spread_above): #if window_number % 2 == 0: count = window_number + 1 color = matplotlib.cm.gnuplot(float(count) / (n_windows + 1)) #label = "%.3f seconds" % (float(count) * stft.HOPSIZE/sample_rate) bin_freqs = [ stft.bin2hertz(bin_num, sample_rate) for bin_num, amplitude in enumerate(fft_amplitude) ] harms_freqs = [ stft.bin2hertz(h.fft_bin, sample_rate) for h in harms if h.mag > 0 ] harms_mags = [stft.amplitude2db(h.mag) for h in harms if h.mag > 0] plot_ideal_lines(bin_f0, len(harms), sample_rate) plot_stiff_area(bin_f0, B, bin_spread_below, bin_spread_above, harms, sample_rate) pylab.plot( bin_freqs, stft.amplitude2db(fft_amplitude), '-', color=color, label="fft", ) pylab.plot( harms_freqs, harms_mags, 'o', color=color, label="harmonics", ) #for est in bins_estimate: # low = stft.bin2hertz(est - bin_spread, sample_rate) # high = stft.bin2hertz(est + bin_spread, sample_rate) # pylab.axvspan(low, high, color='c', alpha=0.3) if window_number >= n_windows - 1: import calc_noise initial_noise_floor, initial_noise_freqs, _, _, _ = calc_noise.get_noise( wav_filename) pylab.plot( initial_noise_freqs, stft.amplitude2db(initial_noise_floor), label="initial noise floor", color="black", )
def plot_stft_first_n(window_number, n_windows, fft_amplitude, sample_rate, harms, wav_filename, bin_f0, B, bin_spread_below, bin_spread_above ): #if window_number % 2 == 0: count = window_number+1 color = matplotlib.cm.gnuplot(float(count)/(n_windows+1)) #label = "%.3f seconds" % (float(count) * stft.HOPSIZE/sample_rate) bin_freqs = [ stft.bin2hertz(bin_num, sample_rate) for bin_num, amplitude in enumerate(fft_amplitude) ] harms_freqs = [ stft.bin2hertz(h.fft_bin, sample_rate) for h in harms if h.mag > 0] harms_mags = [ stft.amplitude2db(h.mag) for h in harms if h.mag > 0] plot_ideal_lines(bin_f0, len(harms), sample_rate) plot_stiff_area(bin_f0, B, bin_spread_below, bin_spread_above, harms, sample_rate) pylab.plot(bin_freqs, stft.amplitude2db(fft_amplitude), '-', color=color, label="fft", ) pylab.plot(harms_freqs, harms_mags, 'o', color=color, label="harmonics", ) #for est in bins_estimate: # low = stft.bin2hertz(est - bin_spread, sample_rate) # high = stft.bin2hertz(est + bin_spread, sample_rate) # pylab.axvspan(low, high, color='c', alpha=0.3) if window_number >= n_windows - 1: import calc_noise initial_noise_floor, initial_noise_freqs, _, _, _ = calc_noise.get_noise( wav_filename) pylab.plot(initial_noise_freqs, stft.amplitude2db(initial_noise_floor), label="initial noise floor", color="black", )
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
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