How to use PSIM Simplified C Block sum function

User: "Hallay"
Altair Community Member

Hello expert

How to use sum function in PSIM Simplified C block?

Such like sum+=buffer[index].

index++;
if (index >= 10)
index = 0;

Regards

Find more posts tagged with

Sort by:
1 - 2 of 21
    User: "RSGarciarivas"
    Altair Employee
    Accepted Answer

    hello again @Hallay,

    There are several ways of doing this and you can even avoid using C blocks, although these are a very efficient way of creating a moving average filter.

    You can avoid using C code if you implement something like this:

    image.png

    Here, using unit delays to keep track of the latest 10 values and dividing over 10 to get the average at the end.

    If you want to use a C block, you just have to define a mechanism for each to get executed, if you don't need it running at each time step and use an array as a buffer internally.

    I implemented a simple edge detector for the block to run only at each rising/falling edge. Note that I used the standard C block because I was having a small issue declaring an array in the simplified C block, though you can try that on your side). This is the meaningful part of the code:

    image.png

    Both implementations will produce very similar (although not identical) results:

    image.png

    This plot shows the moving average from the C block, the square wave that triggers the C block and the moving average from the sums and unit delays implementation.

    Hope these are useful examples! I'm attaching the model with both options for your reference.

    Rafael

    User: "RSGarciarivas"
    Altair Employee
    Accepted Answer

    Hi @Hallay. Glad to see it's useful.

    The main difference is that in the C block you have to manually define all variables you use, have to read inputs from a single input array and write them into a single output array. Think of the simplified C block as a version that wraps around the standard C block and gives you the capability to define inputs, outputs and state variables in a friendlier way (from the block parameters instead of in the C code itself). Also, the standard C block allows you to run some code to initialize it and at the end of the simulation.


    Probably you can build this same system in the simplified C block, I just picked the standard C block to declare the array at the beginning of the simulation.

    Rafael