Skip to content

Instantly share code, notes, and snippets.

@potass13
Created January 2, 2021 23:40
Show Gist options
  • Save potass13/1092220a07a7197c0317d47cb64b83d5 to your computer and use it in GitHub Desktop.
Save potass13/1092220a07a7197c0317d47cb64b83d5 to your computer and use it in GitHub Desktop.
# coding: utf-8
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
import rainflow
def f(x):
return 0.2+0.4*np.sin(x)-0.6*np.cos(2*x)+1.2*np.sin(8*x)
def main():
time = np.linspace(0, 4, 200)
sgnl = f(time)
maxid = signal.argrelmax(sgnl, order=1)
minid = signal.argrelmin(sgnl, order=1)
peakid = np.append(maxid, minid)
print(np.array([time[peakid], sgnl[peakid]]).T)
print('-----------------------------------------')
print('range, mean, count, i_start, i_end')
for rng, mean, count, i_start, i_end in rainflow.extract_cycles(sgnl):
print(rng, mean, count, i_start, i_end)
plt.xlabel('time')
plt.ylabel('signal')
plt.xlim(np.min(time), np.max(time))
plt.ylim(-3, 3)
plt.plot(time, sgnl, color='blue', marker='o', label='Signal')
plt.plot(time[peakid], sgnl[peakid], color='red', marker='*', markersize=12, markeredgewidth=2, linestyle='None', label='Peak')
plt.show()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment