%% Find Spectrogram of Chirps
% Specify the path to four files included with Signal Processing Toolbox™. Each file contains a chirp and a random sample rate, fs, ranging from 100 to 150 Hz. Create a signal datastore that points to the specified folder.

folder = fullfile(matlabroot,'examples','signal','data','sample_chirps', ...
    ["chirp_1.mat","chirp_4.mat","chirp_9.mat","chirp_10.mat"]);
sds = signalDatastore(folder,'SampleRateVariableName','fs');

% Define a function that takes the output of the read function and computes and returns:
%  The spectrograms of the chirps.
% The vector of time instants corresponding to the centers of the windowed segments.
% The frequencies corresponding to the estimates.
% function [dataOut,infoOut] = extractSpectrogram(dataIn,info)
%     [dataOut,F,T] = pspectrum(dataIn,info.SampleRate,'spectrogram',...
%                               'TimeResolution',0.25,...
%                               'OverlapPercent',40,'Leakage',0.8);
%     infoOut = info;
%     infoOut.CenterFrequencies = F;
%     infoOut.TimeInstants = T;
% end
%
% Call the transform function to create a datastore that computes the spectrogram of each chirp using the function you defined.

sdsNew = transform(sds,@extractSpectrogram,'IncludeInfo',true);

% While the transformed datastore has unread files, read from the new datastore and visualize the spectrograms in three-dimensional space.

t = tiledlayout('flow');
while hasdata(sdsNew)
    nexttile
    [sig,infoOut] = read(sdsNew);
    waterfall(infoOut.TimeInstants,infoOut.CenterFrequencies,sig)
    xlabel('Frequency (Hz)')
    ylabel('Time (S)')
    view([30 70])
end

%
% Copyright 2019 The MathWorks, Inc.