Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

spectogramer.py 2.4 KB

You have to be logged in to leave a comment. Sign In
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
  1. from __future__ import division, print_function
  2. from os import listdir
  3. from os.path import isfile, join
  4. from matplotlib import pyplot as plt
  5. import scipy.io.wavfile as wav
  6. def wav_to_spectrogram(audio_path, save_path, spectrogram_dimensions=(64, 64), noverlap=16, cmap='gray_r'):
  7. """ Creates a spectrogram of a wav file.
  8. :param audio_path: path of wav file
  9. :param save_path: path of spectrogram to save
  10. :param spectrogram_dimensions: number of pixels the spectrogram should be. Defaults (64,64)
  11. :param noverlap: See http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.spectrogram.html
  12. :param cmap: the color scheme to use for the spectrogram. Defaults to 'gray_r'
  13. :return:
  14. """
  15. sample_rate, samples = wav.read(audio_path)
  16. fig = plt.figure()
  17. fig.set_size_inches((spectrogram_dimensions[0]/fig.get_dpi(), spectrogram_dimensions[1]/fig.get_dpi()))
  18. ax = plt.Axes(fig, [0., 0., 1., 1.])
  19. ax.set_axis_off()
  20. fig.add_axes(ax)
  21. ax.specgram(samples, cmap=cmap, Fs=2, noverlap=noverlap)
  22. ax.xaxis.set_major_locator(plt.NullLocator())
  23. ax.yaxis.set_major_locator(plt.NullLocator())
  24. fig.savefig(save_path, bbox_inches="tight", pad_inches=0)
  25. def dir_to_spectrogram(audio_dir, spectrogram_dir, spectrogram_dimensions=(64, 64), noverlap=16, cmap='gray_r'):
  26. """ Creates spectrograms of all the audio files in a dir
  27. :param audio_dir: path of directory with audio files
  28. :param spectrogram_dir: path to save spectrograms
  29. :param spectrogram_dimensions: tuple specifying the dimensions in pixes of the created spectrogram. default:(64,64)
  30. :param noverlap: See http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.spectrogram.html
  31. :param cmap: the color scheme to use for the spectrogram. Defaults to 'gray_r'
  32. :return:
  33. """
  34. file_names = [f for f in listdir(audio_dir) if isfile(join(audio_dir, f)) and '.wav' in f]
  35. for file_name in file_names:
  36. print(file_name)
  37. audio_path = audio_dir + file_name
  38. spectogram_path = spectrogram_dir + file_name.replace('.wav', '.png')
  39. wav_to_spectrogram(audio_path, spectogram_path, spectrogram_dimensions=spectrogram_dimensions, noverlap=noverlap, cmap=cmap)
  40. if __name__ == '__main__':
  41. audio_dir = "/Users/Jackson/development/free-spoken-digit-dataset/recordings/"
  42. spectrogram_dir = "/Users/Jackson/development/free-spoken-digit-dataset/spectrograms/"
  43. dir_to_spectrogram(audio_dir, spectrogram_dir)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...