polar2rect.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. polar2rect.c
  3. David Rowe 28 July 2013
  4. Convert a file of sparse phases in polar (angle) format to a file in rect
  5. format.
  6. */
  7. #include <assert.h>
  8. #include <math.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. typedef struct {
  12. float real;
  13. float imag;
  14. } COMP;
  15. int main(int argc, char *argv[]) {
  16. FILE *fpolar;
  17. FILE *frect;
  18. float polar;
  19. COMP rect;
  20. if (argc != 3) {
  21. printf("usage: %s polarFile rectFile\n", argv[0]);
  22. exit(0);
  23. }
  24. fpolar = fopen(argv[1], "rb");
  25. assert(fpolar != NULL);
  26. frect = fopen(argv[2], "wb");
  27. assert(frect != NULL);
  28. while (fread(&polar, sizeof(float), 1, fpolar) != 0) {
  29. if (polar == 0.0) {
  30. /* this values indicates the VQ training should ignore
  31. this vector element. It's not a valid phase as it
  32. doesn't have mangitude of 1.0 */
  33. rect.real = 0.0;
  34. rect.imag = 0.0;
  35. }
  36. else {
  37. rect.real = cos(polar);
  38. rect.imag = sin(polar);
  39. }
  40. fwrite(&rect, sizeof(COMP), 1, frect);
  41. }
  42. fclose(fpolar);
  43. fclose(frect);
  44. return 0;
  45. }