Mercurial > hg > octave-nkf
annotate scripts/signal/stft.m @ 17744:d63878346099
maint: Update copyright notices for release.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 23 Oct 2013 22:09:27 -0400 |
parents | 6ed0a8532bcf |
children | dffb1e9a6d3b |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17718
diff
changeset
|
1 ## Copyright (C) 1995-2013 Andreas Weingessel |
3426 | 2 ## |
3922 | 3 ## This file is part of Octave. |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
3191 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 13 ## General Public License for more details. |
14 ## | |
3191 | 15 ## You should have received a copy of the GNU General Public License |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3191 | 18 |
3449 | 19 ## -*- texinfo -*- |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
20 ## @deftypefn {Function File} {@var{y} =} stft (@var{x}) |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{y} =} stft (@var{x}, @var{win_size}) |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{y} =} stft (@var{x}, @var{win_size}, @var{inc}) |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
23 ## @deftypefnx {Function File} {@var{y} =} stft (@var{x}, @var{win_size}, @var{inc}, @var{num_coef}) |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
24 ## @deftypefnx {Function File} {@var{y} =} stft (@var{x}, @var{win_size}, @var{inc}, @var{num_coef}, @var{win_type}) |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
25 ## @deftypefnx {Function File} {[@var{y}, @var{c}] =} stft (@dots{}) |
9072
bd8e388043c4
Cleanup documentation for signal.texi, image.texi, audio.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
26 ## Compute the short-time Fourier transform of the vector @var{x} with |
3449 | 27 ## @var{num_coef} coefficients by applying a window of @var{win_size} data |
28 ## points and an increment of @var{inc} points. | |
3191 | 29 ## |
30 ## Before computing the Fourier transform, one of the following windows | |
3449 | 31 ## is applied: |
32 ## | |
33 ## @table @asis | |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
34 ## @item @qcode{"hanning"} |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
35 ## win_type = 1 |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
36 ## |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
37 ## @item @qcode{"hamming"} |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
38 ## win_type = 2 |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
39 ## |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
40 ## @item @qcode{"rectangle"} |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
41 ## win_type = 3 |
3449 | 42 ## @end table |
43 ## | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
44 ## The window names can be passed as strings or by the @var{win_type} number. |
3191 | 45 ## |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
46 ## The following defaults are used for unspecifed arguments: |
3449 | 47 ## @var{win_size} = 80, @var{inc} = 24, @var{num_coef} = 64, and |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
48 ## @var{win_type} = 1. |
3191 | 49 ## |
3449 | 50 ## @code{@var{y} = stft (@var{x}, @dots{})} returns the absolute values |
51 ## of the Fourier coefficients according to the @var{num_coef} positive | |
52 ## frequencies. | |
53 ## | |
54 ## @code{[@var{y}, @var{c}] = stft (@code{x}, @dots{})} returns the | |
55 ## entire STFT-matrix @var{y} and a 3-element vector @var{c} containing | |
56 ## the window size, increment, and window type, which is needed by the | |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
57 ## @code{synthesis} function. |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
58 ## @seealso{synthesis} |
3449 | 59 ## @end deftypefn |
3191 | 60 |
3457 | 61 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> |
9072
bd8e388043c4
Cleanup documentation for signal.texi, image.texi, audio.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
62 ## Description: Short-Time Fourier Transform |
3191 | 63 |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
64 function [y, c] = stft (x, win_size = 80, inc = 24, num_coef = 64, win_type = 1) |
3426 | 65 |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
66 if (nargin < 1 || nargin > 5) |
6046 | 67 print_usage (); |
3191 | 68 endif |
69 | |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
70 if (ischar (win_type)) |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
71 switch (tolower (win_type)) |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
72 case "hanning" win_type = 1; |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
73 case "hamming" win_type = 2; |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
74 case "rectangle" win_type = 3; |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
75 otherwise |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
76 error ("stft: unknown window type '%s'", win_type); |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
77 endswitch |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
78 endif |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
79 |
8506 | 80 ## Check whether X is a vector. |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
81 if (! isvector (x)) |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
82 error ("stft: X must be a vector"); |
3191 | 83 endif |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
84 x = x(:); |
3191 | 85 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
86 ncoef = 2 * num_coef; |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
87 if (win_size > ncoef) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
88 win_size = ncoef; |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
89 printf ("stft: window size adjusted to %f\n", win_size); |
3191 | 90 endif |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
91 num_win = fix ((rows (x) - win_size) / inc); |
3191 | 92 |
93 ## compute the window coefficients | |
17718
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
94 switch (win_type) |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
95 case 1 win_coef = hanning (win_size); |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
96 case 2 win_coef = hamming (win_size); |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
97 case 3 win_coef = ones (win_size, 1); |
6ed0a8532bcf
Overhaul stft and synthesis functions
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
98 endswitch |
3426 | 99 |
8506 | 100 ## Create a matrix Z whose columns contain the windowed time-slices. |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
101 z = zeros (ncoef, num_win + 1); |
3191 | 102 start = 1; |
103 for i = 0:num_win | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
104 z(1:win_size, i+1) = x(start:start+win_size-1) .* win_coef; |
3191 | 105 start = start + inc; |
106 endfor | |
107 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
108 y = fft (z); |
3191 | 109 |
110 if (nargout == 1) | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
111 y = abs (y(1:num_coef, :)); |
3191 | 112 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
113 c = [win_size, inc, win_type]; |
3191 | 114 endif |
115 | |
116 endfunction | |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
117 |