问题描述
我想实现的Python方法产生正弦波,其中坡道两个频率成倍之间。线性变化解决了[this提问] 有以下Python code:
I'm trying to implement Python method that generates sine wave, which ramps up between two freq exponentially.Linear change was solved in [this question] with following Python code:
from math import pi, sin
def sweep(f_start, f_end, interval, n_steps):
for i in range(n_steps):
delta = i / float(n_steps)
t = interval * delta
phase = 2 * pi * t * (f_start + (f_end - f_start) * delta / 2)
print t, phase * 180 / pi, 3 * sin(phase)
sweep(1, 10, 5, 1000)
如何改变这种线性叠加阶段/三角的方法来expotential频率扫描,并顺利的人耳。
How to change this linear accumulative phase/delta approach to expotential frequency sweep and be smooth to human ear.
推荐答案
巴斯的回答是伟大的,但实际上并没有给出一个解析解,所以这里的那部分......
Bas's answer is great, but doesn't actually give an analytic solution, so here's that part...
据我所知,你想要的东西,比如罪(Aexp(BT))
,其中 A
和 B
是常数。我会认为时间开始于 0
,并继续 C
(如果它开始在其他时间,减,从两者)。
As far as I can tell, you want something like sin(Aexp(Bt))
where A
and B
are constants. I'll assume time starts at 0
and continues to C
(if it starts at some other time, subtract that from both).
然后,巴斯说,我认为,如果我们有罪(G(T))
频率 F
是这样的: 2 * PI * F = DG / DT
。我们希望这是 F0 code>在时间
0
和 FC
在时间 C
。
Then, as Bas said, I think, if we have sin(g(t))
frequency f
is such that 2 * pi * f = dg / dt
. And we want that to be f0
at time 0
and fC
at time C
.
如果您通过了数学,这是很容易(它确实是 - 学校层面的最后一年),您可以:
If you go through the maths, which is easy (it really is - last year of school level), you get:
B = 1/C * log(fC/f0)
A = 2 * pi * f0 / B
和这里的一些code,使用1000个样品是从1到10Hz在5秒:
and here's some code that goes from 1 to 10Hz in 5 seconds using 1000 samples:
from math import pi, sin, log, exp
def sweep(f_start, f_end, interval, n_steps):
b = log(f_end/f_start) / interval
a = 2 * pi * f_start / b
for i in range(n_steps):
delta = i / float(n_steps)
t = interval * delta
g_t = a * exp(b * t)
print t, 3 * sin(g_t)
sweep(1, 10, 5, 1000)
这给:
(你可以添加一个常数 - 罪(G_T + K)
- 无论你想要得到的开始阶段)。
(and you can add in a constant - sin(g_t + k)
- to get the starting phase wherever you want).
更新
要显示你所看到的问题是抽样的人工制品,这里有一个版本,确实过取样(如果将其设置为参数):
To show that the issue you are seeing is an artefact of sampling, here's a version that does oversampling (if you set it as an argument):
from math import pi, sin, log, exp
def sweep(f_start, f_end, interval, n_steps, n_oversample=1):
b = log(f_end/f_start) / interval
a = 2 * pi * f_start / b
for i in range(n_steps):
for oversample in range(n_oversample):
fractional_step = oversample / float(n_oversample)
delta = (i + fractional_step) / float(n_steps)
t = interval * delta
g_t = a * exp(b * t)
print t, 3 * sin(g_t)
sweep(16000.0, 16500.0, 256.0/48000.0, 256) # looks strange
sweep(16000.0, 16500.0, 256.0/48000.0, 256, 4) # looks fine with better resolution
如果您检查code,你将看到 n_oversample
所有设置4做(第二个呼叫)就是添加一个更高的分辨率的时间步。特别是,code时,过采样= 0
(即 fractional_step = 0
)是的一样之前,所以第二个情节包括分在第一条曲线,加上额外的那些填补了丢失的数据,让一切看起来不太令人意外。
If you check the code you'll see that all that setting n_oversample
to 4 does (the second call) is add a higher resolution to the timesteps. In particular, the code when oversample = 0
(ie fractional_step = 0
) is identical to before, so the second plot includes the points in the first plot, plus extra ones that "fill in" the missing data and make everything look much less surprising.
下面是一个近距离的原件和附近展开过采样曲线,显示出正在发生的事情的详细信息:
Here's a close-up of the original and the oversampled curve near the start, showing what is happening in detail:
最后,这种事情是完全正常的,不表示任何类型的错误。当从数字波形产生的模拟信号,你会得到正确的结果(假设硬件工作的权利)。 这个优秀的影片会解释的事情,如果他们都不清楚。
Finally, this kind of thing is completely normal and does not indicate any kind of error. When an analogue signal is generated from the digital waveform you'll get "the right" result (assuming the hardware is working right). This excellent video will explain things if they are not clear.
这篇关于该exponentialy频率f1和f2之间的变化,在样品的给定的时间/量正弦波的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!