Verify FIR Filter on ARM Cortex-M Processor in MATLAB
This example shows how to use the Code Replacement Library (CRL) for ARM Cortex-M processor with DSP System object™. The example uses a dsp.FIRFilter System object to filter two sine waves of different frequencies.
Task 1: Setup and Simulate
1.Open theex_fircmsis_tut_mlexample function, which implements a lowpass FIR filter object.
2.Create two sine wave signals with 1KHz and 3KHz frequency, respectively.
sin1 = dsp.SineWave('Amplitude',1,'Frequency',1000,...'SampleRate',8000,'SamplesPerFrame', 75,...'OutputDataType','single'); sin2 = dsp.SineWave('Amplitude',4,'Frequency',3000,...'SampleRate',8000,'SamplesPerFrame', 75,...'OutputDataType','single');
3.Create a spectrum analyzer to view the spectrum of the input and filtered output.
= dsp范围。简介('SampleRate',8e3,'ShowLegend',true,...'PlotAsTwoSidedSpectrum', false,...'RBWSource','Property',...'RBW',8000/260,'Window','Kaiser',...'OverlapPercent', 80,...'YLimits', [-76 56],'SpectralAverages',10);
4.Simulate the example
NN = 2000;fork = 1:NN x1k = sin1();% generate 1K Hz sine wavex3k = sin2();% generate 3K Hz sine waven1 = randn(size(x1k),'single')*sqrt(.05);% generate noise signalu1 = x1k+x3k+n1; y1 = ex_fircmsis_tut_ml(u1); scope([u1,y1]);end
Task 2: Configure for Code Replacement
1.Create a code generation configuration object for use with codegen when generating a C/C++ static library.
cfgEx = coder.config('lib'); cfgEx.CodeReplacementLibrary ='ARM Cortex-M'; cfgEx.HardwareImplementation.ProdHWDeviceType ='ARM Compatible->ARM Cortex'; cfgEx.GenCodeOnly = true;
2.Open theCustom codepanel of the configuration dialog and verify the settings.
cfgEx.dialog
Task 3: Generate code
1.Change your current folder in MATLAB to a temporary writable folder. Copy the MATLAB file to the temporary folder.
tempdirObj = dstarmexample.dstTempdir('ex_fircmsis_tut_ml_workflow'); dstarmsrc = which('ex_fircmsis_tut_ml'); dstarmtmpdir = tempdirObj.tempDir; type(fullfile(dstarmsrc)) copyfile(dstarmsrc, dstarmtmpdir,'f');
function y1 = ex_fircmsis_tut_ml(u1) % Copyright 2013-2016 The MathWorks, Inc. %#codegen persistent firfilter; if isempty(firfilter) firfilter = dsp.FIRFilter('Numerator', fir1(63, 0.33)); end y1 = firfilter(u1); end
2.Generate C code for the MATLAB functionex_fircmsis_tut_ml.m.
codegenex_fircmsis_tut_ml-argssingle(u1)-configcfgEx-report
3.When code generation finishes successfully, clickView reportto display the code generation report.
4.Click on the ex_fircmsis_tut_ml.c file. Notice the CMSIS functions,arm_fir_init_f32andarm_fir_f32in the ex_fircmsis_tut_ml function.
Task 4: Verify the generated C code on target
The generated code can be compiled and executed on ARM Cortex-M target by using a user- selected tool chain, for example,ARM® KEIL™ uVision® IDE.
Task 5: Fixed-Point FIR Filter
Following similar steps of Task 3, you can generate fixed-point C code for the MATLAB functionex_fircmsis_tut_ml_q15.m.
dstarmsrc = which('ex_fircmsis_tut_ml_q15'); copyfile(dstarmsrc, dstarmtmpdir,'f'); codegenex_fircmsis_tut_ml_q15-argsfi(u1, true, 16, 15)-configcfgEx-report
Run the following code to delete the temporary directory.
status = tempdirObj.cleanUp;
dstarmexample.displayDSPARMEndOfDemoMessage (mfilename)