You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.4 KiB
Python

import matplotlib.pyplot as plt
8 years ago
import numpy as np
from wafo.sg_filter import SavitzkyGolay, smoothn # calc_coeff, smooth
9 years ago
def example_reconstruct_noisy_chirp():
8 years ago
"""
Example
-------
>>> example_reconstruct_noisy_chirp()
"""
plt.figure(figsize=(7, 12))
9 years ago
# generate chirp signal
8 years ago
tvec = np.arange(0, 6.28, .02)
true_signal = np.sin(tvec * (2.0 + tvec))
true_d_signal = (2+tvec) * np.cos(tvec * (2.0 + tvec))
9 years ago
# add noise to signal
8 years ago
noise = np.random.normal(size=true_signal.shape)
9 years ago
signal = true_signal + .15 * noise
9 years ago
# plot signal
8 years ago
plt.subplot(311)
plt.plot(signal)
plt.title('signal')
9 years ago
# smooth and plot signal
8 years ago
plt.subplot(312)
9 years ago
savgol = SavitzkyGolay(n=8, degree=4)
s_signal = savgol.smooth(signal)
s2 = smoothn(signal, robust=True)
8 years ago
plt.plot(s_signal)
plt.plot(s2)
plt.plot(true_signal, 'r--')
plt.title('smoothed signal')
9 years ago
# smooth derivative of signal and plot it
8 years ago
plt.subplot(313)
9 years ago
savgol1 = SavitzkyGolay(n=8, degree=1, diff_order=1)
8 years ago
dt = tvec[1]-tvec[0]
d_signal = savgol1.smooth(signal) / dt
8 years ago
plt.plot(d_signal)
plt.plot(true_d_signal, 'r--')
plt.title('smoothed derivative of signal')
9 years ago
if __name__ == '__main__':
8 years ago
from wafo.testing import test_docstrings
test_docstrings(__file__)
# example_reconstruct_noisy_chirp()
# plt.show('hold') # show plot