【问题标题】:Numpy: Generate sine wave signal with time-varying frequencyNumpy:生成具有时变频率的正弦波信号
【发布时间】:2021-03-05 13:32:36
【问题描述】:

我尝试生成频率随时间变化的正弦波信号。频率是通过在[0.5, 2] 范围内生成一些随机值并在它们之间插入点来随机定义的。

预期的输出信号是一个幅度不变但频率变化的正弦波。 但是有一些较小的颠簸,信号不是“平滑”的正弦波。例如。 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()

【问题讨论】:

    标签: numpy trigonometry


    【解决方案1】:

    freq * x 可能没有按照预期进行。频率需要乘以 x 中每个点的 变化 而不是累积的 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 2022-11-07
      相关资源
      最近更新 更多