C Code generation FFT with variable input size
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Darshan Ramakant Bhat
am 7 Okt. 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
am 25 Okt. 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 Kommentar
Fred Smith
am 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
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!