How i do my RMS function on Embedded Matlab Function

4 views (last 30 days)
Hello.
I need to create my RMS block to use with simulink. This is the first part of a protection relay project. So i'll test many algoritms to do this. I already did this on m-file project but im want to do this a dinamic mode.
I've read about of coder.extrinsic.
Example:
% Mann & Morrison algoritm
f0=60;
w=2*pi*f0;
b=(1/w)^2;
c=1/sqrt(2);
fs=32*f0; % sample frenq. 32 sample per cycle.
dt=1/fs;
der=(I(n) - I(n-2))/(2*dt); %I is a imput (sine wave block)
Ief(n-1)=c*(sqrt(I(n-1)^2 + b*(der)^2)); %Ief is a RMS value at sample n
n=n+1;
I dont know how i work with arrays on the function block. I need a buffer?
Thanks

Accepted Answer

Ryan G
Ryan G on 29 Aug 2012
If you don't need to generate code from this Embedded MATLAB block and you are not overly concerned with simulation speed then you can call the entire function extrinsically as long as the output of the block is always the same size. You will need to initialize the output regardless by doing something like:
y = 0;
before the function call.
If the output is variable size you will need to buffer the output. Say you know the output will never exceed 100 elements, you can initialize the output like so:
y = zeros(1,100);
and fill in the block as needed with the actual values as they become available.
  3 Comments
Ryan G
Ryan G on 30 Aug 2012
I'm assuming this is still giving you issues I don't see x0 anywhere but the first branch of the if-statement. x0 will need to be initialized in all branches of the if statement because otherwise, say flag never equaled 0, x0 would never be initialized.
You may have an issue with circshift if it's not supported by code generation, however, you can set it with
coder.extrinsic('circshift')
if needed. Same may go for abs, but I bet that is supported.

Sign in to comment.

More Answers (0)

Categories

Find more on Event Functions in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!