-
Notifications
You must be signed in to change notification settings - Fork 17
/
istft.m
72 lines (57 loc) · 2 KB
/
istft.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Inverse Short-Time Fourier Transform %
% with MATLAB Implementation %
% %
% Author: M.Sc. Eng. Hristo Zhivomirov 12/26/13 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [x, t] = istft(stft, wlen, hop, nfft, fs)
% function: [x, t] = istft(stft, wlen, hop, nfft, fs)
% stft - STFT matrix (only unique points, time across columns, freq across rows)
% wlen - length of the sinthesis Hamming window
% hop - hop size
% nfft - number of FFT points
% fs - sampling frequency, Hz
% x - signal in the time domain
% t - time vector, s
% signal length estimation and preallocation
coln = size(stft, 2);
xlen = wlen + (coln-1)*hop;
x = zeros(1, xlen);
% form a periodic hamming window
win = hamming(wlen, 'periodic');
% initialize the signal time segment index
indx = 0;
% perform ISTFT (via IFFT and Weighted-OLA)
if rem(nfft, 2) % odd nfft excludes Nyquist point
for col = 1:coln
% extract FFT points
X = stft(:, col);
X = [X; conj(X(end:-1:2))];
% IFFT
xprim = real(ifft(X));
xprim = xprim(1:wlen);
% weighted-OLA
x((indx+1):(indx+wlen)) = x((indx+1):(indx+wlen)) + (xprim.*win)';
% update the index
indx = indx + hop;
end
else % even nfft includes Nyquist point
for col = 1:coln
% extract FFT points
X = stft(:, col);
X = [X; conj(X(end-1:-1:2))];
% IFFT
xprim = real(ifft(X));
xprim = xprim(1:wlen);
% weighted-OLA
x((indx+1):(indx+wlen)) = x((indx+1):(indx+wlen)) + (xprim.*win)';
% update the index
indx = indx + hop;
end
end
% scale the signal
W0 = sum(win.^2);
x = x.*hop/W0;
% generate time vector
t = (0:xlen-1)/fs;
end