简体   繁体   中英

Why does numerical differentiation with Python changes original signal amplitude for sine wave?

I have calculated numerical derivative of a sine wave with amplitude 1 with different methods. Although the phase seems ok, I'm getting a derivative signal amplitude of ~6 when I was expecting the same amplitude of original signal (~1). I would appreciate any help in understanding why is this happening. Here's my code and a plot with derivative values scaled and not scaled:

# -*- coding: utf-8 -*- 
from __future__ import division

import numpy as np
from scipy.interpolate import InterpolatedUnivariateSpline
import matplotlib.pyplot as plt
from scipy.interpolate import splrep, splder, splev

## Useful Links
## https://stackoverflow.com/questions/9876290/how-do-i-compute-derivative-using-numpy
## https://stackoverflow.com/questions/42197460/numpy-diff-and-scipy-fftpack-diff-giving-different-results-when-differentiat
## ## https://stackoverflow.com/questions/42197460/numpy-diff-and-scipy-fftpack-diff-giving-different-results-when-differentiat

x = np.linspace(0,1,361)

# Create sin wave values
sin = np.sin(np.radians(np.linspace(0,361,361)))

# Create cosine wave values
cos = np.cos(np.radians(np.linspace(0,361,361)))

# Create scale factor for derivative values
scale = 6

## Method 1
# Get a function that evaluates the linear spline at any x
f = InterpolatedUnivariateSpline(x, sin, k=3)

# Get a function that evaluates the derivative of the linear spline at any x
dfdx = f.derivative()

# Evaluate the derivative dydx at each x location...
dydx_1_no_scaled = dfdx(x)
dydx_1_scaled = dfdx(x)/scale

## Method 2
# Calculate time step
dx = x[1] - x[0]

# Gradient method :  central differences
dydx_2_no_scaled = (np.gradient(sin, dx))
dydx_2_scaled = (np.gradient(sin, dx))/6


## Method 3
# Approximations of derivatives
dydx_3_no_scaled = (np.diff(sin) / np.diff(x))
dydx_3_scaled = (np.diff(sin) / np.diff(x))/6

# Method 4 : Spline
time = np.linspace(0,1,361)

# Calculate signal spline func 'tck'
func = splrep(time, sin, s=0, k=3)

# Calculate derivative spline func 'tck'
der_func = splder(func, n=1)

# Calculate derivative values
dydx_4_no_scaled = splev(x, der_func, der=0, ext=0 )
dydx_4_scaled = splev(x, der_func, der=0, ext=0 )/6

plt.plot(sin)
plt.plot(cos)
plt.plot(dydx_1_no_scaled)
plt.plot(dydx_1_scaled)
plt.plot(dydx_2_no_scaled)
plt.plot(dydx_2_scaled)
plt.plot(dydx_3_no_scaled)
plt.plot(dydx_3_scaled)
plt.plot(dydx_4_no_scaled)
plt.plot(dydx_4_scaled)
plt.axvline(90)
plt.axvline(180)
plt.axvline(270)
plt.title('Sine Wave and respective derivative with 4 different methods')
plt.legend(['sin',
            'cos',
            'dydx_1_no_scaled', 'dydx_1_scaled',
            'dydx_2_no_scaled', 'dydx_2_scaled',
            'dydx_3_no_scaled', 'dydx_3_scaled',
            'dydx_4_no_scaled', 'dydx_4_scaled'])
plt.show()

Thank you for all your help. Ivo

The array sin contains one full period of the sine function, and the corresponding x values range from 0 to 1. So the function that you have computed is sin(2*pi*x) . The derivative is therefore 2*pi*cos(2*pi*x) . (Notice that ~6 is ~2π.)

Here's a script that uses the derivative() method to create an interpolator of the derivative the interpolated sine function. The interpolator is created on the interval [0, 2π], which is one period of sin(x).

import numpy as np
from scipy.interpolate import InterpolatedUnivariateSpline
import matplotlib.pyplot as plt


num_samples = 250
x = np.linspace(0, 2*np.pi, num_samples)
y = np.sin(x)

f = InterpolatedUnivariateSpline(x, y, k=3)
dfdx = f.derivative()

print(np.max(np.abs(np.cos(x) - dfdx(x))))

plt.plot(x, dfdx(x), '--', label='dfdx(x)', linewidth=1)
plt.plot(x, np.cos(x), label='cos(x)', linewidth=4, alpha=0.25)
plt.legend(loc='lower left')
plt.xlabel('x')
plt.show()

The program prints 7.05390776901e-08 and generates the following plot:

情节

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM