Mercurial > forge
comparison main/signal/decimate.m @ 0:6b33357c7561 octave-forge
Initial revision
author | pkienzle |
---|---|
date | Wed, 10 Oct 2001 19:54:49 +0000 |
parents | |
children | a65fbfa12206 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6b33357c7561 |
---|---|
1 ## Copyright (C) 2000 Paul Kienzle | |
2 ## | |
3 ## This program is free software; you can redistribute it and/or modify | |
4 ## it under the terms of the GNU General Public License as published by | |
5 ## the Free Software Foundation; either version 2 of the License, or | |
6 ## (at your option) any later version. | |
7 ## | |
8 ## This program is distributed in the hope that it will be useful, | |
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 ## GNU General Public License for more details. | |
12 ## | |
13 ## You should have received a copy of the GNU General Public License | |
14 ## along with this program; if not, write to the Free Software | |
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | |
17 ## usage: y = decimate(x, q [, n] [, ftype]) | |
18 ## | |
19 ## Downsample the signal x by a factor of q, using an order n filter | |
20 ## of ftype 'fir' or 'iir'. By default, an order 8 Chebyshev type I | |
21 ## filter is used or a 30 point FIR filter if ftype is 'fir'. Note | |
22 ## that q must be an integer for this rate change method. | |
23 ## | |
24 ## Example | |
25 ## ## Generate a signal that starts away from zero, is slowly varying | |
26 ## ## at the start and quickly varying at the end, decimate and plot. | |
27 ## ## Since it starts away from zero, you will see the boundary | |
28 ## ## effects of the antialiasing filter clearly. Next you will see | |
29 ## ## how it follows the curve nicely in the slowly varying early | |
30 ## ## part of the signal, but averages the curve in the quickly | |
31 ## ## varying late part of the signal. | |
32 ## t=0:0.01:2; x=chirp(t,2,.5,10,'quadratic')+sin(2*pi*t*0.4); | |
33 ## y = decimate(x,4); # factor of 4 decimation | |
34 ## stem(t(1:121)*1000,x(1:121),"-g;Original;"); hold on; # plot original | |
35 ## stem(t(1:4:121)*1000,y(1:31),"-r;Decimated;"); hold off; # decimated | |
36 | |
37 function y = decimate(x, q, n, ftype) | |
38 | |
39 if nargin < 1 || nargin > 4, | |
40 usage("y=decimate(x, q [, n] [, ftype])"); | |
41 endif | |
42 if q != fix(q), error("decimate only works with integer q."); endif | |
43 | |
44 if nargin<3 | |
45 ftype='iir'; | |
46 n=[]; | |
47 elseif nargin==3 | |
48 if isstr(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, 1/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. |