在正弦波中找到每个周期的起点(时间) [英] Find start point (time) of each cycle in a sine wave

查看:728
本文介绍了在正弦波中找到每个周期的起点(时间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现正弦波在5秒内逐渐从8Hz变为2Hz:

I am tying to achieve sine wave gradually changing from 8Hz to 2Hz over 5 seconds:

此波形是在酷编辑中产生的.我给它一个开始频率8Hz,一个结束频率2Hz和持续时间5秒.在给定的时间内,正弦波从一个频率逐渐变为另一个频率.

This waveform was produced in Cool Edit. I gave it a start frequency of 8Hz, an end frequency of 2Hz and a duration of 5 seconds. The sine wave gradually changes from one frequency to the other over the given time.

我的问题是,如何使用FOR循环准确地找到每个循环的开始时间(用红点突出显示)?

My question is, how can I accurately find the start time of each cycle (highlighted with a red dot), using a FOR loop?

伪代码:

time   = 5 //Duration
freq1  = 8 //Start frequency
freq2  = 2 //End frequency

cycles = ( (freq1 + freq2) / 2 ) * time //Total number of cycles

for(i = 0; i < cycles; i++) {
    /* Formula to find start time of each cycle */
}

推荐答案

这是导致该程序疯狂的问题的向后思考.更不用说单个波将不是正弦波,因为频率在变化(它们会稍微失真),这是您的发电机无法实现的,并且结束信号在5秒后将停止为零的可能性很小.而是连续不断地改变频率来产生正弦波:

That is backward thinking for this problem which leads to madness in the program. Not to mention the individual waves will not be a sin wave because the frequency is changing (they will be slightly distorted) which you will not achieve with your generator and also there is very slight chance the ending signal will stop on zero after 5sec. Instead do a continuous sin wave with variable frequency:

  1. 首先计算实际频率

线性插值就足够了(除非您需要进行其他更改)

linear interpolation will suffice (unless you need different change)

f=f0+(f1-f0)*t/T

其中:

f0=8 [Hz] start frequency
f1=2 [Hz] stop frequency
T =5 [s]  change time
t =<0,T> is actual time in [s]

  • 计算正弦波数据

    for (t=0.0,angle=0.0;t<=T;t+=dt)
     {
     f=f0+((f1-f0)*t/T); // actual frequency
     signal=Amplitude*sin(angle); // your signal put it in a array or output somewhere ...
     angle+=6.283185307179586476925286766559*dt*f; // update phase
     while (angle>6.283185307179586476925286766559) // cut just to avoid floating rounding problems
      angle-=6.283185307179586476925286766559;
     }
    

    dt [s]是您要用来采样信号的时间步长.如果您是在实时中生成并输出到实时硬件,则可以使用timer或直接测量时间(在 Windows 或按RDTSC或您可以使用的任何方式

    Where dt [s] is a time step you want to sample your signal with. If you are generating this in Real Time and outputting to real HW you can use a timer or measure the time directly (with performance counters on Windows or by RDTSC or whatever you have at disposal)

    如果您为此获得了预定义数量的样本n,则

    If you got predefined number of samples n for this then

    dt=T/double(n-1);
    

    此处示例输出(n=image width):

    如果还需要周期数,则在angle cut while循环内添加计数器增量,并且也有零点(但是如果采样率太小或需要高精度,则需要对实零位置).

    If you also need the number of periods then add counter increment inside the angle cut while loop And also there is your zero point too (but if samplerate is too small or you need high precision you need to interpolate the real zero position).

    这篇关于在正弦波中找到每个周期的起点(时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆