Basic math with AOs


Most of the basic math operations supported by MATLAB have been implemented as AO class methods. For example, suppose you want to add two AOs together, then you can do

    a = ao(1);
    b = ao(2);
    c = a+b
    plot(c.hist)

Note: the units of the two AOs for addition and subtraction must be the same. You can't add apples to oranges, but you can add dimensionless (empty units) to oranges.

Some of the standard operators can act as modifiers. For example, if you want to square an AO:

    a = ao(2);
    a.^2

will do the job.

The operators follow MATLAB rules whenever possible. So if you want to add a single AO to a vector of AOs, you can. However, if you want to add two vectors of AOs together, the two vectors must contain the same number of AOs. For example,

    a = [ao(1) ao(2) ao(3)];
    b = ao(4);
    c = a + b

will work fine and result in b being added to each element of a. However,

    a = [ao(1) ao(2) ao(3)];
    b = [ao(4) ao(5)];
    c = a + b

will give an error.

You can do all of this on the workbench as well, of course.

Try the following:

  1. Start up the workbench, and/or open a new pipeline
  2. Drag an ao constructor block to the canvas
  3. Set the AO block to be constructed "From XY Values"
  4. Duplicate the block two more times (ctrl/cmd-d)
  5. Enter a number for the sampling ferquency fs
  6. Enter a vector the same length for the y value of each block
  7. Drag a plus block to the canvas
  8. You'll see that by default the "plus" block has two inputs. To add another input, right-click on the block and choose "Add input" from the context menu
  9. Connect each AO block to the plus block. The easiest way to do that is to select the AO block (source) and then ctrl+left-click (cmd+left-click on Mac systems) on the plus (destination) block. You can also drag a pipe from the output terminal of the AO blocks to the input terminals of the plus block if you want to be explicit about which input ports are used.
  10. Add an iplot block to the canvas and connect the output of the plus block to the input of the iplot block. You should now have a pipeline something like:
    AO plus example
  11. Execute the pipeline and you should see a plot something like the one below, depending on what data values you gave to the ao constructors AO plus plot



©LTP Team