Produce random wavefunction

2024/9/27 17:26:38

I need to produce a random curve in matplotlib.

My x values are from say 1 to 1000 for example. I don't want to generate scattered random y values, I need a smooth curve. Like some kind of very distorted sin curve with varying amplitude and wavelength.

Does something already exist to allow me to easily do this?

Answer

Try this code:

import matplotlib.pyplot as pl
import numpy as npx = np.linspace(1, 10)def f(x):return np.sin(x) + np.random.normal(scale=0.1, size=len(x))pl.plot(x, f(x))

It will give you a sin wave with some noise added to it:

sin wave with noise

Edit:

It seems like some kind of random walk is what you're looking for. This function will do that for you:

def f(x):y = 0result = []for _ in x:result.append(y)y += np.random.normal(scale=1)return np.array(result)

This is an example of what it can look like (with x = np.linspace(0, 1000, 1000)):

random walk

It's not a function of x any longer though, so the code should probably be refactored to generate a random walk with n steps instead. I'll leave that to you :)

Edit 2:

If you want a smoother curve, you can apply a running mean (stolen from this question):

def runningMean(x, N):return np.convolve(x, np.ones((N,))/N)[(N-1):]pl.plot(x, runningMean(f(x), 10))

The bigger window (the N parameter) you use, the smoother the result.

Example:

random walk with running mean

https://en.xdnf.cn/q/71436.html

Related Q&A

How to reference groupby index when using apply, transform, agg - Python Pandas?

To be concrete, say we have two DataFrames:df1:date A 0 12/1/14 3 1 12/1/14 1 2 12/3/14 2 3 12/3/14 3 4 12/3/14 4 5 12/6/14 5df2:B 12/1/14 10 12/2/14 20 12/3/14 10 12/4/14 30 12/5/14 10 …

Google AppEngine Endpoints Error: Fetching service config failed (status code 404)

I am implementing the steps in the Quickstart.I did notice another question on this. I double checked that env_variables section in app.yaml has the right values for ENDPOINTS_SERVICE_NAME and ENDPOIN…

How to unload a .NET assembly reference in IronPython

After loading a reference to an assembly with something like:import clr clr.AddRferenceToFileAndPath(rC:\foo.dll)How can I unload the assembly again?Why would anyone ever want to do this? Because Im …

Bad key axes.prop_cycle Error while using an mplstyle in matplotlib (Python)

I am getting the following error when I try to use an external style sheet loaded locally. Bad key "axes.prop_cycle" on line 270 in idt.mplstyle. You probably need to get an updated matplotli…

Dollar notation in script languages - why? [closed]

Closed. This question is off-topic. It is not currently accepting answers.Want to improve this question? Update the question so its on-topic for Stack Overflow.Closed 12 years ago.Improve this questio…

Failure to build wheel / Error: INCLUDE Environment Variable is empty

I am using Python 2.7.11 and am trying to pip install modules however a few of them are failing. The message I get is "Failure to build wheel for X" and "Error: INCLUDE Environment Varia…

Calculating the position of QR Code alignment patterns

I need to know how to calculate the positions of the QR Code alignment patterns as defined in the table of ISO/IEC 18004:2000 Annex E.I dont understand how its calculated. If you take the Version 16, f…

Lowlevel introspection in python3?

Is there some introspection method allowing to reliably obtain the underlying data structure of an object instance, that is unaffected by any customizations? In Python 3 an objects low-level implement…

Efficiently find indices of nearest points on non-rectangular 2D grid

I have an irregular (non-rectangular) lon/lat grid and a bunch of points in lon/lat coordinates, which should correspond to points on the grid (though they might be slightly off for numerical reasons).…

How to code a sequence to sequence RNN in keras?

I am trying to write a sequence to sequence RNN in keras. I coded this program using what I understood from the web. I first tokenized the text then converted the text into sequence and padded to form …