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.
You can also create your own System objects (see Define 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
).
This section of the example shows how to configure the components for your system by setting the component objects' properties.
Use this procedure if you have created your components separately from configuring them. You can also create and configure your components at the same time, as described in a later example.
For the file reader object, specify the file to read and set the output data type.
For the filter object, specify the filter numerator coefficients using the fir1
function, which specifies the lowpass filter order and the cutoff frequency.
For the audio device writer object, specify the sample rate. In this case, use the same sample rate as the input data.
audioIn.Filename = 'speech_dft_8kHz.wav'; audioIn.OutputDataType = 'single'; filtLP.Numerator = fir1(160,.15); audioOut.SampleRate = audioIn.SampleRate;
This example shows how to create your System object™ components and configure the desired properties at the same time. Specify each property with a 'Name'
, Value
argument pair.
Create the file reader object, specify the file to read, and set the output data type.
audioIn = dsp.AudioFileReader('speech_dft_8kHz.wav',... 'OutputDataType','single');
Create the filter object and specify the filter numerator using the fir1
function. Specify the lowpass filter order and the cutoff frequency of the fir1
function.
filtLP = dsp.FIRFilter('Numerator',fir1(160,.15));
Create the audio player object and set the sample rate to the same rate as the input data.
audioOut = audioDeviceWriter('SampleRate',audioIn.SampleRate);
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.
You can change the filter type to a high-pass filter as your code is running by modifying the Numerator
property of the filter object. The change takes effect at the next iteration of the while loop.
reset(audioIn); % Reset audio file filtLP.Numerator = fir1(160,0.15,'high'); while ~isDone(audioIn) audio = audioIn(); % Read audio source file y = filtLP(audio); % Filter the data audioOut(y); % Play the filtered data end