# CMSIS-DSP Python package example

## Installing and importing the needed packages

Installation below may generate an error in google colab. It is related to conflicting versions of the Jinja2 package. It will not prevent the use of the cmsisdsp package in google colab.


```python
!pip install cmsisdsp
```

    Requirement already satisfied: cmsisdsp in c:\benchresults\pythonwrappertests\testenv\lib\site-packages (1.2.1)
    Requirement already satisfied: numpy>=1.19 in c:\benchresults\pythonwrappertests\testenv\lib\site-packages (from cmsisdsp) (1.22.2)
    Requirement already satisfied: jinja2>=3.0 in c:\benchresults\pythonwrappertests\testenv\lib\site-packages (from cmsisdsp) (3.0.3)
    Requirement already satisfied: networkx>=2.5 in c:\benchresults\pythonwrappertests\testenv\lib\site-packages (from cmsisdsp) (2.6.3)
    Requirement already satisfied: sympy>=1.6 in c:\benchresults\pythonwrappertests\testenv\lib\site-packages (from cmsisdsp) (1.9)
    Requirement already satisfied: MarkupSafe>=2.0 in c:\benchresults\pythonwrappertests\testenv\lib\site-packages (from jinja2>=3.0->cmsisdsp) (2.1.0)
    Requirement already satisfied: mpmath>=0.19 in c:\benchresults\pythonwrappertests\testenv\lib\site-packages (from sympy>=1.6->cmsisdsp) (1.2.1)



```python
import numpy as np
import cmsisdsp as dsp
```


```python
import matplotlib.pyplot as plt
```

## Creating the signal


```python
nb = 512
f=100
signal = np.sin(2 * np.pi * np.arange(nb)*f / nb) + 0.1*np.random.randn(nb)
```


```python
plt.plot(signal)
plt.show()
```


```python
# Array of complex numbers as an array of real numbers
def imToReal1D(a):
    ar=np.zeros(np.array(a.shape) * 2)
    ar[0::2]=a.real
    ar[1::2]=a.imag
    return(ar)

# Array of real numbers as an array of complex numbers
def realToIm1D(ar):
    return(ar[0::2] + 1j * ar[1::2])
```

## Using the F32 CMSIS-DSP FFT


```python
# CMSIS-DSP FFT F32 initialization
cfftf32=dsp.arm_cfft_instance_f32()
status=dsp.arm_cfft_init_f32(cfftf32,nb)
print(status)
```

    0



```python
# Re-evaluate this each time you change the signal
signalR = imToReal1D(signal)
resultR = dsp.arm_cfft_f32(cfftf32,signalR,0,1)
resultI = realToIm1D(resultR)
mag=20 * np.log10(np.abs(resultI))
```


```python
plt.plot(mag[1:nb//2])
plt.show()
```

## Using the Q15 CMSIS-DSP FFT


```python
# Convert the signal to Q15 and viewed as a real array
import cmsisdsp.fixedpoint as f
signalR = imToReal1D(signal)
signalRQ15 = f.toQ15(signalR)
```


```python
# Initialize the Q15 CFFT
cfftq15 = dsp.arm_cfft_instance_q15()
status = dsp.arm_cfft_init_q15(cfftq15,nb)
print(status)
```

    0



```python
# Compute the Q15 CFFT and convert back to float and complex array
resultR = dsp.arm_cfft_q15(cfftq15,signalRQ15,0,1)
resultR = f.Q15toF32(resultR)
resultI = realToIm1D(resultR)*nb
```


```python
mag = 20 * np.log10(np.abs(resultI))
plt.plot(mag[1:nb//2])
plt.show()
```


```python

```

fb0e365d4632c974d9850ab659c5519c63ae63a7
ipynb
Jupyter Notebook
CMSIS/DSP/PythonWrapper/examples/cmsisdsp_tests.ipynb
hqzzzz/CMSIS_5
6cdb1a09f63a051ca2a4c60676090304385cf2c2
Apache-2.0
CMSIS/DSP/PythonWrapper/examples/cmsisdsp_tests.ipynb
hqzzzz/CMSIS_5
6cdb1a09f63a051ca2a4c60676090304385cf2c2
Apache-2.0
CMSIS/DSP/PythonWrapper/examples/cmsisdsp_tests.ipynb
hqzzzz/CMSIS_5
6cdb1a09f63a051ca2a4c60676090304385cf2c2
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES