1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| from __future__ import unicode_literals import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np from scipy.signal import lfilter from scipy.io import wavfile
fs, data = wavfile.read("./1_u_sox.wav") x = data.copy() x = x / 32767
u = lfilter ([1, -0.99], [1], x)
wlen2 = len(u)//2
fft_val = np.fft . fft (u)
abs_fft = np.abs(fft_val)
nyquist_fft = np.abs(fft_val)[: wlen2]
log_fft = np.log(np.abs(np.fft . fft (u) [: wlen2]))
Cepst = np. fft . ifft(log_fft)
cepst = np.zeros(wlen2, dtype=np.complex)
cepstL = 30
cepst [: cepstL] = Cepst[:cepstL] cepst[-cepstL + 1:] = Cepst[-cepstL + 1:]
spec = np.real(np. fft . fft (cepst) )
def local_maxium(x): d = np.diff(x) l_d = len(d) maxium = [] loc = [] for i in range(l_d - 1): if d[i] > 0 and d[i + 1] <= 0: maxium.append(x[i + 1]) loc.append(i + 1) return maxium, loc
val, loc = local_maxium(spec)
wlen = len(u) wlen2 = wlen // 2 freq = [i * fs / wlen for i in range(wlen2)]
color_spectrum = "#1f165b" color_envlope = "#141414" color_text_label = "#f82912"
plt.plot(freq, log_fft, 'k', color = color_spectrum)
plt.rcParams['text.usetex'] = True
plt.plot(freq, spec, 'k', color=color_envlope) plt.title('$Ceptrum_{Formants}$') plt.legend(("wavform", "envelope"), shadow=True, loc=(1.05, 0.38), handlelength=1.5, fontsize=16)
plt.xlabel("$Frequency$", color="C0", fontsize=20) plt.ylabel("$dB$", color="C0", fontsize=20)
formant_list = [] for i in range(4): plt.plot([freq[loc[i]], freq[loc[i]]], [np.min(spec), spec[loc[i]]], '-.k') plt.text(freq[loc[i]], spec[loc[i]], '$F_{}={}$'.format(i+1, int(freq[loc[i]])), color="green",horizontalalignment='center', verticalalignment='center', fontsize=12) plt.text(28000, i+1,'$F_{}={}$'.format(i+1, int(freq[loc[i]])), color=color_text_label,horizontalalignment='center', verticalalignment='center', fontsize=12) formant_list.append(freq[loc[i]])
VTL = ((1 * (35000/(4 * formant_list[0]))) + (3 * (35000/(4 * formant_list[1]))) + (5 * (35000/(4 * formant_list[2]))) + (7 * (35000/(4 * formant_list[3]))))/4
plt.text(28000, -4,'$VTL={}cm$'.format(round(VTL)), color=color_text_label,horizontalalignment='center', verticalalignment='center', fontsize=12) plt.savefig('Ceptrum Formants_demo.png', bbox_inches='tight', dpi = 600)
|