Ejemplo n.º 1
0
def h_2(raw_sample):
    w = windowed(raw_sample)
    s = np.fft.fft(w.data, n=N)
    p = np.sqrt(1 + (KV / K_C)**2)
    pp = np.concatenate([p, np.ones(len(s) - 2 * K_MAX), p[::-1]])
    s = s * pp
    f = np.fft.fftfreq(len(w.data), T_E)[:K_MAX]
    n = len(s)
    c, ndelay = complex_cepstrum(s)
    rc = real_cepstrum(s)
    K_C_MIN_SEARCH = int(np.round(4 / (200 * T_E)))
    K_C_MAX_SEARCH = int(np.round(4 / (75 * T_E)))
    k_f_p = np.argmax(rc[K_C_MIN_SEARCH:K_C_MAX_SEARCH]) + K_C_MIN_SEARCH
    f_p = 4 / (k_f_p * T_E)
    print(f_p)
    k_f_p -= 200
    print(k_f_p)
    # plt.plot(rc)
    # plt.axvline(x=K_C_MIN_SEARCH, color="b")
    # plt.axvline(x=K_C_MAX_SEARCH, color="r")
    # plt.axvline(x=k_f_p + 200, color="g", alpha=0.2)
    # plt.axvline(x=k_f_p, color="y")
    # plt.show()
    c_altered = np.concatenate(
        [c[:k_f_p], np.zeros(len(c) - 2 * k_f_p), c[-k_f_p:]])
    # print(len(c))
    assert (len(c) == len(c_altered))
    new_s = inverse_complex_cepstrum(c_altered, ndelay)
    return normalize(
        Spectrum(data=np.concatenate(
            [np.zeros(K_MIN), np.abs(new_s[K_MIN:K_MAX])]),
                 freq=f,
                 file_name=raw_sample.file_name,
                 phoneme=raw_sample.phoneme))
Ejemplo n.º 2
0
def biased_exp(spectrum: Spectrum, beta: float) -> Spectrum:
    """TODO
    """
    data = np.exp(spectrum.data) - beta

    return Spectrum(data=data,
                    file_name=spectrum.file_name,
                    freq=spectrum.freq,
                    phoneme=spectrum.phoneme)
Ejemplo n.º 3
0
def enhance_high_freqs(spectrum: Spectrum) -> Spectrum:
    """TODO
    """
    p = np.sqrt(1 + (KV / K_C)**2)
    data = spectrum.data * p

    return Spectrum(data=data,
                    file_name=spectrum.file_name,
                    freq=spectrum.freq,
                    phoneme=spectrum.phoneme)
Ejemplo n.º 4
0
def zero_low_frequencies(spectrum: Spectrum) -> Spectrum:
    """TODO
    """

    data = np.concatenate([np.zeros(K_MIN), spectrum.data[K_MIN:]])

    return Spectrum(data=data,
                    file_name=spectrum.file_name,
                    freq=spectrum.freq,
                    phoneme=spectrum.phoneme)
Ejemplo n.º 5
0
def biased_log(spectrum: Spectrum) -> (Spectrum, float):
    """TODO
    """
    avg = np.average(spectrum.data)
    beta = RO * avg
    data = np.log(spectrum.data + beta)

    return Spectrum(data=data,
                    file_name=spectrum.file_name,
                    freq=spectrum.freq,
                    phoneme=spectrum.phoneme), beta
Ejemplo n.º 6
0
def normalize(spectrum: Spectrum) -> Spectrum:
    """TODO
    """

    data = np.copy(spectrum.data)

    norm = np.sqrt(np.sum(np.power(data, 2)))
    data /= norm

    return Spectrum(data=data,
                    file_name=spectrum.file_name,
                    freq=spectrum.freq,
                    phoneme=spectrum.phoneme)
Ejemplo n.º 7
0
def load_samples():
    data_bank = []
    for r, d, f in os.walk(NEW_SAMPLES_STORE):
        for filepath in f:
            filename = path.basename(filepath)
            if filename.startswith("."):
                continue
            phoneme = filename.split("_")[0]
            assert (phoneme in PHONEMES)
            data_bank.append(
                Spectrum(data=np.load(path.join(NEW_SAMPLES_STORE, filepath)),
                         freq=None,
                         file_name=filepath,
                         phoneme=Phoneme(phoneme)))
    return data_bank
Ejemplo n.º 8
0
def smooth(spectrum: Spectrum) -> Spectrum:
    """TODO
    """
    # normalize weight function
    weight_func = (1 / 2) * (1 + np.cos(2 * pi * (QSV - Q / 2) / Q))
    total = np.sum(weight_func)
    weight_func = weight_func / total

    # smooth the signal
    data = np.convolve(spectrum.data, weight_func, mode='same')

    return Spectrum(data=data,
                    file_name=spectrum.file_name,
                    freq=spectrum.freq,
                    phoneme=spectrum.phoneme)
Ejemplo n.º 9
0
def spectrum_of(sample: Sample) -> Spectrum:
    """Applies the discrete Fourier Transform on the current sample.
    Frequencies above K_MAX / T (about 3000 Hz) are cut.

    :remark: Spectrum.data only contains the modulus of the fft spectrum

    :param sample: audio sample to work on

    :returns: the generated Spectrum object
    """
    data = np.abs(np.fft.fft(sample.data, n=N))[:K_MAX]

    freq = np.fft.fftfreq(N, T_E)[:K_MAX]

    return Spectrum(data=data,
                    file_name=sample.file_name,
                    freq=freq,
                    phoneme=sample.phoneme)