Numpy:生成具有时变频率的正弦波信号
Numpy: Generate sine wave signal with time-varying frequency
我尝试生成一个频率随时间变化的正弦波信号。通过在 [0.5, 2]
范围内生成一些随机值并在它们之间插入点来随机定义频率。
预期的输出信号是幅度不变、频率变化的正弦波。
但是有一些较小的颠簸,信号不是 'smooth' 正弦波。例如。 x = 200
的周期应该大于x = 10
的周期,但恰恰相反。
有谁知道,这里发生了什么?
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
x_samples = np.arange(-100, 3100, 50)
freq_samples = np.random.random(x_samples.shape) * 1.5 + 0.5
x = np.arange(0, 3000, 0.1)
interpolation = interp1d(x_samples, freq_samples, kind='quadratic')
freq = interpolation(x)
y = np.sin(freq * x)
plt.plot(x, y, label="sin(freq(x) * x)")
plt.plot(x, freq, label="freq(x)")
plt.legend()
plt.show()
freq * x
可能没有按预期进行。对于每个点,频率需要乘以 x 中的 change 而不是累积 x。
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
x_samples = np.arange(-10, 350, 50)
freq_samples = np.random.random(x_samples.shape) * 1.5 + 0.5
x = np.arange(0, 300, 0.1)
dx = np.full_like(x, 0.1 ) # Change in x
interpolation = interp1d(x_samples, freq_samples, kind='quadratic')
freq = interpolation(x)
x_plot = (freq * dx ).cumsum() # Cumsum freq * change in x
y = np.sin(x_plot)
plt.plot(x, y, label="sin(freq(x) * x)")
plt.plot(x, freq, label="freq(x)")
plt.legend()
plt.show()
我尝试生成一个频率随时间变化的正弦波信号。通过在 [0.5, 2]
范围内生成一些随机值并在它们之间插入点来随机定义频率。
预期的输出信号是幅度不变、频率变化的正弦波。
但是有一些较小的颠簸,信号不是 'smooth' 正弦波。例如。 x = 200
的周期应该大于x = 10
的周期,但恰恰相反。
有谁知道,这里发生了什么?
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
x_samples = np.arange(-100, 3100, 50)
freq_samples = np.random.random(x_samples.shape) * 1.5 + 0.5
x = np.arange(0, 3000, 0.1)
interpolation = interp1d(x_samples, freq_samples, kind='quadratic')
freq = interpolation(x)
y = np.sin(freq * x)
plt.plot(x, y, label="sin(freq(x) * x)")
plt.plot(x, freq, label="freq(x)")
plt.legend()
plt.show()
freq * x
可能没有按预期进行。对于每个点,频率需要乘以 x 中的 change 而不是累积 x。
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
x_samples = np.arange(-10, 350, 50)
freq_samples = np.random.random(x_samples.shape) * 1.5 + 0.5
x = np.arange(0, 300, 0.1)
dx = np.full_like(x, 0.1 ) # Change in x
interpolation = interp1d(x_samples, freq_samples, kind='quadratic')
freq = interpolation(x)
x_plot = (freq * dx ).cumsum() # Cumsum freq * change in x
y = np.sin(x_plot)
plt.plot(x, y, label="sin(freq(x) * x)")
plt.plot(x, freq, label="freq(x)")
plt.legend()
plt.show()