A Raspberry Pi is a mini computer based on the ARM architecture designed to increase accessibility to computing. It is designed to be cheap and easy to use, and is a great way to learn about computers and programming.
[^note]: same as PlayStation3, Xbox 360, PSP, PS Vita, Nintendo Switch, and many Android devices
Raspberry Pi OS comes with a number of programming languages pre-installed, including python and C++.
If you want to install additional software, you can use the apt package manager to install software from the official repositories.
sudo su -
apt-get update
apt-get install python3-matplotlib
apt-get install python3-scipy
It should be noticed that the Raspberry Pi is not a very powerful computer, and the official package manager may provide the most optimised version of the required software, even if not up-to-date.
Before starting the Raspberry Pi, you need to install the operating system on a microSD card.
Once the OS is installed, you can insert the microSD card into the Raspberry Pi and power it on by plugging in the power adapter.
To use the Raspberry Pi, you need to connect it to a monitor (HDMI), keyboard and mouse (USB).
There is official software from Raspberry Pi to acquire images and videos from the camera, namely the libcamera software stack and the Picamera2 Python library.
We found though that the OpenCV library is more convenient to use, and it is also more powerful. See the tutorial next.
You can use whatever you like to analyse the data, but we recommend using Python and the Jupyter Notebook. In case you find the Raspberry Pi too slow for your analysis, you can use your own computer or Google Colab to analyse the data.
To install Jupyter Notebook on the Raspberry Pi, you can use the following commands (assuming python3-matplotlib
and python3-scipy
are already installed, see above):
pip3 install --upgrade pip
reboot
pip3 install jupyter
A quick reminder on how to plot data in Python using the matplotlib
library.
from matplotlib import pyplot as plt
import numpy as np
An example of plotting a cosine and sine wave:
X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C, S = np.cos(X), np.sin(X)
plt.figure(figsize=(4,3))
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cos(x)")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sin(x)")
plt.xlabel("x")
plt.ylabel("y")
plt.legend(loc='upper left')
<matplotlib.legend.Legend at 0x122035010>
Let's see how to use it to fit a sine distribution to a set of data.
X = np.array([1,2,3,4,5,6,7,8,9,10])
Y = np.sin(X)+np.random.normal(0,0.05,10)
sY= np.ones(10)*0.1
plt.figure(figsize=(4,3))
plt.errorbar(X,Y,sY,fmt='o',color='black')
plt.xlabel("x")
plt.ylabel("y")
Text(0, 0.5, 'y')
We need to specify the fitting function, which is a sine function in this case:
def func(x,A,w,phi):
return A*np.sin(w*x+phi)
iMinuit
can be loaded with the following commands:
from iminuit import Minuit
from iminuit.cost import LeastSquares
and is called with the following syntax:
least_squares = LeastSquares (X, Y, sY, func)
my_minuit = Minuit (least_squares, A = 1, w = 1, phi=0) # starting values for A, w, and phi
my_minuit.migrad () # finds minimum of least_squares function
my_minuit.hesse () # accurately computes uncertainties
Migrad | |
---|---|
FCN = 1.175 (χ²/ndof = 0.2) | Nfcn = 70 |
EDM = 2.87e-05 (Goal: 0.0002) | |
Valid Minimum | Below EDM threshold (goal x 10) |
No parameters at limit | Below call limit |
Hesse ok | Covariance accurate |
Name | Value | Hesse Error | Minos Error- | Minos Error+ | Limit- | Limit+ | Fixed | |
---|---|---|---|---|---|---|---|---|
0 | A | 0.99 | 0.04 | |||||
1 | w | 0.995 | 0.016 | |||||
2 | phi | 0.02 | 0.10 |
A | w | phi | |
---|---|---|---|
A | 0.00201 | 0.01e-3 (0.013) | -0.0002 (-0.037) |
w | 0.01e-3 (0.013) | 0.000253 | -1.50e-3 (-0.902) |
phi | -0.0002 (-0.037) | -1.50e-3 (-0.902) | 0.011 |
Fit results are stored in the Minuit
object and can be accessed with the following commands:
fit_info = []
for p, v, e in zip(my_minuit.parameters, my_minuit.values, my_minuit.errors):
fit_info.append(f"{p} = ${v:.3f} \\pm {e:.3f}$")
print(fit_info)
['A = $0.988 \\pm 0.045$', 'w = $0.995 \\pm 0.016$', 'phi = $0.016 \\pm 0.105$']
If you want to use iminuit
in Colab, you can install it via pip in your notebook by calling
%pip install iminuit
As alternative to iminuit
, you can use ROOT with C++ to fit your data.