我正在写一个c程序来产生一个正弦波,在一段给定的时间间隔内,将频率从f1缓慢上升到f2.
我已经写了这个c程序,将频率从0 Hz提高到10 Hz,但问题是频率在360度完成后发生变化.如果我尝试改变0和360度之间的频率,转换不平滑,并且它是突然的.
这是我使用的sin的方程y = Amplitude * sin(freq * phase)
int main(int argc,char *argv[]) { double y,freq,phase; int count; // for convenience of plotting in matlab so all the waves are spread on x axis. for (freq = 0; freq < 10; freq+=1) { for (phase = 0; phase < 360; phase++) { // phase is 360 degrees y = 3 * sin((count*6.283185)+(freq*(phase*(3.14159/180)))); printf("%f %f %f \n",phase,y); } count++; } return EXIT_SUCCESS; }
>如何在给定的时间内平滑地改变频率?
>我应该研究傅里叶变换吗?
解决方法
如果您希望角频率(w = 2 pi f)随时间线性变化,则dw / dt = a和w = w0(wn-w0)* t / tn(其中t从0到tn,w从w0到w WN).相位是其积分,因此相位= w0 t(wn-w0)* t ^ 2 /(2tn)(如oli所述):
void sweep(double f_start,double f_end,double interval,int n_steps) { for (int i = 0; i < n_steps; ++i) { double delta = i / (float)n_steps; double t = interval * delta; double phase = 2 * PI * t * (f_start + (f_end - f_start) * delta / 2); while (phase > 2 * PI) phase -= 2 * PI; // optional printf("%f %f %f",t,phase * 180 / PI,3 * sin(phase)); } }
(其中间隔为tn,delta为t / tn).
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)
顺便说一下,如果你正在听这个(或看着它 – 涉及人类感知的任何东西),我怀疑你不想要线性增长,而是一个指数级的增长.但那是a different question …