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.
43 lines
912 B
Python
43 lines
912 B
Python
10 years ago
|
import matplotlib.pyplot as plt
|
||
|
from pylab import subplot, plot, title, figure
|
||
|
from numpy import random, arange, sin
|
||
|
from sg_filter import SavitzkyGolay, smoothn # calc_coeff, smooth
|
||
15 years ago
|
|
||
|
|
||
10 years ago
|
figure(figsize=(7, 12))
|
||
15 years ago
|
|
||
|
|
||
|
# generate chirp signal
|
||
|
tvec = arange(0, 6.28, .02)
|
||
10 years ago
|
true_signal = sin(tvec * (2.0 + tvec))
|
||
15 years ago
|
|
||
|
# add noise to signal
|
||
10 years ago
|
noise = random.normal(size=true_signal.shape)
|
||
|
signal = true_signal + .15 * noise
|
||
15 years ago
|
|
||
|
# plot signal
|
||
|
subplot(311)
|
||
|
plot(signal)
|
||
|
title('signal')
|
||
|
|
||
|
# smooth and plot signal
|
||
|
subplot(312)
|
||
10 years ago
|
savgol = SavitzkyGolay(n=8, degree=4)
|
||
|
s_signal = savgol.smooth(signal)
|
||
|
s2 = smoothn(signal, robust=True)
|
||
15 years ago
|
plot(s_signal)
|
||
10 years ago
|
plot(s2)
|
||
|
plot(true_signal, 'r--')
|
||
15 years ago
|
title('smoothed signal')
|
||
|
|
||
|
# smooth derivative of signal and plot it
|
||
|
subplot(313)
|
||
10 years ago
|
savgol1 = SavitzkyGolay(n=8, degree=1, diff_order=1)
|
||
|
|
||
|
d_signal = savgol1.smooth(signal)
|
||
15 years ago
|
|
||
|
plot(d_signal)
|
||
|
title('smoothed derivative of signal')
|
||
|
|
||
10 years ago
|
plt.show('hold') # show plot
|