依次计算多个正弦扫描

calculate multiple sine sweeps one after another

我正在编写一个 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 倍,也仍然不会合并为一个波形

这是数学题还是编程题?

正如@jaket 在评论中指出的那样,您必须使相位在各段之间连续变化(我稍微解释了一下)。这是您的代码的变体,显示了您可以执行此操作的一种方法。我没有你所有的其他代码,所以 LinearSineSweep 的第一个参数不是 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()

剧情如下: