[SciPy-User] lfilter produce very low output

Warren Weckesser warren.weckesser at gmail.com
Fri Feb 28 09:47:45 EST 2014


On 2/28/14, f5owl at free.fr <f5owl at free.fr> wrote:
> Hello,
> I have identified my problem : the phil1.wav is a stereo file.
> I converted it to mono with audacity and now I have consistent results.
> Probably  r= np.hsplit(sig,2)[0] is not the good way to select only one
> channel.
> Philipp


Hi Philipp,

The array sig returned by wavfile.read has shape (2634096, 2), and
when you use hsplit like that, r has shape (2634096, 1).  It is a 2-d array
containing a single column.  The problem occurred when you passed this
array to lfilter.  lfilter can handle n-dimensional arrays; the axis
to be filtered
is specified with the 'axis' argument.  The default axis is -1 (i.e.
the last axis
of the array).  So when you called lfilter with r, it treated r as a
collection of
2634096 1-dimensional arrays each with length 1.   The quick fix for that
would be to use 'axis=0' in the call to lfilter.

A better solution is to not use hsplit, and instead use regular numpy slicing
to pull apart the signal.  To get the first and second channels into 1-d arrays,
you can use

    channel1 = sig[:, 0]
    channel2 = sig[:, 1]

Each of those variables is a 1-d array, and can  be passed to lfilter without
requiring a change to the axis argument.

Warren


>
> ----- Mail original -----
> De: "Warren Weckesser" <warren.weckesser at gmail.com>
> À: "SciPy Users List" <scipy-user at scipy.org>
> Envoyé: Jeudi 27 Février 2014 17:09:12
> Objet: Re: [SciPy-User] lfilter produce very low output
>
> On 2/27/14, f5owl at free.fr <f5owl at free.fr> wrote:
>> Dear list readers,
>> I am new to scipy and I am trying to filter some wav file containing
>> experimental data.
>> The files contain an (almost) sine wave at 5kHz with some 50 and 100Hz
>> noise
>> and other spurious.
>> The fs=44100Hz and samples are 16 bits.
>> I use firwin and lfilter to filter my data between 4000 and 6000 Hz.
>> My problem is that the output of lfilter is very low and seem to be almost
>> independent of the filter frequencies.
>> I have made a test file with a pure 5kHz sine wave and result are
>> consistant.
>> I have to normalize data between -1 and 1 but I have the same problem.
>> I can figure out what is wrong.
>> I put my code and the end of the message. phil1.wav is real data,
>> sin_0_5000_44100.wav is generated sine.
>> Attachment is two plots of the first 400 samples made with the two files.
>> With the simulated sine wave, result is consistent filter delay and low
>> attenuation.
>> With the real data, no filter delay and output very low.
>> Can someone help ?
>
>
> Can you put the file phil1.wav somewhere accessible?   It would help
> us reproduce the output you are seeing.
>
> Warren
>
>
>> Thank you
>>
>> Here is my code :
>>
>> import matplotlib.pyplot as plt
>> import numpy as np
>> from scipy.signal import lfilter, firwin
>> from scipy.io import wavfile
>> #filename='phil1.wav'
>> filename='sin_0_5000_44100.wav'
>>
>> fs, sig = wavfile.read(filename)
>>
>> # extract first channel
>> r= np.hsplit(sig,2)[0]
>>
>> print('fs = %f'%fs)
>> print('min %f max %f mean %f rms
>> %f'%(r.min(),r.max(),r.mean(),np.sqrt(np.mean(r**2))))
>> flow=4000
>> fup=6000
>> taps = firwin(256,[flow,fup],nyq=fs/2,pass_zero=False, scale=False)
>>
>> rf = lfilter(taps, 1.0, r)
>>
>> print('min %f max %f mean %f rms
>> %f'%(rf.min(),rf.max(),rf.mean(),np.sqrt(np.mean(rf**2))))
>> t=np.arange(len(r))*(1./fs)
>> plt.subplot(211)
>> plt.title(filename)
>> plt.plot(t[:400],r[:400])
>> plt.subplot(212)
>> plt.title(filename+' filtered %.0f %.0f'%(flow,fup))
>> plt.plot(t[:400],rf[:400])
>>
>> plt.show()
>>
>>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-User mailing list