extract.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*--------------------------------------------------------------------------*\
  2. FILE........: extract.c
  3. AUTHOR......: David Rowe
  4. DATE CREATED: 23/2/95
  5. This program extracts a float file of vectors from a text file
  6. of vectors. The float files are easier to process quickly
  7. during VQ training. A subset of the text file VQ may be
  8. extracted to faciltate split VQ of scaler VQ design.
  9. \*--------------------------------------------------------------------------*/
  10. /*
  11. Copyright (C) 2009 David Rowe
  12. All rights reserved.
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU Lesser General Public License version 2, as
  15. published by the Free Software Foundation. This program is
  16. distributed in the hope that it will be useful, but WITHOUT ANY
  17. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  19. License for more details.
  20. You should have received a copy of the GNU Lesser General Public License
  21. along with this program; if not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #define MAX_STR 2048 /* maximum string length */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <assert.h>
  29. void scan_line(FILE *fp, float f[], int n);
  30. int main(int argc, char *argv[]) {
  31. FILE *ftext; /* text file of vectors */
  32. FILE *ffloat; /* float file of vectors */
  33. int st,en; /* start and end values of vector to copy */
  34. float *buf; /* ptr to vector read from ftext */
  35. long lines; /* lines read so far */
  36. if (argc != 5) {
  37. printf("usage: %s TextFile FloatFile start(1 .. 10) end(1 .. 10)\n", argv[0]);
  38. exit(1);
  39. }
  40. /* read command line arguments and open files */
  41. ftext = fopen(argv[1],"rt");
  42. if (ftext == NULL) {
  43. printf("Error opening text file: %s\n",argv[1]);
  44. exit(1);
  45. }
  46. ffloat = fopen(argv[2],"wb");
  47. if (ffloat == NULL) {
  48. printf("Error opening float file: %s\n",argv[2]);
  49. exit(1);
  50. }
  51. st = atoi(argv[3]);
  52. en = atoi(argv[4]);
  53. buf = (float*)malloc(en*sizeof(float));
  54. if (buf == NULL) {
  55. printf("Error in malloc()\n");
  56. exit(1);
  57. }
  58. lines = 0;
  59. while(!feof(ftext)) {
  60. scan_line(ftext, buf, en);
  61. if (!feof(ftext)) {
  62. fwrite(&buf[st-1], sizeof(float), en-st+1, ffloat);
  63. printf("\r%ld lines",++lines);
  64. }
  65. }
  66. printf("\n");
  67. /* clean up and exit */
  68. free(buf);
  69. fclose(ftext);
  70. fclose(ffloat);
  71. return 0;
  72. }
  73. /*---------------------------------------------------------------------------*\
  74. FUNCTION....: scan_line()
  75. AUTHOR......: David Rowe
  76. DATE CREATED: 20/2/95
  77. This function reads a vector of floats from a line in a text file.
  78. \*---------------------------------------------------------------------------*/
  79. void scan_line(FILE *fp, float f[], int n)
  80. /* FILE *fp; file ptr to text file */
  81. /* float f[]; array of floats to return */
  82. /* int n; number of floats in line */
  83. {
  84. char s[MAX_STR];
  85. char *ps,*pe;
  86. int i;
  87. memset(s, 0, MAX_STR);
  88. ps = pe = fgets(s,MAX_STR,fp);
  89. if (ps == NULL)
  90. return;
  91. for(i=0; i<n; i++) {
  92. while( isspace(*pe)) pe++;
  93. while( !isspace(*pe)) pe++;
  94. sscanf(ps,"%f",&f[i]);
  95. ps = pe;
  96. }
  97. }