I have 276 audio file (.wav). I want to plot signal and spectrogram together like following code:
import matplotlib.pyplot as plot
from scipy.io import wavfile
samplingfrequency, signaldata = wavfile.read('/home/narges/dataset/seri1.16khz.128kbps/Voice Recorder/N00xxxx/N000200.wav')
plot.subplot(211)
plot.title('spec of vowel')
plot.plot(signaldata)
plot.xlabel('sample')
plot.ylabel('amp')
plot.subplot(212)
plot.specgram(signaldata,Fs=samplingfrequency)
plot.xlabel('time')
plot.ylabel('freq')
plot.show()
But i want to read all folders and sub-folders. I use this code for reading all folders and sub-folders:
path = Path('/home/narges/dataset/dataset-CV-16kHz-128kbps/train/').glob('**/*.wav')
wavs = [str(wavf) for wavf in path if wavf.is_file()]
wavs.sort()
And i'm using this code for saving Speaker ID:
number_of_files=len(wavs) spk_ID = [wavs[i].split('/')[-1].lower() for i in range(number_of_files)]
Now, how can i change following code for read all of my .wav file in directory (path) and plot signal and spectrogram (like first code), and save it with the name of spk_ID?
def graph_spectrogram(wav_file):
wavs = rate, data = wavfile.read('/home/narges/dataset/dataset-CV-16kHz-128kbps/train/speaker_00/s_00_0_00.wav')
pxx, freq, bins, im = plt.specgram(x=data, Fs=rate, noverlap=384, NFFT=512)
spk_ID = [wavs.split('/')[-1].lower()]
plt.xlabel('time')
plt.ylabel('freq')
plt.savefig('xyz.png',bbox_inches='tight', dpi=300, frameon='false')
if __name__=='__main__':
graph_spectrogram('...')
I'm using this code and it's correct, if any one want to use:
from pathlib import Path
import matplotlib.pyplot as plot
from scipy.io import wavfile
path = Path('/home/narges/dataset/dataset-CV-16kHz-128kbps/train/').glob('**/*.wav')
wavs = [str(wavf) for wavf in path if wavf.is_file()]
wavs.sort()
number_of_files=len(wavs)
spk_ID = [wavs[i].split('/')[-1].lower() for i in range(number_of_files)]
for i in range(number_of_files):
samplingfrequency, signaldata = wavfile.read(wavs[i])
pxx, freq, bins, im = plot.specgram(x=signaldata, Fs=samplingfrequency, noverlap=384, NFFT=512)
plot.title('spec of vowel')
plot.xlabel('time')
plot.ylabel('freq')
plot.savefig("spk_ID:{}.png".format(spk_ID[i]), bbox_inches='tight', dpi=300, frameon='false')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.