annotate scripts/signal/freqz_plot.m @ 18450:694e8f0b3862 stable

freqz: Make frequency response plot visually compatible with Matlab (bug #41464) * freqz_plot.m: Eliminate the extraneous "pass band" subplot for visual compatibility with Matlab. Use y-axis labels instead of legends. Always autoscale the y-axis. Label the frequency axis in units of normalized radians or Hz depending on a new optional argument. * freqz.m: Tell freqz_plot whether the frequency vector is in normalized radians or Hz.
author Mike Miller <mtmiller@ieee.org>
date Thu, 13 Feb 2014 00:00:10 -0500
parents d63878346099
children 9d59bc3dc12d
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) 2002-2013 John W. Eaton
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
2 ##
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
4 ##
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
6 ## 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: 6746
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6746
diff changeset
8 ## your option) any later version.
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
9 ##
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
14 ##
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
15 ## 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: 6746
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6746
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
18
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
18450
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
20 ## @deftypefn {Function File} {} freqz_plot (@var{w}, @var{h})
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
21 ## @deftypefnx {Function File} {} freqz_plot (@var{w}, @var{h}, @var{freq_hz})
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
22 ## Plot the pass band, stop band and phase response of @var{h}. If the
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
23 ## optional @var{freq_norm} argument is true, the frequency vector @var{w}
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
24 ## is in units of normalized radians and the x-axis is labeled as such.
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
25 ## @end deftypefn
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
26
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
27 ## Author: Paul Kienzle <pkienzle@users.sf.net>
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
28
18450
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
29 function freqz_plot (w, h, freq_norm = false)
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
30
18450
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
31 if (nargin < 2 || nargin > 3)
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
32 print_usage ();
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
33 endif
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
34
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
35 n = length (w);
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
36
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
37 ## ## exclude zero-frequency
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
38 ## h = h (2 : length (h));
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
39 ## w = w (2 : length (w));
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
40 ## n = n-1;
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
41
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
42 mag = 20 * log10 (abs (h));
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
43 phase = unwrap (arg (h));
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
44
18450
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
45 if (freq_norm)
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
46 x_label = "Normalized Frequency (\\times\\pi rad/sample)";
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
47 else
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
48 x_label = "Frequency (Hz)";
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
49 endif
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
50
18450
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
51 subplot (2, 1, 1);
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
52 plot (w, mag);
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
53 grid ("on");
18450
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
54 axis ([w(1), w(n)], "autoy");
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
55 xlabel (x_label);
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
56 ylabel ("Magnitude (dB)");
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
57
18450
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
58 subplot (2, 1, 2);
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
59 plot (w, phase*360/(2*pi));
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
60 grid ("on");
18450
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
61 axis ([w(1), w(n)], "autoy");
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
62 xlabel (x_label);
694e8f0b3862 freqz: Make frequency response plot visually compatible with Matlab (bug #41464)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
63 ylabel ("Phase (degrees)");
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
64
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
65 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 14138
diff changeset
66