samples to and reads samples from a first-in, first-out (FIFO) buffer.
write method writes data to the buffer and the
reads data from the buffer. When creating the object, you can set
the number of samples (rows) of the buffer using the
The number of channels (columns) is set during the first call to
Initialize the buffer by calling
the first call to
The data that you write occupies the next available space in
the buffer. If the buffer is full and all the data within it is unread,
that is, if
asyncBuff.NumUnreadSamples == asyncBuff.Capacity,
the object overwrites the oldest data with any new data that comes
in. The buffer removes data only when the data is overwritten, so
you can reread data from the past. The
supports writing and reading variable frame size signals. For examples,
see Read Variable Frame Sizes from Buffer and Write Variable Frame Sizes to Buffer.
To write and read samples from a FIFO buffer:
asyncBuff = dsp.AsyncBuffer returns an
using the default properties.
asyncBuff = dsp.AsyncBuffer(cap) sets the
asyncBuff = dsp.AsyncBuffer(Name,Value) specifies
Name,Value pairs. Unspecified
properties have default values.
asyncBuff = dsp.AsyncBuffer(200000);
Capacity— Number of writable/readable rows in buffer
192000(default) | positive integer greater than or equal to 2
Number of writable/readable rows in buffer. The number of rows
during each write to the buffer must not exceed the capacity of the
buffer. If the buffer is full and all the data within is unread, the
object overwrites the oldest data with any new data that comes in.
CumulativeOverrun property returned by the
gives the number of samples overrun per channel since the last call
reset. The number of samples overrun is the number
of unread samples overwritten.
By default, this property is of data type
NumUnreadSamples— Number of unread samples in each channel
0(default) | scalar greater than or equal to 0
This property is read-only.
Number of unread samples in each channel (column) of the buffer.
The total number of unread samples in the buffer is
numChann is the number of channels in the
CumulativeUnderrun property returned
info method gives the number of samples
underrun per channel since the last call to
Underrun occurs if you attempt to read more samples than available.
Before calling the
read method, you must initialize
the buffer by calling either the
For an example, see Why Does the dsp.AsyncBuffer Object Error When You Call read Before write?
|info||Get cumulative overrun and underrun|
|read||Read data from buffer|
|reset||Reset internal states of System object|
|write||Write data to buffer|
dsp.AsyncBuffer System object™ supports reading variable frame sizes from the buffer.
dsp.AsyncBuffer System object. The input is white Gaussian noise with zero mean, a standard deviation of 1, and a frame size of 512 samples. Write the input to the buffer using the
write method. Store the data that is read from the buffer in
asyncBuff = dsp.AsyncBuffer; input = randn(512,1); write(asyncBuff,input); outTotal = ; plot(input) hold on
Plot the input signal and data that is read from the buffer in the same plot. Read data from the buffer until all samples are read. In each loop of iteration,
randi determines the number of samples to read. Therefore, the signal is read in as a variable-size signal.
while asyncBuff.NumUnreadSamples ~= 0 numToRead = randi([1,64]); out = read(asyncBuff,numToRead); outTotal = [outTotal;out]; plot(outTotal,'r'); pause(0.2) end hold off
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.
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
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
Usage notes and limitations:
See System Objects in MATLAB Code Generation (MATLAB Coder).