C Code generation FFT with variable input size

조회 수: 4 (최근 30일)
Roman Föll
Roman Föll 2021년 10월 6일
댓글: Fred Smith 2021년 11월 18일
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

답변 (2개)

Darshan Ramakant Bhat
Darshan Ramakant Bhat 2021년 10월 7일
You can use coder.typeof() :
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
Roman Foell 2021년 10월 25일
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
Fred Smith
Fred Smith 2021년 11월 18일
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

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by