-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_STFT.m
47 lines (38 loc) · 1.16 KB
/
calc_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
function [X,f] = calc_STFT(x,fs,nfft,noverlap)
%CALC_STFT short-time fourier transform using OLA. The STFT uses a
%sqrt(hann(nfft)) window.
%
% INPUT:
% x : input time signal(s) (samples x channels)
% fs : sampling rate
% nfft : FFT size
% noverlap : frame overlap; default: 2 (50%)
%
% OUTPUT:
% X : STFT matrix (channels x bins x frames)
% f : frequency vector for bins
%
% See also: calc_ISTFT, fft.
% Author: Daniel Marquardt & Nico Goessling
% Date: 27.05.2016
if nargin < 4
noverlap = 2;
end
% synthesis window
window = sqrt(hann(nfft, 'periodic'));
% use only half FFT spectrum
N_half = nfft / 2 + 1;
% get frequency vector
f = 0:(fs / 2) / (N_half - 1):fs / 2;
% init
L = floor((length(x) - nfft + (nfft / noverlap)) / (nfft / noverlap));
X = zeros(N_half, L, size(x,2));
% OLA processing
for l = 0:L-1 % Frame index
x_frame = x(floor(l*(nfft / noverlap) + 1):floor(l*(nfft / noverlap) + nfft),:);
x_windowed = x_frame.*repmat(window, 1, size(x_frame,2));
X_frame = fft(x_windowed,[],1);
X(:,l+1,:) = X_frame(1 : floor(size(X_frame,1) / 2) + 1, :);
end
X = permute(X, [3 1 2]);
end