1.. zephyr:code-sample:: number_crunching
2   :name: Number crunching using optimized library
3
4   Set up and use different backends for complex math operations.
5
6Overview
7********
8
9Number crunching sample does vector operations, Fast Fourier Transformation and filtering.
10This example demonstrates how to include a proprietary static library into the Zephyr build system.
11The library is in an out of tree location.
12
13To use this sample, with an out of tree library, one needs to define an environment variable
14``LIB_LOCATION``.
15In that location, one needs to have a ``CMakeLists.txt`` that defines:
16
17.. code-block:: cmake
18
19	# Link with the external 3rd party library.
20	set(LIB_DIR         "lib"                   CACHE STRING "")
21	set(INCLUDE_DIR     "include"               CACHE STRING "")
22	set(LIB_NAME        "proprietary_lib.a"     CACHE STRING "")
23
24If the environment variable ``LIB_LOCATION`` is not defined, a default Zephyr API is used instead.
25
26This sample executes some mathematical functions that can be used for audio processing like
27filtering (Fast Fourier Transform (FFT)) or echo cancellation (Least Mean Square (LMS) filter
28algorithm).
29
30The sample has:
31
32- :file:`main.c`: calls the generic math functions;
33- :file:`math_ops.c`: executes the math functions, computes the cycles it took to execute and checks the output;
34- :file:`cmsis_dsp_wrapper.c`: calls the exact math functions from CMSIS-DSP if :kconfig:option:`CONFIG_CMSIS_DSP` is defined and ``LIB_LOCATION`` is not defined;
35- :file:`nature_dsp_wrapper`: if ``LIB_LOCATION`` is defined and points to an out of tree location where that NatureDSP lib and headers can be found, calls the exact math functions from NatureDSP library.
36
37If one wants to include a new backend it needs to create a new wrapper for that library or backend.
38
39Requirements
40************
41
42CMSIS-DSP is an optional module and needs to be added explicitly to your Zephyr workspace:
43
44.. code-block:: shell
45
46   west config manifest.project-filter -- +cmsis-dsp
47   west update cmsis-dsp
48
49NatureDSP can be taken from Github: https://github.com/foss-xtensa/ndsplib-hifi4/tree/main.
50To compile it use the steps described in the repository.
51
52Building and Running
53*********************
54
55To build the sample with ``west`` for the ``imx8mp_evk/mimx8ml8/adsp``, which is the HiFi4 DSP core
56from NXP i.MX8M Plus board, run:
57
58.. zephyr-app-commands::
59   :zephyr-app: samples/boards/nxp/adsp/number_crunching/
60   :board: imx8mp_evk/mimx8ml8/adsp
61   :goals: build run
62   :compact:
63
64An output example, for CMSIS-DSP is:
65
66.. code-block:: console
67
68	*** Booting Zephyr OS build v3.7.0-2815-g9018e424d7a1 ***
69
70	Proprietary library example!
71
72	[Library Test] == Vector Sum test  ==
73	[Backend] CMSIS-DSP module
74	[Library Test] Vector Sum takes 6886 cycles
75	[Library Test] == Vector Sum test end with 1 ==
76
77	[Library Test] == Vector power sum test  ==
78	[Backend] CMSIS-DSP module
79	[Library Test] Vector power sum takes 6659 cycles
80	[Library Test] == Vector power sum test end with 1 ==
81
82	[Library Test] == Vector power sum test  ==
83	[Backend] CMSIS-DSP module
84	[Library Test] Vector power sum takes 3681 cycles
85	[Library Test] == Vector power sum test end ==
86
87	[Library Test] == Fast Fourier Transform on Real Data test  ==
88	[Backend] CMSIS-DSP module
89	[Library Test] Fast Fourier Transform on Real Data takes 67956 cycles
90	[Library Test] == Fast Fourier Transform on Real Data test end ==
91
92	[Library Test] == Bi-quad Real Block IIR test  ==
93	[Backend] CMSIS-DSP module
94	[Library Test] Bi-quad Real Block IIR takes 506702 cycles
95	[Library Test] == Bi-quad Real Block IIR end ==
96
97	[Library Test] == Least Mean Square (LMS) Filter for Real Data test  ==
98	[Backend] CMSIS-DSP module
99	[Library Test] Least Mean Square (LMS) Filter for Real Data test takes 184792 cycles
100	[Library Test] == Least Mean Square (LMS) Filter for Real Data test end ==
101
102For NatureDSP, the output looks like this:
103
104.. code-block:: console
105
106	*** Booting Zephyr OS build v3.7.0-2815-g9018e424d7a1 ***
107
108	Proprietary library example!
109
110	[Library Test] == Vector Sum test  ==
111	[Backend] NatureDSP library
112	[Library Test] Vector Sum takes 3829 cycles
113	[Library Test] == Vector Sum test end with 1 ==
114
115	[Library Test] == Vector power sum test  ==
116	[Backend] NatureDSP library
117	[Library Test] Vector power sum takes 2432 cycles
118	[Library Test] == Vector power sum test end with 1 ==
119
120	[Library Test] == Vector power sum test  ==
121	[Backend] NatureDSP library
122	[Library Test] Vector power sum takes 2594 cycles
123	[Library Test] == Vector power sum test end ==
124
125	[Library Test] == Fast Fourier Transform on Real Data test  ==
126	[Backend] NatureDSP library
127	[Library Test] Fast Fourier Transform on Real Data takes 3338 cycles
128	[Library Test] == Fast Fourier Transform on Real Data test end ==
129
130	[Library Test] == Bi-quad Real Block IIR test  ==
131	[Backend] NatureDSP library
132	[Library Test] Bi-quad Real Block IIR takes 13501 cycles
133	[Library Test] == Bi-quad Real Block IIR end ==
134
135	[Library Test] == Least Mean Square (LMS) Filter for Real Data test  ==
136	[Backend] NatureDSP library
137	[Backend] NatureDSP library
138	[Library Test] Least Mean Square (LMS) Filter for Real Data test takes 7724 cycles
139	[Library Test] == Least Mean Square (LMS) Filter for Real Data test end ==
140