-1

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.

Radonic
  • 47
  • 2
  • 6
  • As you have it, `idx` corresponds to the **frequency** of the chirp, and has nothing to do with the **time** that it occurs (ie, where in the audio file). One possible way to find the time is to do an auto-correlation with a chirp of the same frequency, or there are fancier mathematical approaches using the FFT. – tom10 Oct 10 '18 at 23:26
  • But the idx is the index of the maximum value of the FFT but this has no correlation with the original data or? The len(w) is equal to len(data) – Radonic Oct 11 '18 at 06:20

1 Answers1

0

you could make a sonogram by using a short time fourier transform to see how frequency changes over time. the stft returns 3 values: timestamp, amplitude, and frequency. If the amplitude isnt relevant, you can just use peak detection from there.

  • I tried that but the frequency array and the time array are not equal sized and the frequency array contains the same values regardless of the sound file input – Radonic Oct 14 '18 at 06:37
  • I found another way around but thank you for the answer :) – Radonic Oct 14 '18 at 09:49