Mercurial > octave-antonio
annotate scripts/signal/periodogram.m @ 19152:0f9c5a15c8fa
doc: Periodic grammarcheck of documentation.
* doc/interpreter/contrib.txi, doc/interpreter/expr.txi,
doc/interpreter/func.txi, doc/interpreter/linalg.txi, doc/interpreter/plot.txi,
libinterp/corefcn/data.cc, libinterp/corefcn/debug.cc,
libinterp/corefcn/graphics.cc, libinterp/corefcn/help.cc,
libinterp/corefcn/load-save.cc, libinterp/corefcn/profiler.cc,
libinterp/corefcn/syscalls.cc, libinterp/corefcn/utils.cc,
libinterp/corefcn/variables.cc, libinterp/dldfcn/__init_fltk__.cc,
scripts/general/deal.m, scripts/help/__gripe_missing_component__.m,
scripts/miscellaneous/desktop.m, scripts/pkg/private/default_prefix.m,
scripts/plot/appearance/__getlegenddata__.m, scripts/plot/util/pan.m,
scripts/plot/util/rotate.m, scripts/plot/util/rotate3d.m,
scripts/plot/util/zoom.m, scripts/signal/periodogram.m,
scripts/specfun/factor.m, scripts/specfun/primes.m,
scripts/statistics/base/lscov.m, scripts/testfun/private/compare_plot_demos.m,
scripts/testfun/private/dump_demos.m,
scripts/testfun/private/html_compare_plot_demos.m, scripts/testfun/test.m:
Periodic grammarcheck of documentation.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 22 Sep 2014 20:46:54 -0700 |
parents | 9e3c35d934ab |
children | db92e7e28e1f |
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 | 4 ## |
3922 | 5 ## This file is part of Octave. |
6 ## | |
7 ## Octave is free software; you can redistribute it and/or modify it | |
8 ## under the terms of the GNU General Public License as published by | |
7016 | 9 ## the Free Software Foundation; either version 3 of the License, or (at |
10 ## your option) any later version. | |
3426 | 11 ## |
3922 | 12 ## Octave is distributed in the hope that it will be useful, but |
3191 | 13 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 15 ## General Public License for more details. |
16 ## | |
3191 | 17 ## You should have received a copy of the GNU General Public License |
7016 | 18 ## along with Octave; see the file COPYING. If not, see |
19 ## <http://www.gnu.org/licenses/>. | |
3191 | 20 |
3449 | 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 |
19152
0f9c5a15c8fa
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19038
diff
changeset
|
58 ## [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
|
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. |
19038
9e3c35d934ab
doc: Periodic spell check of documentation.
Rik <rik@octave.org>
parents:
18998
diff
changeset
|
63 ## 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
|
64 ## 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
|
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 |
19038
9e3c35d934ab
doc: Periodic spell check of documentation.
Rik <rik@octave.org>
parents:
18998
diff
changeset
|
70 ## 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
|
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 | 76 ## @end deftypefn |
3426 | 77 |
3191 | 78 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at> |
3457 | 79 ## Description: Compute the periodogram |
3191 | 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 | 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 | 85 print_usage (); |
86 endif | |
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 | 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 | 119 endif |
3426 | 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 | 204 |
3191 | 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 |