0
|
1 ## Copyright (C) 2001 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 ## y = interpft (x, n) |
|
18 ## Resample x with n points using Fourier interpolation. |
|
19 ## The data x must be equally spaced and n >= length(x). |
|
20 |
|
21 ## Author: Paul Kienzle |
|
22 ## 2001-02-11 |
|
23 ## * initial version |
|
24 |
|
25 function z = interpft (x, n) |
|
26 if (nargin < 2 || nargin > 3) |
|
27 usage ("y=interpft (x, n)"); |
|
28 endif |
|
29 |
|
30 transpose = ( rows (x) == 1 ); |
|
31 if (transpose) x = x (:); endif |
|
32 [nr, nc] = size (x); |
|
33 if n > nr |
|
34 y = fft (x) / nr; |
|
35 k = floor (nr / 2); |
|
36 z = n * ifft ( [ y(1:k,:); zeros(n-nr,nc); y(k+1:nr) ] ); |
|
37 if isreal (x), z = real (z); endif |
|
38 elseif n < nr |
|
39 error ("interpft: n must be bigger than x"); |
|
40 ## XXX FIXME XXX the following doesn't work so well |
|
41 y = fft (x) / nr; |
|
42 k = ceil (n / 2); |
|
43 z = n * ifft ( [ y(1:k,:); y(nr-k+2:nr) ] ); |
|
44 if isreal (x), z = real (z); endif |
|
45 else |
|
46 z = x; |
|
47 endif |
|
48 if transpose, z = z.'; endif |
|
49 endfunction |
|
50 |
|
51 %!demo |
|
52 %! t = 0 : 0.3 : pi; dt = t(2)-t(1); |
|
53 %! n = length (t); k = 100; |
|
54 %! ti = t(1) + [0 : k-1]*dt*n/k; |
|
55 %! y = sin (4*t + 0.3) .* cos (3*t - 0.1); |
|
56 %! yp = sin (4*ti + 0.3) .* cos (3*ti - 0.1); |
|
57 %! plot (ti, yp, 'g;sin(4t+0.3)cos(3t-0.1);', ... |
|
58 %! ti, interp1 (t, y, ti, 'spline'), 'b;spline;', ... |
|
59 %! ti, interpft (y, k), 'c;interpft;', ... |
|
60 %! t, y, 'r+;data;'); |
|
61 |
|
62 %!#demo |
|
63 %! t = 0:0.3:pi; dt = t(2)-t(1); |
|
64 %! n = length(t); k = 100; |
|
65 %! ti = t(1)+[0:k-1]*dt*n/k; |
|
66 %! y = sin (4*t+0.3).*cos(3*t-0.1); |
|
67 %! yp = sin (4*ti+0.3).*cos(3*ti-0.1); |
|
68 %! plot (ti, yp, 'g;sin(4t+0.3)cos(3t-0.1);', ... |
|
69 %! t, interp1(ti,yp,t,'spline'), 'bx;spline;', ... |
|
70 %! t, interpft(yp,n), 'co;interpft;', ... |
|
71 %! t, y, 'r+;data;'); |