The example in the next section shows how to use System objects that are predefined in the software. If you use a function to create and use a System object, specify the object creation using conditional code. This will prevent errors if that function is called within a loop.
For a list of predefined System objects that are supported for HDL code generation, see Predefined System Objects Supported for HDL Code Generation. You can use these objects as components in your system. Each of these System objects has default property settings, so you can create instances of them using just the object name without any input properties. You can also generate HDL code from System objects that you define (see Generate Code for User-Defined System Objects).
This section of the example shows how to set up your system. The predefined components you need are:
dsp.AudioFileReader
— Read the file of audio data
dsp.FIRFilter
— Filter the audio data
audioDeviceWriter
— Play the filtered audio data
First, create the component objects, using default property settings.
audioIn = dsp.AudioFileReader; filtLP = dsp.FIRFilter; audioOut = audioDeviceWriter;
If you did not set an object's properties when you created it and do not want to use default values, you must explicitly set those properties. Some properties allow you to change their values while your system is running. See Reconfiguring Objects for information.
Most properties are independent of each other. However, some System object™ properties enable or disable another property or limit the values of another property. To avoid errors or warnings, you should set the controlling property before setting the dependent property.
To display the current property values for an object, type that
object's handle name at the command line (such as audioIn
).
To display the value of a specific property, type objecthandle.propertyname
(such
as audioIn.FileName
).
After you have determined the components you need and have created and configured your System objects, assemble your system. You use the System objects like other MATLAB® variables and include them in MATLAB code. You can pass MATLAB variables into and out of System objects.
The main difference between using System objects and using functions is that System objects use a two-step process. First you create the object and set its parameters and then, you run the object. Running the object initializes it and controls the data flow and state management of your system. You typically call a System object within a code loop.
You use the output from an object as the input to another object.
For some System objects, you can use properties of those objects to
change the number of inputs or outputs. To verify that the appropriate
number of input and outputs are being used, you can use getNumInputs
and getNumOutputs
on
any System object. For information on all available System object
methods, see System Object Methods.
This section shows how to connect the components together to read, filter, and play a file of audio data. The while loop uses the isDone
method to read through the entire file.
while ~isDone(audioIn) audio = audioIn(); % Read audio source file y = filtLP(audio); % Filter the data audioOut(y); % Play the filtered data end
Run your code either by typing directly at the command line or running a file containing your program. When you run the code for your system, data is processed through your objects.
The first call to a System object initializes and then
locks the object. When a System object has started processing
data, it is locked to prevent changes that would disrupt its processing.
Use the isLocked
method to verify whether an object
is locked. When the object is locked, you cannot change:
Number of inputs or outputs
Data type of inputs or outputs
Data type of any tunable property
Dimensions of inputs or tunable properties, except for System objects that support variable-size data
Value of any nontunable property
To make changes to your system while it is running, see Reconfiguring Objects.
When a System object has started processing data, it is locked
to prevent changes that would disrupt its processing. You can use isLocked
on
any System object to verify whether it is locked or not. When processing
is complete, you can use the release
method to unlock
a System object.
Some object properties are tunable, which enables you to change them even if the object is locked. Unless otherwise specified, System objects properties are nontunable. Refer to the object's reference page to determine whether an individual property is tunable. Typically, tunable properties are not critical to how the System object processes data.
During simulation, some System objects do not allow complex data if the object was initialized with real data. You cannot change any input complexity during code generation.
You can change the value of a tunable property without a warning or error being produced. For all other changes at run time, an error occurs.