Created
February 14, 2022 11:27
-
-
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
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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Initial plots here: https://imgur.com/a/bLzsO3n