-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspectrogram_analysis.py
More file actions
79 lines (47 loc) · 1.92 KB
/
spectrogram_analysis.py
File metadata and controls
79 lines (47 loc) · 1.92 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
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
# Function to create Spectrogram & waveform for a wav file
# Author : Upasana Pandey
# The frequencies of the audio or the pitch are identified with the brighter yellow columns present in the spectrum.
import matplotlib.pyplot as plot
from scipy.io import wavfile
import wave
def spectrogram(file, directory):
'''
Function to perform spectrogram analysis
'''
obj = wave.open(file, 'r')
channels = obj.getnchannels()
Sample_width = obj.getsampwidth()
Frame_rate = obj.getframerate()
Number_of_frames = obj.getnframes()
parameters = obj.getparams()
obj.close()
if(channels == 1):
# Read the wav file (mono)
samplingFrequency, signalData = wavfile.read(file)
# Plot the signal read from wav file
plot.subplot(211) # nrows=2, ncols=1, plot_number=1
plot.title('Spectrogram of wav file')
plot.plot(signalData)
plot.xlabel('Sample')
plot.ylabel('Amplitude[dB]')
plot.subplot(212) # nrows=2, ncols=1, plot_number=2
plot.specgram(signalData, Fs=samplingFrequency)
plot.xlabel('Time[sec]')
plot.ylabel('Frequency[Hz]')
plot.savefig(directory+'.png', bbox_inches='tight')
plot.show()
elif(channels == 2):
# Read the wav file (stereo)
samplingFrequency, signalData = wavfile.read(file)
# Plot the signal read from wav file
plot.subplot(211)
plot.title('Spectrogram of a wav file with piano music')
plot.plot(signalData[:, 1])
plot.xlabel('Sample')
plot.ylabel('Amplitude')
plot.subplot(212)
plot.specgram(signalData[:, 1], Fs=samplingFrequency)
plot.xlabel('Time')
plot.ylabel('Frequency')
plot.savefig(directory+'.png', bbox_inches='tight')
plot.show()