在python中创建不规则锯齿函数 [英] Creating an irregular sawtooth function in python

查看:42
本文介绍了在python中创建不规则锯齿函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的练习,我无法弄清楚.这是练习:

<块引用>

现在从 1 Myr 的时间序列开始,锯齿的平均波长为 0.1 Myr.波长随机变化±0.02Myr(因此在 80 到 120 kyrs 之间),振幅再次为 1.

所以我必须创建一个锯齿函数的时间序列,长度为 1000 kyr,时间步长为 1 kyr.锯齿波的波长必须是随机的,平均为 100 kyr,从 80 kyr 到 120 kyr 不等.我希望时间序列从零开始并以零结束,所以不要在锯齿波中途停止.现在我正在使用这个:

time = np.arange(0,1000,1)func = scipy.signal.sawtooth(2 * np.pi* 0.01 * time-np.pi)

结果如下:

I have an interesting exercise that I cannot figure out. This is the exercise:

Start now with a time series of 1 Myr, a saw tooth with a wavelength of 0.1 Myr on average. The length of the wave varies randomly ±0.02 Myr (so between 80 and 120 kyrs), amplitude again 1.

So I have to create a time series of a sawtooth function, of 1000 kyrs long with timesteps of 1 kyr. The wavelength of the sawtooths have to be random, with an average of 100 kyr and varying from 80 kyr to 120 kyr. I want the time series to start at zero and end at zero, so not stop halfway during a sawtooth. Right now I am using this:

time = np.arange(0,1000,1)
func = scipy.signal.sawtooth(2 * np.pi* 0.01 * time-np.pi) 

Which results in this:

Sawtooth function

I am trying to figure out how to make the wavelength irregular, but still let the time series end at zero. Does anyone have an idea?

Thanks in advance

解决方案

You could concatenate waves with distinct wavelengths which you create with np.linspace and random.randrange:

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
import random

waveform = np.concatenate(
    [signal.sawtooth(2 * np.pi * np.linspace(0, 1, random.randrange(30, 150))) for _ in range(10)]
)

plt.plot(waveform)
plt.show()

The output looks like this:

这篇关于在python中创建不规则锯齿函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆