2
0

genphdata.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. genphdata.c
  3. Generates test phase data for vqtrainph testing.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <math.h>
  9. #include <ctype.h>
  10. #include <assert.h>
  11. #include "../src/defines.h"
  12. typedef struct {
  13. float real;
  14. float imag;
  15. } COMP;
  16. #define NVEC 100000
  17. #define D 2
  18. #define E 8
  19. int main(void) {
  20. FILE *f=fopen("testph.flt", "wb");
  21. int i, m, L, index;
  22. float angle, noisey_angle, pitch, Wo;
  23. COMP c;
  24. COMP sparse_pe[MAX_AMP];
  25. #ifdef TEST1
  26. for(i=0; i<D*E; i++) {
  27. c.real = cos(i*TWO_PI/(M*D));
  28. c.imag = sin(i*TWO_PI/(M*D));
  29. fwrite(&c, sizeof(COMP), 1, f);
  30. }
  31. #endif
  32. #ifdef TEST2
  33. /*
  34. Bunch of random phases, should get std dev per element of
  35. pi/(sqrt(3)*pow(2,b/D)), or 0.321 for (b=5, D=2):
  36. ./vqtrainph testph.flt 2 32 test.txt
  37. */
  38. for(i=0; i<NVEC; i++) {
  39. angle = PI*(1.0 - 2.0*rand()/RAND_MAX);
  40. c.real = cos(angle);
  41. c.imag = sin(angle);
  42. fwrite(&c, sizeof(COMP), 1, f);
  43. }
  44. #endif
  45. #define TEST3
  46. #ifdef TEST3
  47. /*
  48. Data for testing training in sparse phases. No correlation, so
  49. should be same performance as TEST2. Attempting to train a
  50. MAX_AMP/4 = 20 (first 1 kHz) phase quantiser.
  51. */
  52. angle = 0;
  53. for(i=0; i<NVEC; i++) {
  54. pitch = P_MIN + (P_MAX-P_MIN)*((float)rand()/RAND_MAX);
  55. //pitch = 40;
  56. Wo = TWO_PI/pitch;
  57. L = floor(PI/Wo);
  58. //printf("pitch %f Wo %f L %d\n", pitch, Wo, L);
  59. for(m=0; m<MAX_AMP; m++) {
  60. sparse_pe[m].real = 0.0;
  61. sparse_pe[m].imag = 0.0;
  62. }
  63. angle += PI/8;
  64. for(m=1; m<=L; m++) {
  65. noisey_angle = angle + (PI/16)*(1.0 - 2.0*rand()/RAND_MAX);
  66. //angle = (PI/16)*(1.0 - 2.0*rand()/RAND_MAX);
  67. index = MAX_AMP*m*Wo/PI;
  68. assert(index < MAX_AMP);
  69. sparse_pe[index].real = cos(noisey_angle);
  70. sparse_pe[index].imag = sin(noisey_angle);
  71. }
  72. fwrite(&sparse_pe, sizeof(COMP), MAX_AMP/4, f);
  73. }
  74. #endif
  75. return 0;
  76. }