如何在Python中生成平滑的2D行波?
我想生成一个大小为 (M*N)
的矩阵或二维数组,其中矩阵的每一列(python 中的二维数组)包含行进正弦波的数据,y = sin(k*xw*t)
。因此,我使用以下脚本,但我没有得到如 图。谁能帮助我如何使用上面的公式生成非常平滑的正弦曲线?谢谢。
import numpy as np
import random
import matplotlib.pyplot as plt
x = np.linspace(-5, 5, 10)
y = np.linspace(-5, 5, 10)
dx = np.array(x); M = len(dx)
dy = np.array(y); N = len(dy)
#** Generation of Sine wave #
t = np.linspace(1, 36, 360)
# Amplitude
amp = 5;
# frequency
f1=20; f2 = 10;
# Assume the value of K, wavenumber
k1 = 1; k2 = -2;
rows, cols = (len(t), M)
##*********************************************
np.random.seed(12345)
arr=[]
for i in range(rows):
col = []
for j in range(cols):
w = round(random.uniform(-2*np.pi,2*np.pi), 1)*f2
d = amp * np.sin(k1*dx[j] -w*t[i]) + 2*amp * np.sin(k2*dy[j] + w*t[i])
col.append(d)
arr.append(col)
sig = np.array(arr)
print('The shape of the signal :', sig.shape)#
aa=sig[:,2] # Sinusoid at 3rd column
plt.figure()
plt.plot(t, aa, 'b')
plt.xlabel('location (x)')
plt.ylabel('y')
plt.show()
I want to generate a matrix or a 2d array of size (M*N)
where each column of the matrix (2D array in python) contain the data of a travelling sine wave, y = sin(k*x-w*t)
. So, I use the following script, but I don't get the sine wave as smooth as of my expectation shown in figure. Can anyone help me how to generate very smooth sine curve by using the above formula? Thanks.
import numpy as np
import random
import matplotlib.pyplot as plt
x = np.linspace(-5, 5, 10)
y = np.linspace(-5, 5, 10)
dx = np.array(x); M = len(dx)
dy = np.array(y); N = len(dy)
#** Generation of Sine wave #
t = np.linspace(1, 36, 360)
# Amplitude
amp = 5;
# frequency
f1=20; f2 = 10;
# Assume the value of K, wavenumber
k1 = 1; k2 = -2;
rows, cols = (len(t), M)
##*********************************************
np.random.seed(12345)
arr=[]
for i in range(rows):
col = []
for j in range(cols):
w = round(random.uniform(-2*np.pi,2*np.pi), 1)*f2
d = amp * np.sin(k1*dx[j] -w*t[i]) + 2*amp * np.sin(k2*dy[j] + w*t[i])
col.append(d)
arr.append(col)
sig = np.array(arr)
print('The shape of the signal :', sig.shape)#
aa=sig[:,2] # Sinusoid at 3rd column
plt.figure()
plt.plot(t, aa, 'b')
plt.xlabel('location (x)')
plt.ylabel('y')
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论