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.
  1 件のコメント
Roman Föll
Roman Föll 2021 年 10 月 7 日
Thank you for this very fast answer!

サインインしてコメントする。


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