如何在Python中生成平滑的2D行波?

发布于 2025-01-16 21:13:00 字数 1161 浏览 4 评论 0原文

我想生成一个大小为 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文