# Port To Python/scipy Of GNU Octave's Function Decimate.m

Join the DZone community and get the full member experience.

Join For Free```
// description of your code here
``````
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
"""port to python of the GNU Octave function decimate.m"""
from scipy.signal import cheby1, firwin, lfilter
def decimate(x, q, n=None, ftype='iir', axis=-1):
"""downsample the signal x by an integer factor q, using an order n filter
By default, an order 8 Chebyshev type I filter is used or a 30 point FIR
filter with hamming window if ftype is 'fir'.
(port to python of the GNU Octave function decimate.)
Inputs:
x -- the signal to be downsampled (N-dimensional array)
q -- the downsampling factor
n -- order of the filter (1 less than the length of the filter for a
'fir' filter)
ftype -- type of the filter; can be 'iir' or 'fir'
axis -- the axis along which the filter should be applied
Outputs:
y -- the downsampled signal
"""
if type(q) != type(1):
raise Error, "q should be an integer"
if n is None:
if ftype == 'fir':
n = 30
else:
n = 8
if ftype == 'fir':
b = firwin(n+1, 1./q, window='hamming')
y = lfilter(b, 1., x, axis=axis)
else:
(b, a) = cheby1(n, 0.05, 0.8/q)
y = lfilter(b, a, x, axis=axis)
return y.swapaxes(0,axis)[::q].swapaxes(0,axis)
```

GNU

Opinions expressed by DZone contributors are their own.

Comments