Generating C code from MATLAB functions

조회 수: 7 (최근 30일)
User 864
User 864 2017년 10월 25일
편집: User 864 2017년 10월 26일
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!

답변 (2개)

Honglei Chen
Honglei Chen 2017년 10월 25일
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
  댓글 수: 1
User 864
User 864 2017년 10월 25일
편집: User 864 2017년 10월 25일
Thank you!

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


Sean de Wolski
Sean de Wolski 2017년 10월 25일
You could use the dsp.SpectrumEstimator which does the same thing as pwelch and supports code generation.
  댓글 수: 4
Sean de Wolski
Sean de Wolski 2017년 10월 26일
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);
User 864
User 864 2017년 10월 26일
편집: User 864 2017년 10월 26일
I tried it but it shows me an error saying: Error using matlab.system.StringSet/findMatch Invalid setting for the Window property. And it also says: getFrequencyVector before locking the object is not supported.

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

카테고리

Help CenterFile Exchange에서 C Code Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by