40 #include "sigpr/EST_sigpr_frame.h"
41 #include "sigpr/EST_fft.h"
42 #include "EST_inline_utils.h"
44 #include "EST_error.h"
45 #include "EST_TBuffer.h"
47 #define ALMOST1 0.99999
48 #define MAX_ABS_CEPS 4.0
50 float Hz2Mel(
float frequency_in_Hertz);
51 float Mel2Hz(
float frequency_in_Mel);
53 float lpredict(
float *adc,
int wsize,
54 float *acf,
float *ref,
float *lpc,
56 float ogi_lpc(
float *adc,
int wsize,
int order,
57 float *acf,
float *ref,
float *lpc);
71 sig2lpc(in_frame, out_frame);
72 else if (in_type ==
"lsf")
73 lsf2lpc(in_frame, out_frame);
74 else if (in_type ==
"ref")
75 ref2lpc(in_frame, out_frame);
77 EST_error(
"Cannot convert coefficient type %s to lpc\n",
78 (
const char *) in_type);
87 lpc2ref(in_frame, out_frame);
88 else if (in_type ==
"sig")
91 sig2lpc(in_frame, tmp);
92 lpc2ref(tmp, out_frame);
94 else if (in_type ==
"lsf")
97 lsf2lpc(in_frame, tmp);
98 lpc2ref(tmp, out_frame);
101 EST_error(
"Cannot convert coefficient type %s to reflection coefs\n",
102 (
const char *) in_type);
110 if (in_type ==
"lpc")
111 lpc2ref(in_frame, out_frame);
112 else if (in_type ==
"sig")
115 sig2lpc(in_frame, tmp);
116 lpc2ref(tmp, out_frame);
118 else if (in_type ==
"lsf")
121 lsf2lpc(in_frame, tmp);
122 lpc2ref(tmp, out_frame);
125 EST_error(
"Cannot convert coefficient type %s to reflection coefs\n",
126 (
const char *) in_type);
134 if (in_type ==
"lpc")
135 lpc2cep(in_frame, out_frame);
136 else if (in_type ==
"sig")
139 sig2lpc(in_frame, tmp);
140 lpc2cep(tmp, out_frame);
142 else if (in_type ==
"lsf")
145 lsf2lpc(in_frame, tmp);
146 lpc2cep(tmp, out_frame);
148 else if (in_type ==
"ref")
151 ref2lpc(in_frame, tmp);
152 lpc2cep(tmp, out_frame);
155 EST_error(
"Cannot convert coefficient type %s to cepstrum coefs\n",
156 (
const char *) in_type);
185 if (in_type ==
"lpc")
186 lpc2lsf(in_frame, out_frame);
187 else if (in_type ==
"sig")
190 sig2lpc(in_frame, tmp);
191 lpc2lsf(tmp, out_frame);
193 else if (in_type ==
"ref")
196 ref2lpc(in_frame, tmp);
197 lpc2lsf(tmp, out_frame);
200 EST_error(
"Cannot convert coefficient type %s to reflection coefs\n",
201 (
const char *) in_type);
207 if (out_type ==
"lpc")
208 convert2lpc(in_frame, in_type, out_frame);
209 else if (out_type ==
"lsf")
210 convert2lsf(in_frame, in_type, out_frame);
211 else if (out_type ==
"ref")
212 convert2ref(in_frame, in_type, out_frame);
213 else if (out_type ==
"cep")
214 convert2cep(in_frame, in_type, out_frame);
215 else if (out_type ==
"area")
216 convert2area(in_frame, in_type, out_frame);
218 EST_error(
"Cannot convert coefficients to type %s\n",
219 (
const char *) out_type);
248 sig2lpc(sig, acf, ref, lpc);
261 for (
int i = 1; i < ref.
length(); i++)
262 area[i] = (1.0 - ref(i)) / (1.0 + ref(i));
267 int order = ref.
length() -1;
269 for(
int i = 1; i <= order; i++)
271 if (ref(i) > ALMOST1)
272 logarea[i] = log((1.0 - ALMOST1) / (1.0 + ALMOST1));
273 else if (ref(i) < -ALMOST1)
274 logarea[i] = log((1.0 + ALMOST1)/(1.0 - ALMOST1));
276 logarea[i] = log((1.0 - ref(i)) / (1.0 + ref(i)));
282 int order = ref.
length() -1;
284 area[1] = (1.0 - ref(1)) / (1.0 + ref(1));
285 for(
int i = 2; i <= order; i++)
286 area[i] = area[i - 1] * (1.0 - ref(i)) / (1.0 + ref(i));
293 int order = lpc.
length() - 1;
295 for (n = 1; n <= order && n <= cep.
length(); n++)
298 for (k = 1; k < n; k++)
299 sum += k * cep(k-1) * lpc(n - k);
300 cep[n-1] = lpc(n) + sum / n;
304 for(n = order + 1; n <= cep.
length(); n++)
307 for (k = n - (order - 1); k < n; k++)
308 sum += k * cep(k-1) * lpc(n - k);
314 for (n = 0; n < cep.
length(); n++)
317 if (isnanf(cep[n]) ) cep[n] = 0.0;
319 if (cep[n] > MAX_ABS_CEPS){
320 cerr <<
"WARNING : cepstral coeff " << n <<
" was " <<
322 cerr <<
"lpc coeff " << n <<
" = " << lpc(n + 1) << endl;
324 cep[n] = MAX_ABS_CEPS;
326 if (cep[n] < -MAX_ABS_CEPS) {
327 cerr <<
"WARNING : cepstral coeff " << n <<
" was " <<
329 cep[n] = -MAX_ABS_CEPS;
339 EST_error(
"lpc2ref Code unfinished\n");
351 int order = lpc.
length() - 1;
355 float *vn =
new float[order];
358 ref[i] = lpc(i+lpc_offset);
359 ai = lpc(i+lpc_offset);
364 ref[j] = (lpc(j+lpc_offset)+((ai*lpc(i-j+lpc_offset))))/f;
368 vo =
new float[order];
369 for (i = 0; i < order; ++i)
378 vn[j] = (vo[j]+((ai*vo[i-j])))/f;
395 int order = ref.
length() - 1;
399 for (n=0; n < order; n++)
402 for (k=0; 2 * (k+1) <= n + 1; k++)
406 lpc[k] = a-b * lpc[n];
407 lpc[n-(k+1)] = b-a * lpc[n];
422 EST_error(
"LSF Code unfinished\n");
429 EST_error(
"LSF Code unfinished\n");
438 int order = lpc.
length() -1;
444 EST_error(
"sig2lpc: acf, ref are not of lpc's order");
449 for (i = 0; i <= order; i++)
452 for(j = 0; j < sig.
length() - i; j++)
461 for (i = 1; i <= order; i++)
464 for(j = 1; j < i; j++)
471 if (absval(ci) < 1.000000)
474 for (j = 1; j < i; j++)
477 for( j = 1; j < i; j++)
480 e = (1 - ci * ci) * e;
485 if (stableorder != order)
488 "warning:levinson instability, order restricted to %d\n",
490 for (; i <= order; i++)
501 for (
int i = 0; i < frame.
length(); i++)
502 power += pow(frame(i), float(2.0));
507 void sig2rms(
EST_FVector &frame,
float &rms_energy)
509 sig2pow(frame, rms_energy);
510 rms_energy = sqrt(rms_energy);
525 for (i = 0; i <= order; i++)
528 for (j = 0; j < wsize - i; j++)
529 sum += adc[j] * adc[j + i];
535 for(i = 1; i <= order; i++) {
537 for(j = 1; j < i; j++)
538 ci += lpc[j] * acf[i-j];
539 ref[i] = ci = (acf[i] - ci) / e;
541 if (absval(ci) < 1.000000) {
543 for(j = 1; j < i; j++)
544 tmp[j] = lpc[j] - ci * lpc[i-j];
545 for(j = 1; j < i; j++)
547 e = (1 - ci * ci) * e;
552 if (stableorder != order) {
554 "warning:levinson instability, order restricted to %d\n",
566 const float sample_rate,
567 const bool use_power_rather_than_energy,
573 float Hz_per_fft_coeff;
579 float mel_high=Hz2Mel(sample_rate / 2);
583 sig2fft(sig,fft_frame,use_power_rather_than_energy);
588 Hz_per_fft_coeff = 0.5 * sample_rate / fft_frame.
length();
590 fbank_order = fbank_frame.
length();
594 EST_FVector mel_fbank_centre_frequencies(fbank_order+2);
596 mel_fbank_centre_frequencies[0]=mel_low;
598 for(i=1;i<=fbank_order;i++)
599 mel_fbank_centre_frequencies[i] = mel_low +
600 (
float)(i) * (mel_high - mel_low) / (fbank_order+1);
602 mel_fbank_centre_frequencies[fbank_order+1]=mel_high;
608 mel_fbank_centre_frequencies);
611 for(i=0;i<fbank_frame.
length();i++)
612 fbank_frame[i] = safe_log(fbank_frame[i]);
618 const bool use_power_rather_than_energy)
621 int i,half_fft_order;
623 float window_size = sig.
length();
624 int fft_order = fft_vec.
length();
628 while (window_size > fft_order)
634 fft_vec.
resize(fft_order);
637 (void)fastFFT(fft_vec);
640 half_fft_order = fft_order/2;
642 for(i=0;i<half_fft_order;i++)
645 imag = fft_vec(i*2 + 1);
647 fft_vec[i] = real * real + imag * imag;
649 if(!use_power_rather_than_energy)
650 fft_vec[i] = sqrt(fft_vec(i));
655 fft_vec.
resize(half_fft_order);
663 const float Hz_per_fft_coeff,
676 float this_mel_centre,this_mel_low,this_mel_high;
681 if(mel_fbank_frequencies.
length() != fbank_vec.
length() + 2)
683 EST_error(
"Filter centre frequencies length (%i) is not equal to fbank order (%i) plus 2\n",mel_fbank_frequencies.
length(),
689 for(i=0;i<fbank_vec.
length();i++)
693 this_mel_low=mel_fbank_frequencies(i);
694 this_mel_centre=mel_fbank_frequencies(i+1);
695 this_mel_high=mel_fbank_frequencies(i+2);
697 make_mel_triangular_filter(this_mel_centre,this_mel_low,this_mel_high,
698 Hz_per_fft_coeff,fft_frame.
length(),
699 fft_index_start,filter);
703 for(k=0;k<filter.
length();k++)
704 fbank_vec[i] += fft_frame(fft_index_start + k) * filter(k);
712 const float liftering_parameter,
713 const bool include_c0)
718 int i,j,actual_mfcc_index;
719 float pi_i_over_N,cos_xform_order,const_factor;
720 float PI_over_liftering_parameter;
722 if(liftering_parameter != 0.0)
723 PI_over_liftering_parameter = PI / liftering_parameter;
725 PI_over_liftering_parameter = PI;
729 cos_xform_order = include_c0 ? mfcc_vec.
length() : mfcc_vec.
length() + 1;
731 const_factor = sqrt(2 / (
float)(fbank_vec.
length()));
733 for(i=0;i<mfcc_vec.
length();i++)
736 actual_mfcc_index = include_c0 ? i : i+1;
739 PI * (float)(actual_mfcc_index) / (float)(fbank_vec.
length());
741 for(j=0;j<fbank_vec.
length();j++)
743 mfcc_vec[i] += fbank_vec(j) * cos(pi_i_over_N * ((
float)j + 0.5));
745 mfcc_vec[i] *= const_factor;
748 mfcc_vec[i] *= 1 + (0.5 * liftering_parameter
749 * sin(PI_over_liftering_parameter * (
float)(actual_mfcc_index)));
755 void make_mel_triangular_filter(
const float this_mel_centre,
756 const float this_mel_low,
757 const float this_mel_high,
758 const float Hz_per_fft_coeff,
759 const int half_fft_order,
760 int &fft_index_start,
767 int i,filter_vector_length,fft_index_stop;
768 float rise_slope,fall_slope,this_mel;
773 rise_slope = 1/(this_mel_centre - this_mel_low);
774 fall_slope = 1/(this_mel_centre - this_mel_high);
782 if(this_mel_low == 0)
785 fft_index_start = irint(0.5 + (Mel2Hz(this_mel_low) / Hz_per_fft_coeff));
788 fft_index_stop = irint((Mel2Hz(this_mel_high) / Hz_per_fft_coeff) - 0.5);
790 if(fft_index_stop > half_fft_order-1)
791 fft_index_stop = half_fft_order-1;
794 filter_vector_length = fft_index_stop - fft_index_start + 1;
795 filter.
resize(filter_vector_length);
797 for(i=0;i<filter_vector_length;i++)
799 this_mel = Hz2Mel( (i + fft_index_start) * Hz_per_fft_coeff );
801 if(this_mel <= this_mel_centre)
803 filter[i] = rise_slope * (this_mel - this_mel_low);
807 filter[i] = 1 + fall_slope * (this_mel - this_mel_centre);
815 float Hz2Mel(
float frequency_in_Hertz)
817 return 1127 * log(1 + frequency_in_Hertz/700.0);
820 float Mel2Hz(
float frequency_in_Mel)
822 return (exp(frequency_in_Mel / 1127) - 1) * 700;