def process_spectrogram(filename, block_size, hop_size):
    x, times, fs = open_file(filename, block_size, hop_size)
    w = create_window(block_size)
    X, X_cross_time, X_cross_freq, X_inst_freqs, X_group_delays = compute_spectra(x, w)

    X_reassigned_f = requantize_f_spectrogram(X_cross_time, X_inst_freqs)
    # N = X_cross.shape[1]
    # magnitude_spectrum = abs(X_cross_time) / N
    # weights = db_scale(magnitude_spectrum)
    X_magnitudes = abs(X_cross_time) / X.shape[1]
    weights = X_magnitudes
    X_reassigned_tf = requantize_tf_spectrogram(X_group_delays, X_inst_freqs, times, block_size, fs, weights)[0]
    X_reassigned_tf = db_scale(X_reassigned_tf ** 2)
    image_filename = os.path.basename(filename).replace('.wav', '.png')
    scipy.misc.imsave('reassigned_f_' + image_filename, real_half(X_reassigned_f).T[::-1])
    scipy.misc.imsave('reassigned_tf_' + image_filename, real_half(X_reassigned_tf).T[::-1])
    scipy.misc.imsave('normal_' + image_filename, real_half(X_magnitudes).T[::-1])
def chromagram(x, w, fs, bin_range=(-48, 67), bin_division=1, to_log=True):
    "complete reassigned spectrogram with requantization to pitch bins"
    # TODO: better give frequency range
    X, X_cross_time, X_cross_freq, X_inst_freqs, X_group_delays = compute_spectra(x, w)
    n_blocks, n_freqs = X_cross_time.shape
    X_mag = abs(X_cross_time) / n_freqs
    weights = real_half(X_mag).flatten()
    eps = np.finfo(np.float32).eps
    pitch_bins = quantize_freqs_to_pitch_bins(np.maximum(fs * real_half(X_inst_freqs), eps), bin_division=bin_division).flatten()
    nonzero_ix = abs(weights) > eps
    X_chromagram = np.histogram2d(
        np.repeat(np.arange(n_blocks), n_freqs / 2),
        pitch_bins,
        bins=(np.arange(n_blocks + 1),
              np.arange(bin_range[0], bin_range[1] + 1, 1 / bin_division)),
        weights=weights
    )[0]
    X_chromagram = X_chromagram ** 2
    if to_log:
        X_chromagram = db_scale(X_chromagram)
    return X_chromagram
def reassigned_spectrogram(x, w, to_log=True):
    X, X_cross_time, X_cross_freq, X_inst_freqs, X_group_delays = compute_spectra(x, w)
    X_reassigned_f = requantize_f_spectrogram(X_cross_time, X_inst_freqs, to_log)
    return real_half(X_reassigned_f)