Find more posts tagged with
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:
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:
Both implementations will produce very similar (although not identical) results:
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
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
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:
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:
Both implementations will produce very similar (although not identical) results:
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
hi @Hallay,
What do you want to implement in the C block? If you give a bit more detail, I'll be able to advise.
Rafael
Hi Rafael
Thanks for your reply.
I want to calculate average value every 10 sample in the C block.
Regards