annotate scripts/signal/periodogram.m @ 18998:956fc864c39f

periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859). * periodogram.m: Don't use nargin to decide when normalized frequencies should be used. Instead check whether Fs has been specified to make the decision.
author Rik <rik@octave.org>
date Thu, 07 Aug 2014 20:24:59 -0700
parents 8ac4ab4ae5f4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17338
diff changeset
1 ## Copyright (C) 1995-2013 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
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
3 ## Copyright (C) 2014 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
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
9 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
10 ## 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
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3238
diff changeset
15 ## General Public License for more details.
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 -*-
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
22 ## @deftypefn {Function File} {[@var{Pxx}, @var{w}] =} periodogram (@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
23 ## @deftypefnx {Function File} {[@var{Pxx}, @var{w}] =} periodogram (@var{x}, @var{win})
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
24 ## @deftypefnx {Function File} {[@var{Pxx}, @var{w}] =} periodogram (@var{x}, @var{win}, @var{nfft})
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
25 ## @deftypefnx {Function File} {[@var{Pxx}, @var{f}] =} periodogram (@var{x}, @var{win}, @var{nfft}, @var{Fs})
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
26 ## @deftypefnx {Function File} {[@var{Pxx}, @var{f}] =} periodogram (@dots{}, "@var{range}")
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
27 ## @deftypefnx {Function File} {} periodogram (@dots{})
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
28 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
29 ## 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
30 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
31 ## The possible inputs are:
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
32 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
33 ## @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
34 ## @item x
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
35 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
36 ## 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
37 ## 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
38 ## @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
39 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
40 ## @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
41 ## 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
42 ## 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
43 ## (@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
44 ## 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
45 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
46 ## @item nfft
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
47 ## number of frequency bins. The default is 256 or the next higher power of
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
48 ## 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
49 ## (@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
50 ## 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
51 ## of @var{nfft}.
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
52 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
53 ## @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
54 ## sampling rate. The default is 1.
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
55 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
56 ## @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
57 ## range of spectrum. @qcode{"@nospell{onesided}"} computes spectrum from
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/2+1]. @qcode{"@nospell{twosided}"} computes spectrum from
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
59 ## [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
60 ## @end table
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
61 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
62 ## The optional second output @var{w} are the normalized angular frequencies.
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
63 ## For a onesided calculation @var{w} is in the range [0, pi] if @var{nfft}
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
64 ## is even and [0, pi) if @var{nfft} is odd. Similarly, for a twosided
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
65 ## 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
66 ## @var{nfft}.
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
67 ##
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
68 ## 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
69 ## @var{f} will be in the range [0, @var{Fs}/2] or [0, @var{Fs}/2) for
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
70 ## onesided calculations. For twosided calculations the range will be
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
71 ## [0, @var{Fs}).
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
72 ##
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
73 ## 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
74 ## 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
75 ## @seealso{fft}
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
76 ## @end deftypefn
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3238
diff changeset
77
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
78 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at>
3457
e031284eea27 [project @ 2000-01-19 08:49:56 by jwe]
jwe
parents: 3449
diff changeset
79 ## Description: Compute the periodogram
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
80
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
81 function [pxx, f] = periodogram (x, varargin)
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
82
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
83 ## check input arguments
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
84 if (nargin < 1 || nargin > 5)
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
85 print_usage ();
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
86 endif
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
87
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
88 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
89 j = 2;
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
90 for k = 1:length (varargin)
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
91 if (ischar (varargin{k}))
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
92 range = varargin{k};
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
93 else
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
94 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
95 case 2
17174
c3c1ebfaa7dc maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents: 16826
diff changeset
96 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
97 case 3
17174
c3c1ebfaa7dc maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents: 16826
diff changeset
98 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
99 case 4
17174
c3c1ebfaa7dc maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents: 16826
diff changeset
100 fs = varargin{k};
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
101 endswitch
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
102 j++;
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
103 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
104 endfor
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
105
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
106 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
107 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
108 endif
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
109 x = x(:); # Use column vectors from now on
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
110
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
111 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
112
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 (! 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
114 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
115 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
116 endif
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
117 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
118 x .*= window;
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
119 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3238
diff changeset
120
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
121 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
122 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
123 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
124 error ("periodogram: NFFT must be a scalar");
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
125 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
126
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
127 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
128 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
129 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
130 endif
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
131
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
132 if (strcmpi (range, "onesided"))
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
133 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
134 elseif (strcmpi (range, "twosided"))
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
135 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
136 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
137 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
138 else
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
139 range = 2-isreal (x);
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
140 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
141
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
142 ## compute periodogram
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
143
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
144 if (n > nfft)
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
145 Pxx = 0;
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
146 rr = rem (length (x), nfft);
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
147 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
148 x = [x(:); zeros(nfft-rr, 1)];
11113
a8ac114ec9ab Stylefixes, replace end by endif.
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 11087
diff changeset
149 endif
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
150 x = sum (reshape (x, nfft, []), 2);
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
151 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
152
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
153 if (! isempty (window))
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
154 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
155 endif;
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
156 Pxx = (abs (fft (x, nfft))) .^ 2 / n;
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
157
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
158 if (use_w_freq)
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
159 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
160 else
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
161 Pxx /= fs;
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
162 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
163
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
164 ## generate output arguments
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
165
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
166 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
167 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
168 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
169 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
170 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
171 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
172 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
173 endif
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
174 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
175
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
176 if (nargout != 1)
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
177 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
178 f = (0:nfft/2)' / nfft;
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
179 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
180 f = (0:nfft-1)' / nfft;
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
181 endif
18998
956fc864c39f periodogram.m: Fix bug when nfft < length (x) and "onesided" specified (bug #42859).
Rik <rik@octave.org>
parents: 18995
diff changeset
182 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
183 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
184 else
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
185 f *= fs;
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 endif
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
188
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
189 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
190 if (use_w_freq)
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
191 plot (f/(2*pi), 10*log10 (Pxx));
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
192 xlabel ("normalized frequency [x pi rad]");
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
193 ylabel ("Power density [dB/rad/sample]");
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
194 else
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
195 plot (f, 10*log10 (Pxx));
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
196 xlabel ("frequency [Hz]");
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
197 ylabel ("Power density [dB/Hz]");
11113
a8ac114ec9ab Stylefixes, replace end by endif.
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 11087
diff changeset
198 endif
10984
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
199 grid on;
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
200 title ("Periodogram Power Spectral Density Estimate");
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
201 else
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
202 pxx = Pxx;
58c57161626d extend periodogram, thanks to Alois Schloegl
Jaroslav Hajek <highegg@gmail.com>
parents: 7125
diff changeset
203 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3238
diff changeset
204
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
205 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17281
diff changeset
206
18995
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
207
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
208 ## 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
209
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
210
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
211 %% Test input validation
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 ()
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 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
214 %!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
215 %!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
216 %!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
217 %!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
218 %!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
219 %!error <"centered" range type is not implemented> periodogram (1:5, "centered")
8ac4ab4ae5f4 periodogram.m: Overhaul function (bug #39279, bug #42859).
Drew Abbot <drewabbot@gmail.com> and Rik <rik@octave.org>
parents: 17744
diff changeset
220