annotate scripts/signal/periodogram.m @ 23220:092078913d54

maint: Merge stable to default.
author John W. Eaton <jwe@octave.org>
date Wed, 22 Feb 2017 12:58:07 -0500
parents ef4d915df748 3ac9f9ecfae5
children 194eb4bd202b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23219
3ac9f9ecfae5 maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents: 23083
diff changeset
1 ## Copyright (C) 1995-2017 Friedrich Leisch
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
2 ## Copyright (C) 2010 Alois Schloegl
22323
bac0d6f07a3e maint: Update copyright notices for 2016.
John W. Eaton <jwe@octave.org>
parents: 20852
diff changeset
3 ## Copyright (C) 2014-2016 Drew Abbot
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3238
diff changeset
4 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
5 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
6 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
7 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
8 ## under the terms of the GNU General Public License as published by
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
9 ## the Free Software Foundation; either version 3 of the License, or
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
10 ## (at your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3238
diff changeset
11 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
12 ## Octave is distributed in the hope that it will be useful, but
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
15 ## GNU General Public License for more details.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3238
diff changeset
16 ##
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
17 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
18 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
19 ## <http://www.gnu.org/licenses/>.
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
20
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
21 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
22 ## @deftypefn {} {[@var{Pxx}, @var{w}] =} periodogram (@var{x})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
23 ## @deftypefnx {} {[@var{Pxx}, @var{w}] =} periodogram (@var{x}, @var{win})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
24 ## @deftypefnx {} {[@var{Pxx}, @var{w}] =} periodogram (@var{x}, @var{win}, @var{nfft})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
25 ## @deftypefnx {} {[@var{Pxx}, @var{f}] =} periodogram (@var{x}, @var{win}, @var{nfft}, @var{Fs})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
26 ## @deftypefnx {} {[@var{Pxx}, @var{f}] =} periodogram (@dots{}, "@var{range}")
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
27 ## @deftypefnx {} {} periodogram (@dots{})
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
28 ## Return the periodogram (Power Spectral Density) of @var{x}.
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
29 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
30 ## The possible inputs are:
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
31 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
32 ## @table @var
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
33 ## @item x
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
34 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
35 ## data vector. If @var{x} is real-valued a one-sided spectrum is estimated.
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
36 ## If @var{x} is complex-valued, or @qcode{"@var{range}"} specifies
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
37 ## @qcode{"@nospell{twosided}"}, the full spectrum is estimated.
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
38 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
39 ## @item win
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
40 ## window weight data. If window is empty or unspecified a default rectangular
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
41 ## window is used. Otherwise, the window is applied to the signal
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
42 ## (@code{@var{x} .* @var{win}}) before computing the periodogram. The window
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
43 ## data must be a vector of the same length as @var{x}.
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
44 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
45 ## @item nfft
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19152
diff changeset
46 ## number of frequency bins. The default is 256 or the next higher power of
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
47 ## 2 greater than the length of @var{x}
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
48 ## (@code{max (256, 2.^nextpow2 (length (x)))}). If @var{nfft} is greater
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
49 ## than the length of the input then @var{x} will be zero-padded to the length
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
50 ## of @var{nfft}.
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
51 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
52 ## @item Fs
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
53 ## sampling rate. The default is 1.
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
54 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
55 ## @item range
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
56 ## range of spectrum. @qcode{"@nospell{onesided}"} computes spectrum from
19152
0f9c5a15c8fa doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 19038
diff changeset
57 ## [0..nfft/2+1]. @qcode{"@nospell{twosided}"} computes spectrum from
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
58 ## [0..nfft-1].
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
59 ## @end table
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
60 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
61 ## The optional second output @var{w} are the normalized angular frequencies.
19038
9e3c35d934ab doc: Periodic spell check of documentation.
Rik <rik@octave.org>
parents: 18998
diff changeset
62 ## For a one-sided calculation @var{w} is in the range [0, pi] if @var{nfft}
9e3c35d934ab doc: Periodic spell check of documentation.
Rik <rik@octave.org>
parents: 18998
diff changeset
63 ## is even and [0, pi) if @var{nfft} is odd. Similarly, for a two-sided
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
64 ## calculation @var{w} is in the range [0, 2*pi] or [0, 2*pi) depending on
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
65 ## @var{nfft}.
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
66 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
67 ## If a sampling frequency is specified, @var{Fs}, then the output frequencies
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
68 ## @var{f} will be in the range [0, @var{Fs}/2] or [0, @var{Fs}/2) for
19038
9e3c35d934ab doc: Periodic spell check of documentation.
Rik <rik@octave.org>
parents: 18998
diff changeset
69 ## one-sided calculations. For two-sided calculations the range will be
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
70 ## [0, @var{Fs}).
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19152
diff changeset
71 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
72 ## When called with no outputs the periodogram is immediately plotted in the
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
73 ## current figure window.
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
74 ## @seealso{fft}
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
75 ## @end deftypefn
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3238
diff changeset
76
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
77 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at>
3457
e031284eea27 [project @ 2000-01-19 08:49:56 by jwe]
jwe
parents: 3449
diff changeset
78 ## Description: Compute the periodogram
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
79
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
80 function [pxx, f] = periodogram (x, varargin)
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
81
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
82 ## check input arguments
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
83 if (nargin < 1 || nargin > 5)
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
84 print_usage ();
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
85 endif
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
86
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
87 nfft = fs = range = window = [];
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
88 j = 2;
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
89 for k = 1:length (varargin)
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
90 if (ischar (varargin{k}))
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
91 range = varargin{k};
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
92 else
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
93 switch (j)
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
94 case 2
17174
c3c1ebfaa7dc maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents: 16826
diff changeset
95 window = varargin{k};
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
96 case 3
17174
c3c1ebfaa7dc maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents: 16826
diff changeset
97 nfft = varargin{k};
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
98 case 4
17174
c3c1ebfaa7dc maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents: 16826
diff changeset
99 fs = varargin{k};
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
100 endswitch
20735
418ae0cb752f Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents: 20165
diff changeset
101 j += 1;
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
102 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
103 endfor
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
104
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
105 if (! isvector (x))
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
106 error ("periodogram: X must be a real or complex vector");
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
107 endif
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
108 x = x(:); # Use column vectors from now on
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19152
diff changeset
109
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
110 n = rows (x);
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
111
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
112 if (! isempty (window))
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
113 if (! isvector (window) || length (window) != n)
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
114 error ("periodogram: WIN must be a vector of the same length as X");
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
115 endif
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
116 window = window(:);
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
117 x .*= window;
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
118 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3238
diff changeset
119
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
120 if (isempty (nfft))
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
121 nfft = max (256, 2.^nextpow2 (n));
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
122 elseif (! isscalar (nfft))
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
123 error ("periodogram: NFFT must be a scalar");
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
124 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
125
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
126 use_w_freq = isempty (fs);
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
127 if (! use_w_freq && ! isscalar (fs))
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
128 error ("periodogram: FS must be a scalar");
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
129 endif
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
130
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
131 if (strcmpi (range, "onesided"))
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
132 range = 1;
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
133 elseif (strcmpi (range, "twosided"))
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
134 range = 2;
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
135 elseif (strcmpi (range, "centered"))
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
136 error ('periodogram: "centered" range type is not implemented');
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
137 else
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
138 range = 2-isreal (x);
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
139 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
140
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
141 ## compute periodogram
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
142
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
143 if (n > nfft)
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
144 Pxx = 0;
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
145 rr = rem (length (x), nfft);
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
146 if (rr)
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
147 x = [x(:); zeros(nfft-rr, 1)];
11113
a8ac114ec9ab Stylefixes, replace end by endif.
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 11087
diff changeset
148 endif
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
149 x = sum (reshape (x, nfft, []), 2);
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
150 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
151
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
152 if (! isempty (window))
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
153 n = sumsq (window);
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
154 endif;
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
155 Pxx = (abs (fft (x, nfft))) .^ 2 / n;
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
156
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
157 if (use_w_freq)
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
158 Pxx /= 2*pi;
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
159 else
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
160 Pxx /= fs;
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
161 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
162
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
163 ## generate output arguments
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
164
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
165 if (range == 1) # onesided
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
166 if (! rem (nfft,2)) # nfft is even
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
167 psd_len = nfft/2+1;
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
168 Pxx = Pxx(1:psd_len) + [0; Pxx(nfft:-1:psd_len+1); 0];
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
169 else # nfft is odd
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
170 psd_len = (nfft+1)/2;
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
171 Pxx = Pxx(1:psd_len) + [0; Pxx(nfft:-1:psd_len+1)];
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
172 endif
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
173 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
174
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
175 if (nargout != 1)
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
176 if (range == 1)
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
177 f = (0:nfft/2)' / nfft;
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
178 elseif (range == 2)
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
179 f = (0:nfft-1)' / nfft;
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
180 endif
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
181 if (use_w_freq)
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
182 f *= 2*pi; # generate w=2*pi*f
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
183 else
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
184 f *= fs;
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
185 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
186 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
187
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
188 if (nargout == 0)
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
189 if (use_w_freq)
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
190 plot (f/(2*pi), 10*log10 (Pxx));
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
191 xlabel ("normalized frequency [x pi rad]");
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
192 ylabel ("Power density [dB/rad/sample]");
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
193 else
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
194 plot (f, 10*log10 (Pxx));
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
195 xlabel ("frequency [Hz]");
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
196 ylabel ("Power density [dB/Hz]");
11113
a8ac114ec9ab Stylefixes, replace end by endif.
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 11087
diff changeset
197 endif
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
198 grid on;
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
199 title ("Periodogram Power Spectral Density Estimate");
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
200 else
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
201 pxx = Pxx;
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
202 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3238
diff changeset
203
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
204 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17281
diff changeset
205
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
206
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
207 ## FIXME: Need some functional tests
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
208
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
209
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
210 ## Test input validation
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
211 %!error periodogram ()
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
212 %!error periodogram (1,2,3,4,5,6)
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
213 %!error <X must be a real or complex vector> periodogram (ones (2,2))
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
214 %!error <WIN must be a vector.*same length> periodogram (1:5, ones (2,2))
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
215 %!error <WIN must be a vector.*same length> periodogram (1:5, 1:6)
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
216 %!error <NFFT must be a scalar> periodogram (1:5, 1:5, 1:5)
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
217 %!error <FS must be a scalar> periodogram (1:5, [], [], 1:5)
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
218 %!error <"centered" range type is not implemented> periodogram (1:5, "centered")