-
Notifications
You must be signed in to change notification settings - Fork 3
/
stft.m
58 lines (47 loc) · 1.53 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function [stft, f, t] = stft(x, wlen, h, nfft, fs)
% function: [stft, f, t] = stft(x, wlen, h, nfft, fs)
% x - signal in the time domain
% wlen - length of the hamming window
% h - hop size
% nfft - number of FFT points
% fs - sampling frequency, Hz
% f - frequency vector, Hz
% t - time vector, s
% stft - STFT matrix (only unique points, time across columns, freq across rows)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Short-Time Fourier Transform %
% with MATLAB Implementation %
% %
% Author: M.Sc. Eng. Hristo Zhivomirov 12/21/13 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% represent x as column-vector if it is not
if size(x, 2) > 1
x = x';
end
% length of the signal
xlen = length(x);
% form a periodic hamming window
win = hamming(wlen, 'periodic');
% form the stft matrix
rown = ceil((1+nfft)/2); % calculate the total number of rows
coln = 1+fix((xlen-wlen)/h); % calculate the total number of columns
stft = zeros(rown, coln); % form the stft matrix
% initialize the indexes
indx = 0;
col = 1;
% perform STFT
while indx + wlen <= xlen
% windowing
xw = x(indx+1:indx+wlen).*win;
% FFT
X = fft(xw, nfft);
% update the stft matrix
stft(:, col) = X(1:rown);
% update the indexes
indx = indx + h;
col = col + 1;
end
% calculate the time and frequency vectors
t = (wlen/2:h:wlen/2+(coln-1)*h)/fs;
f = (0:rown-1)*fs/nfft;
end