Generating C code from MATLAB functions
7 views (last 30 days)
Show older comments
Hello,
I have two functions in MATLAB. One of them implements a filter for some signals and I really need this. I have an APP in android studio, and I'm trying to generate C code from these two functions so I can use them in my APP, but when I do this, I have some functions that are not supported in C.
First, this is my filter code:
function Hd = hpfilter
Fs = 1000; % Sampling Frequency
Fstop = 0.4; % Stopband Frequency
Fpass = 0.8; % Passband Frequency
Astop = 60; % Stopband Attenuation (dB)
Apass = 1; % Passband Ripple (dB)
match = 'passband'; % Band to match exactly
% Construct an FDESIGN object and call its ELLIP method.
h = fdesign.highpass(Fstop, Fpass, Astop, Apass, Fs);
Hd = design(h, 'cheby2', 'MatchExactly', match);
end
And it says the functions design and fdesign.highpass are not supported. I tried using coder.ceval, but then it says it doesn't recognize the type of my variable h.
In my other function, the unsupported variables are:
pwelch and periodogram
Do you know if I can replace them with any others that are supported with the same result? or any suggestions for it to work?
Thank you a lot!
0 Comments
Answers (2)
Honglei Chen
on 25 Oct 2017
Could you share which release are you using? Looks like your script is generated from filterDesigner (or FDAToll)? If you are using recent releases, you can choose 'Generate MATLAB Code' -> 'Data Filtering Function' to get the code that supports code generation.
If your release doesn't support this, you may want to use coder.extrinsic to compute the coefficients and then use filter() function to filter the data, that should work too.
pwelch and periodogram don't support code generation yet, but periodogram is simply the magnitude square of FFT so you can just use fft, which supports code generation, to compute it.
HTH
Sean de Wolski
on 25 Oct 2017
You could use the dsp.SpectrumEstimator which does the same thing as pwelch and supports code generation.
4 Comments
Sean de Wolski
on 26 Oct 2017
Something along these lines, not tested in ML:
pest = dsp.SpectrumEstimator('Window',rectwin(length(x)),'SampleRate',fs,'FFTLength',N)
f = getFrequencyVector(pest)
p = pest(x);
See Also
Categories
Find more on C Code Generation 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!