![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1225932/image.png)
HDL coder implementation of dsp.MovingAverage (moving/block average)
10 次查看(过去 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1225932/image.png)
Attaching out of the box resource consumption for this code. You could further pipeline and optimize the design.
4 个评论
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!