49 #define ALMOST1 0.99999 50 #define MAX_ABS_CEPS 4.0 52 float Hz2Mel(
float frequency_in_Hertz);
53 float Mel2Hz(
float frequency_in_Mel);
55 float lpredict(
float *adc,
int wsize,
56 float *acf,
float *ref,
float *lpc,
58 float ogi_lpc(
float *adc,
int wsize,
int order,
59 float *acf,
float *ref,
float *lpc);
74 else if (in_type ==
"lsf")
76 else if (in_type ==
"ref")
79 EST_error(
"Cannot convert coefficient type %s to lpc\n",
80 (
const char *) in_type);
90 else if (in_type ==
"sig")
96 else if (in_type ==
"lsf")
103 EST_error(
"Cannot convert coefficient type %s to reflection coefs\n",
104 (
const char *) in_type);
112 if (in_type ==
"lpc")
114 else if (in_type ==
"sig")
120 else if (in_type ==
"lsf")
127 EST_error(
"Cannot convert coefficient type %s to reflection coefs\n",
128 (
const char *) in_type);
136 if (in_type ==
"lpc")
138 else if (in_type ==
"sig")
144 else if (in_type ==
"lsf")
150 else if (in_type ==
"ref")
157 EST_error(
"Cannot convert coefficient type %s to cepstrum coefs\n",
158 (
const char *) in_type);
187 if (in_type ==
"lpc")
189 else if (in_type ==
"sig")
195 else if (in_type ==
"ref")
202 EST_error(
"Cannot convert coefficient type %s to reflection coefs\n",
203 (
const char *) in_type);
209 if (out_type ==
"lpc")
211 else if (out_type ==
"lsf")
213 else if (out_type ==
"ref")
215 else if (out_type ==
"cep")
217 else if (out_type ==
"area")
220 EST_error(
"Cannot convert coefficients to type %s\n",
221 (
const char *) out_type);
263 for (
int i = 1; i < ref.
length(); i++)
264 area[i] = (1.0 - ref(i)) / (1.0 + ref(i));
269 int order = ref.
length() -1;
271 for(
int i = 1; i <= order; i++)
278 logarea[i] = log((1.0 - ref(i)) / (1.0 + ref(i)));
284 int order = ref.
length() -1;
286 area[1] = (1.0 - ref(1)) / (1.0 + ref(1));
287 for(
int i = 2; i <= order; i++)
288 area[i] = area[i - 1] * (1.0 - ref(i)) / (1.0 + ref(i));
295 int order = lpc.
length() - 1;
297 for (n = 1; n <= order && n <= cep.
length(); n++)
300 for (k = 1; k < n; k++)
301 sum += k * cep(k-1) * lpc(n - k);
302 cep[n-1] = lpc(n) + sum / n;
306 for(n = order + 1; n <= cep.
length(); n++)
309 for (k = n - (order - 1); k < n; k++)
310 sum += k * cep(k-1) * lpc(n - k);
316 for (n = 0; n < cep.
length(); n++)
319 if (
isnanf(cep[n]) ) cep[n] = 0.0;
322 cerr <<
"WARNING : cepstral coeff " << n <<
" was " <<
324 cerr <<
"lpc coeff " << n <<
" = " << lpc(n + 1) << endl;
329 cerr <<
"WARNING : cepstral coeff " << n <<
" was " <<
346 x =
new float[order+1];
347 xp =
new float[order+1];
348 for (i=0;i<lpc.
length();i++)
350 for (p=order-1;p>0;p--) {
351 memcpy(xp,x,(order+1)*
sizeof(
float));
355 x[m+1] = (xp[m+1]+ref[p]*xp[p-m])/f;
369 int order = ref.
length() - 1;
373 for (n=0; n < order; n++)
376 for (k=0; 2 * (k+1) <= n + 1; k++)
380 lpc[k] = a-b * lpc[n];
381 lpc[n-(k+1)] = b-a * lpc[n];
412 int order = lpc.
length() -1;
418 EST_error(
"sig2lpc: acf, ref are not of lpc's order");
423 for (i = 0; i <= order; i++)
426 for(j = 0; j < sig.
length() - i; j++)
435 for (i = 1; i <= order; i++)
438 for(j = 1; j < i; j++)
445 if (absval(ci) < 1.000000)
448 for (j = 1; j < i; j++)
451 for( j = 1; j < i; j++)
454 e = (1 - ci * ci) * e;
459 if (stableorder != order)
462 "warning:levinson instability, order restricted to %d\n",
464 for (; i <= order; i++)
475 for (
int i = 0; i < frame.
length(); i++)
476 power += pow(frame(i), float(2.0));
484 rms_energy = sqrt(rms_energy);
499 for (i = 0; i <= order; i++)
502 for (j = 0; j < wsize - i; j++)
503 sum += adc[j] * adc[j + i];
509 for(i = 1; i <= order; i++) {
511 for(j = 1; j < i; j++)
512 ci += lpc[j] * acf[i-j];
513 ref[i] = ci = (acf[i] - ci) / e;
515 if (absval(ci) < 1.000000) {
517 for(j = 1; j < i; j++)
518 tmp[j] = lpc[j] - ci * lpc[i-j];
519 for(j = 1; j < i; j++)
521 e = (1 - ci * ci) * e;
526 if (stableorder != order) {
528 "warning:levinson instability, order restricted to %d\n",
540 const float sample_rate,
541 const bool use_power_rather_than_energy,
547 float Hz_per_fft_coeff;
553 float mel_high=
Hz2Mel(sample_rate / 2);
557 sig2fft(sig,fft_frame,use_power_rather_than_energy);
562 Hz_per_fft_coeff = 0.5 * sample_rate / fft_frame.
length();
564 fbank_order = fbank_frame.
length();
568 EST_FVector mel_fbank_centre_frequencies(fbank_order+2);
570 mel_fbank_centre_frequencies[0]=mel_low;
572 for(i=1;i<=fbank_order;i++)
573 mel_fbank_centre_frequencies[i] = mel_low +
574 (
float)(i) * (mel_high - mel_low) / (fbank_order+1);
576 mel_fbank_centre_frequencies[fbank_order+1]=mel_high;
582 mel_fbank_centre_frequencies);
585 for(i=0;i<fbank_frame.
length();i++)
586 fbank_frame[i] =
safe_log(fbank_frame[i]);
592 const bool use_power_rather_than_energy)
595 int i,half_fft_order;
597 float window_size = sig.
length();
602 while (window_size > fft_order)
608 fft_vec.
resize(fft_order);
614 half_fft_order = fft_order/2;
616 for(i=0;i<half_fft_order;i++)
619 imag = fft_vec(i*2 + 1);
621 fft_vec[i] = real * real + imag * imag;
623 if(!use_power_rather_than_energy)
624 fft_vec[i] = sqrt(fft_vec(i));
629 fft_vec.
resize(half_fft_order);
637 const float Hz_per_fft_coeff,
650 float this_mel_centre,this_mel_low,this_mel_high;
655 if(mel_fbank_frequencies.
length() != fbank_vec.
length() + 2)
657 EST_error(
"Filter centre frequencies length (%i) is not equal to fbank order (%i) plus 2\n",mel_fbank_frequencies.
length(),
663 for(i=0;i<fbank_vec.
length();i++)
667 this_mel_low=mel_fbank_frequencies(i);
668 this_mel_centre=mel_fbank_frequencies(i+1);
669 this_mel_high=mel_fbank_frequencies(i+2);
672 Hz_per_fft_coeff,fft_frame.
length(),
673 fft_index_start,filter);
677 for(k=0;k<filter.
length();k++)
678 fbank_vec[i] += fft_frame(fft_index_start + k) * filter(k);
686 const float liftering_parameter,
687 const bool include_c0)
692 int i,j,actual_mfcc_index;
693 float pi_i_over_N,const_factor;
695 float PI_over_liftering_parameter;
697 if(liftering_parameter != 0.0)
698 PI_over_liftering_parameter =
PI / liftering_parameter;
700 PI_over_liftering_parameter =
PI;
706 const_factor = sqrt(2 / (
float)(fbank_vec.
length()));
708 for(i=0;i<mfcc_vec.
length();i++)
711 actual_mfcc_index = include_c0 ? i : i+1;
714 PI * (float)(actual_mfcc_index) / (float)(fbank_vec.
length());
716 for(j=0;j<fbank_vec.
length();j++)
718 mfcc_vec[i] += fbank_vec(j) * cos(pi_i_over_N * ((
float)j + 0.5));
720 mfcc_vec[i] *= const_factor;
723 mfcc_vec[i] *= 1 + (0.5 * liftering_parameter
724 * sin(PI_over_liftering_parameter * (
float)(actual_mfcc_index)));
731 const float this_mel_low,
732 const float this_mel_high,
733 const float Hz_per_fft_coeff,
734 const int half_fft_order,
735 int &fft_index_start,
742 int i,filter_vector_length,fft_index_stop;
743 float rise_slope,fall_slope,this_mel;
748 rise_slope = 1/(this_mel_centre - this_mel_low);
749 fall_slope = 1/(this_mel_centre - this_mel_high);
757 if(this_mel_low == 0)
760 fft_index_start = irint(0.5 + (
Mel2Hz(this_mel_low) / Hz_per_fft_coeff));
763 fft_index_stop = irint((
Mel2Hz(this_mel_high) / Hz_per_fft_coeff) - 0.5);
765 if(fft_index_stop > half_fft_order-1)
766 fft_index_stop = half_fft_order-1;
769 filter_vector_length = fft_index_stop - fft_index_start + 1;
770 filter.
resize(filter_vector_length);
772 for(i=0;i<filter_vector_length;i++)
774 this_mel =
Hz2Mel( (i + fft_index_start) * Hz_per_fft_coeff );
776 if(this_mel <= this_mel_centre)
778 filter[i] = rise_slope * (this_mel - this_mel_low);
782 filter[i] = 1 + fall_slope * (this_mel - this_mel_centre);
792 return 1127 * log(1 + frequency_in_Hertz/700.0);
797 return (exp(frequency_in_Mel / 1127) - 1) * 700;
void make_mel_triangular_filter(const float this_mel_centre, const float this_mel_low, const float this_mel_high, const float Hz_per_fft_coeff, const int half_fft_order, int &fft_index_start, EST_FVector &filter)
void fft2fbank(const EST_FVector &fft_frame, EST_FVector &fbank_vec, const float Hz_per_fft_coeff, const EST_FVector &mel_fbank_frequencies)
void frame_convert(const EST_FVector &in_frame, const EST_String &in_type, EST_FVector &out_frame, const EST_String &out_type)
double safe_log(const double x)
void ref2area(const EST_FVector &ref, EST_FVector &area)
void sig2ref(const EST_FVector &sig, EST_FVector &ref)
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
void convert2cep(const EST_FVector &in_frame, const EST_String &in_type, EST_FVector &out_frame)
void convert2lsf(const EST_FVector &in_frame, const EST_String &in_type, EST_FVector &out_frame)
INLINE const T & a_no_check(ssize_t n) const
read-only const access operator: without bounds checking
void sig2pow(EST_FVector &frame, float &power)
void power(EST_Wave &sig, EST_Track &a, float factor)
float lpredict(float *adc, int wsize, float *acf, float *ref, float *lpc, int order)
float lpredict2(EST_FVector &adc, int wsize, EST_FVector &acf, float *ref, float *lpc, int order)
void lsf2lpc(const EST_FVector &lpc, EST_FVector &lsf)
float Hz2Mel(float frequency_in_Hertz)
void fbank2melcep(const EST_FVector &fbank_vec, EST_FVector &mfcc_vec, const float liftering_parameter, const bool include_c0)
INLINE ssize_t length() const
number of items in vector.
void sig2rms(EST_FVector &frame, float &rms_energy)
void sig2fft(const EST_FVector &sig, EST_FVector &fft_vec, const bool use_power_rather_than_energy)
void ref2logarea(const EST_FVector &ref, EST_FVector &logarea)
void ref2truearea(const EST_FVector &ref, EST_FVector &area)
void convert2area(const EST_FVector &in_frame, const EST_String &in_type, EST_FVector &out_frame)
float ogi_lpc(float *adc, int wsize, int order, float *acf, float *ref, float *lpc)
void ref2lpc(const EST_FVector &ref, EST_FVector &lpc)
void convert2lpc(const EST_FVector &in_frame, const EST_String &in_type, EST_FVector &out_frame)
void lpc2ref(const EST_FVector &lpc, EST_FVector &ref)
void sig2fbank(const EST_FVector &sig, EST_FVector &fbank_frame, const float sample_rate, const bool use_power_rather_than_energy, const bool take_log)
void convert2ref(const EST_FVector &in_frame, const EST_String &in_type, EST_FVector &out_frame)
void lpc2lsf(const EST_FVector &lpc, EST_FVector &lsf)
float sum(const EST_FMatrix &a)
sum of elements
void lpc2cep(const EST_FVector &lpc, EST_FVector &cep)
float Mel2Hz(float frequency_in_Mel)
void resize(int n, int set=1)
resize vector
void sig2lpc(const EST_FVector &sig, EST_FVector &acf, EST_FVector &ref, EST_FVector &xlpc)