【问题标题】:calculate multiple sine sweeps one after another一个接一个地计算多个正弦扫描
【发布时间】:2015-10-14 09:05:12
【问题描述】:

我正在编写一个 Python 程序,以一个接一个地生成多个正弦扫描,具有不同的开始和结束频率以及不同的时间间隔。

一个例子是:

  1. 在 1 毫秒内从 0Hz 扫描到 170Hz

  2. 在 1 毫秒内从 170Hz 扫描到 170Hz

  3. 在 1 毫秒内从 170Hz 扫描到 10Hz

所以它应该是一个斜坡上升,斜坡下降波形

我使用的方程式的灵感来自this thread

def LinearSineSweep(self, fStart, fEnd, samplingTime, samplesPerSecond):
    nValues = int(samplesPerSecond * samplingTime)
    for i in range(0, nValues):
        delta = float(i) / nValues
        t = samplingTime * delta
        phase = 2 * math.pi * t * (fStart + (fEnd - fStart) * delta / 2)
        return self._amplitude * math.sin(phase) + self._dcOffset

LinearSineSweep(0, 170, 0.001, 44100)
LinearSineSweep(170, 170, 0.001, 44100)
LinearSineSweep(170, 10, 0.001, 44100)

但是我得到的输出是不正确的:

即使是频率的 10 倍,仍然不能组合成一个波形

这是数学问题还是编程问题?

【问题讨论】:

  • 仅供参考:scipy 提供了函数chirp (docs.scipy.org/doc/scipy/reference/generated/…),它可以做到这一点(以及更多)。
  • 是的,我想过这个,但是 chirp 返回一个数组,我的数据集太大了,数组无法处理
  • 您需要一种将灵态从一个呼叫转移到下一个呼叫的方法。您可以在图表中看到相位每毫秒重置为零。
  • 你能检查问题中代码的缩进吗?你在循环中有你的return 语句。
  • 正如@jaket 所说,您必须提供一种方法,以便将一个段末尾的阶段用作下一个段的开始阶段。这就是 scipy.signal.chirpphi 参数所提供的。

标签: python audio signals trigonometry


【解决方案1】:

正如@jaket 在评论中指出的那样,您必须使相位在各个片段之间不断变化(我在解释一下)。这是您的代码的一种变体,它显示了您可以执行此操作的一种方法。我没有你所有的其他代码,所以self 的第一个参数不是self,而是一个将样本作为文本写入的文件。 (我还调整了代码以补偿请求的间隔通常不会是采样周期的精确倍数这一事实。)numpymatplotlib 用于创建绘图。

from __future__ import print_function, division

import math


def LinearSineSweep(f, fStart, fEnd, samplingTime, samplesPerSecond,
                    t0=0, phi0=0):
    nValues = int(samplesPerSecond * samplingTime)
    actualSamplingTime = nValues / samplesPerSecond
    for i in range(0, nValues):
        delta = float(i) / nValues
        t = actualSamplingTime * delta
        phase = 2 * math.pi * t * (fStart + (fEnd - fStart) * delta / 2)
        value = math.sin(phase + phi0)
        # Write the time and sample value to the output...
        print(t0 + t, value, file=f)
    phase = 2 * math.pi * actualSamplingTime * (fStart + (fEnd - fStart) / 2)
    return t0 + actualSamplingTime, phi0 + phase


if __name__ == "__main__":
    with open('out.csv', 'w') as f:
        t, phi = LinearSineSweep(f, 0, 1700, 0.001, 44100)
        t, phi = LinearSineSweep(f, 1700, 1700, 0.001, 44100, t, phi)
        t, phi = LinearSineSweep(f, 1700, 100, 0.001, 44100, t, phi)

    import numpy as np
    import matplotlib.pyplot as plt

    tvals, v = np.loadtxt('out.csv', unpack=True)
    plt.figure(figsize=(10, 4))
    plt.plot(tvals, v)
    plt.grid()
    plt.show()

剧情如下:

【讨论】:

  • 就是这样,我保存了最后一个阶段,但忘记在每个波形段之后将其添加在一起。很好的答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-10
相关资源
最近更新 更多