Generate C code from a filter which created by fdatool or filterbuilder

조회 수: 21 (최근 30일)
Hello everyone, Can I generate C code from a fileter which created by fdatool or filterbuilder. For example, I have created a lowpass filter with filterbuilder, and it works well in matlab. Now,I hope matlab can generate c code for this filter, so I can use this filter in my DSP board. I am looking for some simple examples. Thank you.

채택된 답변

Wayne King
Wayne King 2012년 7월 1일
Look under File -> Generate Matlab Code -> Filter Design Function. If I choose that option and then make some adjustments in the output file (code generation does not support dfilt objects or fdesign as you discovered)
function b = myfilter %#codegen
%MYFILTER Returns a discrete-time filter object.
%
% MATLAB Code
% Generated by MATLAB(R) 7.14 and the Signal Processing Toolbox 6.17.
%
% Generated on: 01-Jul-2012 05:38:14
%
% Equiripple Lowpass filter designed using the FIRPM function.
% All frequency values are normalized to 1.
Fpass = 0.45; % Passband Frequency
Fstop = 0.55; % Stopband Frequency
Dpass = 0.057501127785; % Passband Ripple
Dstop = 0.001; % Stopband Attenuation
dens = 20; % Density Factor
% Calculate the order from the parameters using FIRPMORD.
[N, Fo, Ao, W] = firpmord([Fpass, Fstop], [1 0], [Dpass, Dstop]);
% Calculate the coefficients using the FIRPM function.
b = firpm(N, Fo, Ao, W);
I am able to generate C code

추가 답변 (2개)

Wayne King
Wayne King 2012년 6월 30일
Yes, you can, but you'll need a coder product, e.g. Matlab Coder, or Simulink Coder. You can export MATLAB code for your filter and then generate C code using MATLAB Coder. Or, you can create a Simulink model for you filter and generate C code that way.
  댓글 수: 1
adream307
adream307 2012년 6월 30일
편집: Walter Roberson 2012년 6월 30일
Thank you for your replay. My matlab has matlab coder.
I generate M file from the filterbuilder, this is my M file
%%my M file
function y = FilterTest(x)
%FILTERTEST Filters input x and returns output y.
%
% MATLAB Code
% Generated by MATLAB(R) 7.14 and the Signal Processing Toolbox 6.17.
%
% Generated on: 30-Jun-2012 19:42:08
%
persistent Hd;
if isempty(Hd)
Fpass = 0.45; % Passband Frequency
Fstop = 0.55; % Stopband Frequency
Apass = 1; % Passband Ripple (dB)
Astop = 60; % Stopband Attenuation (dB)
h = fdesign.lowpass('fp,fst,ap,ast', Fpass, Fstop, Apass, Astop);
Hd = design(h, 'equiripple', ...
'MinOrder', 'any', ...
'StopbandShape', 'flat');
set(Hd,'PersistentMemory',true);
end
y = filter(Hd,x);
% [EOF]
when I try to generate C code with coder product. I get this error:
>> coder -build FilterTest.prj
??? The 'fdesign' class does not support code generation.
Error in ==> FilterTest Line: 20 Column: 9
thank you

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


Wayne King
Wayne King 2012년 6월 30일
What version of Matlab are you using? If you are using R2012a, generate the Matlab code with "Filter Design Function" or with the System object, both those options support code generation.
  댓글 수: 1
adream307
adream307 2012년 7월 1일
Yes, I am using R2012a. Where is "Filter Derisng Function",can you show me an example?
Thank you.

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

카테고리

Help CenterFile Exchange에서 Filter Design에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by