%% Deep Learning Short-Time Fourier Transform of Sinusoid
% Generate a 3-by-160(-by-1) array containing one batch of a three-channel, 160-sample sinusoidal signal. The normalized sinusoid frequencies are \pi /4 rad/sample, \pi /2 rad/sample, and 3\pi/4 rad/sample. Save the signal as a dlarray, specifying the dimensions in order. dlarray permutes the array dimensions to the 'CBT' shape expected by a deep learning network. Display the array dimension sizes.

x = dlarray(cos(pi.*(1:3)'/4*(0:159)),'CTB');
[nchan,nbtch,nsamp] = size(x)

%
% Compute the deep learning short-time Fourier transform of the signal. Specify a 64-sample rectangular window and an FFT length of 1024.

[re,im,f,t] = dlstft(x,'Window',rectwin(64),'FFTLength',1024);

% dlstft computes the transform along the 'T' dimension. The output arrays are in 'SCBT' format. The 'S' dimension corresponds to frequency in the short-time Fourier transform.
% Extract the data from the deep learning arrays.

re = squeeze(extractdata(re));
im = squeeze(extractdata(im));

f = extractdata(f);
t = extractdata(t);

%
% Compute the magnitude of the short-time Fourier transform. Plot the magnitude separately for each channel in a waterfall plot.

z = abs(re + 1j*im);

for kj = 1:nchan
    subplot(nchan,1,kj)
    waterfall(f/pi,t,squeeze(z(:,kj,:))')
    view(30,45)
end

xlabel('Frequency (\times\pi rad/sample)')
ylabel('Samples')

% Copyright 2020 The MathWorks, Inc.