C Code generation FFT with variable input size
4 views (last 30 days)
Show older comments
Hi together,
I want to generate C Code from the FFT from following function:
function spectrum = myFFT(in)
Fs = size(in,1);
fft_out = fft(in,Fs);
P = abs(fft_out/Fs);
spectrum = P(1:Fs/2+1);
spectrum(2:end-1) = 2*spectrum(2:end-1);
end
When I generate C Code with the following script, i have to define an input:
dlcfg = coder.DeepLearningConfig('arm-compute');
dlcfg.ArmArchitecture = 'armv7';
dlcfg.ArmComputeVersion = '20.02.1';
cfg = coder.config('lib');
cfg.TargetLang = 'C++';
cfg.GenCodeOnly = true;
cfg.DeepLearningConfig = dlcfg;
codegen -config cfg myFFT -args {rand(400,1,'single')} -report
But when I define an input, Matlab generates C Code specific for this input size of 400.
When I use i.e. rand(100,1,'single') the generated C Code is much smaller.
How can I generate C Code of the FFT with variable input sizes?
Thanks
0 Comments
Answers (2)
Darshan Ramakant Bhat
on 7 Oct 2021
codegen -config cfg myFFT -args {coder.typeof(0,[400,1],[1,1])} -report
You can refer above mentioned document for more info about the syntax.
Roman Foell
on 25 Oct 2021
One further question, which came up is:
How can I tell Matlab to generate C Code with variable input-size, when myFFT-function is not the most upper function.
What I mean is, that I have a meta-function, which calls myFFT with two input-sizes, lets say:
function out = mymeta_function(in)
in_1 = in(1:1000);
in_2 = in(1001:10000);
out_FFT_1 = myFFT(in_1);
out_FFT_2 = myFFT(in_2);
out = some_other_function(out_FFT_1,out_FFT_2);
end
Thanks for your answer!
1 Comment
Fred Smith
on 18 Nov 2021
There are a few different approaches you can take.
You can make in_1 and in_2 variable-sized using a coder.varsize declaration. Alternatively you can use coder.ignoreSize on the inputs to myFFT if you only want to control these specific call sites.
These solutions require managing each call to myFFT. It allows the flexibility for a particular call to use fixed-size code if desired. However, if you never want myFFT to generate custom code for specific sizes the solution below shows how to do that:
% This code shows how to enforce that only one version of myFFT (see above) is
% generated for all sizes without changing the calls to myFFT.
function spectrum = myFFT(in)
coder.inline('always'); % Eliminate the overhead of the wrapper.
spectrum = myFFT_internal(coder.ignoreSize(in));
end
function spectrum = myFFT_internal(in)
% Original content of myFFT
Fs = size(in,1);
fft_out = fft(in,Fs);
P = abs(fft_out/Fs);
spectrum = P(1:Fs/2+1);
spectrum(2:end-1) = 2*spectrum(2:end-1);
end
See Also
Categories
Find more on DSP Algorithm Acceleration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!