fixed an SBR bug/cheat using a bunch of ugly hacks
[ghost.git] / libghost / quant_sinusoids.c
blobf8a0054082cab70c407b176dbb0bdddf152f958a
1 /**
2 @file sinusoids.c
3 @brief Sinusoid extraction/synthesis
4 */
6 /* Copyright (C) 2005
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <math.h>
25 #include "sinusoids.h"
26 #include <stdio.h>
27 #include "ghost.h"
29 #define MIN(a,b) ((a)<(b) ? (a):(b))
30 #define MAX(a,b) ((a)>(b) ? (a):(b))
35 /* Quantizes the extracted sinusoids
36 * w are the frequencies in current frame
37 * N is the number of sinusoids
38 * SineStruct is structure holding quantized sine params
41 void quantize_sinusoids(float *wi,int N,QuantSine SineStruct[SINUSOIDS])
44 /* sort the current sinusoids */
45 float w[SINUSOIDS];
46 int i,j,lindex = 0;
47 static QuantSine prevSineStruct[SINUSOIDS];
50 for(i=0;i<N;i++)
52 w[i] = wi[i];
55 for(i=0;i<N;i++)
57 for(j=(i+1);j<N;j++)
59 if(w[j]< w[i])
61 float t = w[i];
62 w[i] = w[j];
63 w[j] = t;
68 /* start with lowest freq and group its harmonics. Go on till
69 all sinuosids belong to a group*/
71 for(i=0;i<N;i++)
73 if(w[i]!= -1)
75 SineStruct[lindex].basefreq = w[i];
76 SineStruct[lindex].harmonicstate;
77 for(j=(i+1);j<N;j++)
79 if(w[j]!= -1)
81 int harmonic = w[j]/SineStruct[lindex].basefreq;
83 if((w[j]/SineStruct[lindex].basefreq - (float)(harmonic) < 0.01)&&
84 (w[j]/SineStruct[lindex].basefreq - (float)(harmonic) > -0.01))
86 w[j] = -1;
87 SineStruct[lindex].harmonicstate = (SineStruct[lindex].harmonicstate)
88 | (1<<harmonic);
93 lindex++;
98 /* compare the current groups with groups of prev frames
99 and see which all have to be transmitted */
100 i= 0;
101 while(i<lindex)
103 /* compare the current set with all the freq in the prev bands*/
104 if(SineStruct[i].basefreq < prevSineStruct[j].basefreq)
106 //add i
107 i++;
109 else if(SineStruct[i].basefreq > prevSineStruct[j].basefreq)
111 //rem j
112 j++;
114 else
116 if(SineStruct[i].harmonicstate != prevSineStruct[j].harmonicstate)//state[i] != prevstate [j]
118 //update state of i
125 /* Inverse quantizes the extracted sinusoids
126 * w are the frequencies in current frame
127 * N is the number of sinusoids
128 * len is the frame size
131 void invquantize_sinusoids(float *x,float *w,int N,int len)
134 /*add the sinuosids newly sent in current frame to wi vector */
136 /* check for all teh sinusoids; add the ones which are to be added and remove the ones which are to be removed to the
137 w vector already existing */
141 /* Synthesizes the signal from the sinusoids
142 * w are the frequencies
143 * ai are the cos(x) coefficients
144 * bi are the sin(x) coefficients
145 * y is the synthesized signal by summing all the params
146 * N is the number of sinusoids
147 * len is the frame size
149 void generate_sinusoids(float *w,
150 float *window,
151 float *ai,
152 float *bi,
153 float *y,
154 int N,
155 int len)
157 int i,j;
158 float cos_table[SINUSOIDS][LENGTH];
159 float sin_table[SINUSOIDS][LENGTH];
161 for (i=0;i<N;i++)
163 float tmp1=0, tmp2=0;
164 float tmp3=0, tmp4=0;
165 for (j=0;j<len;j++)
167 cos_table[i][j] = cos(w[i]*j)*window[j];
168 sin_table[i][j] = sin(w[i]*j)*window[j];
172 for (i=0;i<N;i++)
174 for (j=0;j<len;j++)
176 y[j] += ai[i]*cos_table[i][j] + bi[i]*sin_table[i][j];