genampdata.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. genampdata.c
  3. Generates test sparse amplitude data for vqtrainsp 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 200000
  17. #define D 2
  18. #define E 8
  19. int main(void) {
  20. FILE *f=fopen("testamp.flt", "wb");
  21. int i, j, m, L, index;
  22. float amp, noisey_amp, pitch, Wo;
  23. float sparse_pe[MAX_AMP];
  24. #ifdef TEST1
  25. /* D fixed amplitude vectors of E elements long,
  26. with D=2, E=8:
  27. $ ./vqtrainsp testamp.flt 2 8 test.txt
  28. test.txt should be same as training data.
  29. */
  30. for(i=0; i<E; i++) {
  31. amp = i+1;
  32. for(j=0; j<D; j++)
  33. fwrite(&amp, sizeof(float), 1, f);
  34. }
  35. #endif
  36. #ifdef TEST2
  37. /*
  38. Bunch of amps uniformly distributed between -1 and 1. With e
  39. entry "codebook" (1 dimensional vector or scalar):
  40. $ ./vqtrainsp testamp.flt 1 e test.txt
  41. should get std dev of 1/(e*sqrt(3))
  42. */
  43. for(i=0; i<NVEC; i++) {
  44. amp = 1.0 - 2.0*rand()/RAND_MAX;
  45. fwrite(&amp, sizeof(float), 1, f);
  46. }
  47. #endif
  48. #define TEST3
  49. #ifdef TEST3
  50. /*
  51. Data for testing training of spare amplitudes. Similar to TEST1, each
  52. sparse vector is set to the same amplitude.
  53. /vqtrainsp testamp.flt 20 8 test.txt
  54. */
  55. for(i=0; i<NVEC; i++) {
  56. for(amp=1.0; amp<=8.0; amp++) {
  57. pitch = P_MIN + (P_MAX-P_MIN)*((float)rand()/RAND_MAX);
  58. Wo = TWO_PI/pitch;
  59. L = floor(PI/Wo);
  60. //printf("pitch %f Wo %f L %d\n", pitch, Wo, L);
  61. for(m=0; m<MAX_AMP; m++) {
  62. sparse_pe[m] = 0.0;
  63. }
  64. for(m=1; m<=L; m++) {
  65. index = MAX_AMP*m*Wo/PI;
  66. assert(index < MAX_AMP);
  67. noisey_amp = amp + 0.2*(1.0 - 2.0*rand()/RAND_MAX);
  68. sparse_pe[index] = noisey_amp;
  69. #ifdef DBG
  70. if (m < MAX_AMP/8)
  71. printf(" %4.3f ", noisey_amp);
  72. #endif
  73. }
  74. #ifdef DBG
  75. printf("\n");
  76. #endif
  77. fwrite(sparse_pe, sizeof(float), MAX_AMP/8, f);
  78. }
  79. }
  80. #endif
  81. return 0;
  82. }