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 object™. The example uses a dsp.FIRFilter System object to filter two sine waves of different frequencies.

Task 1: Setup and Simulate

1. Open the ex_fir_ne10_tut_ml example 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', 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', 8000, 'ShowLegend', true,...
                             'PlotAsTwoSidedSpectrum', false, ...
                             'RBWSource', 'property', ...
                             'RBW', 8000/260, ...
                             'YLimits', [-76, 56], ...

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);

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 = 'GCC ARM Cortex-A';
   cfgEx.HardwareImplementation.ProdHWDeviceType = 'ARM Compatible->ARM Cortex';
   cfgEx.GenCodeOnly = true;

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


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 = tempdir;
   dstarmsrc = which('ex_fir_ne10_tut_ml');
   dstarmtmpdir = tempdirObj;
   copyfile(dstarmsrc, dstarmtmpdir, 'f');
function y1 = ex_fir_ne10_tut_ml(u1)
% Copyright 2014-2016 The MathWorks, Inc.
    persistent fir;
    if isempty(fir)
        fir = dsp.FIRFilter('Numerator', fir1(63, 0.33));
    y1 = fir(u1);

2. Generate C code for the MATLAB function ex_fir_ne10_tut_ml.m.

codegen ex_fir_ne10_tut_ml -args single(u1) -config cfgEx -report

3. When code generation finishes successfully, click View report to display the code generation report.

4. Click on the ex_fir_ne10_tut_ml.c file. Notice the NE10 functions, ne10_fir_init_float and ne10_fir_float_neon in the ex_fir_ne10_tut_ml function.

Task 4: Verify the Generated C Code on Target

The generated code can be compiled and executed on ARM Cortex-A target by using a user- selected tool chain.