C Code generation FFT with variable input size

4 次查看(过去 30 天)
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
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 DSP Algorithm Acceleration 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by