[PYTHON] Differentiation of time series data (discrete)

Generally, the data obtained from the sensor is discrete data. In the case of discrete data, the simplest approximation is f'(x) =\frac{f(x + h) - f(x -h)}{2h} The error is $ \ omicron (h ^ 2) $.

This calculation is easy with the slice function.

import numpy as np

def diff(x, h):
    res = x[2:] - x[:-2]
    return res/(2*h)

In the following formula, the error is a little smaller and the error is $ \ omicron (h ^ 4) $. f'(x) = \frac{-f(x +2h) + 8f(x+h)-8f(x-h)+f(x-2h)}{12h}

def diff4(x, h):
    """
1st derivative{-f(x+2h)+8f(x+h)-8f(x-h)+f(x-2h)}/12h
x is time series data,h is the time between data(second)
    """
    res = -x[4:] + 8*x[3:-1] - 8*x[1:-3] + x[:-4]
    return res/(12*h)

Check each error This time, a 1 Hz cos wave was created and differentiated. f(x) = cos(2\pi x) The error of the maximum value is measured as the most understandable value this time. max(f'(x))=2\pi

#10second 1 Hz cos wave
time = np.linspace(0, 10, num=10000, endpoint=False)
wave = np.cos(2*np.pi*time)

#Method with many errors
vel2 = diff(wave, time[1] - time[0])
#How to reduce the error
vel4 = diff4(wave, time[1] - time[0])

#Compare with theoretical value 2π
print(vel2.max()-2*np.pi)
print(vel4.max()-2*np.pi)

Output result -4.134161924973512e-05 -3.241460433400789e-10

You can see that the latter formula has less error.

In the case of double differentiation f''(x) = \frac{-f(x +2h) + 16f(x+h)-30f(x)+16f(x-h)-f(x-2h)}{12h^2} Will be.

def diffdiff4(x, h):
    """
Derivative twice,x is time series data,h is the time between data(second)
    {-f(x+2h)+16f(x+h)-30f(x)+16f(x-h)-f(x-2h)}/12h^2
    """
    res = -x[4:] +16*x[3:-1] -30*x[2:-2] +16*x[1:-3] -x[:-4]
    return res/(12*h*h)

Recommended Posts

Differentiation of time series data (discrete)
Time series analysis 3 Preprocessing of time series data
Smoothing of time series and waveform data 3 methods (smoothing)
View details of time series data with Remotte
[Python] Plot time series data
Anomaly detection of time series data by LSTM (Keras)
Calculation of time series customer loyalty
Python: Time Series Analysis: Preprocessing Time Series Data
About time series data and overfitting
A story about clustering time series data of foreign exchange
How to extract features of time series data with PySpark Basics
Comparison of time series data predictions between SARIMA and Prophet models
Predict time series data with neural network
Time series analysis 4 Construction of SARIMA model
How to handle time series data (implementation)
Reading OpenFOAM time series data and sets data
[For beginners] Script within 10 lines (5. Resample of time series data using pandas)
measurement of time
Power of forecasting methods in time series data analysis Semi-optimization (SARIMA) [Memo]
Time Series Decomposition
Plot CSV of time series data with unixtime value in Python (matplotlib)
[Kaggle] I tried feature engineering of multidimensional time series data using tsfresh.
Get time series data from k-db.com in Python
Kaggle Kernel Method Summary [Table Time Series Data]
Numerical summary of data
Python: Time Series Analysis
Features that can be extracted from time series data
[Latest method] Visualization of time series data and extraction of frequent patterns using Pan-Matrix Profile
Python time series question
RNN_LSTM1 Time series analysis
Time series analysis 1 Basics
Time series data prediction by AutoML (automatic machine learning)
Implementation of clustering k-shape method for time series data [Unsupervised learning with python Chapter 13]
Preprocessing of prefecture data
Measurement of execution time
Selection of measurement data
Display TOPIX time series
Time series plot / Matplotlib
"Measurement Time Series Analysis of Economic and Finance Data" Solving Chapter End Problems with Python
How to calculate the sum or average of time series csv data in an instant
What you should not do in the process of time series data analysis (including reflection)
How to generate exponential pulse time series data in python
Reformat the timeline of the pandas time series plot with matplotlib
Library tsfresh that automatically extracts features from time series data
Create data for series labeling (part of speech tagging) quickly
Time series analysis related memo
Tuning experiment of Tensorflow data
Visualization of data by prefecture
Differentiation of sort and generalization of sort
Fourier transform of raw data
Time series analysis part 4 VAR
Average estimation of capped data
Time series analysis Part 1 Autocorrelation
About data management of anvil-app-server
Probability prediction of imbalanced data
[numpy] Create a moving window matrix from multidimensional time series data
<Pandas> How to handle time series data in a pivot table
When plotting time series data and getting a matplotlib Overflow Error
Rewrite the field creation node of SPSS Modeler with Python. Feature extraction from time series sensor data