%% Write Variable Frame Sizes to Buffer
% Write a sine wave of variable frame size to the buffer. Compute the FFT of 
% the sine wave and visualize the result on an array plot.
%%
% Initialize the |dsp.AsyncBuffer|, |dsp.ArrayPlot|, and |dsp.FFT| System 
% objects.

asynBuff = dsp.AsyncBuffer;
plotter = dsp.ArrayPlot;
fftObj = dsp.FFT('FFTLengthSource','Property','FFTLength',256);
%% 
% The sine wave is generated using the |sin| function in MATLAB. The  |start| 
% and |finish| variables mark the start and finish indices of each frame. If enough 
% data is cached, read from the buffer and perform the FFT. View the FFT on an 
% array plot.
%%
start = 1;

for Iter = 1 : 2000
    numToWrite = randi([200,800]);
    finish = start + numToWrite;

    inputData = sin(start:finish)';
    start = finish + 1;
    
    write(asynBuff,inputData);
    while asynBuff.NumUnreadSamples >= 256
        x = read(asynBuff,256);
        X = abs(fftObj(x));
        plotter(log(X));
    end
end
%%
% 
% <<../arrayplot_varframesize.png>>
% 
%%
% Copyright 2019 The MathWorks, Inc.