1 ## Copyright (C) 1995-2012 Andreas Weingessel
3 ## This file is part of Octave.
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {[@var{y}, @var{c}] =} stft (@var{x}, @var{win_size}, @var{inc}, @var{num_coef}, @var{win_type})
21 ## Compute the short-time 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.
25 ## Before computing the Fourier transform, one of the following windows
29 ## @item @nospell{hanning}
32 ## @item @nospell{hamming}
39 ## The window names can be passed as strings or by the @var{win_type} number.
41 ## If not all arguments are specified, the following defaults are used:
42 ## @var{win_size} = 80, @var{inc} = 24, @var{num_coef} = 64, and
43 ## @var{win_type} = 1.
45 ## @code{@var{y} = stft (@var{x}, @dots{})} returns the absolute values
46 ## of the Fourier coefficients according to the @var{num_coef} positive
49 ## @code{[@var{y}, @var{c}] = stft (@code{x}, @dots{})} returns the
50 ## entire STFT-matrix @var{y} and a 3-element vector @var{c} containing
51 ## the window size, increment, and window type, which is needed by the
52 ## synthesis function.
55 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
56 ## Description: Short-Time Fourier Transform
58 function [y, c] = stft(x, win_size, inc, num_coef, win_type)
60 ## Default values of unspecified arguments.
73 if (ischar (win_type))
74 if (strcmp (win_type, "hanning"))
76 elseif (strcmp (win_type, "hamming"))
78 elseif (strcmp (win_type, "rectangle"))
81 error ("stft: unknown window type `%s'", win_type);
88 ## Check whether X is a vector.
95 error ("stft: X must be a vector");
100 if (win_size > ncoef)
102 printf ("stft: window size adjusted to %f\n", win_size);
104 num_win = fix ((nr - win_size) / inc);
106 ## compute the window coefficients
108 ## Rectangular window.
109 win_coef = ones (win_size, 1);
110 elseif (win_type == 2)
112 win_coef = hamming (win_size);
115 win_coef = hanning (win_size);
118 ## Create a matrix Z whose columns contain the windowed time-slices.
119 z = zeros (ncoef, num_win + 1);
122 z(1:win_size, i+1) = x(start:start+win_size-1) .* win_coef;
129 y = abs (y(1:num_coef, :));
131 c = [win_size, inc, win_type];