Skip to content

Instantly share code, notes, and snippets.

@0xfe
Created March 2, 2020 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xfe/231d285b53f15b0ace08f93934100e6c to your computer and use it in GitHub Desktop.
Save 0xfe/231d285b53f15b0ace08f93934100e6c to your computer and use it in GitHub Desktop.
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