[SciPy-User] Boxcar smoothing of 1D data array...?

1,070 views
Skip to first unread message

tho...@nbi.ku.dk

unread,
Jun 12, 2010, 11:28:45 AM6/12/10
to scipy...@scipy.org
Hello list;

This seems like it should be a simple task, but I couldn't seem to find
anything in the docs about it - or rather, what I found seems to be from
the Numeric/Numarray days and not valid anymore.

As the subject line suggests, I have a 1D array that I want to
smooth/convolve with a Boxcar kernel of a certain width. In IDL there's
simply a function to do this, and there might be something people have
hacked together out there to do it too - but isn't there a simple way to
do it using built-in NumPy and SciPy tools?

Cheers;
Emil

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

Benjamin Root

unread,
Jun 15, 2010, 11:46:37 AM6/15/10
to SciPy Users List
Emil,

You can find various windowing functions like boxcar, hamming and hanning in scipy.signals module.

Ben Root

David Baddeley

unread,
Jun 15, 2010, 8:26:35 PM6/15/10
to SciPy Users List
Alternatively you could just use scipy.convolve with a tophat kernel ie (for a filter of length N & signal y):

scipy.convolve(y, ones(N)/N)

see the docs for scipy.convolve for more info (you might want to specify how it handles the ends, for example)

cheers,
David


From: Benjamin Root <ben....@ou.edu>
To: SciPy Users List <scipy...@scipy.org>
Sent: Wed, 16 June, 2010 3:46:37 AM
Subject: Re: [SciPy-User] Boxcar smoothing of 1D data array...?

Davide Lasagna

unread,
Jun 16, 2010, 2:25:04 PM6/16/10
to scipy...@scipy.org

You could use the Savitzky-Golay smoothing filter function present in the cookbook. It is very well suited for such operation.

My two cents,

Davide

Sturla Molden

unread,
Jun 18, 2010, 7:22:45 AM6/18/10
to scipy...@scipy.org

Den 16.06.2010 02:26, skrev David Baddeley:
Alternatively you could just use scipy.convolve with a tophat kernel ie (for a filter of length N & signal y):

scipy.convolve(y, ones(N)/N)

see the docs for scipy.convolve for more info (you might want to specify how it handles the ends, for example)


You should not use convolution for boxcar filtering. It can be solved using a recursive filter, basically

    y[n] = y[n-1] + x[n] - x[n-m]

then normalize y by 1/m.

Sturla

Anne Archibald

unread,
Jun 18, 2010, 10:51:28 AM6/18/10
to SciPy Users List

How does the numerical stability of this compare to a FIR
implementation (with or without a Fourier transform)?

Anne

> Sturla

David Baddeley

unread,
Jun 18, 2010, 4:13:26 PM6/18/10
to SciPy Users List
Out of curiosity, are there any reasons other than performance (which might be moot if you have to implement the recursive filter as a python loop) for not using a convolution?

cheers,
David


From: Sturla Molden <stu...@molden.no>
To: scipy...@scipy.org
Sent: Fri, 18 June, 2010 11:22:45 PM

Subject: Re: [SciPy-User] Boxcar smoothing of 1D data array...?

Sturla Molden

unread,
Jun 19, 2010, 6:00:33 AM6/19/10
to SciPy Users List

Den 18. juni 2010 kl. 16.51 skrev Anne Archibald <aarc...@physics.mcgill.ca
>:

>>
>>
>>
>> y[n] = y[n-1] + x[n] - x[n-m]
>>
>> then normalize y by 1/m.
>
> How does the numerical stability of this compare to a FIR
> implementation (with or without a Fourier transform)?
>
>>

For practical purposes, x will be a digital signal (from an ADC) or a
digital image. Thus the recursive boxcar can be implemented with
integer maths. Stability is excellent as numerical error is 0. :-)

You just have to make sure that y does not overflow (e.g. let y be 32
bit if x is 16 bit).

Anne Archibald

unread,
Jun 19, 2010, 12:15:59 PM6/19/10
to SciPy Users List
On 19 June 2010 06:00, Sturla Molden <stu...@molden.no> wrote:
>
>
> Den 18. juni 2010 kl. 16.51 skrev Anne Archibald <aarc...@physics.mcgill.ca
>  >:
>
>>>
>>>
>>>
>>>     y[n] = y[n-1] + x[n] - x[n-m]
>>>
>>> then normalize y by 1/m.
>>
>> How does the numerical stability of this compare to a FIR
>> implementation (with or without a Fourier transform)?
>>
>>>
>
> For practical purposes, x will be a digital signal (from an ADC) or a
> digital image. Thus the recursive boxcar can be implemented with
> integer maths. Stability is excellent as numerical error is 0. :-)
>
> You just have to make sure that y does not overflow (e.g. let y be 32
> bit if x is 16 bit).

Heh. You have a point there. But I should say that in the application
in which we use boxcar filtering (searching for single pulses in radio
pulsar search data), the data has already been processed sufficiently
that we can't use integers any more, and in fact we use 32-bit floats
rather than doubles. It's kind of moot for us in any case since we
plan to modify the code to do matched filtering with a different
filter, so convolution will be necessary.

It's also worth checking: while scipy.signal does implement IIR
filters, I don't think it takes advantage of zero coefficients to
avoid arithmetic, so using it to implement a boxcar is probably worse
than using even a non-FFT convolution. Is this right?

Anne

Charles R Harris

unread,
Jun 19, 2010, 2:00:23 PM6/19/10
to SciPy Users List

Note that the transfer functions are the same if the zero at 1 in the numerator exactly cancels the zero in the denominator. However, the output has to be correctly initialized, since the initial value is otherwise carried along, which you can see by setting the inputs to zero. So the algebraic cancellation doesn't remove all the side effects of using a recursive filter.

Chuck

Reply all
Reply to author
Forward
0 new messages