generate and use simulated data ensemble -凯发k8网页登录
this example shows how to generate a data ensemble for predictive-maintenance algorithm design by simulating a simulink® model of a machine while varying a fault parameter. the example then illustrates some of the ways you interact with a simulation ensemble datastore. the example shows how to read data from the datastore into the matlab® workspace, process the data to compute derived variables, and write the new variables back to the datastore.
the model in this example is a simplified version of the gear-box model described in using simulink to generate fault data. load the simulink model.
mdl = 'transmissioncasingsimplified';
open_system(mdl)
for this example, only one fault mode is modeled. the gear-tooth fault is modeled as a disturbance in the gear tooth fault
subsystem. the magnitude of the disturbance is controlled by the model variable toothfaultgain
, where toothfaultgain = 0
corresponds to no gear tooth fault (healthy operation).
generate the ensemble of simulated data
to generate a simulation ensemble datastore of fault data, you use generatesimulationensemble
to simulate the model at different values of toothfaultgain
, ranging from -2 to zero. this function simulates the model once for each entry in an array of simulink.simulationinput
objects that you provide. each simulation generates a separate member of the ensemble. create such an array, and use setvariable
to assign a tooth-fault gain value for each run.
toothfaultvalues = -2:0.5:0; % 5 toothfaultgain values for ct = numel(toothfaultvalues):-1:1 tmp = simulink.simulationinput(mdl); tmp = setvariable(tmp,'toothfaultgain',toothfaultvalues(ct)); simin(ct) = tmp; end
for this example, the model is already configured to log certain signal values, vibration
and tacho
(see (simulink)). the generatesimulationensemble
function further configures the model to:
save logged data to files in the folder you specify
use the
timetable
format for signal loggingstore each
simulink.simulationinput
object in the saved file with the corresponding logged data
specify a location for the generated data. for this example, save the data to a folder called data
within your current folder. if all the simulations complete without error, the function returns true
in the indicator output, status
.
mkdir data location = fullfile(pwd,'data'); [status,e] = generatesimulationensemble(simin,location);
[03-mar-2023 08:25:02] running simulations... [03-mar-2023 08:25:09] completed 1 of 5 simulation runs [03-mar-2023 08:25:11] completed 2 of 5 simulation runs [03-mar-2023 08:25:13] completed 3 of 5 simulation runs [03-mar-2023 08:25:14] completed 4 of 5 simulation runs [03-mar-2023 08:25:16] completed 5 of 5 simulation runs
status
status = logical
1
inside the data
folder, examine one of the files. each file is a mat-file containing the following matlab® variables:
simulationinput
— thesimulink.simulationinput
object that was used to configure the model for generating the data in the file. you can use this to extract information about the conditions (such as faulty or healthy) under which this simulation was run.logsout
— adataset
object containing all the data that the simulink model is configured to log.pmsignallogname
— the name of the variable that contains the logged data ('logsout'
in this example). thesimulationensembledatastore
command uses this name to parse the data in the file.simulationmetadata
— other information about the simulation that generated the data logged in the file.
now you can create the simulation ensemble datastore using the generated data. the resulting simulationensembledatastore
object points to the generated data. the object lists the data variables in the ensemble, and by default all the variables are selected for reading.
ensemble = simulationensembledatastore(location)
ensemble = simulationensembledatastore with properties: datavariables: [4x1 string] independentvariables: [0x0 string] conditionvariables: [0x0 string] selectedvariables: [4x1 string] readsize: 1 nummembers: 5 lastmemberread: [0x0 string] files: [5x1 string]
ensemble.datavariables
ans = 4x1 string
"simulationinput"
"simulationmetadata"
"tacho"
"vibration"
ensemble.selectedvariables
ans = 4x1 string
"simulationinput"
"simulationmetadata"
"tacho"
"vibration"
read data from ensemble members
suppose that for the analysis you want to do, you need only the vibration
data and the simulink.simulationinput
object that describes the conditions under which each member was simulated. set ensemble.selectedvariables
to specify the variables you want to read. the read
command then extracts those variables from the first ensemble member, as determined by the software.
ensemble.selectedvariables = ["vibration";"simulationinput"]; data1 = read(ensemble)
data1=1×2 table
vibration simulationinput
_________________ ______________________________
{589x1 timetable} {1x1 simulink.simulationinput}
data.vibration
is a cell array containing one timetable
row storing the simulation times and the corresponding vibration signal. you can now process this data as needed. for instance, extract the vibration data from the table and plot it.
vibdata1 = data1.vibration{1};
plot(vibdata1.time,vibdata1.data)
title('vibration - first ensemble member')
the lastmemberread
property of the ensemble contains the file name of the most recently read member. the next time you call read
on this ensemble, the software advances to the next member of the ensemble. (see for more information.) read the selected variables from the next member of the ensemble.
data2 = read(ensemble)
data2=1×2 table
vibration simulationinput
_________________ ______________________________
{603x1 timetable} {1x1 simulink.simulationinput}
to confirm that data1
and data2
contain data from different ensemble members, examine the values of the varied model parameter, toothfaultgain
. for each ensemble, this value is stored in the variables
field of the simulationinput
variable.
siminput1 = data1.simulationinput{1}; siminput1.variables
ans = variable with properties: name: 'toothfaultgain' value: -2 workspace: 'global-workspace' context: '' description: ""
siminput2 = data2.simulationinput{1}; siminput2.variables
ans = variable with properties: name: 'toothfaultgain' value: -1.5000 workspace: 'global-workspace' context: '' description: ""
this result confirms that data1
is from the ensemble with toothfaultgain
= –2, and data2
is from the ensemble with toothfaultgain
= –1.5.
append data to ensemble member
suppose that you want to convert the toothfaultgain
values for each ensemble member into a binary indicator of whether or not a tooth fault is present. suppose further that you know from your experience with the system that tooth-fault gain values less than 0.1 in magnitude are small enough to be considered healthy operation. convert the gain value for the ensemble member you just read into an indicator that is 0 (no fault) for –0.1 < gain < 0.1, and 1 (fault) otherwise.
st = (abs(siminput2.variables.value) < 0.1);
to append the new tooth-fault indicator to the corresponding ensemble data, first expand the list of data variables in the ensemble.
ensemble.datavariables = [ensemble.datavariables;"toothfault"];
ensemble.datavariables
ans = 5x1 string
"simulationinput"
"simulationmetadata"
"tacho"
"vibration"
"toothfault"
then, use writetolastmemberread
to write a value for new variable to the last-read member of the ensemble.
writetolastmemberread(ensemble,'toothfault',st);
batch process data from all ensemble members
in practice, you want to append the tooth-fault indicator to every member in the ensemble. to do so, reset the ensemble to its unread state, so that the next read begins at the first ensemble member. then, loop through all the ensemble members, computing toothfault
for each member and appending it.
reset(ensemble); st = false; while hasdata(ensemble) data = read(ensemble); siminputvars = data.simulationinput{1}.variables; tfgain = siminputvars.value; st = (abs(tfgain) < 0.1); writetolastmemberread(ensemble,'toothfault',st); end
finally, designate the new tooth-fault indicator as a condition variable in the ensemble. you can use this designation to track and refer to variables in the ensemble data that represent conditions under which the member data was generated.
ensemble.conditionvariables = "toothfault";
ensemble.conditionvariables
ans = "toothfault"
now, each ensemble member contains the original unprocessed data and an additional variable indicating the fault condition under which the data was collected. in practice, you might compute and append other values derived from the raw vibration data, to identify potential condition indicators that you can use for fault detection and diagnosis. for a more detailed example that shows more ways to manipulate and analyze data stored in a simulationensembledatastore
object, see using simulink to generate fault data.
read multiple members at once
if it is efficient or useful for the processing you want to do, you can configure the ensemble to read data from multiple members at once. to do so, use the readsize
property. the read
command uses this property to determine how many ensemble members to read at one time. for example, configure the ensemble to read two members at a time.
ensemble.readsize = 2;
changing the value of readsize
also resets the ensemble to its unread state. thus, the next read operation reads the first two ensemble members. read
returns a table with a number of rows equal to readsize
.
ensemble.selectedvariables = ["vibration";"toothfault"]; data3 = read(ensemble)
data3=2×2 table
vibration toothfault
_________________ __________
{589x1 timetable} false
{603x1 timetable} false
the lastmemberread
property of the ensemble contains the file names of all ensemble members that were read in this operation.
ensemble.lastmemberread
ans = 2x1 string
"/tmp/bdoc23a_2213998_1395893/tp259924b3/predmaint-ex54897023/data/transmissioncasingsimplified_log_1.mat"
"/tmp/bdoc23a_2213998_1395893/tp259924b3/predmaint-ex54897023/data/transmissioncasingsimplified_log_2.mat"
when you append data to an ensemble datastore that has readsize
> 1, you must write to the same number of ensemble members as you read. thus, for instance, when readsize
= 2, supply a two-row table to writetolastmemberread
.
see also
| | |