0

I was following the following post: Produce random wavefunction

The first answer is close to what I need, but I can't modify it properly. Specfically I need an amplitude of 100, and 5 periods over the 1000 that are being displayed.

x = np.linspace(1, 1000)
y = 100*(np.sin(x) + np.random.normal(scale=0.1, size=len(x))

I've tried multiplying y by 100 as shown above, which changes the amplitude, but not by 100. Also I have no idea how to change frequency.

bruhsmh
  • 321
  • 2
  • 4
  • 12
  • The angle for `np.sin` is in radian. Period of a sine wave is 2 pi radian. 5 periods is 5 * 2pi = 10 pi radian. – Crawl Cycle Nov 15 '20 at 08:14

2 Answers2

2

enter image description here

import numpy as np
from matplotlib.ticker import FuncFormatter, MultipleLocator
import matplotlib.pyplot as plt

x = np.linspace(0, 10 * np.pi, num=600)
n = np.random.normal(scale=8, size=x.size)
s = 100 * np.sin(x)
y = 100 * np.sin(x) + n

plt.Figure(figsize=(1, 1.5), dpi=80)
plt.plot(x, y, label='Total')
plt.plot(x, s, label='Sine')
plt.plot(x, n, label='Gaussian White Noise')
ax = plt.gca()

ax.xaxis.set_major_formatter(FuncFormatter(
    lambda val, pos: '{:.0f}$\pi$'.format(val / np.pi) if val != 0 else '0'
))
ax.xaxis.set_major_locator(MultipleLocator(base=np.pi))

plt.legend()
plt.savefig("noisey_sine.png", dpi=80)
plt.show()

Crawl Cycle
  • 257
  • 2
  • 8
1

Is this what you are looking for? Note the 2*pi (rad!):

import numpy as np
import matplotlib.pyplot as plt
import math

x = np.linspace(1, 1000)
#y = 100*(np.sin(x/5) + np.random.normal(scale=0.1, size=len(x))) # <- your solution
#y = 100*(np.sin(x/1000*2*math.pi)) # <- one full period
#y = 100*(np.sin(x/1000*2*math.pi*5)) # <- 5 full periods
y = 100*(np.sin(x/1000*2*math.pi*5) + np.random.normal(scale=0.1, size=len(x))) # <- 5 full periods + noise
plt.scatter(x, y)
plt.show()

resulting in

enter image description here

GrimTrigger
  • 571
  • 1
  • 5
  • 10