MATLAB Answers

How do I call an FFT multiple times with HDL Coder: System Object Methods in Loops?

3 views (last 30 days)
Brian Stevens
Brian Stevens on 5 Mar 2020
I want to create a 128 point FFT and call it multiple times in an HDL project.
But first I wanted to create a 128 point FFT, and found that the dsp.HDLFFT system object it needs to be called iteratively.
I tried following this example:
Create Vector-Input FFT for HDL Generation:
In the example, I can create the HDL code for the FFT that uses the dsp.HDLFFT system object if...
ONLY the object blocks instantiation and call are done once.
However, this design needs to be run multiple times as the dsp.HDLFFT input is maxed to 64 points.
This is because the dsp.HDLFFT is an iterative FFT.
I tried to move the loop that loops through all the 128 input points from the test script into code generation.
However, I found that system objects [both instantiations and method calls] (those that come from dsp.HDLFFT) can not be put within a loop.
Below is the code that generates this error: "System object methods, in file 'fft_function_fixpt' line 0, col 0, cannot be called inside 'for' loops, 'while' loops and switch statements in HDL code generation."
Question #1: This then leads to the question, how do I call an FFT multiple times in an HDL coder design if the system object method can not be put in a loop.
Wouldn't it make sense that you could instantiate an FFT on an FPGA and then call it multiple times? I don't want to instantiate all the FFTs that I need to do upfront.
Question #2: I also thought, could synthesize the dsp system call in a different HDL coder project. Then call this sub-module multiple times in for loop?
Is there an easy way to do this?
%% Create Vector-Input FFT for HDL Generation
% Create specifications and input signal. This example uses a
% 128-point FFT and computes the transform over 16 samples at a time.
N = 128;
V = 16;
Fs = 40;
t = (0:N-1)'/Fs;
x = sin(2*pi*15*t) + 0.75*cos(2*pi*10*t);
y = x + .25*randn(size(x));
y_fixed = sfi(y,32,24);
y_vect = zeros(128,1);
y_vect = reshape(y_fixed,N,1);
% Write a function that creates and calls the System object(TM). The
% function does not need to know the vector size. The
% object saves the size of the input signal the first time you call it.
% *Note:* This object syntax runs only in R2016b or later. If you are using an
% earlier release, replace each call of an object with the equivalent |step|
% syntax. For example, replace |myObject(x)| with |step(myObject,x)|.
% <include>HDLFFT128V16.m</include>
[Yf_flat,loop_count] = fft_function(y_vect);
Yf_flat = reshape(Yf_flat,N,[]);
% Plot the frequency channel data from the FFT. The FFT output is in
% bit-reversed order. Reorder it before plotting.
Yr = bitrevorder(Yf_flat);
title('Single-Sided Amplitude Spectrum of Noisy Signal y(t)')
xlabel('Frequency (Hz)')
ylabel('Output of FFT (f)')
function [output,loopCount] = fft_function(y_fixed)
V = 16;
N = 128;
y_vect = reshape(y_fixed,V,N/V);
% Compute the FFT by passing 16-element vectors to the object. Use the
% |getLatency| function to find out when the first output data sample will be
% ready. Then, add the frame length to determine how many times to call the
% object. Because the object variable is inside the function, use a second
% object to call |getLatency|. Use the loop counter to flip |validIn|
% to |false| after _N_ input samples.
tempfft = dsp.HDLFFT;
%loopCount = getLatency(tempfft,N,V)+N/V;
loopCount = 68;
Yf = complex(zeros(V,loopCount));
inin = complex(zeros(V,1));
validOut = false(V,loopCount);
persistent fft128v16;
if isempty(fft128v16)
fft128v16 = dsp.HDLFFT('FFTLength',128);
for loop = 1:1:loopCount
if ( mod(loop,N/V) == 0 )
i = N/V;
i = mod(loop,N/V);
inin = complex(y_vect(:,i));
logical_test = (loop<=N/V);
[Yf(:,loop),validOut(loop)] = fft128v16(inin(1:16),logical_test);
%[Yf(:,loop),validOut(loop)] = HDLFFT128V16(inin(1:16),logical_test);
% Discard invalid output samples.
C = complex(zeros(16,8));
%C = Yf(:,validOut==1);
C = Yf(:,1+end-8:end);
output = complex(zeros(128,1));
output(1:128) = C(:);


Sign in to comment.

Answers (1)

Bharath Venkataraman
Bharath Venkataraman on 9 Mar 2020
As shown in the example, the HDL FFT needs into the separated out into a separte design file. The example puts the HDL-optimized FFT into the design, while putting the for-loop into the "testbench". The way to think about this is that the HDL FFT design will do the 128-point FFT operation, while the rest of teh design needs to pass in 16 values in every clock.


Show 3 older comments
Bharath Venkataraman
Bharath Venkataraman on 10 Mar 2020
Thanks for clarifying the question.
Yes, this FFT is callable as needed from a larger HDL Coder project. To do that, you can just use a subsystem that has the FFT logic in it. You can also use model reference for this behavior.
In HDL, you can instantiate the highest level entity from the HDL files as many times you need.
If you need FFTs that need a change in parameters or input sizes, you will have to use a different HDL Optimized FFT blocks for each case that you need. One exception to this is a variable-size FFT which can be implementated using the largest FFT as shown in this example.
Brian Stevens
Brian Stevens on 11 Mar 2020
Thank you Bharath!
Just to clarify. If I use model reference, I can programmatically create a subsystem, put the FFT that I generated with HDL coder in that subsystem.
Then call that subsystem from a matlab script and code generate the entire 'system'/script which includes calls to that subsystem(the FFT)?
Bharath Venkataraman
Bharath Venkataraman on 11 Mar 2020
You can just put the FFT into a subsystem with other logic. This logic for example, could feed data from different sources and figure out which one to feed into the FFT subsystem.
I still feel that I am missing something you are asking - not sure why you asked about a MATLAB script.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!