]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/interp.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / interp.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 = interp(x, q [, n [, Wc]])
17 ##
18 ## Upsample the signal x by a factor of q, using an order 2*q*n+1 FIR
19 ## filter. Note that q must be an integer for this rate change method.
20 ## n defaults to 4 and Wc defaults to 0.5.
21 ##
22 ## Example
23 ##                                          # Generate a signal.
24 ##    t=0:0.01:2; x=chirp(t,2,.5,10,'quadratic')+sin(2*pi*t*0.4); 
25 ##    y = interp(x(1:4:length(x)),4,4,1);   # interpolate a sub-sample
26 ##    stem(t(1:121)*1000,x(1:121),"-g;Original;"); hold on;
27 ##    stem(t(1:121)*1000,y(1:121),"-r;Interpolated;");
28 ##    stem(t(1:4:121)*1000,x(1:4:121),"-b;Subsampled;"); hold off;
29 ##
30 ## See also: decimate, resample
31
32 function y = interp(x, q, n = 4, Wc = 0.5)
33
34   if nargin < 1 || nargin > 4, 
35     print_usage;
36   endif
37   if q != fix(q), error("decimate only works with integer q."); endif
38
39   if rows(x)>1
40     y = zeros(length(x)*q+q*n+1,1);
41   else
42     y = zeros(1,length(x)*q+q*n+1);
43   endif
44   y(1:q:length(x)*q) = x;
45   b = fir1(2*q*n+1, Wc/q);
46   y=q*fftfilt(b, y);
47   y(1:q*n+1) = [];  # adjust for zero filter delay
48 endfunction
49
50 %!demo
51 %! ## Generate a signal.
52 %! t=0:0.01:2; x=chirp(t,2,.5,10,'quadratic')+sin(2*pi*t*0.4); 
53 %! y = interp(x(1:4:length(x)),4,4,1);   # interpolate a sub-sample
54 %! plot(t(1:121)*1000,y(1:121),"r-+;Interpolated;"); hold on;
55 %! stem(t(1:4:121)*1000,x(1:4:121),"ob;Original;"); hold off;
56 %!
57 %! % graph shows interpolated signal following through the
58 %! % sample points of the original signal.