We are going to simulate  time series using basic function in python. curious

This is the first post of a series I’m going to write on signal processing, machine learning and data analysis stuff.

 

Let’s simulate a signal buried in random Gaussian noise

We will use also Matplolib http://matplotlib.org/ to produce plots.
Matplotlib is an excellent python library to produce any kind of plot you want.

In [1]:
### importing the library
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
##number of point of the time series
nsample = 1024
## simulate a simple sinusoidal function
x1 = np.linspace(0, 100, nsample)
y=np.sin(x1)
In [3]:
fig, ax = plt.subplots()
ax.plot(x1, y, label="Data")
Out[3]:
[<matplotlib.lines.Line2D at 0x7f1236aa0d50>]
Now we will add some noise.

We use the numpy function random.normal to add Gaussian Noise with amplitude sigma.
The Gaussian, or normal, distribution has the following probability density
Latex formula
A time series in which each sample is distributed according to a Gaussian is called Gaussian, white noise

In [4]:
sigma = 0.3
y =sigma * np.random.normal(size=nsample)
In [5]:
fig, ax = plt.subplots()
ax.plot(x1, y, label="Data")
Out[5]:
[<matplotlib.lines.Line2D at 0x7f1236d39fd0>]
In [6]:
sigma = 0.3
y =np.sin(x1)+sigma * np.random.normal(size=nsample)
fig, ax = plt.subplots()
ax.plot(x1, y, label="Data")
Out[6]:
[<matplotlib.lines.Line2D at 0x7f12368d9e90>]

ARMA model

Now, let’s do something more complex and simulate the time series using an AutoRegressive Moving Average (ARMA) model.

The Notation ARMA(p,q) indicates and AR part with p poles and MA part with q zeros.

Latex formula

where w is a white noise with unit variance.

We can do this in many different ways and using different python library, such as scipy and its module signal, or you can write all the code from scratch.
I want to learn more about this library StatsModels: http://statsmodels.sourceforge.net/.
So I’ll learn with you how to do signal processing using statsmodels.

In [7]:
import statsmodels.api as sm
from statsmodels.tsa.arima_process import arma_generate_sample
np.random.seed(12345)

arparams = np.array([.75, -.25])
maparams = np.array([.65, .35])
arparams = np.r_[1, -arparams]
maparam = np.r_[1, maparams]

y = arma_generate_sample(arparams, maparams, nsample)
fig, ax = plt.subplots()
ax.plot(x1, y, label="Data") 
Out[7]:
[<matplotlib.lines.Line2D at 0x7f122cb63550>]

We will see in a next posts the difference of these time series in the Frequency domain representation

You can fork, download the code at the  GitHub Repo