README.rst
1.. zephyr:code-sample:: cmsis-dsp-moving-average
2 :name: CMSIS-DSP moving average
3
4 Use the CMSIS-DSP library to calculate the moving average of a signal.
5
6Overview
7********
8
9This sample demonstrates how to use the CMSIS-DSP library to calculate the moving average of a
10signal.
11
12It can be run on any board supported in Zephyr, but note that CMSIS-DSP is specifically optimized
13for ARM Cortex-A and Cortex-M processors.
14
15A **moving average** filter is a common method used for smoothing noisy data. It can be implemented
16as a finite impulse response (FIR) filter where the filter coefficients are all equal to 1/N, where
17N is the number of "taps" (i.e. the size of the moving average window).
18
19The sample uses a very simple input signal of 32 samples, and computes the moving average using a
20"window" of 10 samples. The resulting output is computed in one single call to the ``arm_fir_q31()``
21CMSIS-DSP function, and displayed on the console.
22
23.. note::
24 In order to allow an easy comparison of the efficiency of the CMSIS-DSP library when used on ARM
25 processors vs. other architectures, the sample outputs the time and number of cycles it took to
26 compute the moving average.
27
28Requirements
29************
30
31CMSIS-DSP is an optional module and needs to be added explicitly to your Zephyr workspace:
32
33.. code-block:: shell
34
35 west config manifest.project-filter -- +cmsis-dsp
36 west update cmsis-dsp
37
38Building and Running
39*********************
40
41The demo can be built as follows:
42
43.. zephyr-app-commands::
44 :zephyr-app: samples/modules/cmsis-dsp/moving_average
45 :host-os: unix
46 :board: qemu_cortex_m0
47 :goals: run
48 :compact:
49
50The sample will output the number of cycles it took to compute the moving averages, as well as the
51computed average for each 10-sample long window of the input signal.
52
53.. code-block:: console
54
55 *** Booting Zephyr OS build v3.6.0-224-gb55824751d6c ***
56 Time: 244 us (244 cycles)
57 Input[00]: 0 0 0 0 0 0 0 0 0 0 | Output[00]: 0.00
58 Input[01]: 0 0 0 0 0 0 0 0 0 1 | Output[01]: 0.10
59 Input[02]: 0 0 0 0 0 0 0 0 1 2 | Output[02]: 0.30
60 Input[03]: 0 0 0 0 0 0 0 1 2 3 | Output[03]: 0.60
61 ...
62 Input[30]: 21 22 23 24 25 26 27 28 29 30 | Output[30]: 25.50
63 Input[31]: 22 23 24 25 26 27 28 29 30 31 | Output[31]: 26.50
64