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
| import matplotlib.pyplot as plt
import numpy as np
from scipy.io import wavfile as wf
from scipy import fft
sons =["yeah.wav","beep_02.wav", "beep_03.wav","a6.wav"]
def traitement(son):
sampFreq, snd = wf.read(son)
snd = snd / (2.**15)
shap = snd.shape[0]
# print(snd)
try:
s1 = snd[:,0]
except:
s1 = snd
timeArray = np.arange(0, shap, 1)
timeArray = timeArray / sampFreq
timeArray = timeArray * 1000 #scale to milliseconds
plt.plot(timeArray, s1, color='b')
plt.ylabel('Amplitude')
plt.xlabel('Time (ms)')
plt.show()
plt.savefig("im"+str(son.split(".")[0])+".png")
n = len(s1)
p = fft(s1)
nUniquePts = int(np.ceil((n+1)/2.0))
p = p[0:nUniquePts]
p = abs(p)
p = p / float(n)
p = p**2
if n % 2 > 0:
p[1:len(p)] = p[1:len(p)] * 2
else:
p[1:len(p) -1] = p[1:len(p) - 1] * 2
freqArray = np.arange(0, nUniquePts, 1.0) * (sampFreq / n);
plt.plot(freqArray/50, 10*np.log10(p), color='r')
plt.xlabel('Frequency (kHz)')
plt.ylabel('Power (dB)')
plt.show()
rms_val = np.sqrt(np.mean(s1**2))
print("rms : ",rms_val)
for i in range(len(sons)):
print(sons[i])
traitement(sons[i]) |
Partager