Created
January 31, 2022 22:29
-
-
Save schollz/4aa0d3811bccf1fc7e3bfebd24659581 to your computer and use it in GitHub Desktop.
This file contains 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
import shutil | |
from scipy import signal | |
from scipy.io import wavfile | |
import numpy as np | |
debug_depop = True | |
if debug_depop: | |
import matplotlib.pyplot as plt | |
def popfind(filename,channel=0): | |
# read in file | |
samplerate, data = wavfile.read(filename) | |
print(samplerate) | |
# collect data-type information and normalize | |
original_type = data.dtype | |
data_max = (np.iinfo(original_type).max) | |
data = data.astype(float) | |
data = data / data_max | |
# compute basic properties | |
length = data.shape[0] / samplerate | |
num_channels = data.shape[1] | |
time = np.linspace(0., length, data.shape[0]) | |
# stft of the channel | |
# go through each band and, if its soft, | |
# collect it to be averaged later | |
f, t, Zxx = signal.stft(data[:, channel], samplerate, window=signal.get_window("hann",128),nperseg=128) | |
zmean = np.zeros(Zxx[1,:].shape) | |
znum = 0 | |
for i in range(Zxx.shape[0]): | |
z = 20*np.log10(np.abs(Zxx[i,:])) | |
z = z - np.mean(z[-500:]) | |
# find soft audio regions and use them for the average | |
# arbitrary but works okay | |
if np.sum(z) < 10000: | |
zmean = np.add(zmean,z) | |
znum += 1 | |
# average all the soft bands | |
if znum>0: | |
zmean = zmean / znum | |
zmean = zmean - np.mean(zmean[-500:]) | |
z_std = np.std(zmean[-500:]) | |
peaks, _ = signal.find_peaks(zmean,height=z_std*6,prominence=10) | |
if debug_depop: | |
print(peaks) | |
plt.plot(t,zmean) | |
plt.plot(t[peaks],zmean[peaks],'o') | |
plt.title("ch {}".format(channel)) | |
plt.show() | |
import sys | |
filename = sys.argv[1] | |
popfind(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment