简体   繁体   中英

Butterworth filter python

I want to filter a wav file with butterworth bandpass. I've tried to convert the wav file to fft then to apply band-pass filter and then to apply ifft . From the graphics it seems to be a little filtered but when I lessen to both input and output wav there is no difference in audio, it is like the filter it`s not working. Is there a mistake?

# FFT - Fast Fourier Transform
frequency,data = wavfile.read('Input_sound.wav')
signalFFT = fft(data)
def butter_bandpass_filter(data, lowcut, highcut, frequency, order):
    nyq = 0.5 * frequency
    low = lowcut / nyq
    high = highcut / nyq

    b, a = butter(order, [low, high], btype='bandpass')
    y = lfilter(b, a, data)
    return y 
funct=butter_bandpass_filter(signalFFT,2100,3400,44100,4)

filtered_sound=ifft(funct)
filtered_sound=sp.real(filtered_sound)
filtered_sound=sp.int16(filtered_sound/sp.absolute(filtered_sound).max()*32767)
wavfile.write('Filtered_ouput.wav',frequency,filtered_sound)

Looking at the exemple of the lfilter documentation , it seems that you don't need to perform FFT, but directly provide the data to the filter. It is then much simpler

frequency,data = wavfile.read('Input_sound.wav')

def butter_bandpass_filter(data, lowcut, highcut, frequency, order):
    nyq = 0.5 * frequency
    low = lowcut / nyq
    high = highcut / nyq

    b, a = butter(order, [low, high], btype='bandpass')
    y = lfilter(b, a, data)
    return y 

filtered_sound=butter_bandpass_filter(data,2100,3400,44100,4)
wavfile.write('Filtered_ouput.wav',frequency,filtered_sound)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM