File: genlsp.c

package info (click to toggle)
codec2 0.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 113,072 kB
  • sloc: ansic: 412,877; python: 4,004; sh: 1,540; objc: 817; asm: 683; makefile: 588
file content (183 lines) | stat: -rw-r--r-- 4,760 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*--------------------------------------------------------------------------*\

	FILE........: genlsp.c
	AUTHOR......: David Rowe
	DATE CREATED: 23/2/95

	This program genrates a text file of LSP vectors from an input
	speech file.

\*--------------------------------------------------------------------------*/

/*
  Copyright (C) 2009 David Rowe

  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  This program is
  distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#define P 	12	/* LP order					*/
#define LSP_DELTA1 0.01 /* grid spacing for LSP root searches */
#define NW	279	/* frame size in samples 			*/
#define	N  	80 	/* frame to frame shift				*/
#define THRESH	40.0	/* threshold energy/sample for frame inclusion 	*/
#define PI         3.141592654	/* mathematical constant                */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "lpc.h"	/* LPC analysis functions 			*/
#include "lsp.h"	/* LSP encode/decode functions 			*/

int switch_present(sw,argc,argv)
  char sw[];     /* switch in string form */
  int argc;      /* number of command line arguments */
  char *argv[];  /* array of command line arguments in string form */
{
  int i;       /* loop variable */

  for(i=1; i<argc; i++)
    if (!strcmp(sw,argv[i]))
      return(i);

  return 0;
}

int main(int argc, char *argv[]) {
    FILE   *fspc;	/* input file ptr for test database		*/
    FILE   *flsp;	/* output text file of LSPs 			*/
    short  buf[N];	/* input frame of speech samples 		*/
    float  Sn[NW];	/* float input speech samples 			*/
    float  ak[P+1];	/* LPCs for current frame 			*/
    float  lsp[P];	/* LSPs for current frame 			*/
    float  lsp_prev[P];	/* LSPs for previous frame 			*/
    float  E;		/* frame energy 				*/
    long   f;		/* number of frames                             */
    long   af;		/* number frames with "active" speech 		*/
    float  Eres;	/* LPC residual energy 				*/
    int    i;
    int    roots;
    int    unstables;
    int    lspd, log, lspdt;
    float  diff;

    /* Initialise ------------------------------------------------------*/

    if (argc < 3) {
	printf("usage: %s RawFile LSPTextFile [--lspd] [--log] [--lspdt] \n", argv[0]);
	exit(1);
    }

    /* Open files */

    fspc = fopen(argv[1],"rb");
    if (fspc == NULL) {
	printf("Error opening input SPC file: %s",argv[1]);
	exit(1);
    }

    flsp = fopen(argv[2],"wt");
    if (flsp == NULL) {
	printf("Error opening output LSP file: %s",argv[2]);
	exit(1);
    }

    lspd = switch_present("--lspd", argc, argv);
    log = switch_present("--log", argc, argv);
    lspdt = switch_present("--lspdt", argc, argv);

    for(i=0; i<NW; i++)
	Sn[i] = 0.0;

    /* Read SPC file, and determine aks[] for each frame ------------------*/

    f = af = 0;
    unstables = 0;
    while(fread(buf,sizeof(short),N,fspc) == N) {

	for(i=0; i<NW-N; i++)
	    Sn[i] = Sn[i+N];
	E = 0.0;
	for(i=0; i<N; i++) {
	    Sn[i+NW-N] = buf[i];
	    E += Sn[i]*Sn[i];
	}

	E = 0.0;
	for(i=0; i<NW; i++) {
	    E += Sn[i]*Sn[i];
	}
	E = 10.0*log10(E/NW);

	/* If energy high enough, include this frame */

	f++;
	if (E > THRESH) {
	    af++;
	    printf("Active Frame: %ld  unstables: %d\n",af, unstables);

	    find_aks(Sn, ak, NW, P, &Eres);
	    roots = lpc_to_lsp(ak, P , lsp, 5, LSP_DELTA1);
	    if (roots == P) {
		if (lspd) {
		    if (log) {
			fprintf(flsp,"%f ",log10(lsp[0]));
			for(i=1; i<P; i++) {
			    diff = lsp[i]-lsp[i-1];
			    if (diff < (PI/4000.0)*25.0) diff = (PI/4000.0)*25.0;
			    fprintf(flsp,"%f ",log10(diff));
			}
		    }
		    else {
			fprintf(flsp,"%f ",lsp[0]);
			for(i=1; i<P; i++)
			    fprintf(flsp,"%f ",lsp[i]-lsp[i-1]);
		    }

		    fprintf(flsp,"\n");

		}
		else if (lspdt) {
		    for(i=0; i<P; i++)
			fprintf(flsp,"%f ",lsp[i]-lsp_prev[i]);
		    fprintf(flsp,"\n");

		}
		else {
		    if (log) {
			for(i=0; i<P; i++)
			    fprintf(flsp,"%f ",log10(lsp[i]));
			fprintf(flsp,"\n");
		    }
		    else {
			for(i=0; i<P; i++)
			    fprintf(flsp,"%f ",lsp[i]);
			fprintf(flsp,"\n");
		    }

		}
		memcpy(lsp_prev, lsp, sizeof(lsp));
	    }
	    else
		unstables++;
	}
    }

    printf("%3.2f %% active frames\n", 100.0*(float)af/f);
    fclose(fspc);
    fclose(flsp);

    return 0;
}