3191
|
1 ## Copyright (C) 1995, 1996, 1997 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 -*- |
6547
|
20 ## @deftypefn {Function File} {[@var{y}, @var{c}] =} stft (@var{x}, @var{win_size}, @var{inc}, @var{num_coef}, @var{w_type}) |
3449
|
21 ## Compute the short-term Fourier transform of the vector @var{x} with |
|
22 ## @var{num_coef} coefficients by applying a window of @var{win_size} data |
|
23 ## points and an increment of @var{inc} points. |
3191
|
24 ## |
|
25 ## Before computing the Fourier transform, one of the following windows |
3449
|
26 ## is applied: |
|
27 ## |
|
28 ## @table @asis |
|
29 ## @item hanning |
|
30 ## w_type = 1 |
|
31 ## @item hamming |
|
32 ## w_type = 2 |
|
33 ## @item rectangle |
|
34 ## w_type = 3 |
|
35 ## @end table |
|
36 ## |
|
37 ## The window names can be passed as strings or by the @var{w_type} number. |
3191
|
38 ## |
|
39 ## If not all arguments are specified, the following defaults are used: |
3449
|
40 ## @var{win_size} = 80, @var{inc} = 24, @var{num_coef} = 64, and |
|
41 ## @var{w_type} = 1. |
3191
|
42 ## |
3449
|
43 ## @code{@var{y} = stft (@var{x}, @dots{})} returns the absolute values |
|
44 ## of the Fourier coefficients according to the @var{num_coef} positive |
|
45 ## frequencies. |
|
46 ## |
|
47 ## @code{[@var{y}, @var{c}] = stft (@code{x}, @dots{})} returns the |
|
48 ## entire STFT-matrix @var{y} and a 3-element vector @var{c} containing |
|
49 ## the window size, increment, and window type, which is needed by the |
|
50 ## synthesis function. |
|
51 ## @end deftypefn |
3191
|
52 |
3457
|
53 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> |
|
54 ## Description: Short-term Fourier transform |
3191
|
55 |
|
56 function [Y, c] = stft(X, win, inc, coef, w_type) |
3426
|
57 |
3191
|
58 ## default values of unspecified arguments |
|
59 if (nargin < 5) |
|
60 w_type = 1; |
|
61 if (nargin < 4) |
|
62 coef = 64; |
|
63 if (nargin < 3) |
3426
|
64 inc = 24; |
|
65 if (nargin < 2) |
|
66 win = 80; |
|
67 endif |
3191
|
68 endif |
|
69 endif |
3426
|
70 elseif (nargin == 5) |
5443
|
71 if (ischar (w_type)) |
3191
|
72 if (strcmp (w_type, "hanning")) |
3426
|
73 w_type = 1; |
3191
|
74 elseif (strcmp (w_type, "hamming")) |
3426
|
75 w_type = 2; |
3191
|
76 elseif (strcmp (w_type, "rectangle")) |
3426
|
77 w_type = 3; |
3191
|
78 else |
3457
|
79 error ("stft: unknown window type `%s'", w_type); |
3191
|
80 endif |
|
81 endif |
|
82 else |
6046
|
83 print_usage (); |
3191
|
84 endif |
|
85 |
|
86 ## check whether X is a vector |
|
87 [nr, nc] = size (X); |
|
88 if (nc != 1) |
|
89 if (nr == 1) |
3426
|
90 X = X'; |
3191
|
91 nr = nc; |
|
92 else |
3457
|
93 error ("stft: X must be a vector"); |
3191
|
94 endif |
|
95 endif |
|
96 |
|
97 num_coef = 2 * coef; |
|
98 if (win > num_coef) |
|
99 win = num_coef; |
3457
|
100 printf ("stft: window size adjusted to %f\n", win); |
3191
|
101 endif |
|
102 num_win = fix ((nr - win) / inc); |
|
103 |
|
104 ## compute the window coefficients |
3426
|
105 if (w_type == 3) # rectangular window |
3191
|
106 WIN_COEF = ones (win, 1); |
3426
|
107 elseif (w_type == 2) # Hamming window |
3191
|
108 WIN_COEF = hamming (win); |
3426
|
109 else # Hanning window |
3191
|
110 WIN_COEF = hanning (win); |
|
111 endif |
3426
|
112 |
3191
|
113 ## create a matrix Z whose columns contain the windowed time-slices |
|
114 Z = zeros (num_coef, num_win + 1); |
|
115 start = 1; |
|
116 for i = 0:num_win |
|
117 Z(1:win, i+1) = X(start:start+win-1) .* WIN_COEF; |
|
118 start = start + inc; |
|
119 endfor |
|
120 |
|
121 Y = fft (Z); |
|
122 |
|
123 if (nargout == 1) |
|
124 Y = abs (Y(1:coef, :)); |
|
125 else |
|
126 c = [win, inc, w_type]; |
|
127 endif |
|
128 |
|
129 endfunction |