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