HDL coder implementation of dsp.MovingAverage (moving/block average)

조회 수: 9 (최근 30일)
Anze Slosar
Anze Slosar 2022년 12월 8일
편집: zs-ea11r 2023년 5월 18일
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.

채택된 답변

Kiran Kintali
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.
  댓글 수: 4
zs-ea11r
zs-ea11r 2023년 5월 18일
편집: zs-ea11r 2023년 5월 18일
Trying to implement your code (with slight modifications) and I keep getting errors when running Generate HDL Code.
The error I get is as follows: "Found division expression with unsupported Rounding Method for HDL code generation at Function 'divide'.... "
The only changes I made to your code listed above are the window size is an input (SIZE) to the function and the variable is then passed to the persistent call. The issue is the window_inverse persistent call creates an error when I perform fi casting to 1/SIZE.
I cannot use code.const since it cannot make an expression a constant.
When looking up the error above, I found solutions that suggest changing the properties of the MATLAB Functions to fimath("RoundingMethod','Floor'....
I entered the following into the Specify Other Field Entry box in properties:
fimath('RoundingMethod','Floor','OverflowAction','Saturate','ProductMode','FullPrecision','SumMode',FullPrecision').
I get this error: "Conversion to double from embedded.fimath is not possible."
Not sure what is a double, since I am initializing each input as fixdt.
Please help with a work around.
Kiran Kintali
Kiran Kintali 2023년 5월 18일
Can you share your design, testbench and project files?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Code Generation에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by