R:产生可变频率的正弦波 [英] R: Generate sine wave with variable frequency
问题描述
这可能更像是一个数学问题而不是一个 R 问题,但它在这里......
This might be more of a math question than an R question but here it goes...
我正在尝试生成一个低频振荡器 (LFO2),其中的频率由另一个低频振荡器 (LFO1) 控制.LFO1 的频率为 0.02 Hz,而我希望 LFO2 的频率在 0.00 到 0.11 Hz 之间振荡,具体取决于 LFO1 的输出.
I'm trying to generate a low frequency oscillator (LFO2) where the frequency is controlled by another low frequency oscillator (LFO1). LFO1 has a frequency of 0.02 Hz while I want LFO2 to have a frequency that oscillates between 0.00 and 0.11 Hz dependent on the output of LFO1.
# length in seconds
track_length <- 356
upsample <- 10 # upsample the signal
# LFO rates (Hz)
rate1 <- 0.02
rate2_range <- list(0.00, 0.11)
# make plot of LFO1
x1 <- 1:(track_length*upsample)/upsample
amp <- (rate2_range[[2]] - rate2_range[[1]])/2
y1 <- amp*cos(2*pi*rate1*x1) + amp
plot(x1, y1, type='l')
由 LFO1 生成的 LFO2 可变频率看起来完全符合我的预期.
The variable frequency for LFO2 generated by LFO1 looks exactly as I expected.
所以我继续使用 LFO1 的输出制作 LFO2 就像这样..
So I go on to make LFO2 using the output of LFO1 like so..
# make plot of LFO2
x2 <- x1
y2 <- cos(2*pi*y1*x2)
plot(x2, y2, type='l')
然而,LFO2 的输出并不是我所期望的......它似乎在不断变快,并且还有一些不在全范围内振荡的峰值.我不明白这一点,因为我唯一要调整的是频率,它不应快于 0.11 Hz.起初我认为这可能是采样不足的问题,但在对时间序列进行任何程度的上采样时,我得到了相同的结果.
However, the output of LFO2 is not what I expected... It seems to be continuously getting faster and also has some peaks that don't oscillate at the full range. I don't understand this as the only thing I'm adjusting is the frequency and it shouldn't be faster than 0.11 Hz. At first I thought it might be an under sampling issue but I get the same results when upsampling the time series to any degree.
知道我在这里遗漏了什么吗?
Any idea what I'm missing here?
推荐答案
cos(f(t)) 的频率"不是 f(t).它是f(t) 的导数.你有:
The "frequency" of cos(f(t)) is not f(t). It's the derivative of f(t). You have:
y1(t) = A*cos(2πf1t) + A
y1(t) = A*cos(2πf1t) + A
y2(t) = cos(2πy1(t))
y2(t) = cos(2πy1(t))
如果您想要的频率是 Acos(2πf1t) + A,那么您需要对其进行积分以获得 cos:
If the frequency you want is Acos(2πf1t) + A, then you need to integrate that to get the argument to cos:
y1(t) = A*sin(2πf1t)/2πf1 + At
y1(t) = A*sin(2πf1t)/2πf1 + At
y2(t) = cos(2πy1(t))
y2(t) = cos(2πy1(t))
在 R 中:
# length in seconds
track_length <- 356
upsample <- 10 # upsample the signal
# LFO rates (Hz)
rate1 <- 0.02
rate2_range <- list(0.00, 2)
# make integral of LFO1
x1 <- 1:(track_length*upsample)/upsample
amp <- (rate2_range[[2]] - rate2_range[[1]])/2
y1 <- amp*sin(2*pi*rate1*x1)/(2*pi*rate1) + amp*x1
plot(x1, y1, type='l')
# make plot of LFO2
x2 <- x1
y2 <- cos(2*pi*y1 / upsample)
plot(x2, y2, type='l')
这篇关于R:产生可变频率的正弦波的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!