This repository was archived by the owner on Mar 9, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspectrogram.py
More file actions
Latest commit
36 lines (29 loc) · 1.04 KB
/
Copy pathspectrogram.py
File metadata and controls
36 lines (29 loc) · 1.04 KB
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
importnumpyasnp
importmatplotlib.pyplotasplt
fromscipy.ioimportwavfile
defspectrogram(audio_file):
rate, data=wavfile.read(audio_file)
iflen(data.shape) >1:
data=data.mean(axis=1)
window_size=1024
hop_size=512
n_samples=len(data)
n_overlap=window_size-hop_size
n_windows= (n_samples-n_overlap) //hop_size
spectrogram=np.zeros((n_windows, window_size//2+1))
foriinrange(n_windows):
start=i*hop_size
end=start+window_size
segment=data[start:end]
windowed_segment=segment*np.hamming(window_size)
spectrum=np.abs(np.fft.rfft(windowed_segment, window_size))
spectrogram[i] =spectrum
plt.figure(figsize=(7, 5))
plt.imshow(np.log1p(spectrogram.T), aspect='auto', origin='lower')
plt.colorbar(format='%+2.0f dB')
plt.title('Spectrogram')
plt.xlabel('Time')
plt.ylabel('Frequency')
plt.show()
audio_file_path='./data/file_example_WAV_2MG.wav'
spectrogram(audio_file_path)