Skip to content

Instantly share code, notes, and snippets.

@ES-Alexander
Created February 14, 2022 11:27
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 ES-Alexander/aac7bf5d5ba441cbc1ea0b32a0adac35 to your computer and use it in GitHub Desktop.
Save ES-Alexander/aac7bf5d5ba441cbc1ea0b32a0adac35 to your computer and use it in GitHub Desktop.
Very rough initial analysis of some sound recordings to identify whale songs
from pathlib import Path
from scipy.io import wavfile
from scipy.fft import fft, fftfreq
import matplotlib.pyplot as plt
import numpy as np
path = Path('.') # path to whale song files
files = list(path.glob('*.wav'))
# use every nth sample, to speed up processing
# NOTE: divides maximum detectable frequency
downsample = 20
fig, ax = plt.subplots()
for file in files:
sample_rate, data = wavfile.read(file)
# basically copy scipy 1D Fourier transform example
N = len(data) // downsample
T = downsample / sample_rate
x = np.linspace(0, N / sample_rate, N, endpoint=False)
y = data[::downsample]
valid_frequencies = N // 2 # Nyquist limit
yf = 2 / N * np.abs(fft(y))[:valid_frequencies]
xf = fftfreq(N, T)[:valid_frequencies]
ax.loglog(xf, yf) # plot power spectrum in log-log form
ax.set_xlabel('frequency [Hz]')
plt.show()
@ES-Alexander
Copy link
Author

Initial plots here: https://imgur.com/a/bLzsO3n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment