3 Code-Excited Fourier Transform -- This is highly experimental and
4 it's not clear at all it even has a remote chance of working
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 int qbank
[] = {1, 2, 4, 6, 8, 12, 16, 20, 24, 28, 36, 44, 52, 68, 84, 116, 128};
31 //int qpulses[] = {2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
32 //int qpulses[] = {2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0};
33 //int qpulses[] = {2, 3, 2, 2, 3, 2, 2, 2, 1, 2, 1, 0, 0, 0, 0};
34 //int qpulses[] = {3, 4, 3, 2, 3, 2, 2, 2, 1, 2, 1, 0, 0, 0, 0};
37 int qpulses
[] = {3, 4, 4, 3, 3, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0};
40 //int qpulses[] = {4, 7, 6, 4, 4, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0};
42 //int qpulses[] = {5, 5, 5, 5, 5, 5, 2, 2, 1, 2, 1, 0, 0, 0, 0};
43 //int qpulses[] = {5, 5, 2, 2, 3, 2, 5, 5, 5, 5, 5, 5, 0, 0, 0};
46 //int qpulses[] = {4, 4, 3, 3, 3, 3, 3, 2, 2, 3, 2, 2, 0, 0, 0};
47 //int qpulses[] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
51 int pbank
[] = {1, 4, 8, 12, 20, 44};
53 void alg_quant(float *x
, int N
, int K
, float *p
)
71 float best_xy
=0, best_yy
=0, best_yp
= 0;
74 float tmp_xy
, tmp_yy
, tmp_yp
;
77 tmp_xy
= xy
+ fabs(x
[j
]);
78 tmp_yy
= yy
+ 2*fabs(y
[j
]) + 1;
83 g
= (sqrt(tmp_yp
*tmp_yp
+ tmp_yy
- tmp_yy
*Rpp
) - tmp_yp
)/tmp_yy
;
84 score
= 2*g
*tmp_xy
- g
*g
*tmp_yy
;
106 x
[i
] = p
[i
]+gain
*y
[i
];
110 void alg_quant2(float *x
, int N
, int K
, float *p
)
121 float best_scores
[L
];
136 xy
[m
] = yy
[m
] = yp
[m
] = gain
[m
] = 0;
147 best_scores
[m
] = -1e10
;
153 //fprintf (stderr, "%d/%d %d/%d %d/%d\n", i, K, m, L2, j, N);
154 float tmp_xy
, tmp_yy
, tmp_yp
;
157 tmp_xy
= xy
[m
] + fabs(x
[j
]);
158 tmp_yy
= yy
[m
] + 2*fabs(y
[m
][j
]) + 1;
160 tmp_yp
= yp
[m
] + p
[j
];
162 tmp_yp
= yp
[m
] - p
[j
];
163 g
= (sqrt(tmp_yp
*tmp_yp
+ tmp_yy
- tmp_yy
*Rpp
) - tmp_yp
)/tmp_yy
;
164 score
= 2*g
*tmp_xy
- g
*g
*tmp_yy
;
166 if (score
>best_scores
[L
-1])
170 while (id
> 0 && score
> best_scores
[id
-1])
178 //fprintf(stderr, "%d %d \n", N, k);
180 ny
[k
][n
] = ny
[k
-1][n
];
182 best_scores
[k
] = best_scores
[k
-1];
195 best_scores
[id
] = score
;
214 x
[i
] = p
[i
]+gain
[0]*y
[0][i
];
218 void noise_quant(float *x
, int N
, int K
, float *p
)
224 x
[i
] = (rand()%1000)/500.-1;
234 void compute_bank(float *X
, float *bank
)
237 for (i
=0;i
<NBANDS
;i
++)
241 for (j
=qbank
[i
];j
<qbank
[i
+1];j
++)
243 bank
[i
] += X
[j
*2-1]*X
[j
*2-1];
244 bank
[i
] += X
[j
*2]*X
[j
*2];
246 //bank[i] = sqrt(.5*bank[i]/(qbank[i+1]-qbank[i]));
247 bank
[i
] = sqrt(bank
[i
]);
251 void normalise_bank(float *X
, float *bank
)
254 for (i
=0;i
<NBANDS
;i
++)
257 float x
= 1.f
/bank
[i
];
258 for (j
=qbank
[i
];j
<qbank
[i
+1];j
++)
264 for (i
=2*qbank
[NBANDS
]-1;i
<256;i
++)
268 void denormalise_bank(float *X
, float *bank
)
271 for (i
=0;i
<NBANDS
;i
++)
275 for (j
=qbank
[i
];j
<qbank
[i
+1];j
++)
285 float norm2(float *x
, int len
)
294 void crappy_fft(float *X
, int len
, int R
, int dir
)
297 float fact
= 2*M_PI
/(len
>>1);
299 for (i
=0;i
<len
>>1;i
++)
303 for (j
=0;j
<len
>>1;j
++)
312 out
[2*i
] += c
*X
[2*j
] + d
*X
[2*j
+1];
313 out
[2*i
+1] += d
*X
[2*j
] + c
*X
[2*j
+1];
317 X
[i
] = out
[i
]/sqrt(len
/2);
320 void crappy_rfft(float *X
, int len
, int R
, int dir
)
325 /*for (i=0;i<len;i++)
326 printf ("%f ", X[i]);*/
334 crappy_fft(x
, N
, R
, 1);
340 for (i
=1;i
<len
>>1;i
++)
342 x
[2*i
] = sqrt(.5)*X
[2*i
];
343 x
[2*i
+1] = sqrt(.5)*X
[2*i
+1];
344 x
[2*(len
-i
)] = sqrt(.5)*X
[2*i
];
345 x
[2*(len
-i
)+1] = -sqrt(.5)*X
[2*i
+1];
351 crappy_fft(x
, N
, R
, -1);
357 printf ("%f ", X[i]);
361 void exp_rotation(float *X
, int len
, float theta
, int dir
)
369 for (i
=0;i
<(len
/2)-1;i
++)
374 X
[2*i
] = c
*x1
- s
*x2
;
375 X
[2*i
+2] = c
*x2
+ s
*x1
;
379 X
[2*i
+1] = c
*x1
- s
*x2
;
380 X
[2*i
+3] = c
*x2
+ s
*x1
;
382 for (i
=(len
/2)-3;i
>=0;i
--)
387 X
[2*i
] = c
*x1
- s
*x2
;
388 X
[2*i
+2] = c
*x2
+ s
*x1
;
392 X
[2*i
+1] = c
*x1
- s
*x2
;
393 X
[2*i
+3] = c
*x2
+ s
*x1
;
397 for (i
=0;i
<(len
/2)-2;i
++)
402 X
[2*i
] = c
*x1
+ s
*x2
;
403 X
[2*i
+2] = c
*x2
- s
*x1
;
407 X
[2*i
+1] = c
*x1
+ s
*x2
;
408 X
[2*i
+3] = c
*x2
- s
*x1
;
411 for (i
=(len
/2)-2;i
>=0;i
--)
416 X
[2*i
] = c
*x1
+ s
*x2
;
417 X
[2*i
+2] = c
*x2
- s
*x1
;
421 X
[2*i
+1] = c
*x1
+ s
*x2
;
422 X
[2*i
+3] = c
*x2
- s
*x1
;
427 void random_rotation(float *X
, int R
, int dir
)
430 for (i
=0;i
<NBANDS
-4;i
++)
432 //crappy_rfft(X+qbank[i]*2-1, 2*(qbank[i+1]-qbank[i]), R+i, dir);
433 //printf ("%f ", norm2(X+qbank[i]*2-1, 2*(qbank[i+1]-qbank[i])));
434 //rotate_vect(X+qbank[i]*2-1, 2*(qbank[i+1]-qbank[i]), R+i, dir);
437 if (qbank
[i
+1]-qbank
[i
] < 12)
441 exp_rotation(X
+qbank
[i
]*2-1, 2*(qbank
[i
+1]-qbank
[i
]), theta
, dir
);
447 void quant_bank(float *X
, float *P
, float centre
)
450 for (i
=0;i
<NBANDS
;i
++)
461 alg_quant2(X
+qbank
[i
]*2-1, 2*(qbank
[i
+1]-qbank
[i
]), q
, P
+qbank
[i
]*2-1);
463 noise_quant(X
+qbank
[i
]*2-1, 2*(qbank
[i
+1]-qbank
[i
]), q
, P
+qbank
[i
]*2-1);
465 //FIXME: This is a kludge, even though I don't think it really matters much
469 void compute_pitch_gain(float *X
, float *P
, float *gains
, float *bank
)
473 for (i
=0;i
<NBANDS
;i
++)
476 for (j
=qbank
[i
];j
<qbank
[i
+1];j
++)
481 for (i
=0;i
<PBANDS
;i
++)
487 for (j
=pbank
[i
];j
<pbank
[i
+1];j
++)
489 Sxy
+= X
[j
*2-1]*P
[j
*2-1]*w
[j
];
490 Sxy
+= X
[j
*2]*P
[j
*2]*w
[j
];
491 Sxx
+= X
[j
*2-1]*X
[j
*2-1]*w
[j
] + X
[j
*2]*X
[j
*2]*w
[j
];
493 gain
= Sxy
/(1e-10+Sxx
);
494 //gain = Sxy/(2*(pbank[i+1]-pbank[i]));
496 //gain *= 1+.02*gain;
504 for (i
=pbank
[PBANDS
]*2-1;i
<256;i
++)
506 /*if (rand()%10 == 0)
508 for (i=0;i<PBANDS;i++)
509 printf ("%f ", gains[i]*gains[i]);
514 void pitch_quant_bank(float *X
, float *P
, float *gains
)
517 for (i
=0;i
<PBANDS
;i
++)
520 for (j
=pbank
[i
];j
<pbank
[i
+1];j
++)
522 P
[j
*2-1] *= gains
[i
];
525 //printf ("%f ", gain);
527 for (i
=pbank
[PBANDS
]*2-1;i
<256;i
++)
532 void pitch_renormalise_bank(float *X
, float *P
)
535 for (i
=0;i
<NBANDS
;i
++)
542 for (j
=qbank
[i
];j
<qbank
[i
+1];j
++)
544 Rxp
+= X
[j
*2-1]*P
[j
*2-1];
545 Rxp
+= X
[j
*2 ]*P
[j
*2 ];
546 Rpp
+= P
[j
*2-1]*P
[j
*2-1];
547 Rpp
+= P
[j
*2 ]*P
[j
*2 ];
548 Rxx
+= X
[j
*2-1]*X
[j
*2-1];
549 Rxx
+= X
[j
*2 ]*X
[j
*2 ];
551 //Rxx *= .5/(qbank[i+1]-qbank[i]);
552 //Rxp *= .5/(qbank[i+1]-qbank[i]);
553 //Rpp *= .5/(qbank[i+1]-qbank[i]);
554 float arg
= Rxp
*Rxp
+ 1 - Rpp
;
557 printf ("arg: %f %f %f %f\n", arg
, Rxp
, Rpp
, Rxx
);
560 gain1
= sqrt(arg
)-Rxp
;
563 //gain1 = sqrt(1.-Rpp);
565 //gain2 = -sqrt(Rxp*Rxp + 1 - Rpp)-Rxp;
566 //if (fabs(gain2)<fabs(gain1))
568 //printf ("%f ", Rxx, Rxp, Rpp, gain);
569 //printf ("%f ", gain1);
571 for (j
=qbank
[i
];j
<qbank
[i
+1];j
++)
573 X
[j
*2-1] = P
[j
*2-1]+gain1
*X
[j
*2-1];
574 X
[j
*2 ] = P
[j
*2 ]+gain1
*X
[j
*2 ];
575 Rxx
+= X
[j
*2-1]*X
[j
*2-1];
576 Rxx
+= X
[j
*2 ]*X
[j
*2 ];
578 //printf ("%f %f ", gain1, Rxx);
591 CEFTState
*ceft_init(int len
)
593 CEFTState
*st
= malloc(sizeof(CEFTState
));
595 st
->frame_fft
= spx_fft_init(st
->length
);
601 void ceft_encode(CEFTState
*st
, float *in
, float *out
, float *pitch
, float *window
)
603 //float bark[BARK_BANDS];
605 float Xbak
[st
->length
];
606 float Xp
[st
->length
];
609 float pitch_bank
[NBANDS
];
613 static float obank
[NBANDS
+1];
614 spx_fft_float(st
->frame_fft
, in
, X
);
616 /* Bands for the input signal */
617 compute_bank(X
, bank
);
619 for (i
=0;i
<st
->length
;i
++)
620 p
[i
] = pitch
[i
]*window
[i
];
622 spx_fft_float(st
->frame_fft
, p
, Xp
);
624 /* Bands for the pitch signal */
625 compute_bank(Xp
, pitch_bank
);
627 if (rand()%10 ==0 && fabs(X
[0]) > 2 && (fabs(X
[0]) > 10 || rand() % 5 == 0))
629 /*printf ("%f ", 20*log10(1+fabs(X[0])));
630 for (i=0;i<NBANDS;i++)
631 printf ("%f ", 20*log10(1+bank[i]));
632 for (i=0;i<NBANDS+1;i++)
633 printf ("%f ", obank[i]);
634 printf ("%f ", 20*log10(1+fabs(Xp[0])));
635 for (i=0;i<NBANDS;i++)
636 printf ("%f ", 20*log10(1+pitch_bank[i]));
638 for (i
=0;i
<NBANDS
;i
++)
639 printf ("%f ", 20*log10(1+bank
[i
])-.9*obank
[i
+1]);
640 /*printf ("%f ", 20*log10(1+fabs(X[0])));
641 for (i=0;i<NBANDS;i++)
642 printf ("%f ", 20*log10(1+bank[i]));*/
646 obank
[0] = 20*log10(1+fabs(X
[0]));
647 for (i
=0;i
<NBANDS
;i
++)
648 obank
[i
+1] = 20*log10(1+bank
[i
]);
653 float tmp
= 1.+X
[0]*X
[0];
654 for (i
=0;i
<NBANDS
;i
++)
656 tmp
= 1. + .1*tmp
+ bank
[i
]*bank
[i
];
657 mask
[i
] = bank
[i
]*bank
[i
]/tmp
;
658 printf ("%f ", 10*log10(mask
[i
]));
665 float Sxw
= 0, Sw
= 0;
666 for (i
=0;i
<NBANDS
;i
++)
668 printf ("%f ", 20*log10(bank
[i
]));
669 Sxw
+= i
*sqrt(bank
[i
]);
673 //printf ("%f\n", Sxw/Sw);
677 normalise_bank(X
, bank
);
679 float in_bank
[NBANDS
];
681 static float last_err
[NBANDS
];
682 static float last_bank
[NBANDS
];
684 for (i
=0;i
<NBANDS
;i
++)
686 in_bank
[i
] = 20*log10(bank
[i
]+1) + .0*last_err
[i
+1] - .9*last_bank
[i
];
688 for (i
=0;i
<NBANDS
;i
++)
689 qbank
[i
] = in_bank
[i
];
691 quantise_bands(in_bank
, qbank
, NBANDS
);
695 for (i
=0;i
<NBANDS
+1;i
++)
709 int sc
= floor(.5 + (in_bank
[i
]-qbank
[i
])/q
);
716 /*for (i=0;i<NBANDS+1;i++)
717 printf ("%f ", in_bank[i]-qbank[i]);
721 for (i
=0;i
<NBANDS
;i
++)
722 last_err
[i
] = qbank
[i
]-in_bank
[i
];
724 for (i
=0;i
<NBANDS
;i
++)
726 qbank
[i
] += .9*last_bank
[i
];
729 bank
[i
] = pow(10,(qbank
[i
])/20)-1;
733 for (i
=0;i
<NBANDS
;i
++)
734 last_bank
[i
] = qbank
[i
];
744 id
= floor(.5+20/q
*log10(1+fabs(X
[0])));
749 printf("%d %f\n", id
, X
[0]);
752 //printf ("%d %f ", id, X[0]);
753 X
[0] = sign
*pow(10,(q
*id
)/20)-1;
754 //printf ("%f\n", X[0]);
757 normalise_bank(Xp
, pitch_bank
);
760 for(i=0;i<st->length;i++)
762 for(i=0;i<st->length;i++)
763 printf ("%f ", X[i]);
767 random_rotation(X
, 10, -1);
768 random_rotation(Xp
, 10, -1);
770 compute_pitch_gain(X
, Xp
, gains
, bank
);
771 quantise_pitch(gains
, PBANDS
);
772 pitch_quant_bank(X
, Xp
, gains
);
774 for (i
=1;i
<st
->length
;i
++)
778 quant_bank(X
, Xp
, centre
);
780 random_rotation(X
, 10, 1);
781 //pitch_renormalise_bank(X, Xp);
786 err
+= (Xbak
[i
] - X
[i
])*(Xbak
[i
] - X
[i
]);
787 printf ("%f\n", err
);
791 /* Denormalise back to real power */
792 denormalise_bank(X
, bank
);
795 spx_ifft_float(st
->frame_fft
, X
, out
);