Пример #1
0
def plot_resynth(harmonics, fft, sample_rate):
    freqs = [ stft.bin2hertz(i, sample_rate) for i in xrange(len(fft)) ]
    synth_min_cutoff = 0.5 * min(numpy.where(fft>0, fft, 1))
    length = len(fft)
    for i, sp in enumerate(harmonics):
        synthesized_peak = partials.qifft_synthesize(sp.fft_bin,
            sp.mag, sp.curvature_a, length)
        synthesized_peak = numpy.where(
            synthesized_peak > synth_min_cutoff,
            synthesized_peak, 0)
        if i == 0:
            pylab.plot(
                [ f for f, m in zip(freqs, synthesized_peak) if m > 0.0 ],
                stft.amplitude2db(
                    numpy.array(
                    [ m for f, m in zip(freqs, synthesized_peak) if m > 0.0 ],
                    )),
                '.-', color="yellow", alpha=0.8,
                label="resynthesized peak")
        else:
            pylab.plot(
                [ f for f, m in zip(freqs, synthesized_peak) if m > 0.0 ],
                stft.amplitude2db(
                    numpy.array(
                    [ m for f, m in zip(freqs, synthesized_peak) if m > 0.0 ],
                    )),
                '.-', color="yellow", alpha=0.8)
Пример #2
0
def plot_resynth(harmonics, fft, sample_rate):
    freqs = [stft.bin2hertz(i, sample_rate) for i in xrange(len(fft))]
    synth_min_cutoff = 0.5 * min(numpy.where(fft > 0, fft, 1))
    length = len(fft)
    for i, sp in enumerate(harmonics):
        synthesized_peak = partials.qifft_synthesize(sp.fft_bin, sp.mag,
                                                     sp.curvature_a, length)
        synthesized_peak = numpy.where(synthesized_peak > synth_min_cutoff,
                                       synthesized_peak, 0)
        if i == 0:
            pylab.plot(
                [f for f, m in zip(freqs, synthesized_peak) if m > 0.0],
                stft.amplitude2db(
                    numpy.array([
                        m for f, m in zip(freqs, synthesized_peak) if m > 0.0
                    ], )),
                '.-',
                color="yellow",
                alpha=0.8,
                label="resynthesized peak")
        else:
            pylab.plot(
                [f for f, m in zip(freqs, synthesized_peak) if m > 0.0],
                stft.amplitude2db(
                    numpy.array([
                        m for f, m in zip(freqs, synthesized_peak) if m > 0.0
                    ], )),
                '.-',
                color="yellow",
                alpha=0.8)
Пример #3
0
def f0_snr(buf, f0, fs):
    window = scipy.signal.gaussian(len(buf), len(buf)/8)
    fft = scipy.fftpack.fft(buf*window)
    fft_abs = abs(fft[:len(fft)/2+1])
    cutoff_low_bin = float(20.0) / (fs/2) * (len(fft_abs)-1)
    cutoff_bin = 1.95*float(f0) / (fs/2) * (len(fft_abs)-1)
    low_bin = float(f0*0.95) / (fs/2) * (len(fft_abs)-1)
    high_bin = float(f0*1.05) / (fs/2) * (len(fft_abs)-1)

    freqs = numpy.array([ float(i) / (fs/2) * (len(fft_abs)-1) for
        i in range(len(fft_abs)) ])
    cutoff_bin = int(cutoff_bin)
    fft_abs = fft_abs[:cutoff_bin]
    freqs = freqs[:cutoff_bin]

    fft_power = fft_abs**2
    examine = fft_abs[low_bin:high_bin]**2

    # multiply the FREQUENCY domain with a window to ensure
    # that the maximum will be close to 660.
    examine *= tukeywin(len(examine))

    peak_index_within = numpy.argmax(examine)
    peak_index = low_bin + peak_index_within

    while not (fft_power[peak_index-1] < fft_power[peak_index] > fft_power[peak_index+1]):
        # find the next-highest peak, whatever it is
        peak_index += 1


    if False:
        pylab.plot(examine)
        pylab.semilogy(peak_index_within,
            examine[peak_index_within], 'o')
        pylab.figure()
        pylab.plot(fft_power)
        pylab.semilogy(peak_index,
            fft_power[peak_index], 'o')
        pylab.show()


    try:
        peak_bin, peak_mag, curvature_a = partials.quadratic_interpolation(
            peak_index, fft_abs)
    except:
        print "PROBLEM"
        pylab.plot(examine)
        pylab.semilogy(peak_index_within,
            examine[peak_index_within], 'o')
        pylab.figure()
        pylab.semilogy(fft_power)
        pylab.semilogy(peak_index,
            fft_power[peak_index], 'o')
        pylab.show()
        exit(1)


    #print peak_bin, peak_mag, curvature_a
    length = len(fft_power)
    synth = partials.qifft_synthesize(peak_bin, peak_mag, curvature_a, length)
    residual = fft_abs - synth

    synth_min = min(fft_abs)
    residual.clip(synth_min)

    consider_orig = fft_abs[cutoff_low_bin:]
    freqs_orig = freqs[cutoff_low_bin:]

    #consider_residual = fft_abs[cutoff_low_bin:low_bin]
    #freqs_residual = freqs[cutoff_low_bin:low_bin]
    consider_residual = numpy.append(
    #    #residual[cutoff_low_bin:low_bin],
    #    #residual[high_bin:])
        fft_abs[cutoff_low_bin:low_bin],
        fft_abs[high_bin:])
    freqs_residual = numpy.append(
        freqs[cutoff_low_bin:low_bin],
        freqs[high_bin:])
    #consider_residual = residual[cutoff_low_bin:low_bin]
    flatness_orig = (scipy.stats.gmean(consider_orig)
        / consider_orig.mean() )
    flatness_residual= (scipy.stats.gmean(consider_residual)
        / consider_residual.mean() )

    #mean = scipy.stats.gmean(consider)
    #print mean

    value = flatness_orig / flatness_residual
    #print
    #print flatness_orig, flatness_residual, peak_mag
    def rms(sig):
        return numpy.sqrt( numpy.mean( sig**2 ) )
    energy_orig = rms( consider_orig )
    energy_sig = rms( fft_abs[low_bin:high_bin] )
    energy_residual = rms( consider_residual )
    #print energy_sig, energy_orig, energy_residual
    value = energy_residual / energy_sig
    #print value

    if SHOW_F0_SNR:
        pylab.semilogy(freqs, fft_abs, '-')
        pylab.semilogy(peak_bin, peak_mag, 'o')
        #pylab.semilogy(synth)
        pylab.semilogy(freqs_orig, consider_orig)
        pylab.semilogy(freqs_residual, consider_residual)
        #pylab.axhline(mean)
        pylab.show()

    return value