Numpy 中从一个音高到另一个音高的正弦波滑奏

发布于 2024-09-06 04:47:42 字数 355 浏览 10 评论 0原文

我一直在开发一个程序,我需要缓慢而平稳地将正弦波的音调从一个音调更改为另一个音调。我能够获得在任何给定时刻音调应有的频率数组(例如,[440, 526.5, 634.2 794.8, 880],尽管更长很多),但似乎我无法实际应用该频率来一波。我最好的尝试是:

numpy.sin(2*math.pi*x*freq/self.sample_rate)

其中“freq”是频率数组,x是枚举数组([0,1,2,3,4...])。这种方法有点有效,但它会使频率高于预期频率,然后又下降。我已经研究这个问题很长时间了,但在寻找更合适的方法方面一直无法取得任何进展。有什么建议吗?我是否足够清楚地表达了我的困境?

谢谢。

I have been working on a program where I need to slowly and smoothly change the pitch of a sine wave from one pitch to another. I am able to get an array of the frequency the pitch should be at any given moment (for instance, [440, 526.5, 634.2 794.8, 880], though much, much longer) but it seems I am unable to actually apply that frequency to a wave. My best attempt is:

numpy.sin(2*math.pi*x*freq/self.sample_rate)

where "freq" is the array of frequencies and x is an enumeration array ([0,1, 2, 3, 4...]). This method sort of works, however it makes the frequency go above the expected frequency, and then back down. I have been working on this problem for a very long time and have been unable to make any progress on finding a more appropriate method. Any advice? Was I clear enough in expressing my dilemma?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾旎 2024-09-13 04:47:42

问题是,当您逐渐改变频率时,每个频率在给定时间内实际上具有不同的相位。当您快速连续地滚动这些阶段时,它们会以更高的频率(或更低的频率)驱动正弦波。

例如,想象一下,您瞬间改变了频率 - 为此,您必须提供相位校正 p_1 = p_0 + 2*pi*t*(f_0-f_1) 以使阶段在时间t匹配。当您执行此操作时,您还必须进行类似的相位校正,每个相位校正都会添加到前一个相位校正中。

这是结果图,代码如下。上图是中间没有相位校正的频率,下图是连续校正相位的频率。

输入图片此处描述

from pylab import *

sample_rate = .001
f0, f1 = 10, 20
t_change = 2

times = arange(0, 4, sample_rate)

ramp = 1./(1+exp(-6.*(times-t_change)))
freq = f0*(1-ramp)+f1*ramp
phase_correction = add.accumulate(times*concatenate((zeros(1), 2*pi*(freq[:-1]-freq[1:]))))

figure()
subplot(311)
plot(times, freq)
subplot(312)
plot(times, sin(2*pi*freq*times))
subplot(313)
plot(times, sin(2*pi*freq*times+phase_correction))

show()

The issue is that as you ramp through the frequencies, each frequency effectively has a different phase for the given time. When you scroll through these phases quickly and continuously, they drive the sine wave at higher frequency (or lower is also possible).

Imagine, for example, that you changed the frequency instantaneously -- to do this you'd have to supply the phase correction p_1 = p_0 + 2*pi*t*(f_0-f_1) to make the phases match up at time t. As you do this is little steps, you also have to make a similar phase correction, with each phase correction adding to the previous.

Here's the resulting figure, with the code below. The top figure is the frequency the middle is without the phase correction, and the bottom has the continuously corrected phase.

enter image description here

from pylab import *

sample_rate = .001
f0, f1 = 10, 20
t_change = 2

times = arange(0, 4, sample_rate)

ramp = 1./(1+exp(-6.*(times-t_change)))
freq = f0*(1-ramp)+f1*ramp
phase_correction = add.accumulate(times*concatenate((zeros(1), 2*pi*(freq[:-1]-freq[1:]))))

figure()
subplot(311)
plot(times, freq)
subplot(312)
plot(times, sin(2*pi*freq*times))
subplot(313)
plot(times, sin(2*pi*freq*times+phase_correction))

show()
断舍离 2024-09-13 04:47:42

我喜欢将频率视为您步进声音样本的速率 - 在本例中为正弦波。这里尝试使用一些 Python 代码来完成您想要的操作。我们假设 freq() 方法给出的频率是时间的函数。就您的目的而言,它将是某种指数级的。我们正在尝试填充一个名为 wave 的预分配列表。

index = 0
t = 0
while t < len(wave):
  wave[t] = math.sin(2*math.pi*index/sample_rate)
  t = t+1
  index = index + freq(t/sample_rate)

对不起我的Python,我还在学习这门语言。

I like to think of frequency as the rate at which you are stepping through your sound sample - in this case a sine wave. Here's an attempt at some Python code to do what you want. We assume that the freq() method gives frequency as a function of time. For your purposes, it will be some kind of exponential. We are trying to fill a pre-allocated list called wave.

index = 0
t = 0
while t < len(wave):
  wave[t] = math.sin(2*math.pi*index/sample_rate)
  t = t+1
  index = index + freq(t/sample_rate)

Excuse my Python, I'm still learning the language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文