I'm trying to create an application using python that is capable of recording an audio signal and detecting short glitches in the signal. For example, I could be recording a 1000Hz sine wave and looking for instances when the audio cuts out for a sample or two. I've been achieving this by removing all the 1000Hz frequencies using a notch filter and searching where the remaining peaks occur. My program seems to work fine when I begin recording a signal, finish recording and then filter the entire recorded signal, search for peaks etc but I would like to get it working in real time, where I can begin recording, analyze 5 seconds worth of audio then move on to the next 5 seconds of audio and so on until I finish recording. Unfortunately my notch filter that worked previously no longer seems to work correctly when I do this. The code for my notch filter is shown below (I am using scipy for signal processing and pyaudio for recording audio))
f0 = 1000.0 # Frequency to be removed from signal (Hz) Q = 30.0 # Quality factor w0 = f0/(fs/2) # Normalized Frequency # Design notch filter b, a = signal.iirnotch(w0, Q) zi = signal.lfilter_zi(b, a) z, _ = signal.lfilter(b, a, xn, zi=zi*xn) z2, _ = signal.lfilter(b, a, z, zi=zi*z) y = signal.filtfilt(b, a, xn)
When I begin recording audio, finish recording and then check for glitches, these miniature peaks do not occur at all, they only occur when I try to divde the incoming audio into chunks and check for glitches as the audio is being recorded. Can anyone explain what they are (something to do with harmonics?) and whether or not I can get rid of them? Ideally I would also like to get rid of the peaks that occur at the beginning and the end of every 5 second chunk as well. I dont have much of a background in DSP and I'm still learning a lot about it so I would appreciate any help.