You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filter.c 1.8 KiB

21 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * This file is part of Aptdec.
  3. * Copyright (c) 2004-2009 Thierry Leconte (F4DWV), Xerbo (xerbo@protonmail.com) 2019
  4. *
  5. * Aptdec is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <math.h>
  20. #include "filter.h"
  21. // Finite impulse response
  22. float fir(float *buff, const float *coeff, const int len) {
  23. double r;
  24. r = 0.0;
  25. for (int i = 0; i < len; i++) {
  26. r += buff[i] * coeff[i];
  27. }
  28. return(r);
  29. }
  30. /* IQ finite impulse response
  31. * Turn samples into a single IQ sample
  32. */
  33. void iqfir(float *buff, const float *coeff, const int len, double *I, double *Q) {
  34. double i = 0.0, q = 0.0;
  35. for (int k = 0; k < len; k++) {
  36. q += buff[2*k] * coeff[k];
  37. i += buff[2*k];
  38. }
  39. i = buff[len-1] - (i / len);
  40. *I = i, *Q = q;
  41. }
  42. /* Gaussian finite impulse responce compensation
  43. * https://www.recordingblogs.com/wiki/gaussian-window
  44. */
  45. float rsfir(double *buff, const float *coeff, const int len, const double offset, const double delta) {
  46. double out;
  47. out = 0.0;
  48. double n = offset;
  49. for (int i = 0; i < (len-1)/delta-1; n += delta, i++) {
  50. int k;
  51. double alpha;
  52. k = (int)floor(n);
  53. alpha = n - k;
  54. out += buff[i] * (coeff[k] * (1.0 - alpha) + coeff[k + 1] * alpha);
  55. }
  56. return(out);
  57. }