I have used this code how to extract frequency associated with fft values in python and added a firwin filter to detect a short high frequency signal. The signal is 1 second long and occurs at random in a wav audio file of x-seconds. My code looks as follows:
from scipy import signal
from scipy.io import wavfile
from scipy.fftpack import fft, ifft,fftfreq
import matplotlib.pyplot as plt
import wave
import numpy as np
import sys
import struct
frate,data = wavfile.read('SoundPeep.wav')
#print(frate)
b = signal.firwin(101, cutoff=900, fs= frate, pass_zero=False)
data = signal.lfilter(b, [1.0], data)
w = np.fft.fft(data)
freqs = np.fft.fftfreq(len(w))
#print(len(w)/frate)
#print(w.min(), w.max())
# (-0.5, 0.499975)
# Find the peak in the coefficients
idx = np.argmax(np.abs(w))
winner = np.argwhere(np.abs(w) == np.amax(np.abs(w)))
freq = freqs[idx]
freq_in_hertz = abs(freq * frate)
print("HZ")
print(freq_in_hertz)
occurence = idx/frate
print(occurence)
The code works well at detecting the peak HZ frequency. My problem is that I want to calculate where the high frequency signal begins in the audio-file. I thought it could be done simply by dividing the index(idx) by the framerate of the recording but this does not seem to work.