Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compare SciPy vs ANN in generating spectrograms
def make_wave():
# Create four evenly-spaced tones
waves = []
hz = 0
for i in range(0, size, 512):
hz = hz + 50
waves.append(np.sin(np.linspace(0, 2 * np.pi * hz, 512)))
return np.hstack(waves)
wave = make_wave()
# Calculate spectrogram using math
f, t, wave_sxx = spectrogram(wave)
wave_sxx_graph = np.absolute(wave_sxx)
# Calculate spectrogram using neural network
nn_sxx = model.predict(np.reshape(wave, (1, size)))
nn_sxx_graph = np.reshape(np.absolute(nn_sxx[0][rows * cols:] + 1j * nn_sxx[0][:rows * cols]), (rows, cols))
# Plot spectrograms side-by-side
plt.figure(figsize=(12, 3))
plt.subplot(1, 2, 1)
plt.pcolormesh(t, f, wave_sxx_graph)
plt.subplot(1, 2, 2)
plt.pcolormesh(t, f, nn_sxx_graph)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment