Mercurial > octave
annotate scripts/general/interpft.m @ 31198:863730dd0f83 stable
nextpow2: Fix for input between 0.5 and 1 (bug #62947).
* scripts/general/nextpow2.m: Switch to a naïve implementation using log2 with
a single output argument and ceil.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Wed, 24 Aug 2022 17:15:34 +0200 |
parents | 796f54d4ddbf |
children | 5d3faba0342e |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
3 ## Copyright (C) 2001-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
5837 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
5837 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
13 ## (at your option) any later version. |
5837 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
18 ## GNU General Public License for more details. |
5837 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
5837 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
27 ## @deftypefn {} {} interpft (@var{x}, @var{n}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
28 ## @deftypefnx {} {} interpft (@var{x}, @var{n}, @var{dim}) |
5837 | 29 ## |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
30 ## Fourier interpolation. |
5837 | 31 ## |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
32 ## If @var{x} is a vector then @var{x} is resampled with @var{n} points. The |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
33 ## data in @var{x} is assumed to be equispaced. If @var{x} is a matrix or an |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
34 ## N-dimensional array, the interpolation is performed on each column of |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
35 ## @var{x}. |
5837 | 36 ## |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
37 ## If @var{dim} is specified, then interpolate along the dimension @var{dim}. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
38 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
39 ## @code{interpft} assumes that the interpolated function is periodic, and so |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
40 ## assumptions are made about the endpoints of the interpolation. |
5837 | 41 ## @seealso{interp1} |
42 ## @end deftypefn | |
43 | |
44 function z = interpft (x, n, dim) | |
45 | |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
46 if (nargin < 2) |
5837 | 47 print_usage (); |
48 endif | |
49 | |
13768
5f96b91b4e0c
interpft.m: Miscellaneous code cleanup. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
50 if (! (isscalar (n) && n == fix (n))) |
5f96b91b4e0c
interpft.m: Miscellaneous code cleanup. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
51 error ("interpft: N must be a scalar integer"); |
5f96b91b4e0c
interpft.m: Miscellaneous code cleanup. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
52 endif |
5f96b91b4e0c
interpft.m: Miscellaneous code cleanup. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
53 |
5837 | 54 if (nargin == 2) |
13768
5f96b91b4e0c
interpft.m: Miscellaneous code cleanup. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
55 if (isrow (x)) |
5837 | 56 dim = 2; |
57 else | |
58 dim = 1; | |
59 endif | |
60 endif | |
61 | |
5838 | 62 nd = ndims (x); |
5837 | 63 |
64 if (dim < 1 || dim > nd) | |
13768
5f96b91b4e0c
interpft.m: Miscellaneous code cleanup. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
65 error ("interpft: invalid dimension DIM"); |
5837 | 66 endif |
67 | |
5838 | 68 perm = [dim:nd, 1:(dim-1)]; |
69 x = permute (x, perm); | |
13768
5f96b91b4e0c
interpft.m: Miscellaneous code cleanup. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
70 m = rows (x); |
5837 | 71 |
17089
3cbbdb49b8ff
interpft.m: Fix decimation by a non-integer factor (bug #39017)
Mike Miller <mtmiller@ieee.org>
parents:
17087
diff
changeset
|
72 inc = ceil (m/n); |
5837 | 73 y = fft (x) / m; |
17173
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
74 k = ceil (m / 2); |
5838 | 75 sz = size (x); |
5837 | 76 sz(1) = n * inc - m; |
12676
2783fa95cab7
Use common code idiom for creating cell array for indexing ND-arrays
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
77 |
2783fa95cab7
Use common code idiom for creating cell array for indexing ND-arrays
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
78 idx = repmat ({':'}, nd, 1); |
5837 | 79 idx{1} = 1:k; |
5838 | 80 z = cat (1, y(idx{:}), zeros (sz)); |
5837 | 81 idx{1} = k+1:m; |
82 z = cat (1, z, y(idx{:})); | |
17173
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
83 |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
84 ## When m is an even number of rows, the FFT has a single Nyquist bin. |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
85 ## If zero-padded above, distribute the value of the Nyquist bin evenly |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
86 ## between the new corresponding positive and negative frequency bins. |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
87 if (sz(1) > 0 && k == m/2) |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
88 idx{1} = n * inc - k + 1; |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
89 tmp = z(idx{:}) / 2; |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
90 z(idx{:}) = tmp; |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
91 idx{1} = k + 1; |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
92 z(idx{:}) = tmp; |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
93 endif |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
94 |
5837 | 95 z = n * ifft (z); |
96 | |
97 if (inc != 1) | |
98 sz(1) = n; | |
5838 | 99 z = inc * reshape (z(1:inc:end), sz); |
5837 | 100 endif |
101 | |
102 z = ipermute (z, perm); | |
13768
5f96b91b4e0c
interpft.m: Miscellaneous code cleanup. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
103 |
5837 | 104 endfunction |
105 | |
13768
5f96b91b4e0c
interpft.m: Miscellaneous code cleanup. Add more input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
106 |
5837 | 107 %!demo |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
108 %! clf; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
109 %! t = 0 : 0.3 : pi; dt = t(2)-t(1); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
110 %! n = length (t); k = 100; |
5837 | 111 %! ti = t(1) + [0 : k-1]*dt*n/k; |
112 %! y = sin (4*t + 0.3) .* cos (3*t - 0.1); | |
113 %! yp = sin (4*ti + 0.3) .* cos (3*ti - 0.1); | |
28912
0de38a6ef693
maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents:
28886
diff
changeset
|
114 %! plot (ti, yp, 'g', ti, interp1 (t, y, ti, "spline"), 'b', ... |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
115 %! ti, interpft (y, k), 'c', t, y, "r+"); |
28912
0de38a6ef693
maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents:
28886
diff
changeset
|
116 %! legend ("sin (4t+0.3)cos (3t-0.1)", "spline", "interpft", "data"); |
5837 | 117 |
118 %!shared n,y | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
119 %! x = [0:10]'; y = sin(x); n = length (x); |
25586 | 120 %!testif HAVE_FFTW |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28912
diff
changeset
|
121 %! assert (interpft (y, n), y, 20*eps); |
25586 | 122 %!testif HAVE_FFTW |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28912
diff
changeset
|
123 %! assert (interpft (y', n), y', 20*eps); |
25586 | 124 %!testif HAVE_FFTW |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28912
diff
changeset
|
125 %! assert (interpft ([y,y],n), [y,y], 20*eps); |
5837 | 126 |
22489
93ea313301f9
test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
127 ## Test case with complex input |
25586 | 128 %!testif HAVE_FFTW <*39566> |
17173
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
129 %! x = (1 + j) * [1:4]'; |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
130 %! y = ifft ([15 + 15*j; -6; -1.5 - 1.5*j; 0; -1.5 - 1.5*j; -6*j]); |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
131 %! assert (interpft (x, 6), y, 10*eps); |
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
132 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
133 ## Test for correct spectral symmetry with even/odd lengths |
25586 | 134 %!testif HAVE_FFTW |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28912
diff
changeset
|
135 %! assert (max (abs (imag (interpft ([1:8], 20)))), 0, 20*eps); |
25586 | 136 %!testif HAVE_FFTW |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28912
diff
changeset
|
137 %! assert (max (abs (imag (interpft ([1:8], 21)))), 0, 21*eps); |
25586 | 138 %!testif HAVE_FFTW |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28912
diff
changeset
|
139 %! assert (max (abs (imag (interpft ([1:9], 20)))), 0, 20*eps); |
25586 | 140 %!testif HAVE_FFTW |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28912
diff
changeset
|
141 %! assert (max (abs (imag (interpft ([1:9], 21)))), 0, 21*eps); |
17173
3a23cbde59d5
interpft.m: Fix interpolation to preserve spectral symmetry (bug #39566)
Mike Miller <mtmiller@ieee.org>
parents:
17089
diff
changeset
|
142 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
143 ## Test input validation |
28886
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
144 %!error <Invalid call> interpft () |
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
145 %!error <Invalid call> interpft (1) |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
146 %!error <N must be a scalar integer> interpft (1,[2,2]) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
147 %!error <N must be a scalar integer> interpft (1,2.1) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
148 %!error <invalid dimension DIM> interpft (1,2,0) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
149 %!error <invalid dimension DIM> interpft (1,2,3) |