首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加第3次脉冲时iOS生成脉冲失败

添加第3次脉冲时iOS生成脉冲失败
EN

Stack Overflow用户
提问于 2014-05-13 02:16:31
回答 1查看 34关注 0票数 0

当添加第二个for循环创建时,我尝试使用iOS创建自定义的波形信号,它可以在间隔0.8T和0.9Tm之间创建脉冲,其中T是周期。

当我尝试添加第三个for循环来创建一个循环时,它会在0.7T周期后用凸双曲线产生一个奇怪的信号,而不是产生一个新的脉冲。

请您告诉我,我将修改或重置缓冲区变量,以便我可以产生多个脉冲?

下面是我的代码

代码语言:javascript
复制
// Fixed amplitude is good enough for our purposes
const double amplitude = 1.0;

// Get the tone parameters out of the view controller
ToneGeneratorViewController *viewController =
(ToneGeneratorViewController *)inRefCon;
double theta = viewController->theta;
//double theta_increment = 2.0 * M_PI * viewController->frequency / viewController->sampleRate;
double theta_increment = viewController->sampleRate / viewController->frequency;

( sampleRate = 44100;频率= 44.44 )

代码语言:javascript
复制
const int channel = 0;
    Float32 *buffer = (Float32 *)ioData->mBuffers[channel].mData;

    float squareIndex = 0.0;
    //Generate the samples//
    for (UInt32 frame = 0; frame < inNumberFrames; frame++)
    {
        float k =0.0;
        float y = 34.0/75.0;
        if( fmodf(squareIndex, theta_increment)/theta_increment < y) {
            k = 1.0;
        } else {
            k = 0.0;
        }
        buffer[frame] = k * amplitude;
        squareIndex += 1.0;
        if(squareIndex >= theta_increment) squareIndex-=theta_increment;
        viewController->theta = theta;
    }


    for (UInt32 frame = 0; frame < inNumberFrames; frame++)
    {
        float k =0.0;
        float z = 0.8;
        float y = 0.9;
        if( z < fmodf(squareIndex, theta_increment)/theta_increment < y) {
            k = 0.0;
        } else {
            k = 1.0;
        }
        buffer[frame] += k * amplitude;
        squareIndex += 1.0;
        if(squareIndex >= theta_increment) squareIndex-=theta_increment;
        viewController->theta = theta;
    }


    for (UInt32 frame = 0; frame < inNumberFrames; frame++)
    {
        float k =0.0;
        float z = 0.6;
        float y = 0.7;
        if( z < fmodf(squareIndex, theta_increment)/theta_increment < y) {
            k = 0.0;
        } else {
            k = 1.0;
        }
        buffer[frame] += k * amplitude;
        squareIndex += 1.0;
        if(squareIndex >= theta_increment) squareIndex-=theta_increment;
        viewController->theta = theta;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-15 02:58:06

由于我们在提供的代码中没有看到缓冲区分配,所以我假设缓冲区的大小足以容纳所有示例(否则缓冲区溢出会导致各种未定义的行为)。

在提供了代码之后,您应该意识到在循环中,表达式

代码语言:javascript
复制
if( z < fmodf(squareIndex, theta_increment)/theta_increment < y) {

从左到右评估如下:

代码语言:javascript
复制
if( (z < fmodf(squareIndex, theta_increment)/theta_increment) < y) {

让我们看一下第二个循环来说明效果:

只要squareIndex is less than0.8*theta_increment`,这个子表达式

代码语言:javascript
复制
(z < fmodf(squareIndex, theta_increment)/theta_increment)

计算结果为false,在数值提升后,该值小于y=0.9,因此整体表达式为true (so k=0)。一旦squareIndex超过0.8*theta_increment

代码语言:javascript
复制
(z < fmodf(squareIndex, theta_increment)/theta_increment)

变成true,在数值提升之后,它比y=0.9还要多,所以整个表达式变成了false (所以k=1)。然后,循环生成以下曲线:

从上到下,您有第一个,第二个和第三个循环,然后是组合波形。

若要解决此问题,可以将条件更改为:

代码语言:javascript
复制
 float t = fmodf(squareIndex, theta_increment)/theta_increment;
 if (z < t && t < y) {
   k = 1.0;
 } else {
   k = 0.0;
 }

然后产生以下波形:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23621821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档