Efficient decimation with Embedded Coder

2 views (last 30 days)
Jim Clay
Jim Clay on 7 Nov 2012
I have the following code as part of a Matlab Function block in Simulink/Embedded Coder:
[dcsDec, state.dcsDec1FiltState] = filter(state.dcsDec1Filt, 1, state.phaseDiffBuf(1:numSampsToProcess), state.dcsDec1FiltState);
dcsDec = dcsDec(state.dcsDec1Factor:state.dcsDec1Factor:end);
[dcsDec, state.dcsDec2FiltState] = filter(state.dcsDec2Filt, 1, dcsDec, state.dcsDec2FiltState);
dcsDec = dcsDec(state.dcsDec2Factor:state.dcsDec2Factor:end);
[dcsDec, state.dcsDec3FiltState] = filter(state.dcsDec3Filt, 1, dcsDec, state.dcsDec3FiltState);
dcsDec = dcsDec(state.dcsDec3Factor:state.dcsDec3Factor:end);
state.dcsPhaseDiffBuf(state.dcsPhaseDiffBufLen+1:state.dcsPhaseDiffBufLen+length(dcsDec)) = dcsDec;
state.dcsPhaseDiffBufLen = state.dcsPhaseDiffBufLen + length(dcsDec);
I apologize for the ugly indexing, but the gist of it is that it is doing standard FIR filtering on a signal and then decimating it. The same buffer is reused to minimize the memory usage. The problem is that the D_Work global struct that Embedded Coder generates includes the following:
real_T dcsDec_data[1331];
real_T b_dcsDec_data[1328];
real_T c_dcsDec_data[1329];
real_T d_dcsDec_data[1330];
real_T c_dcsDec_data_m[1330];
real_T b_dcsDec_data_c[1329];
real_T state_data[1328];
real_T b_dcsDec_data_k[1331];
There are no other references to "dcsDec" in the Simulink model or Matlab code, so it appears to just be creating a new array for each iteration, without even recognizing that the arrays should be getting smaller. There has to be a better way to do this. What is the "right" way to do this with Embedded Coder?

Accepted Answer

Fred Smith
Fred Smith on 12 Nov 2012
If you could make all occurrences of dcsDec the same size then you would only get one variable.
If that is not possible, you could use variable-sizing. Use coder.varsize('dcsDec',[1 1331]), and that should also force a single variable dcsDec but it will carry run-time size information.
Hope one of these two ideas helps.
  1 Comment
Jim Clay
Jim Clay on 12 Nov 2012
The coder.varsize method worked very nicely. Thank you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by