1 year ago
#334760
Alan22
How to generate a smooth 2D travelling wave in python?
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()
python
animation
time-series
signal-processing
sampling
0 Answers
Your Answer