绘制指数脉冲随时间变化

时间:2016-05-21 18:18:30

标签: javascript php algorithm math

我已经在脑子里挣扎了好几天。

我计划在一段时间内使用PHP和SoX来产生音频脉冲。在这种情况下,我使用频率作为描述每秒发生多少脉冲的方式。所以8的频率是每秒8个脉冲。

我正在使用的脉冲示例(等时音)

img

我需要知道每个脉冲的开始时间,以便我可以在产生音频时及时绘制它们。当频率保持不变时,这很容易做到。但是当频率随时间变化时,我遇到的问题是,完成平均脉冲量所需的时间太长或太少。

以下是Javascript中的一个示例(我现在正在使用JS来使数学工作)

var f0 = 8;        //Start frequency
var f1 = 1;        //End frequency
var interval = 30; //Total time

var average_freq = (f0 + f1) / 2;      //Average frequency
var pulses = average_freq * interval;  //Total number of pulses needed

var pulse_start_time = 0;

for(var i = 0; i < pulses; i++) {
    var delta = i / pulses;
    var this_freq = f0 + (f1 - f0) * delta;

    /*
      need exponential formula to find start time
    */

    console.log(            "pulse: "+ (i+1)     + ", "+ 
                  "pulse frequency: "+ this_freq + ", "+
                       "start time: "+ pulse_start_time);

    pulse_start_time += 1 / this_freq; //This method works fine providing 
                                       //both start and end frequency are 
                                       //the same.
}

我使用以下主题做了很多关于这个主题的研究:

Drawing sine wave with increasing Amplitude and frequency over time

Plot Frequency Sweep In Python

sine wave that slowly ramps up frequency from f1 to f2 for a given time

sine wave that exponentialy changes between frequencies f1 and f2 at given time/amount of samples

但是我仍然无法弄清楚如何使用这些技术获得每个脉冲的开始时间,或者我可能认为这一切都是错误的。

任何帮助将不胜感激! :)

1 个答案:

答案 0 :(得分:0)

您需要使用适当的数学方程式y=f(t)代替信号。通过这种方式,您还可以获得振幅。我不知道你的信号的属性,但它看起来不像对数。我会像这样构建它:

y=( |sin(c0*t)| + sin(c0*t) )^c1 * sin(c2*t) * c3

其中:

  • t是时间
  • c0=frequency of gaps / 2*Pi
  • c1是间隙/信号转换的清晰度
  • c2=frequency of pulses / 2*Pi
  • c3是信号幅度

第一部分只是调整基本信号的间隙和形状的幅度形状,最后一个是你的基本信号。

每秒平均脉冲数不是你的信号频率!相反,它大约是它的一半(取决于信号与间隙比)。现在只需在某个循环中递增t并为您的数据计算y(t)。为避免浮点舍入问题,将t限制为2个sin波之间的公共周期。