
HDL coder implementation of dsp.MovingAverage (moving/block average)
조회 수: 14(최근 30일)
표시 이전 댓글
We're having some trouble with a very simple moving average type code that compiles fine into VHDL, but either crashes the FPGA simulator or uses an insane number of resources. I tried to recode it using dsp.MovingAverage, but it won't compile (System object 'dsp.MovingAverage' is not supported for HDL code generation.) Is there an equivalent dsphdl module or some other ready to use block?
I really need a block average, not moving average (i.e. take a streaming set of numbers and average them in blocks).
Thanks.
댓글 수: 0
채택된 답변
Kiran Kintali
2022년 12월 9일
편집: Kiran Kintali
2022년 12월 9일
function [y, validOut] = moving_average(x, validIn) %#codegen
% Declare persistent array and persistent window size
persistent array window_size window_size_inverse;
% During the first call of the function, variables are initialized.
if isempty(array)
window_size = fi(25,0,8,0);
window_size_inverse = fi(coder.const(1/25),0,16,19);
array = fi(zeros(1, 25),0,8,4); % size of the window in this example is 25
end
% The following loop maintains the values needed by moving window.
% This for loop also sums up the values of the array.
sum = fi(0,0,32,15);
if(validIn)
for i = 1:24
% window size can be configured via non-tunable input parameter
% window_size-fi(1,0,8,4) window size -1
sum = fi(sum + array(i+1),0,32,15);
array(i) = array(i+1);
end
% The last position is updated based on the most recent input.
array(window_size) = x;
sum = fi(sum + array(window_size),0,32,15);
end
y = fi(sum * window_size_inverse,0,8,4); % Divided by window size
validOut = validIn;
end
You should be able to implement a basic MATLAB function implementation. This can work stand alone in MATLAB to HDL workflow or MATLAB Function Block.

Attaching out of the box resource consumption for this code. You could further pipeline and optimize the design.
댓글 수: 2
Kiran Kintali
2022년 12월 16일
MATLAB does not have such limitations. You can make window length as input argument and pass in the necessary constant values.
As you have rightly guessed if you do not declare the input arguments properly you will end up with extra ports in HDL and unnecessary hardware.
You can use https://www.mathworks.com/help/coder/ref/coder.const.html when using the codegen command. I realize there is no ready to use example doc on this. I will create an documentation request for the VHDL coding design pattern.
For defining these in the project GUI you need to right click on the input argument for the window length and mark it as a constant (and not an input variable).
Let us know if this is not clear.
There are forms of indexing and shifting supported in HDL Coder as long as you do not grow array sizes on the fly in doing so.
추가 답변(0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!