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 = interp(x, q [, n [, Wc]])
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.
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;
30 ## See also: decimate, resample
32 function y = interp(x, q, n = 4, Wc = 0.5)
34 if nargin < 1 || nargin > 4,
37 if q != fix(q), error("decimate only works with integer q."); endif
40 y = zeros(length(x)*q+q*n+1,1);
42 y = zeros(1,length(x)*q+q*n+1);
44 y(1:q:length(x)*q) = x;
45 b = fir1(2*q*n+1, Wc/q);
47 y(1:q*n+1) = []; # adjust for zero filter delay
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;
57 %! % graph shows interpolated signal following through the
58 %! % sample points of the original signal.