1 ## Copyright (C) 2000 Paul Kienzle <pkienzle@users.sf.net>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
16 ## usage: y = decimate(x, q [, n] [, ftype])
18 ## Downsample the signal x by a factor of q, using an order n filter
19 ## of ftype 'fir' or 'iir'. By default, an order 8 Chebyshev type I
20 ## filter is used or a 30 point FIR filter if ftype is 'fir'. Note
21 ## that q must be an integer for this rate change method.
24 ## ## Generate a signal that starts away from zero, is slowly varying
25 ## ## at the start and quickly varying at the end, decimate and plot.
26 ## ## Since it starts away from zero, you will see the boundary
27 ## ## effects of the antialiasing filter clearly. Next you will see
28 ## ## how it follows the curve nicely in the slowly varying early
29 ## ## part of the signal, but averages the curve in the quickly
30 ## ## varying late part of the signal.
31 ## t=0:0.01:2; x=chirp(t,2,.5,10,'quadratic')+sin(2*pi*t*0.4);
32 ## y = decimate(x,4); # factor of 4 decimation
33 ## stem(t(1:121)*1000,x(1:121),"-g;Original;"); hold on; # plot original
34 ## stem(t(1:4:121)*1000,y(1:31),"-r;Decimated;"); hold off; # decimated
36 function y = decimate(x, q, n, ftype)
38 if nargin < 1 || nargin > 4
41 error("decimate only works with integer q.");
56 fir = strcmp(ftype, 'fir');
58 if fir, n=30; else n=8; endif
65 [b, a] = cheby1(n, 0.05, 0.8/q);
72 %! t=0:0.01:2; x=chirp(t,2,.5,10,'quadratic')+sin(2*pi*t*0.4);
73 %! y = decimate(x,4); # factor of 4 decimation
74 %! stem(t(1:121)*1000,x(1:121),"-g;Original;"); hold on; # plot original
75 %! stem(t(1:4:121)*1000,y(1:31),"-r;Decimated;"); hold off; # decimated
76 %! %------------------------------------------------------------------
77 %! % The signal to decimate starts away from zero, is slowly varying
78 %! % at the start and quickly varying at the end, decimate and plot.
79 %! % Since it starts away from zero, you will see the boundary
80 %! % effects of the antialiasing filter clearly. You will also see
81 %! % how it follows the curve nicely in the slowly varying early
82 %! % part of the signal, but averages the curve in the quickly
83 %! % varying late part of the signal.