🎉Community Raffle - Win $25

An exclusive raffle opportunity for active members like you! Complete your profile, answer questions and get your first accepted badge to enter the raffle.
Join and Win

How to use PSIM Simplified C Block sum function

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 - 1 of 11

    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