A = 10000/L;
phi = zeros(1,L);
for m=1:L
- phi(m) = m*Wo*0.3*m;
+ phi(m) = m*Wo*50.125;
end
if (randphase == 1)
rand("seed",0);
phase_ = load(phase_name_);
endif
+ snr_name = strcat(samname,"_snr.txt");
+ if (file_in_path(".",snr_name))
+ snr = load(snr_name);
+ endif
+
do
figure(1);
clg;
plot((1:L)*Wo*4000/pi, 20*log10(Amq) - 20*log10(Am), Am_err_label);
endif
- % phase model - determine SNR and error spectrum
+ if (file_in_path(".",snr_name))
+ snr_label = sprintf(";phase SNR %4.2f dB;",snr(f));
+ plot(1,1,snr_label);
+ endif
+
+ % phase model - determine SNR and error spectrum for phase model 1
if (file_in_path(".",phase_name_))
orig = Am.*exp(j*phase(f,1:L));
synth = Am.*exp(j*phase_(f,1:L));
- signal = orig * orig'
- noise = (orig-synth) * (orig-synth)'
- snr = 10*log10(signal/noise);
+ signal = orig * orig';
+ noise = (orig-synth) * (orig-synth)';
+ snr_phase = 10*log10(signal/noise);
- phase_err_label = sprintf(";phase_err SNR %4.2f dB;",snr);
+ phase_err_label = sprintf(";phase_err SNR %4.2f dB;",snr_phase);
plot((1:L)*Wo*4000/pi, 20*log10(orig-synth), phase_err_label);
endif
if (file_in_path(".",phase_name))
figure(3);
- plot((1:L)*Wo*4000/pi, unwrap(phase(f,1:L)), ";phase;");
+ plot((1:L)*Wo*4000/pi, phase(f,1:L), ";phase;");
axis;
if (file_in_path(".",phase_name_))
hold on;
- plot((1:L)*Wo*4000/pi, unwrap(phase_(f,1:L)), ";phase_;");
+ plot((1:L)*Wo*4000/pi, phase_(f,1:L), ";phase_;");
hold off;
endif
figure(2);
endif
+ % autocorrelation function to research voicing est
+
+ %M = length(s);
+ %sw = s .* hanning(M)';
+ %for k=0:159
+ % R(k+1) = sw(1:320-k) * sw(1+k:320)';
+ %endfor
+ %figure(4);
+ %R_label = sprintf(";R(k) %3.2f;",max(R(20:159))/R(1));
+ %plot(R/R(1),R_label);
+ %grid
+
% interactive menu
printf("\rframe: %d menu: n-next b-back p-png q-quit ", f);
static FILE *fe = NULL;
static FILE *fsq = NULL;
static FILE *fdec = NULL;
+static FILE *fsnr = NULL;
static char prefix[MAX_STR];
fclose(fsq);
if (fdec != NULL)
fclose(fdec);
+ if (fsnr != NULL)
+ fclose(fsnr);
}
void dump_Sn(float Sn[]) {
fprintf(fphase_,"\n");
}
+void dump_snr(float snr) {
+ char s[MAX_STR];
+
+ if (!dumpon) return;
+
+ if (fphase_ == NULL) {
+ sprintf(s,"%s_snr.txt", prefix);
+ fsnr = fopen(s, "wt");
+ assert(fsnr != NULL);
+ }
+
+ fprintf(fsnr,"%f\n",snr);
+}
+
void dump_Pw(COMP Pw[]) {
int i;
char s[MAX_STR];
void dump_quantised_model(MODEL *m);
void dump_Pw(COMP Pw[]);
void dump_lsp(float lsp[]);
+
+/* phase modelling */
+
+void dump_snr(float snr);
void dump_phase(float phase[]);
void dump_phase_(float phase[]);
\*---------------------------------------------------------------------------*/
float phase_model_first_order(
- float aks[], /* LPC coeffs for this frame */
- COMP H[], /* LPC filter freq doamin samples */
- int *i_min, /* pulse position for min error */
- COMP *minAm /* complex gain for min error */
+ float aks[], /* LPC coeffs for this frame */
+ COMP H[], /* LPC filter freq doamin samples */
+ float *n_min, /* pulse position for min error */
+ COMP *minAm /* complex gain for min error */
)
{
float G; /* LPC gain */
COMP Em; /* error for m-th band */
float den; /* energy of synthesised */
float snr; /* snr of each excitation source */
+ int Lmax;
+ float n;
+
+ Lmax = model.L;
/* Construct target vector */
sig = 0.0;
- for(m=1; m<=model.L; m++) {
+ for(m=1; m<=Lmax; m++) {
A[m].real = model.A[m]*cos(model.phi[m]);
A[m].imag = model.A[m]*sin(model.phi[m]);
sig += model.A[m]*model.A[m];
/* Sample LPC model at harmonics */
+ #ifdef NO_LPC_PHASE
+ /* useful for testing with Sn[] an impulse train */
+ for(i=1; i<=PHASE_LPC_ORD; i++)
+ aks[i] = 0;
+ #endif
G = 1.0;
aks_to_H(&model,aks,G,H,PHASE_LPC_ORD);
Emin = 1E32;
P = floor(TWO_PI/model.Wo + 0.5);
- for(i=0; i<P; i++) {
+ for(n=0; n<P; n+=0.25) {
/* determine complex gain */
Am.real = 0.0;
Am.imag = 0.0;
den = 0.0;
- for(m=1; m<=model.L; m++) {
- Ex[m].real = cos(model.Wo*m*i);
- Ex[m].imag = sin(-model.Wo*m*i);
+ for(m=1; m<=Lmax; m++) {
+ Ex[m].real = cos(model.Wo*m*n);
+ Ex[m].imag = sin(-model.Wo*m*n);
A_[m].real = H[m].real*Ex[m].real - H[m].imag*Ex[m].imag;
A_[m].imag = H[m].imag*Ex[m].real + H[m].real*Ex[m].imag;
Am.real += A[m].real*A_[m].real + A[m].imag*A_[m].imag;
/* determine error */
E = 0.0;
- for(m=1; m<=model.L; m++) {
+ for(m=1; m<=Lmax; m++) {
+ float new_phi;
Em.real = A_[m].real*Am.real - A_[m].imag*Am.imag;
Em.imag = A_[m].imag*Am.real + A_[m].real*Am.imag;
- E += pow(A[m].real-Em.real,2.0) + pow(A[m].imag-Em.imag,2.0);
+ new_phi = atan2(Em.imag, Em.real+1E-12);
+ E += pow(model.A[m]*(cos(model.phi[m])-cos(new_phi)),2.0);
+ E += pow(model.A[m]*(sin(model.phi[m])-sin(new_phi)),2.0);
}
if (E < Emin) {
Emin = E;
- *i_min = i;
+ *n_min = n;
minAm->real = Am.real;
minAm->imag = Am.imag;
}
void phase_synth_first_order(
float snr, /* SNR from first order model */
COMP H[], /* LPC spectra samples */
- int i_min, /* best pulse position */
+ float n_min, /* best pulse position */
COMP minAm /* best complex gain */
)
{
/* generate excitation */
if (m <= Lrand) {
- Ex[m].real = cos(model.Wo*m*i_min);
- Ex[m].imag = sin(-model.Wo*m*i_min);
+ Ex[m].real = cos(model.Wo*m*n_min);
+ Ex[m].imag = sin(-model.Wo*m*n_min);
}
else {
float phi = TWO_PI*(float)rand()/RAND_MAX;
#define PHASE_LPC_ORD 10
-float phase_model_first_order(float aks[], COMP H[], int *i_min, COMP *min_Am);
+float phase_model_first_order(float aks[], COMP H[], float *n_min, COMP *min_Am);
void phase_synth_zero_order(float snr, COMP H[], float *prev_Wo, float *ex_phase);
-void phase_synth_first_order(float snr, COMP H[], int i_min, COMP min_Am);
+void phase_synth_first_order(float snr, COMP H[], float n_min, COMP min_Am);
#endif
float Wn[AW_ENC]; /* windowed speech samples */
float Rk[PHASE_LPC_ORD+1]; /* autocorrelation coeffs */
COMP H[MAX_AMP]; /* LPC freq domain samples */
- int i_min;
+ float n_min;
COMP min_Am;
dump_phase(&model.phi[0]);
else
assert(order == PHASE_LPC_ORD);
- snr = phase_model_first_order(ak, H, &i_min, &min_Am);
- snr = 5;
- if (phase_model == 0)
+ snr = phase_model_first_order(ak, H, &n_min, &min_Am);
+ dump_snr(snr);
+ if (phase_model == 0) {
phase_synth_zero_order(snr, H, &prev_Wo, &ex_phase);
- if (phase_model == 1)
- phase_synth_first_order(snr, H, i_min, min_Am);
-
- dump_phase_(&model.phi[0]);
+ }
+
+ if (phase_model == 1) {
+ phase_synth_first_order(snr, H, n_min, min_Am);
+ dump_phase_(&model.phi[0]);
+ }
}
/* Synthesise speech */