Can't get lFilter to work (rpi2b)
<p>Some information: I am using a rpi2b with a cirrus logic audio card and have worked out the kernel changes to get the sound card going, I can output without problems, I can record from line in without problems (I am using pyAudio). </p>

<p>Now I only want to filter the signal and have found a lot of functions in the scipy library...</p>

<p>First thing to mention I can only use 2 channels (stereo) input the soundcard seems to doesnt allow 1 channel and I dont have a source to stream mono (iPhone, simple USB player everyone has only stereo)....</p>

<p>The filter line (specifications) is just an example I think it will be an other filter if it works :D</p>

<p>So before the code begins a little to explain. When using stereo input the samples appear to be interleaved so you get on the one channel a sample on the other... I thought this thread was helpful: <a href="http://stackoverflow.com/questions/22636499/convert-multi-channel-pyaudio-into-numpy-array/22644499#comment35021142_22644499">Convert multi-channel PyAudio into NumPy array</a></p>

<p>Thats why I also tried to decode my stream into an array, filter and then code it again into a stream for output. If I neglect the interleaving fact I get weird noises thats why I think I should take care of it :)</p>

<p><strong>Now comes the part where I fail:</strong> lFilter doesnt want to work! If I just use my unprocessed input_data it says the axis is out of range (I tried -1,0,1,2) dont know :/ and when using my reshaped etc. data I get:</p>

<pre><code>return sigtools._linear_filter(b, a, x, axis, zi)
ValueError: object of too small depth for desired array
</code></pre>

<p>Can someone please explain why this is happening? :D Never used signals thats why I am unexperienced but I cant understand why the unprocessed data has axis problems and even though it wouldnt bother because I need the reshaped data like in the linked thread... </p>

<pre><code>WIDTH = 2
CHANNELS = 2
RATE = 44100
p = pyaudio.PyAudio()
[b,a] = signal.iirfilter(2,[50,200],rs=60,btype='band',analog=True,ftype='cheby2')
full_data = np.array([])


def callback(in_data, frame_count, time_info, status):
    global b,a,full_data
    full_data = decode (in_data, 2)
    audio_data = signal.lfilter(b,a, full_data)
    print (audio_data)
    stream_data = encode(audio_data)
    return (stream_data, pyaudio.paContinue)
</code></pre>

<p>I skipped the stream open part and the decode and encode since the last ones are right now exactly the same as in the thread and the first one works thats why I just posted these parts. If needed I can also provide the others.</p>

<p>Any help is highly appreciated!</p>

<ul>
<li>Sanj3k</li>
</ul>

<hr>

<p>This is an example of what the data looks like, at first its a 1d np array and after the reshape etc ist 2d because of the stereo....</p>

<p>What does lFilter need? Since I have axis errors and with 2d its to small....</p>

<p><a href="http://i.stack.imgur.com/xaZte.png" rel="nofollow">screenshot from the terminal</a></p>

<p>Edit: Btw here is the documentation from scipy library for the lfilter function and it says:</p>

<pre><code>x : array_like
An N-dimensional input array.
</code></pre>

<p><a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.lfilter.html" rel="nofollow">http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.lfilter.html</a></p>

<p>For one thing, you need to keep the final conditions from one chunk and feed it as initial conditions to the next chunk.  Otherwise the filter assumes rest at the start of each chunk, which will result in glitches at each chunk.</p>

<pre><code>global zi
audio_data, zi = signal.lfilter(b, a, full_data, zi=zi)`
</code></pre>

<p>So this call will update <code>zi</code> and then on the next cycle it will use it as the input to lfilter, which will update it, etc.  As <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.lfilter.html" rel="nofollow">the docs</a> say:</p>

<pre><code>zi : array_like, optional
    Initial conditions for the filter delays.  It is a vector
    (or array of vectors for an N-dimensional input) of length
    ``max(len(a), len(b)) - 1``.  If `zi` is None or is not given then
    initial rest is assumed.

...

zf : array, optional
    If `zi` is None, this is not returned, otherwise, `zf` holds the
    final filter delay values.
</code></pre>

<p>To initialize <code>zi</code> in the first place, just create a zeros array of shape <code>(max(len(a), len(b)) - 1, number_of_channels)</code>.</p>

python numpy scipy raspberry-pi2