Created
March 10, 2020 13:10
-
-
Save kartikgill/a1a6efdac872a9e66d528b93eb049f74 to your computer and use it in GitHub Desktop.
Spectrogram Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def spectrogram(samples, sample_rate, stride_ms = 10.0, | |
window_ms = 20.0, max_freq = None, eps = 1e-14): | |
stride_size = int(0.001 * sample_rate * stride_ms) | |
window_size = int(0.001 * sample_rate * window_ms) | |
# Extract strided windows | |
truncate_size = (len(samples) - window_size) % stride_size | |
samples = samples[:len(samples) - truncate_size] | |
nshape = (window_size, (len(samples) - window_size) // stride_size + 1) | |
nstrides = (samples.strides[0], samples.strides[0] * stride_size) | |
windows = np.lib.stride_tricks.as_strided(samples, | |
shape = nshape, strides = nstrides) | |
assert np.all(windows[:, 1] == samples[stride_size:(stride_size + window_size)]) | |
# Window weighting, squared Fast Fourier Transform (fft), scaling | |
weighting = np.hanning(window_size)[:, None] | |
fft = np.fft.rfft(windows * weighting, axis=0) | |
fft = np.absolute(fft) | |
fft = fft**2 | |
scale = np.sum(weighting**2) * sample_rate | |
fft[1:-1, :] *= (2.0 / scale) | |
fft[(0, -1), :] /= scale | |
# Prepare fft frequency list | |
freqs = float(sample_rate) / window_size * np.arange(fft.shape[0]) | |
# Compute spectrogram feature | |
ind = np.where(freqs <= max_freq)[0][-1] + 1 | |
specgram = np.log(fft[:ind, :] + eps) | |
return specgram |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment