Accelerating the pace of engineering and science

Documentation Center

• Trial Software

Import and Export Sample-Based Signals

 Note:   Starting in R2010b, many DSP System Toolbox™ blocks received a new parameter to control whether they perform sample- or frame-based processing. The following content has not been updated to reflect this change. For more information, see the Frame-Based Processing section of the Release Notes.

Import Sample-Based Vector Signals

The Signal From Workspace block generates a sample-based vector signal when the variable or expression in the Signal parameter is a matrix and the Samples per frame parameter is set to 1. Each column of the input matrix represents a different channel. Beginning with the first row of the matrix, the block outputs one row of the matrix at each sample time. Therefore, if the Signal parameter specifies an M-by-N matrix, the output of the Signal From Workspace block is M 1-by-N row vectors representing N channels.

The figure below is a graphical representation of this process for a 6-by-4 workspace matrix, A.

In the following example, you use the Signal From Workspace block to import a sample-based vector signal into your model:

1. Open the Signal From Workspace Example 3 model by typing ex_importsbvectorsigsex_importsbvectorsigs at the MATLAB® command line.

2. At the MATLAB command line, type A = [1:100;-1:-1:-100]';

The matrix A represents a two column signal, where each column is a different channel.

3. At the MATLAB command line, type B = 5*ones(100,1);

The vector B represents a single-channel signal.

4. Double-click the Signal From Workspace block, and set the block parameters as follows:

• Signal = [A B]

• Sample time = 1

• Samples per frame = 1

• Form output after final data value = Setting to zero

The Signal expression [A B] uses the standard MATLAB syntax for horizontally concatenating matrices and appends column vector B to the right of matrix A. The Signal From Workspace block outputs a sample-based signal with a sample period of 1 second. After the block has output the signal, all subsequent outputs have a value of zero.

5. Save these parameters and close the dialog box by clicking OK.

6. Run the model.

The following figure is a graphical representation of the model's behavior during simulation.

The first row of the input matrix [A B] is output at time t=0, the second row of the input matrix is output at time t=1, and so on.

You have now successfully imported a sample-based vector signal into your signal processing model using the Signal From Workspace block.

Import Sample-Based Matrix Signals

The Signal From Workspace block generates a sample-based matrix signal when the variable or expression in the Signal parameter is a three-dimensional array and the Samples per frame parameter is set to 1. Beginning with the first page of the array, the block outputs a single page of the array to the output at each sample time. Therefore, if the Signal parameter specifies an M-by-N-by-P array, the output of the Signal From Workspace block is P M-by-N matrices representing M*N channels.

The following figure is a graphical illustration of this process for a 6-by-4-by-5 workspace array A.

In the following example, you use the Signal From Workspace block to import a four-channel, sample-based matrix signal into a Simulink® model:

1. Open the Signal From Workspace Example 4 model by typing ex_importsbmatrixsigsex_importsbmatrixsigs at the MATLAB command line.

Also, the following variables are loaded into the MATLAB workspace:

 Fs 1x1 8 double array dsp_examples_A 2x2x100 3200 double array dsp_examples_sig1 1x1x100 800 double array dsp_examples_sig12 1x2x100 1600 double array dsp_examples_sig2 1x1x100 800 double array dsp_examples_sig3 1x1x100 800 double array dsp_examples_sig34 1x2x100 1600 double array dsp_examples_sig4 1x1x100 800 double array mtlb 4001x1 32008 double array

2. Double-click the Signal From Workspace block. Set the block parameters as follows, and then click OK:

• Signal = dsp_examples_A

• Sample time = 1

• Samples per frame = 1

• Form output after final data value = Setting to zero

The dsp_examples_A array represents a four-channel, sample-based signal with 100 samples in each channel. This is the signal that you want to import, and it was created in the following way:

```dsp_examples_sig1 = reshape(1:100,[1 1 100])
dsp_examples_sig2 = reshape(-1:-1:-100,[1 1 100])
dsp_examples_sig3 = zeros(1,1,100)
dsp_examples_sig4 = 5*ones(1,1,100)
dsp_examples_sig12 = cat(2,sig1,sig2)
dsp_examples_sig34 = cat(2,sig3,sig4)
dsp_examples_A = cat(1,sig12,sig34)	% 2-by-2-by-100 array
```
3. Run the model.

The figure below is a graphical representation of the model's behavior during simulation.

The Signal From Workspace block imports the four-channel sample based signal from the MATLAB workspace into the Simulink model one matrix at a time.

You have now successfully imported a sample-based matrix signal into your model using the Signal From Workspace block.

Export Sample-Based Signals

The Signal To Workspace and Triggered To Workspace blocks are the primary blocks for exporting signals of all dimensions from a Simulink model to the MATLAB workspace.

A sample-based signal, with M*N channels, is represented in Simulink as a sequence of M-by-N matrices. When the input to the Signal To Workspace block is a sample-based signal, the block creates an M-by-N-by-P array in the MATLAB workspace containing the P most recent samples from each channel. The number of pages, P, is specified by the Limit data points to last parameter. The newest samples are added at the end of the array.

The following figure is the graphical illustration of this process using a 6-by-4 sample-based signal exported to workspace array A.

The workspace array always has time running along its third dimension, P. Samples are saved along the P dimension whether the input is a matrix, vector, or scalar (single channel case).

In the following example you use a Signal To Workspace block to export a sample-based matrix signal to the MATLAB workspace:

1. Open the Signal From Workspace Example 6 model by typing ex_exportsbsigsex_exportsbsigs at the MATLAB command line.

Also, the following variables are loaded into the MATLAB workspace:

 dsp_examples_A 2x2x100 3200 double array dsp_examples_sig1 1x1x100 800 double array dsp_examples_sig12 1x2x100 1600 double array dsp_examples_sig2 1x1x100 800 double array dsp_examples_sig3 1x1x100 800 double array dsp_examples_sig34 1x2x100 1600 double array dsp_examples_sig4 1x1x100 800 double array

In this model, the Signal From Workspace block imports a four-channel sample-based signal called dsp_examples_A. This signal is then exported to the MATLAB workspace using a Signal to Workspace block.

2. Double-click the Signal From Workspace block. Set the block parameters as follows, and then click OK:

• Signal = dsp_examples_A

• Sample time = 1

• Samples per frame = 1

• Form output after final data value = Setting to zero

Based on these parameters, the Signal From Workspace block outputs a sample-based signal with a sample period of 1 second. After the block has output the signal, all subsequent outputs have a value of zero.

3. Double-click the Signal To Workspace block. Set the block parameters as follows, and then click OK:

• Variable name = dsp_examples_yout

• Limit data points to last parameter to inf

• Decimation = 1

Based on these parameters, the Signal To Workspace block exports its sample-based input signal to a variable called dsp_examples_yout in the MATLAB workspace. The workspace variable can grow indefinitely large in order to capture all of the input data. The signal is not decimated before it is exported to the MATLAB workspace.

4. Run the model.

5. At the MATLAB command line, type dsp_examples_yout.

The four-channel sample-based signal, dsp_examples_A, is output at the MATLAB command line. The following is a portion of the output that is displayed.

```dsp_examples_yout(:,:,1) =

1    -1
0     5

dsp_examples_yout(:,:,2) =

2    -2
0     5

dsp_examples_yout(:,:,3) =

3    -3
0     5

dsp_examples_yout(:,:,4) =

4    -4
0     5
```

Each page of the output represents a different sample time, and each element of the matrices is in a separate channel.

You have now successfully exported a four-channel sample-based signal from a Simulink model to the MATLAB workspace using the Signal To Workspace block.