-
Notifications
You must be signed in to change notification settings - Fork 9
/
stft.m
44 lines (39 loc) · 1.23 KB
/
stft.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function d = stft(x, f, w, h)
% D = stft(X, F, W, H) Short-time Fourier transform.
% Returns some frames of short-term Fourier transform of x. Each
% column of the result is one F-point fft; each successive frame is
% offset by H points until X is exhausted. Data is hamm-windowed
% at W pts, or rectangular if W=0, or with W if it is a vector.
% See also 'istft.m'.
% dpwe 1994may05. Uses built-in 'fft'
% $Header: /homes/dpwe/public_html/resources/matlab/pvoc/RCS/stft.m,v 1.2 2009/01/07 04:32:42 dpwe Exp $
s = length(x);
if length(w) == 1
if w == 0
% special case: rectangular window
win = ones(1,f);
else
if rem(w, 2) == 0 % force window to be odd-len
w = w + 1;
end
halflen = (w-1)/2;
halff = f/2; % midpoint of win
halfwin = 0.5 * ( 1 + cos( pi * (0:halflen)/halflen));
win = zeros(1, f);
acthalflen = min(halff, halflen);
win((halff+1):(halff+acthalflen)) = halfwin(1:acthalflen);
win((halff+1):-1:(halff-acthalflen+2)) = halfwin(1:acthalflen);
end
else
win = w;
w = length(w);
end
c = 1;
% pre-allocate output array
d = zeros((1+f/2),1+fix((s-f)/h));
for b = 0:h:(s-f)
u = win.*x((b+1):(b+f));
t = fft(u);
d(:,c) = t(1:(1+f/2))';
c = c+1;
end;