I need a help for change the code below for use it as a simple void function.

I found this code on Gretl :

http://gretl.sourcearchive.com/documentation/1.8.2/genfuncs_8c-source.html

Now i ha some problem , can someone help me.

/**
 * bkbp_filter:
 * @y: array of original data.
 * @bk: array into which to write the filtered series.
 * @pdinfo: data set information.
 *
 * Calculates the Baxter & King bandpass filter.
 *
 * Returns: 0 on success, non-zero error code on failure.
 */

int bkbp_filter (const double *y, double *bk, const DATAINFO *pdinfo)
{
    int t1 = pdinfo->t1, t2 = pdinfo->t2;
    int bkl, bku;

    double omubar, omlbar;
    double avg_a;
    double *a;

    int i, k, t;
    int err = 0;

    /*
      periods[0] and periods[1]: threshold periodicities for business cycle
      k: order of the approximation
    */

    /* get user settings if available (or the defaults) */
    get_bkbp_periods(pdinfo, &bkl, &bku);
    k = get_bkbp_k(pdinfo);

#if BK_DEBUG
    fprintf(stderr, "lower limit = %d, upper limit = %d, \n", 
          bkl, bku);
#endif

    if (bkl >= bku) {
      strcpy(gretl_errmsg, "Error in Baxter-King frequencies");
      return 1;
    }

    err = array_adjust_t1t2(y, &t1, &t2);
    if (err) {
      return err;
    } 

    if (2 * k >= t2 - t1 + 1) {
      strcpy(gretl_errmsg, "Insufficient observations");
      return E_DATA;
    }

    a = malloc((k + 1) * sizeof *a);
    if (a == NULL) {
      return E_ALLOC;
    }
    
    omubar = M_2PI / bkl;
    omlbar = M_2PI / bku;
    
    /* first we compute the coefficients */

    avg_a = a[0] = (omubar - omlbar) / M_PI;

    for (i=1; i<=k; i++) {
      a[i] = (sin(i * omubar) - sin(i * omlbar)) / (i * M_PI);
      avg_a += 2 * a[i];
    }

    avg_a /= (2 * k + 1);

    for (i=0; i<=k; i++) {
      a[i] -= avg_a;
#if BK_DEBUG
      fprintf(stderr, "a[%d] = %#9.6g\n", i, a[i]);
#endif
    }

    /* now we filter the series, skipping the first
       and last k observations */

    for (t=0; t<pdinfo->n; t++) {
      if (t < t1 + k || t > t2 - k) {
          bk[t] = NADBL;
      } else {
          bk[t] = a[0] * y[t];
          for (i=1; i<=k; i++) {
            bk[t] += a[i] * (y[t-i] + y[t+i]);
          }
      }
    }

    free(a);

    return err;
}

Recommended Answers

All 5 Replies

what exactly is the problem?

Hi i need a more simple void function

ie void bkbp_filter ( int bkl, int bku , dataentry)

...
/* now we filter the series
calculation
....

return bkbp_filter

Ok I have try to doing some work. But i 'm so far from solution

/**
 * bkbp_filter:
 * @y: array of original data.
 * @bk: array into which to write the filtered series.
 * @pdinfo: data set information.
 *
 * Calculates the Baxter & King bandpass filter.*/
 void bkbp_filter (const double *y, double *bk, Double *pdinfo)
{
    int t1 = pdinfo->t1, t2 = pdinfo->t2;
    int bkl, bku;

    double omubar, omlbar;
    double avg_a;
    double *a;

    int i, k, t;
    int err = 0;
   bkl=15;
   bku=30;
    k = 500;
    err = array_adjust_t1t2(y, &t1, &t2);
    if (err) {
      return err;
    } 
    a = malloc((k + 1) * sizeof *a);
    if (a == NULL) {
      return E_ALLOC;
    }
    
    omubar = M_2PI / bkl;
    omlbar = M_2PI / bku;
    
    /* first we compute the coefficients */

    avg_a = a[0] = (omubar - omlbar) / M_PI;

    for (i=1; i<=k; i++) {
      a[i] = (sin(i * omubar) - sin(i * omlbar)) / (i * M_PI);
      avg_a += 2 * a[i];
    }

    avg_a /= (2 * k + 1);

    for (i=0; i<=k; i++) {
      a[i] -= avg_a;
    }

    /* now we filter the series, skipping the first
       and last k observations */

    for (t=0; t<pdinfo->n; t++) {
      if (t < t1 + k || t > t2 - k) {
          bk[t] = NADBL;
      } else {
          bk[t] = a[0] * y[t];
          for (i=1; i<=k; i++) {
            bk[t] += a[i] * (y[t-i] + y[t+i]);
          }
      }
    }

    free(a);
return bk;}

Sorry the formula posted is c but i need c++ code for it

Il try to do some by myself but it hard because have any skill

#include <math.h>;
void bpass(double **data,double pl, double pu)
{
  int nobs = 0;
  int k = 0;
  double drift = 0;
  double a = 0;
  double b = 0;
  double bnot = 0;
  double bhat = 0;
  double **AA = 0;
  double **AAt = 0;
  int l = 0;
  const double pi = 3.14159265358979;
  double **datanew = 0;
  double **BB = 0;
    nobs = 500;
   for (k = 1; k <= nobs; k++)
  {
    datanew[k][1]=data[k][1];    
  }
  //Removing drift'
  
  drift = (datanew[nobs][1] - datanew[1][1]) / (nobs - 1);
  for (k = 1; k <= nobs; k++)
  {
    datanew[k][1] = datanew[k][1] - (k - 1) * drift;
  }
  //Create the ideal B's then construct the AA matrix'
  b = 2 * pi / pl;
  a = 2 * pi / pu;
  bnot = (b - a) / pi;
  bhat = bnot / 2;
  BB[nobs + 1][2];
  for (k = 1; k < nobs; k++)
  {
    BB[k + 1][1] = (sin(k * b) - sin(k * a)) / (k * pi);
  }
  BB[1][1] = bnot;
  AAt[2 * nobs + 1][2 * nobs + 1];
  AA[nobs + 1][nobs + 1];
  for (k = 1; k <= nobs; k++)
  {
    for (l = 1; l <= nobs; l++)
    {
      AAt[k][l + k - 1] = BB[l][1];
      AAt[k + l - 1][k] = BB[l][1];
    }
  }
  for (k = 1; k <= nobs; k++)
  {
    for (l = 1; l <= nobs; l++)
    {
      AA[k][l] = AAt[k][l];
    }
  }
  AA[1][1] = bhat;
  AA[nobs][nobs] = bhat;
  for (k = 1; k < nobs; k++)
  {
    AA[k + 1][1] = AA[k][1] - BB[k][1];
    AA[nobs - k][nobs] = AA[k][1] - BB[k][1];
  }
  BB[nobs + 1][2];
  for (k = 1; k <= nobs; k++)
  {
    for (l = 1; l <= nobs; l++)
    {
      BB[k][1] = BB[k][1] + AA[k][l] * datanew[l][1];
    }
  }
 return BB; 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.