5837
|
1 ## Copyright (C) 2001 Paul Kienzle |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} interpft (@var{x}, @var{n}) |
|
22 ## @deftypefnx {Function File} {} interpft (@var{x}, @var{n}, @var{dim}) |
|
23 ## |
|
24 ## Fourier interpolation. If @var{x} is a vector, then @var{x} is |
|
25 ## resampled with @var{n} points. The data in @var{x} is assumed to be |
|
26 ## equispaced. If @var{x} is an array, then operate along each column of |
|
27 ## the array seperately. If @var{dim} is specified, then interpolate |
|
28 ## along the dimension @var{dim}. |
|
29 ## |
|
30 ## @code{interpft} assumes that the interpolated function is periodic, |
|
31 ## and so assumption are made about the end points of the inetrpolation. |
|
32 ## |
|
33 ## @seealso{interp1} |
|
34 ## @end deftypefn |
|
35 |
|
36 ## Author: Paul Kienzle |
|
37 ## 2001-02-11 |
|
38 ## * initial version |
|
39 ## 2002-03-17 aadler |
|
40 ## * added code to work on matrices as well |
|
41 ## 2006-05-25 dbateman |
|
42 ## * Make it matlab compatiable, cutting out the 2-D interpolation |
|
43 |
|
44 function z = interpft (x, n, dim) |
|
45 |
|
46 if (nargin < 2 || nargin > 3) |
|
47 print_usage (); |
|
48 endif |
|
49 |
|
50 if (nargin == 2) |
|
51 if (isvector(x) && size(x,1) == 1) |
|
52 dim = 2; |
|
53 else |
|
54 dim = 1; |
|
55 endif |
|
56 endif |
|
57 |
|
58 if (!isscalar (n)) |
|
59 error ("interpft: n must be an integer scalar"); |
|
60 endif |
|
61 |
|
62 nd = ndims(x); |
|
63 |
|
64 if (dim < 1 || dim > nd) |
|
65 error ("interpft: integrating over invalid dimension"); |
|
66 endif |
|
67 |
|
68 perm = [dim:nd,1:(dim-1)]; |
|
69 x = permute(x, perm); |
|
70 m = size (x, 1); |
|
71 |
|
72 inc = 1; |
|
73 while (inc * n < m) |
|
74 inc++; |
|
75 endwhile |
|
76 y = fft (x) / m; |
|
77 k = floor (m / 2); |
|
78 sz = size(x); |
|
79 sz(1) = n * inc - m; |
|
80 idx = cell(nd,1); |
|
81 for i = 2:nd |
|
82 idx{i} = 1:sz(i); |
|
83 endfor |
|
84 idx{1} = 1:k; |
|
85 z = cat (1, y(idx{:}), zeros(sz)); |
|
86 idx{1} = k+1:m; |
|
87 z = cat (1, z, y(idx{:})); |
|
88 z = n * ifft (z); |
|
89 |
|
90 if (inc != 1) |
|
91 sz(1) = n; |
|
92 z = inc * reshape(z(1:inc:end),sz); |
|
93 endif |
|
94 |
|
95 z = ipermute (z, perm); |
|
96 endfunction |
|
97 |
|
98 %!demo |
|
99 %! t = 0 : 0.3 : pi; dt = t(2)-t(1); |
|
100 %! n = length (t); k = 100; |
|
101 %! ti = t(1) + [0 : k-1]*dt*n/k; |
|
102 %! y = sin (4*t + 0.3) .* cos (3*t - 0.1); |
|
103 %! yp = sin (4*ti + 0.3) .* cos (3*ti - 0.1); |
|
104 %! plot (ti, yp, 'g;sin(4t+0.3)cos(3t-0.1);', ... |
|
105 %! ti, interp1 (t, y, ti, 'spline'), 'b;spline;', ... |
|
106 %! ti, interpft (y ,k), 'c;interpft;', ... |
|
107 %! t, y, 'r+;data;'); |
|
108 |
|
109 %!shared n,y |
|
110 %! x = [0:10]'; y = sin(x); n = length (x); |
|
111 %!assert (interpft(y, n), y, eps); |
|
112 %!assert (interpft(y', n), y', eps); |
|
113 %!assert (interpft([y,y],n), [y,y], eps); |
|
114 |
|
115 %!error (interpft(y,n,0)) |
|
116 %!error (interpft(y,[n,n])) |