changeset 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 bd1a5149c53e
children 2ceb734a663f
files scripts/signal/periodogram.m
diffstat 1 files changed, 15 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/signal/periodogram.m	Thu Aug 07 16:12:56 2014 -0700
+++ b/scripts/signal/periodogram.m	Thu Aug 07 20:24:59 2014 -0700
@@ -86,20 +86,18 @@
   endif
 
   nfft = fs = range = window = [];
-  j = 1;
+  j = 2;
   for k = 1:length (varargin)
     if (ischar (varargin{k}))
       range = varargin{k};
     else
       switch (j)
-        case 1
+        case 2
           window = varargin{k};
-        case 2
+        case 3
           nfft   = varargin{k};
-        case 3
+        case 4
           fs     = varargin{k};
-        case 4
-          range  = varargin{k};
       endswitch
       j++;
     endif
@@ -126,6 +124,11 @@
     error ("periodogram: NFFT must be a scalar");
   endif
 
+  use_w_freq = isempty (fs);
+  if (! use_w_freq && ! isscalar (fs))
+    error ("periodogram: FS must be a scalar");
+  endif
+
   if (strcmpi (range, "onesided"))
     range = 1;
   elseif (strcmpi (range, "twosided"))
@@ -152,9 +155,9 @@
   endif;
   Pxx = (abs (fft (x, nfft))) .^ 2 / n;
 
-  if (nargin < 4)
+  if (use_w_freq)
     Pxx /= 2*pi;
-  elseif (! isempty (fs))
+  else
     Pxx /= fs;
   endif
 
@@ -176,15 +179,15 @@
     elseif (range == 2)
       f = (0:nfft-1)' / nfft;
     endif
-    if (nargin < 4)
+    if (use_w_freq)
       f *= 2*pi;  # generate w=2*pi*f
-    elseif (! isempty (fs))
+    else
       f *= fs;
     endif
   endif
 
   if (nargout == 0)
-    if (nargin < 4)
+    if (use_w_freq)
       plot (f/(2*pi), 10*log10 (Pxx));
       xlabel ("normalized frequency [x pi rad]");
       ylabel ("Power density [dB/rad/sample]");
@@ -212,5 +215,6 @@
 %!error <WIN must be a vector.*same length> periodogram (1:5, ones (2,2))
 %!error <WIN must be a vector.*same length> periodogram (1:5, 1:6)
 %!error <NFFT must be a scalar> periodogram (1:5, 1:5, 1:5)
+%!error <FS must be a scalar> periodogram (1:5, [], [], 1:5)
 %!error <"centered" range type is not implemented> periodogram (1:5, "centered")