Main Content

Verify FIR Filter on ARM Cortex-A Processor in MATLAB

This example shows how to use the Code Replacement Library (CRL) for ARM Cortex-A processor with DSP System Toolbox™. The example uses a dsp.FIRFilter (DSP System Toolbox) object to filter two sine waves of different frequencies.

Setup and Simulate

1. Open the ex_fir_ne10_tut_ml example function, which implements a lowpass FIR filter object.

function y1 = ex_fir_ne10_tut_ml(u1)
% Copyright 2014-2022 The MathWorks, Inc.

persistent fir;
if isempty(fir)
    fir = dsp.FIRFilter('Numerator', fir1(63, 0.33));
end
y1 = fir(u1);

end

2. Create two sine wave signals with 1 KHz and 3 KHz frequencies, respectively.

sin1 = dsp.SineWave('Amplitude',1,'Frequency',1000,...
    'SampleRate',8000, 'SamplesPerFrame', 76,...
    'OutputDataType', 'single');

sin2 = dsp.SineWave('Amplitude',4,'Frequency',3000,...
    'SampleRate',8000, 'SamplesPerFrame', 76,...
    'OutputDataType', 'single');

3. Create a spectrum analyzer to view the spectrum of the input and filtered output.

scope = spectrumAnalyzer('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;
for k = 1:NN
    x1k = sin1(); % generate 1K Hz sine wave
    x3k = sin2(); % generate 3K Hz sine wave
    n1 = randn(size(x1k), 'single')*sqrt(.05); % generate noise signal
    u1 = x1k+x3k+n1;
    y1 = ex_fir_ne10_tut_ml(u1);
    scope([u1,y1]);
end

Configure for Code Replacement

1. Create a code generation configuration object for use with codegen when generating a C/C++ static library.

cfg = coder.config( "lib", "ecoder", true );
cfg.GenCodeOnly = true;
cfg.CodeReplacementLibrary = "GCC ARM Cortex-A";
cfg.HardwareImplementation.ProdHWDeviceType = "ARM Compatible->ARM Cortex-A";

2. Open the Custom Code panel of the configuration dialog and verify the settings.

cfg.dialog

Generate Code

1. Generate C code for the MATLAB function ex_fir_ne10_tut_ml.

codegen ex_fir_ne10_tut_ml -args single(0) -config cfg -report
Code generation successful: View report

2. When code generation completes, click View report to display the code generation report.

3. Click on the ex_fir_ne10_tut_ml.c file. The code includes the calls to the accelerated NE10 functions, ne10_fir_init_float and ne10_fir_float_neon in the ex_fir_ne10_tut_ml function.

...

float ex_fir_ne10_tut_ml(float u1)
{
  ...
  if (fir.isInitialized != 1) {
    ...
    ne10_fir_init_float(&fir.cSFunObject.S, 64U, &fir.cSFunObject.qCoeff[0],
                        &fir.cSFunObject.pState[0], 1U);
  }
  ...
  ne10_fir_float_neon(&fir.cSFunObject.S, &U0, &b_y1, 1U);
  ...
}
...

4. Optionally, the generated code can be compiled and executed on ARM Cortex-A target by using an appropriate toolchain.