-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstft.m
More file actions
executable file
·74 lines (59 loc) · 1.83 KB
/
stft.m
File metadata and controls
executable file
·74 lines (59 loc) · 1.83 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function X = stft(x, nbins, nskip)
% STFT - short-time Fourier rransform
%
% X = stft(x, nbins, nskip) returns X, the short-time Fourier transform of the
% input column x, computed using nbins-long signal segments, and a 2*nbins DFT
% with a hop size of nskip samples. A Hanning window is used by default;
% however if nbins has more than one element, it is used as the window, and the
% DFT size is the length of nbins(:). The hop size nskip is half the DFT size
% by default. Note that the DFT size is quantized to a power of 2 using
% nextpow2().
%
% If x has multiple columns, then the STFT is computed for each of the columns,
% and the result is the concatenation of the column spectrograms, nbins+1 tall,
%
% See Also: FTGRAM, STFB.
% Created: 22-Jan-2011.
% Revised: 22-Jan-2011, MJW w/JSA, v1.
% Revised: 11-Mar-2012, JSA, v2 - interface, windowing reworked.
% Revised: 14-Aug-2013, JSA, v3 - comments updated.
% Version: v3.
%% initialization
% input signal size
[nsamp, nc] = size(x); %% signal length, samples; channel count, channels
if (nsamp == 1),
% make x a column
x = x(:);
[nsamp, nc] = size(x);
end;
% dft length
nbins = nbins(:);
if (length(nbins) == 1),
% nbins specifies dft size
nbins = 2^nextpow2(nbins);
window = [hanning(nbins); zeros(nbins,1)];
else,
% nbins contains the window
tempw = nbins;
nbins = 2^(nextpow2(length(nbins))-1);
window = [tempw; zeros(2*nbins-length(tempw),1)];
end;
% fix hop size
if (nargin < 3)
nskip = floor(nbins / 2);
end;
%% form stft
% loop through columns
noverlap = 2*nbins - nskip;
X = [];
for c = [1:nc],
% buffer signal
xb = buffer(x(:,c), 2*nbins, noverlap, 'nodelay');
% window signal
nframes = size(xb,2);
xb = xb .* (window * ones(1, nframes));
% transform signal
temp = fft(xb);
X = [X, temp(1:nbins+1,:)];
end;
end