如何在C中对数据数组执行函数

时间:2013-08-21 13:58:57

标签: c arrays function element

我试图对一组数据执行一个函数,但我不太清楚如何去做,

这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define BUFFER_LEN 10
#define SAMPLE_RATE 48000
#define MAX_DELAY 0.25


int buffer_in[]= {0,1,2,3,4,5,6,7,8,9};
int buffer_out[10];

short int flanger(float , float , int , short int );

int main(void)
{ 
int j,k,l;

    for (j = 0; j <=BUFFER_LEN; j++){
        buffer_out[j] = flanger(buffer_in[j]);    //this is causing the error
        printf("buffer out value = %d",buffer_out[j]);
        }

return 0;
}
// Flanger function

short int flanger(float range, float delay, int rate, short int inData){

  float flangerDelay; /* stores current delay required for flange effect */
  static int i=0;     /* keeps track of time for creating sweep waveform */
  static float sweepValue=0; /* keeps track of current sweep delay in ms */
  static int sweepFlag=1;    /* keeps track of waveform movement         */
  static int writePtr=0;     /* pointer to newest audio sample in buffer */
  static int readPtr=0;      /* pointer to oldest audio sample in buffer */
  float tmp; /* tmp value to see if dly will point to a position in buff */
  float delayArray[50];

  /* convert rate from Hz to Hz according to current sample rate    */
  /* NOTE: If it does not divise exact, take the integer part only! */

  /* is it time to change waveform? if not, increment counter */
  if (i >= rate) {
    /* has the maximum possible delay for sweep been reached? */
    if (sweepValue >= range)
      sweepFlag = 0; /* start the \ of triangular waveform */
    else if (sweepValue <= 0)
      sweepFlag = 1; /* start the / of triangular waveform */

    /* Is the waveform rising or falling? */
    if (sweepFlag==1)
      sweepValue += 0.001; /* increase sweep delay by .001 ms */
    else
      sweepValue -= 0.001; /* decrease sweep delay by .001 ms */

    /* reset i, to start count before waveform changes shape again */
    i=0;
  }
  else i++;

  /* Calculate the total current to delay (in ms, not samples!) */
  flangerDelay = sweepValue + delay;

  /* calculate delay in samples rather than in time */
  tmp = flangerDelay * 22.4f; //(float)(SAMPLE_RATE/1000);

  //printf("flangerDelay: %f samples: %d\n", flangerDelay, tmp);

  /* Calculate position of the read & write pointers */
  if (writePtr < (int)tmp )
    readPtr = (((SAMPLE_RATE/1000)*MAX_DELAY) - ((int)tmp - writePtr));
  else
    readPtr = writePtr - (int)tmp;

  /* has the write pointer reached end of delay buffer? */
  if (writePtr > ((SAMPLE_RATE/1000)*MAX_DELAY)) 
    writePtr=0;
  else
    writePtr++;

  /* now add current audio sample to array and return oldest sample */
  delayArray[writePtr] = inData;  

  /* is tmp a whole value? i.e. will it point to a sample in the buffer? */
  if (tmp > (int)tmp){
    /* not a whole number! therefore, interpolation is required! */
    if (readPtr == ((SAMPLE_RATE/1000) * MAX_DELAY))
      return((delayArray[readPtr] + delayArray[0])/2);
    else
      return((delayArray[readPtr] + delayArray[readPtr+1])/2);
  }
  else {
    /* is a whole number! therefore, can take straight from buffer! */
    return delayArray[readPtr];
  }

}

我在这部分得到了错误

for (j = 0; j <=BUFFER_LEN; j++){
        buffer_out[j] = flanger(buffer_in[j]);    //this is causing the error
        printf("buffer out value = %d",buffer_out[j]);
        }

基本上我想对buffer_in中的数据执行函数并将结果放入buffer_out 我设错了吗? 非常感谢你的帮助!

4 个答案:

答案 0 :(得分:6)

您声明flanger函数接受四个参数,但在调用它时只传递一个参数。您必须使用正确数量的参数调用它,或者将函数更改为仅使用一个参数。

答案 1 :(得分:1)

如果要将数组传递给镶边器,则必须将其声明为接受数组:

short int flanger(int aBuffer[]){

相反,你声明它需要4个不同的参数。

答案 2 :(得分:0)

你需要这样的东西。但是由于您的代码与您想要的代码相差太远,因此很难进一步指定。

void flanger(int buf_out[], int buf_in[], float range, float delay, int rate, short int inData)

也许你只需要:

buffer_out[j] = flanger(range_val, delay_val, rate_val,buffer_in[j])

答案 3 :(得分:0)

也许使用struct数组会更好地为你服务?类似于:(应该按照ANSI C构建)

#include <windows.h>
#include <ansi_c.h>

    typedef struct  {
        float range;
        float delay;
        int rate;
        short inData;
    } PARAMS;

    PARAMS params[10], *pParams;

    void flanger(PARAMS *p);

    void main(void)
    {
        int i;
        pParams = &params[0];

        for(i=0;i<10;i++)
        {
            pParams[i].range = 45.0 + (float)i;
            pParams[i].delay = .003 + (float)i;
            pParams[i].rate = 23 + i;
            pParams[i].inData = 1 + i;
        }


        flanger(pParams);

    }

    void flanger(PARAMS *p)
    {
        //do something with params      
    }

[编辑显示两个struct *参数]
这应该在ANSI C编译器中构建和运行,只需复制和粘贴:

#include <windows.h>
#include <ansi_c.h>

typedef struct  {
    float range;
    float delay;
    int rate;
    short inData;
} PARAMS;

#define DATESIZE 10

void flanger(PARAMS *o, PARAMS *i);

void main(void)
{
    int i;
    PARAMS out[DATESIZE], *pOut, in[DATESIZE], *pIn;

    pOut = &out[0];
    pIn = &in[0];


    printf("Out Data\n");
    printf("Range    Delay    Rate    InData\n");
    for(i=0;i<DATESIZE;i++)
    {
        pOut[i].range = 45.0 + (float)i;
        pOut[i].delay = .003 + (float)i;
        pOut[i].rate = 23 + i;
        pOut[i].inData = 1 + i;
        printf("%4.2f    %5.3f    %d    %d\n", 
                pOut[i].range,
                pOut[i].delay,
                pOut[i].rate,
                pOut[i].inData);
    }


    flanger(pOut, pIn); 

    printf("In Data\n");
    printf("Range    Delay    Rate    InData\n");
    for(i=0;i<DATESIZE;i++)
    {
        printf("%4.2f    %5.3f    %d    %d\n", 
                pIn[i].range,
                pIn[i].delay,
                pIn[i].rate,
                pIn[i].inData);
    }
    getchar();
}

void flanger(PARAMS *out, PARAMS *in)
{
    int i;
    //process "out", pass back "in"
    for(i=0;i<DATESIZE;i++)
    {
        in[i].range = pow(out[i].range, 2.0); 
        in[i].delay = pow(out[i].delay, 2.0);  
        in[i].rate = pow(out[i].rate, 2.0); 
        in[i].inData = pow(out[i].inData, 2.0);
    }


}