I'm working on a DSP lab and I can't figure out how to apply the filter I've created to a sound. The only thing I found online that did something to my sound was signal.filtfilt
but that seems to just output silence for some reason.
import matplotlib.pyplot as plt
import numpy as np
from scipy import signal
import librosa
sf = 44100
t = np.arange(0., 0.5+1./sf, 1./sf)
totalTime = 0.5
mysound = totalTime * np.sin(2*np.pi*697*t) + totalTime * np.sin(2*np.pi*1477*t)
#Creation of the filter
N = 6 # Filter order
fc = 1000/3000 # Cutoff frequency, normalized
b, a = signal.butter(N, fc)
#Apply the filter
tempf = signal.filtfilt(b,a, mysound)
librosa.output.write_wav('3mod.wav',tempf,sf)
I think I'm either making a mistake in the filter creation, or I just can't find the right way to apply it. Since I'm very new to DSP I can't really understand all the documentation material, so I might just be overlooking something simple in one of the documents I've been reading, but I can't figure out what.
EDIT: it seems to work when I change the filter creation code to
#Creation of the filter
cutOff = 1000 # Cutoff frequency
nyq = 0.5 * sf
N = 6 # Filter order
fc = cutOff / nyq # Cutoff frequency normal
b, a = signal.butter(N, fc)
So it seems that having the wrong calculation for the Nyquist frequency gave me silence. I'm not 100% sure that .filtfilt
is the right (or only) way to deploy the filter.
Leaving the question here in case it helps others.
filtfilt
andlfilter
is explained here: dsp.stackexchange.com/a/19086/29 Basically one delays/shifts the signal and the other does not. $\endgroup$fs=
parameter, for instance, so you'd call it assignal.butter(N, 1000, fs=44100)
instead ofsignal.butter(N, 1000/(44100/2))
. Another idea is to make filter objects that you can apply directly to signals instead of generatingb, a
orsos
and applying them as different steps. $\endgroup$fc/(fs/2)
in some functions andfc/(fs/pi)
(I think?) in others, because they use different definitions of normalized frequency. $\endgroup$fs
parameter and a realistic example. $\endgroup$