[SciPy-User] butterworth filter on .WAV file

David Baddeley david_baddeley@yahoo.com...
Wed Jul 7 17:11:12 CDT 2010

In addition to the comments that have already been made (suggesting you plot the 
result etc ...), I think you might have problems with your final cast (the 
asarray(filtered, int) bit). On my computer that results in a 64 bit integer 
type (might be 32 bit on some platforms). I guess you want a 16 bit integer, as 
this is what most sound programs will be expecting. You could try:

asarray(filtered, 'int16)


From: Peter Howard <peterhoward42@gmail.com>
To: scipy-user@scipy.org
Sent: Thu, 8 July, 2010 6:40:31 AM
Subject: [SciPy-User] butterworth filter on .WAV file

I'm trying to write a very simple example of applying a band pass filter  to a 
.WAV music file.
I'm distinctly rusty on DSP and inexperienced  with SciPy/NumPy so apologies if 
I've made a dumb mistake.

It executes without errors or warnings.
It produces the output file,  but this is twice the size of the input file, 
which is clearly wrong.
I'm  most uncertain about casting the filtered data back to integers and  thus 
being suitable for writing back out to .WAV.
I'm also bit uncertain about my interpretation / understanding of the  frequency 
and gain specifications.

Any help and advice very much  appreciated.



from  scipy.io.wavfile import read, write
from scipy.signal.filter_design import butter, buttord
from  scipy.signal import lfilter
from numpy import asarray

def  convert_hertz(freq):
    # convert frequency in hz to units of pi  rad/sample
    # (our .WAV is sampled at 44.1KHz)
    return freq * 2.0 / 44100.0

rate, sound_samples =  read('monty.wav')
pass_freq = convert_hertz(440.0) # pass up to  'middle C'
stop_freq = convert_hertz(440.0 * 4) # max attenuation  from 3 octaves higher
pass_gain = 3.0 # tolerable loss in passband (dB)
stop_gain = 60.0 #  required attenuation in stopband (dB)
ord, wn = buttord(pass_freq,  stop_freq, pass_gain, stop_gain)
b, a = butter(ord, wn, btype =  'low')
filtered = lfilter(b, a, sound_samples)
integerised_filtered =  asarray(filtered, int)
write('monty-filtered.wav', rate,  integerised_filtered) 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100707/1e40f4fd/attachment-0001.html 

More information about the SciPy-User mailing list