I am segmenting drum audio files at each transient and exporting the audio to individual wav files. The problem is, all of my files have a dc offset that I cannot seem to get rid of which is causing popping sounds at the end of the file. I am able to use Audacity's built in high-pass filter to verify that applying a filter would fix my problem, but I have not yet been able to replicate those results with code.
My preference is to use torchaudio's highpass_biquad() method but I am open to using scipy filters too. The main goal is to remove the offset so that the audio files do not have a popping sound at the end.
How do I implement a high pass filter to correct the dc offset like Audacity's high pass filter does as shown in the pictures?
torchaudio approach
from torchaudio.functional import highpass_biquad
import librosa
wav, sample_rate = librosa.load(path, sr=None, mono=False) # files are 24 bit 44.1k
wav_tensor = torch.from_numpy(wav)
cuttoff_freq = 15.0
wav_filtered = highpass_biquad(wav_tensor, sample_rate, cutoff_freq)
scipy approach
from scipy import signal
import librosa
wav, sample_rate = librosa.load(path, sr=None, mono=False) # files are 24 bit 44.1k
cutoff_freq = 15
# METHOD 1
b, a = signal.butter(N=5, Wn=cutoff_freq, btype='high', fs=sample_rate)
wav_filtered = signal.filtfilt(b, a, wav)
# METHOD 2
sos = signal.butter(N=5, Wn=cutoff_freq, btype='hp', fs=sample_rate, output='sos')
wav_filtered = signal.sosfilt(sos, wav)
Picture 1 is the output of torch highpass_biquad method. The scipy approach yields similar results.
Picture 2 is the audio after applying highpass effect in audacity. This is the desired output of my code.
Picture 3 is an example of output with no high pass filtering applied. Most files come out centered below 0dB.


