Studentenversion des ESY6/A Praktikums "signal_processing".
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.

fft.c 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "system/task_fft.h"
  2. #include "system/data_channel.h"
  3. #include "system/Complex.h"
  4. #include "system/float_word.h"
  5. #include <math.h>
  6. #include <complex.h>
  7. #include <stdio.h>
  8. void fft_radix4(complex float *x) {
  9. int n = DATA_CHANNEL_DEPTH;
  10. int stages = log(n) / log(4); // Anzahl der FFT-Stufen
  11. // Bit-Reversal-Rearrangement (Umordnung der Daten für FFT)
  12. for (int i = 0; i < n; i++) {
  13. int rev = 0, num = i;
  14. for (int bit = 0; bit < stages; bit++) {
  15. rev = rev * 4 + (num % 4);
  16. //printf("i: %d, rev: %d\n", i, rev);
  17. num /= 4;
  18. }
  19. if (i < rev) {
  20. complex float temp = x[i];
  21. x[i] = x[rev];
  22. x[rev] = temp;
  23. }
  24. }
  25. // Radix-4 Butterfly-Berechnung
  26. for (int s = 1; s <= stages; s++) {
  27. int m = pow(4, s); // Gruppengröße (4^s)
  28. int quarter_m = m / 4; // Viertel der Gruppengröße
  29. float theta = -2.0f * M_PI / m; // Grundwinkel der Wurzeln der Einheit
  30. //printf("Stage: %d, m: %d, theta: %f\n", s, m, theta);
  31. for (int k = 0; k < n; k += m) { // Iteration über Gruppen
  32. for (int j = 0; j < quarter_m; j++) { // Innerhalb der Gruppe
  33. // Wurzeln der Einheit
  34. complex float w0 = 1.0f; // Wurzel für j = 0
  35. complex float w1 = cexpf(I * theta * j); // Wurzel für j = 1
  36. complex float w2 = cexpf(I * theta * 2 * j); // Wurzel für j = 2
  37. complex float w3 = cexpf(I * theta * 3 * j); // Wurzel für j = 3
  38. // Lade die Werte aus der Gruppe
  39. complex float t0 = x[k + j];
  40. complex float t1 = x[k + j + quarter_m] * w1;
  41. complex float t2 = x[k + j + 2 * quarter_m] * w2;
  42. complex float t3 = x[k + j + 3 * quarter_m] * w3;
  43. //printf("w1: %f + %fi, w2: %f + %fi, w3: %f + %fi\n", crealf(w1), cimagf(w1), crealf(w2), cimagf(w2), crealf(w3), cimagf(w3));
  44. //printf("Before: t0: %f + %fi, t1: %f + %fi, t2: %f + %fi, t3: %f + %fi\n", crealf(t0), cimagf(t0), crealf(t1), cimagf(t1), crealf(t2), cimagf(t2), crealf(t3), cimagf(t3));
  45. // Butterfly-Operationen
  46. x[k + j] = t0 + t1 + t2 + t3;
  47. x[k + j + quarter_m] = t0 - t1 + I * (t3 - t2);
  48. x[k + j + 2 * quarter_m] = t0 - t2 + t1 - t3;
  49. x[k + j + 3 * quarter_m] = t0 - t1 - I * (t3 - t2);
  50. //printf("After: x[%d]: %f + %fi, x[%d]: %f + %fi\n", k + j, crealf(x[k + j]), cimagf(x[k + j]), k + j + quarter_m, crealf(x[k + j + quarter_m]), cimagf(x[k + j + quarter_m]));
  51. }
  52. }
  53. }
  54. }
  55. int task_fft_run(void *task) {
  56. fft_config *config = (fft_config *)task;
  57. complex float x[DATA_CHANNEL_DEPTH];
  58. float c[DATA_CHANNEL_DEPTH];
  59. for (uint32_t i = 0; i < DATA_CHANNEL_DEPTH; ++i) {
  60. float a;
  61. data_channel_read(config->base.sources[0], (uint32_t *) &a);
  62. x[i] = a;
  63. //printf("Input x[%d] = %f + %fi\n", i, crealf(x[i]), cimagf(x[i]));
  64. }
  65. fft_radix4(x);
  66. for (uint32_t i = 0; i < DATA_CHANNEL_DEPTH; ++i) {
  67. //printf("Output complex x[%d] = %f + %fi\n", i, crealf(x[i]), cimagf(x[i]));
  68. c[i] = sqrt(pow(crealf(x[i]), 2) + pow(cimagf(x[i]), 2)); // Betrag
  69. if (i == 0)
  70. c[i] = c[i] * 1/DATA_CHANNEL_DEPTH; // Sklaierung
  71. else
  72. c[i] = c[i] * 2/DATA_CHANNEL_DEPTH; // Sklaierung
  73. printf("Output Magnitude skaliert c[%d] = %f\n", i, c [i]);
  74. float_word output;
  75. output.value = c[i];
  76. data_channel_write(config->base.sink, output.word);
  77. }
  78. return 0;
  79. }