# [SciPy-User] butter() and filtfilt() - differences between MATLAB and scipy

Paul Blelloch paul.blelloch@ata-e....
Fri Jun 28 10:45:54 CDT 2013

```I ran the same butterworth filter problem and got the following results:

>>> w=2./(256./2.)
>>> b,a=butter(9,w)
>>> b
array([  2.52984969e-14,   1.01193988e-13,   2.36119304e-13,
3.54178956e-13,   3.54178956e-13,   2.36119304e-13,
1.01193988e-13,   2.52984969e-14,   2.81094410e-15])
>>> a
array([   1.        ,   -8.71731939,   33.77839104,  -76.36014168,
110.98476353, -107.55300968,   69.49343715,  -28.86903222,
6.99664203,   -0.75373077])

These numerator values are different from Matlab's, which are:

b =
Columns 1 through 6
2.7931e-15   2.5138e-14   1.0055e-13   2.3462e-13   3.5193e-13   3.5193e-13
Columns 7 through 10
2.3462e-13   1.0055e-13   2.5138e-14   2.7931e-15
a =
Columns 1 through 6
1.0000e+00  -8.7173e+00   3.3778e+01  -7.6360e+01   1.1098e+02  -1.0755e+02
Columns 7 through 10
6.9493e+01  -2.8869e+01   6.9966e+00  -7.5373e-01

I don't know why my numerator values are so different from yours.  I'm using a 64-bit MKL optimized version of scipy 0.12.0.  The differences between Matlab and scipy are on very small numbers.  If you use a lower order filter, where the denominator coefficients are significant there are no differences between Matlab and scipy.  The Matlab results may well be more accurate based on a difference in order of operations or something, but when I compared the 'filtfilt' output from the two sets of coefficients applied to white noise I didn't see much difference.  What was more interesting to me was that the filtfilt results from Matlab were quite different in the initial transient than the filtfilt results from scipy using the same coefficients.  It does appear to me that there's a difference in the application of the filtfilt function.

-Paul

-----Original Message-----
From: scipy-user-bounces@scipy.org [mailto:scipy-user-bounces@scipy.org] On Behalf Of scipy-user-request@scipy.org
Sent: Friday, June 28, 2013 4:42 AM
To: scipy-user@scipy.org
Subject: SciPy-User Digest, Vol 118, Issue 40

Send SciPy-User mailing list submissions to
scipy-user@scipy.org

To subscribe or unsubscribe via the World Wide Web, visit
http://mail.scipy.org/mailman/listinfo/scipy-user
or, via email, send a message with subject or body 'help' to
scipy-user-request@scipy.org

You can reach the person managing the list at
scipy-user-owner@scipy.org

When replying, please edit your Subject line so it is more specific than "Re: Contents of SciPy-User digest..."

Today's Topics:

1. SciPy ecosystem and Python 3 (Thomas Kluyver)
2. Re: noob question: numpy copy vs standard lib copy (Joon Ro)
3. butter() and filtfilt() - differences between MATLAB	and
scipy (Tristan Strange)
4. Re: butter() and filtfilt() - differences between MATLAB and
scipy (Fabrice Silva)
5. Re: butter() and filtfilt() - differences between MATLAB	and
scipy (Roger Fearick)
6. Re: butter() and filtfilt() - differences between MATLAB and
scipy (Tristan Strange)

----------------------------------------------------------------------

Message: 1
Date: Fri, 28 Jun 2013 01:22:37 +0100
From: Thomas Kluyver <takowl@gmail.com>
Subject: [SciPy-User] SciPy ecosystem and Python 3
To: SciPy Users List <scipy-user@scipy.org>
Message-ID:
<CAOvn4qgHQU1pRmUkR26rrhhq-kenJc4Q=bgukyDboMLsmk=aMw@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

At a conversation over lunch here at the SciPy conference, a few of us mentioned that we're starting to use Python 3 in earnest for our work.

For new users, the choice of two major Python versions is confusing and offputting, and we're not going to completely get rid of that confusion until we can simply point new users to Python 3. Most of our introductions, like the SciPy stack install page, point to Python 2 because of the ecosystem, but more and more packages now support Python 3, and we're reaching the point where we could reasonably recommend Python 3 for new users.

The aim of this post is to get an overview of where the ecosystem is with:
- What packages don't yet support Python 3, or are still too unstable?
- How important are each of those: how widely relevant are they, and are substitutes available?
- What other conditions need to be met to recommend Python 3? E.g.
Scientific Python distros, Linux distro packaging, documentation, etc.

Thanks,
Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20130628/cfe910ca/attachment-0001.html

------------------------------

Message: 2
Date: Thu, 27 Jun 2013 23:19:00 -0500
From: Joon Ro <joonhyoung.ro@gmail.com>
Subject: Re: [SciPy-User] noob question: numpy copy vs standard lib
copy
To: SciPy Users List <scipy-user@scipy.org>
Message-ID:
<CABeq5B_b5DBYL3=cM3DabxeD7HEB53G8WMpWYpt6_483f9pPEg@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Mon, May 13, 2013 at 3:23 PM, psoriasis <adiamondcsi@gmail.com> wrote:

I'm new to python.  As I understand it, assignment copies by reference
> and to do otherwise requires a function like the standard library's
> copy or deepcopy functions.  However, from what I see numpy has it's
> own copy function and using it on a random object (instance of a test
> class I made up not an array etc) doesn't seem to return the expected
> copy object.    I did try importing the copy module and that worked
> but then the numpy copy module was "shadowed" but I don't know if
> that's a problem.
>
> Still, I'm sure numpy users need to copy regular objects so what's the
> standard solution to this?
>
> Hi,

If you import those modules like import copy and import numpy as np, then you would use those functions with copy.copy() and np.copy() so you would not have the issue.

If you import those modules by from copy import * and from numpy import *, and you would have the problem. The first importing method is recommended one since by looking at your code it is explicit where the function comes from.

But, if you use numpy functions a lot (especially if you are interactively exploring), then I would import numpy with from numpy import * and import copy module with import copy (or import copy as cp) and make copy.copy()explicit.

Let me know if this is not clear.

Best,
Joon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20130627/58a1209d/attachment-0001.html

------------------------------

Message: 3
Date: Fri, 28 Jun 2013 11:08:32 +0100
From: Tristan Strange <tristan.strange@gmail.com>
Subject: [SciPy-User] butter() and filtfilt() - differences between
MATLAB	and scipy
To: scipy-user@scipy.org
Message-ID:
<CABQkF0mZ4_XBKVQQPK47d-uEVSMB_kQHH4LKhkPWQEYxxEcJbw@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi all,

I'm porting a script form MATLAB to Python and am getting very different results form the butter functions in the languages.

In MATLAB when I do the following:

w=2/(256/2);
[b,a]=butter(9,w,'low');

b comes out as a matrix containing :

2.8109e-15   2.5298e-14   1.0119e-13   2.3612e-13   3.5418e-13   3.5418e-13
2.3612e-13   1.0119e-13   2.5298e-14   2.8109e-15

When done in Python using scipy.signal's butter like so:

w = 2.0 / (256.0 / 2.0)
b, a = butter(9, w, 'low')

I get the following array with only a single value for b:

array([  2.81094410e-15])

and the following warning is issued:

/usr/lib/python2.7/dist-packages/scipy/signal/filter_design.py:288:
BadCoefficients: Badly conditioned filter coefficients (numerator): the results may be meaningless  "results may be meaningless", BadCoefficients)

Both functions in MATLAB and Python output the same a.

When using these values in filtfilt() I get totally different results. I've tried exporting b from MATLAB and loading it in to Python and passing that in to filtfilt() but still get totally diffreent results.

Can anyone tell me how to port this MATLAB code to Python such that the results are the same or explain what the problem is?

Many thanks,
Tristan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20130628/7f2f80c7/attachment-0001.html

------------------------------

Message: 4
Date: Fri, 28 Jun 2013 13:18:12 +0200
From: Fabrice Silva <silva@lma.cnrs-mrs.fr>
Subject: Re: [SciPy-User] butter() and filtfilt() - differences
between MATLAB and scipy
To: scipy-user@scipy.org
Message-ID: <1372418292.5177.16.camel@laptop-101>
Content-Type: text/plain; charset="UTF-8"

Tristan Strange a ?crit :
> In MATLAB [...] b comes out as a matrix containing :
>
> 2.8109e-15   2.5298e-14   1.0119e-13   2.3612e-13   3.5418e-13   3.5418e-13
> 2.3612e-13   1.0119e-13   2.5298e-14   2.8109e-15
>
> When done in Python using scipy.signal's butter like so:
> [...] the following warning is issued:
>
> /usr/lib/python2.7/dist-packages/scipy/signal/filter_design.py:288:
> BadCoefficients: Badly conditioned filter coefficients (numerator):
> the results may be meaningless  "results may be meaningless",
> BadCoefficients)

You should maybe worry about getting (in matlab) such values for b. All are pretty close to 0. This is what scipy implementation is warning you about ("Badly conditioned filter coefficients (numerator): the results may be meaningless"). Using this b vector may lead to output signals prone to numerical noise...

------------------------------

Message: 5
Date: Fri, 28 Jun 2013 11:23:42 +0000
From: Roger Fearick <roger.fearick@uct.ac.za>
Subject: Re: [SciPy-User] butter() and filtfilt() - differences
between MATLAB	and scipy
To: SciPy Users List <scipy-user@scipy.org>
Message-ID:
<DD0049064BE06D49B33A7908C703601825547269@srvwinexc002.wf.uct.ac.za>
Content-Type: text/plain; charset="windows-1252"

You're using Python 2.7: maybe 2/(256/2) = 0.
________________________________

Hi all,

I'm porting a script form MATLAB to Python and am getting very different results form the butter functions in the languages.

In MATLAB when I do the following:

w=2/(256/2);
[b,a]=butter(9,w,'low');

b comes out as a matrix containing :

2.8109e-15   2.5298e-14   1.0119e-13   2.3612e-13   3.5418e-13   3.5418e-13 2.3612e-13   1.0119e-13   2.5298e-14   2.8109e-15

When done in Python using scipy.signal's butter like so:

w = 2.0 / (256.0 / 2.0)
b, a = butter(9, w, 'low')

I get the following array with only a single value for b:

array([  2.81094410e-15])

and the following warning is issued:

/usr/lib/python2.7/dist-packages/scipy/signal/filter_design.py:288: BadCoefficients: Badly conditioned filter coefficients (numerator): the results may be meaningless  "results may be meaningless", BadCoefficients)

Both functions in MATLAB and Python output the same a.

When using these values in filtfilt() I get totally different results. I've tried exporting b from MATLAB and loading it in to Python and passing that in to filtfilt() but still get totally diffreent results.

Can anyone tell me how to port this MATLAB code to Python such that the results are the same or explain what the problem is?

Many thanks,
Tristan

________________________________
UNIVERSITY OF CAPE TOWN

This e-mail is subject to the UCT ICT policies and e-mail disclaimer published on our website at http://www.uct.ac.za/about/policies/emaildisclaimer/ or obtainable from +27 21 650 9111. This e-mail is intended only for the person(s) to whom it is addressed. If the e-mail has reached you in error, please notify the author. If you are not the intended recipient of the e-mail you may not use, disclose, copy, redirect or print the content. If this e-mail is not related to the business of UCT it is sent by the sender in the sender's individual capacity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20130628/2830d273/attachment-0001.html

------------------------------

Message: 6
Date: Fri, 28 Jun 2013 12:46:54 +0100
From: Tristan Strange <tristan.strange@gmail.com>
Subject: Re: [SciPy-User] butter() and filtfilt() - differences
between MATLAB and scipy
To: SciPy Users List <scipy-user@scipy.org>
Message-ID:
<CABQkF0mcPmD-4dOpR93X-uqBq3D_JkVDhO19Mcv-ay+UpJdmkw@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On 28 June 2013 12:23, Roger Fearick <roger.fearick@uct.ac.za> wrote:

>  You're using Python 2.7: maybe 2/(256/2) = 0.
>

It's not this I'm afraid. I import division from __future__

>Tristan Strange a ?crit :
>> In MATLAB [...] b comes out as a matrix containing :
>>
>> 2.8109e-15   2.5298e-14   1.0119e-13   2.3612e-13   3.5418e-13
3.5418e-13
>> 2.3612e-13   1.0119e-13   2.5298e-14   2.8109e-15
>>
>> When done in Python using scipy.signal's butter like so:
>> [...] the following warning is issued:
>>
>> /usr/lib/python2.7/dist-packages/scipy/signal/filter_design.py:288:
>> BadCoefficients: Badly conditioned filter coefficients (numerator):
>> the results may be meaningless  "results may be meaningless",
BadCoefficients)

> You should maybe worry about getting (in matlab) such values for b.
> All are pretty close to 0. This is what scipy implementation is
> warning you about ("Badly conditioned filter coefficients (numerator):
> the results may be meaningless"). Using this b vector may lead to
> output signals prone to numerical noise...

Ok, thanks. Apparently the MATLAB implementation functions as expected....

Any one else have any ideas?

Cheers,
Tristan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20130628/5f00483a/attachment.html

------------------------------

_______________________________________________
SciPy-User mailing list
SciPy-User@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user

End of SciPy-User Digest, Vol 118, Issue 40
*******************************************

```

More information about the SciPy-User mailing list