FIRFilter::FIRFilter(double beta, double gamma, double cutoff) { int pointer, increment, numberCoefficients; double coefficient[LIMIT + 1]; /* DETERMINE IDEAL LOW PASS FILTER COEFFICIENTS */ maximallyFlat(beta, gamma, &numberCoefficients, coefficient); /* TRIM LOW-VALUE COEFFICIENTS */ trim(cutoff, &numberCoefficients, coefficient); /* DETERMINE THE NUMBER OF TAPS IN THE FILTER */ numberTaps_ = (numberCoefficients * 2) - 1; /* ALLOCATE MEMORY FOR DATA AND COEFFICIENTS */ data_.resize(numberTaps_); coef_.resize(numberTaps_); /* INITIALIZE THE COEFFICIENTS */ increment = -1; pointer = numberCoefficients; for (int i = 0; i < numberTaps_; i++) { coef_[i] = coefficient[pointer]; pointer += increment; if (pointer <= 0) { pointer = 2; increment = 1; } } /* SET POINTER TO FIRST ELEMENT */ ptr_ = 0; #if 0 /* PRINT OUT */ printf("\n"); for (int i = 0; i < numberTaps_; i++) { printf("coef_[%-d] = %11.8f\n", i, coef_[i]); } #endif }
TRMFIRFilter *TRMFIRFilterCreate(double beta, double gamma, double cutoff) { TRMFIRFilter *newFilter; int i, pointer, increment, numberCoefficients; double coefficient[LIMIT+1]; newFilter = (TRMFIRFilter *)malloc(sizeof(TRMFIRFilter)); if (newFilter == NULL) { fprintf(stderr, "Couldn't malloc() FIRFilter.\n"); return NULL; } /* DETERMINE IDEAL LOW PASS FILTER COEFFICIENTS */ maximallyFlat(beta, gamma, &numberCoefficients, coefficient); /* TRIM LOW-VALUE COEFFICIENTS */ trim(cutoff, &numberCoefficients, coefficient); /* DETERMINE THE NUMBER OF TAPS IN THE FILTER */ newFilter->numberTaps = (numberCoefficients * 2) - 1; /* ALLOCATE MEMORY FOR DATA AND COEFFICIENTS */ newFilter->FIRData = (double *)calloc(newFilter->numberTaps, sizeof(double)); if (newFilter->FIRData == NULL) { fprintf(stderr, "calloc() of FIRData failed.\n"); free(newFilter); return NULL; } newFilter->FIRCoef = (double *)calloc(newFilter->numberTaps, sizeof(double)); if (newFilter->FIRCoef == NULL) { fprintf(stderr, "calloc() of FIRCoef failed.\n"); free(newFilter->FIRData); free(newFilter); return NULL; } /* INITIALIZE THE COEFFICIENTS */ increment = -1; pointer = numberCoefficients; for (i = 0; i < newFilter->numberTaps; i++) { newFilter->FIRCoef[i] = coefficient[pointer]; pointer += increment; if (pointer <= 0) { pointer = 2; increment = 1; } } /* SET POINTER TO FIRST ELEMENT */ newFilter->FIRPtr = 0; #if DEBUG /* PRINT OUT */ printf("\n"); for (i = 0; i < newFilter->numberTaps; i++) printf("FIRCoef[%-d] = %11.8f\n", i, newFilter->FIRCoef[i]); #endif return newFilter; }