-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot2.py
43 lines (31 loc) · 1.79 KB
/
plot2.py
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
# https://kevinsprojects.wordpress.com/2014/12/13/short-time-fourier-transform-using-python-and-numpy/
from matplotlib import pyplot as plt
import numpy as np
import librosa
data, fs = librosa.load('raw/voice/TEST_DR1_FAKS0_SA1.WAV')
print(fs)
fft_size = 1024
overlap_fac = 0.5
# data = data / np.max(data)
# hop_size = np.int32(np.floor(fft_size * (1 - overlap_fac)))
hop_size = np.int32(fs / 100)
pad_end_size = fft_size # the last segment can overlap the end of the data array by no more than one window size
total_segments = np.int32(np.ceil(len(data) / np.float32(hop_size)))
t_max = len(data) / np.float32(fs)
window = np.hanning(fft_size) # our half cosine window
inner_pad = np.zeros(fft_size) # the zeros which will be used to double each segment size
proc = np.concatenate((data, np.zeros(pad_end_size))) # the data to process
result = np.empty((total_segments, fft_size), dtype=np.float32) # space to hold the result
for i in range(total_segments): # for each segment
current_hop = hop_size * i # figure out the current segment offset
segment = proc[current_hop:current_hop + fft_size] # get the current segment
windowed = segment * window # multiply by the half cosine function
padded = np.append(windowed, inner_pad) # add 0s to double the length of the data
spectrum = np.fft.fft(padded) / fft_size # take the Fourier Transform and scale by the number of samples
autopower = np.abs(spectrum * np.conj(spectrum)) # find the autopower spectrum
result[i, :] = autopower[:fft_size] # append to the results array
result = 20 * np.log10(result) # scale to db
# result = np.clip(result, -40, 200) # clip values
print(result.shape)
img = plt.imshow(result, origin='lower', cmap='jet', interpolation='nearest', aspect='auto')
plt.show()