]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/decimate.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / decimate.m
1 ## Copyright (C) 2000 Paul Kienzle <pkienzle@users.sf.net>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## usage: y = decimate(x, q [, n] [, ftype])
17 ##
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.
22 ##
23 ## Example
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
35
36 function y = decimate(x, q, n, ftype)
37
38   if nargin < 1 || nargin > 4
39     print_usage;
40   elseif q != fix(q)
41     error("decimate only works with integer q.");
42   endif
43
44   if nargin<3
45     ftype='iir';
46     n=[];
47   elseif nargin==3
48     if ischar(n)
49       ftype=n; 
50       n=[];
51     else 
52       ftype='iir';
53     endif
54   endif
55
56   fir = strcmp(ftype, 'fir');
57   if isempty(n)
58     if fir, n=30; else n=8; endif
59   endif
60
61   if fir
62     b = fir1(n, 1/q);
63     y=fftfilt(b, x);
64   else
65     [b, a] = cheby1(n, 0.05, 0.8/q);
66     y=filtfilt(b,a,x);
67   endif
68   y = y(1:q:length(x));
69 endfunction
70
71 %!demo
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.