10 #define BLOCK_SIZE 1024
12 static float float_in
[BLOCK_SIZE
*2];
13 static float float_out
[262144];
14 static short short_in
[BLOCK_SIZE
];
15 static int outcount
=0;
17 static void dump_vec(float *data
, int n
, char *base
, int i
){
21 asprintf(&filename
,"%s_%d.m",base
,i
);
22 f
=fopen(filename
,"w");
32 static void dump_vec2(float *x
, float *y
, int n
, char *base
, int i
){
36 asprintf(&filename
,"%s_%d.m",base
,i
);
37 f
=fopen(filename
,"w");
40 fprintf(f
,"%f -120\n",
42 fprintf(f
,"%f %f\n\n",
55 void blackmann_harris(float *out
, float *in
, int n
){
57 float scale
= 2*M_PI
/n
;
61 float w
= A0
- A1
*cos(scale
*i5
) + A2
*cos(scale
*i5
*2) - A3
*cos(scale
*i5
*3);
66 static void hanning(float *out
, float *in
, int n
){
68 float scale
= 2*M_PI
/n
;
72 out
[i
] = in
[i
]*(.5-.5*cos(scale
*i5
));
76 static void hanningW(float *out
, int n
){
78 float scale
= 2*M_PI
/n
;
82 out
[i
] = (.5-.5*cos(scale
*i5
));
87 void mag_dB(float *log
,float *d
, int n
){
89 log
[0] = todB(d
[0]*d
[0])*.5;
91 log
[i
>>1] = todB(d
[i
-1]*d
[i
-1] + d
[i
]*d
[i
])*.5;
92 log
[n
/2] = todB(d
[n
-1]*d
[n
-1])*.5;
95 int main(int argc
, char **argv
){
100 drft_init(&fft
, BLOCK_SIZE
*2);
103 fprintf (stderr
, "usage: testghost input_file output_file\nWhere the input and output are raw mono files sampled at 44.1 kHz or 48 kHz\n");
107 fin
= fopen(argv
[1], "r");
108 fout
= fopen(argv
[2], "w");
111 for(i=0;i<BLOCK_SIZE*2;i++)
113 blackmann_harris(float_in, float_in, BLOCK_SIZE*2);
114 dump_vec(float_in,BLOCK_SIZE*2,"window",0);
115 memset(float_in,0,sizeof(float_in));
120 memmove(float_in
,float_in
+BLOCK_SIZE
,sizeof(*float_in
)*BLOCK_SIZE
);
121 fread(short_in
, sizeof(short), BLOCK_SIZE
, fin
);
125 for (i
=0;i
<BLOCK_SIZE
;i
++)
126 float_in
[i
+BLOCK_SIZE
] = short_in
[i
] * .000030517578125;
130 /* generate a log spectrum */
131 float fft_buf
[BLOCK_SIZE
*2];
132 float log_fft
[BLOCK_SIZE
+1];
133 float weight
[BLOCK_SIZE
+1];
135 // polish the strongest peaks from weighting
137 float Wout
[BLOCK_SIZE
];
138 float Aout
[BLOCK_SIZE
]={0};
139 float Pout
[BLOCK_SIZE
]={0};
140 float dAout
[BLOCK_SIZE
]={0};
141 float ddAout
[BLOCK_SIZE
]={0};
142 float dWout
[BLOCK_SIZE
]={0};
143 float y
[BLOCK_SIZE
*2];
144 float window
[BLOCK_SIZE
*2];
146 hanning(fft_buf
, float_in
, BLOCK_SIZE
*2);
147 dump_vec(float_in
,BLOCK_SIZE
*2,"data",frame
);
148 drft_forward(&fft
, fft_buf
);
149 for(i
=0;i
<BLOCK_SIZE
*2;i
++)fft_buf
[i
] *= 1./BLOCK_SIZE
;
151 mag_dB(log_fft
,fft_buf
,BLOCK_SIZE
*2);
152 dump_vec(log_fft
,BLOCK_SIZE
+1,"logmag",frame
);
154 window_weight(log_fft
,weight
,BLOCK_SIZE
+1, 0.f
, 512,256, 30, 44100);
155 dump_vec(weight
,BLOCK_SIZE
+1,"weight",frame
);
158 w
[0]=.0044*BLOCK_SIZE
;
159 w
[1]=.136*BLOCK_SIZE
;
165 for(i=0;i<BLOCK_SIZE+1;i++){
166 float v = log_fft[i] - weight[i];
180 extract_sinusoids(float_in
, Aout
, w
, Pout
, dAout
, dWout
, y
, j
, BLOCK_SIZE
*2, 200);
182 extract_modulated_sinusoidsB(float_in, w, Aout, Wout, Pout, dAout, dWout, ddAout, y, j, BLOCK_SIZE*2);
185 extract_modulated_sinusoidsB(float_in, w, Aout, Wout, Pout, dAout, dWout, ddAout, y, j, BLOCK_SIZE*2);
188 extract_modulated_sinusoidsB(float_in, w, Aout, Wout, Pout, dAout, dWout, ddAout, y, j, BLOCK_SIZE*2);
191 extract_modulated_sinusoidsB(float_in, w, Aout, Wout, Pout, dAout, dWout, ddAout, y, j, BLOCK_SIZE*2);
198 fprintf(stdout
, "%d %f\n\n",frame
,w
[i
]/BLOCK_SIZE
*22050);
201 for(i
=0;i
<BLOCK_SIZE
*2;i
++)
202 fft_buf
[i
] = float_in
[i
]-y
[i
];
204 dump_vec(fft_buf
,BLOCK_SIZE
*2,"res",0);
206 hanning(fft_buf
, fft_buf
, BLOCK_SIZE
*2);
207 drft_forward(&fft
, fft_buf
);
208 for(i
=0;i
<BLOCK_SIZE
*2;i
++)fft_buf
[i
] *= 1./BLOCK_SIZE
;
209 mag_dB(log_fft
,fft_buf
,BLOCK_SIZE
*2);
211 dump_vec(log_fft
,BLOCK_SIZE
+1,"exlogmag",0);
214 Aout
[i
]=todB(Aout
[i
]);
215 dAout
[i
]=todB(dAout
[i
]);
219 dump_vec2(w
,Aout
,j
,"ex",0);
220 dump_vec2(w
,dAout
,j
,"dA",0);
221 dump_vec2(w
,dWout
,j
,"dW",0);
222 dump_vec(y
,BLOCK_SIZE
*2,"extract",0);
224 //for (i=0;i<BLOCK_SIZE*2;i++)
225 // float_out[i+outcount] += fft_buf[i];
226 //outcount+=BLOCK_SIZE;
228 //if(outcount+BLOCK_SIZE*2>262144){
230 // hanning(float_out, float_out, 262144);
231 //drft_init(&fft, 262144);
232 //drft_forward(&fft, float_out);
233 //for(i=0;i<262144;i++)float_out[i] *= 1./262144;
235 //mag_dB(float_out,float_out,262144);
236 //dump_vec(float_out,262144/2,"res",0);
246 //fwrite(short_in, sizeof(short), BLOCK_SIZE, fout);