From 216c054897d107ac4aba5af19f089006ce19c310 Mon Sep 17 00:00:00 2001 From: drowe67 Date: Tue, 15 Mar 2016 02:17:47 +0000 Subject: [PATCH] refactored for enc/dec and interpolation git-svn-id: https://svn.code.sf.net/p/freetel/code@2746 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2-dev/octave/newamp.m | 36 +++++++++---------- codec2-dev/octave/newamp_batch.m | 60 +++++++++++++++++++------------- codec2-dev/octave/newamp_fbf.m | 18 +++------- 3 files changed, 56 insertions(+), 58 deletions(-) diff --git a/codec2-dev/octave/newamp.m b/codec2-dev/octave/newamp.m index 307c53f4..597ef007 100644 --- a/codec2-dev/octave/newamp.m +++ b/codec2-dev/octave/newamp.m @@ -137,7 +137,7 @@ endfunction function [decmaskdB masker_freqs_kHz min_error mse_log1 mse_log2] = make_decmask_abys(maskdB, AmdB, Wo, L, mask_sample_freqs_kHz, freq_quant, amp_quant) - Nsamples = 3; + Nsamples = 4; % search range @@ -215,14 +215,9 @@ function [decmaskdB masker_freqs_kHz min_error mse_log1 mse_log2] = make_decmask % then quantise differences - % create table of freq quantiser levels with log spacing - %m = (log10(2)-log10(0.2))/(8-1); c = log10(0.2)-m; - %logf_steps = 10.^(m*(1:8)+c) - logf_steps = [0.4 0.7 1 1 1.3 1.6 2 2.4]; - - for i=2:Nsamples + for i=2:4 targ = masker_freqs_kHz(i) - masker_freqs_kHz(i-1); - [q_freq abits] = quantise(logf_steps, targ); + [q_freq abits] = quantise(0.2:0.2:1.6, targ); bits = [bits abits]; masker_freqs_kHz(i) = masker_freqs_kHz(i-1) + q_freq; end @@ -239,13 +234,13 @@ function [decmaskdB masker_freqs_kHz min_error mse_log1 mse_log2] = make_decmask % Fit straight line f = masker_freqs_kHz*1000; - [gradient intercept] = linreg(f, masker_amps_dB, Nsamples); + [gradient intercept] = linreg(f, masker_amps_dB, 4); % use quantised gradient to take into account quantisation % errors in rest of quantisation gradient_ = quantise(-0.04:0.005:0.04, gradient); %gradient_ = gradient; - %printf("gradient; %f gradient_: %f\n", gradient, gradient_); + printf("gradient; %f gradient_: %f\n", gradient, gradient_); % determine deltas, or errors in straight line fit @@ -259,9 +254,10 @@ function [decmaskdB masker_freqs_kHz min_error mse_log1 mse_log2] = make_decmask clf; plot(f, masker_amps_dB, 'r+', 'markersize', 10, 'linewidth', 2) - fplt = 0:100:3900; + fplt = 0:100:3900 hold on; plot(fplt, fplt*gradient + intercept, 'b') + fplt*gradient + intercept % plot lines for deltas @@ -270,28 +266,28 @@ function [decmaskdB masker_freqs_kHz min_error mse_log1 mse_log2] = make_decmask y2 = masker_amps_dB(i); plot([f(i) f(i)], [y1 y2], 'markersize', 10, 'linewidth', 2) end - axis([0 4000 0 80]) hold off; - end - masker_amps_dB_lin_delta_ = zeros(Nsamples,1); + % quantise the deltas + + masker_amps_dB_lin_delta_ = zeros(4,1); if amp_quant == 1 - for i=1:Nsamples + for i=1:4 masker_amps_dB_lin_delta_(i) = quantise(-21:3:21, masker_amps_dB_lin_delta(i)); - %printf("dlin: %f dlin_: %f\n", masker_amps_dB_lin_delta(i), masker_amps_dB_lin_delta_(i)); + printf("dlin: %f dlin_: %f\n", masker_amps_dB_lin_delta(i), masker_amps_dB_lin_delta_(i)); % masker_amps_dB_lin_delta_(i) = masker_amps_dB_lin_delta(i); end end - masker_amps_dB = f*gradient + masker_amps_dB_lin_delta_ + intercept; + masker_amps_dB = f*gradient_ + masker_amps_dB_lin_delta_ + intercept; %decmaskdB = determine_mask(masker_amps_dB, masker_freqs_kHz, mask_sample_freqs_kHz); decmaskdB = determine_mask(masker_amps_dB, masker_freqs_kHz, mask_sample_freqs_kHz); end if 0 printf("\n"); - for i=1:Nsamples + for i=1:4 printf("freq: %f amp: %f\n", masker_freqs_kHz(i), masker_amps_dB(i)); end end @@ -472,7 +468,7 @@ function maskdB_ = decimate_frame_rate(maskdB, model, decimate, f, frames, mask_ right_fraction = mod(f-1,decimate)/decimate; left_fraction = 1 - right_fraction; - %printf("\nf: %d left_f: %d right_f: %d left_fraction: %f right_fraction: %f \n",f,left_f,right_f,left_fraction,right_fraction) + printf("f: %d left_f: %d right_f: %d left_fraction: %f right_fraction: %f \n",f,left_f,right_f,left_fraction,right_fraction) % determine mask for left and right frames, sampling at Wo for this frame @@ -482,8 +478,8 @@ function maskdB_ = decimate_frame_rate(maskdB, model, decimate, f, frames, mask_ maskdB_ = left_fraction*maskdB_left + right_fraction*maskdB_right; else + printf("\n"); maskdB_ = maskdB; - %printf("\n"); end endfunction diff --git a/codec2-dev/octave/newamp_batch.m b/codec2-dev/octave/newamp_batch.m index a1fca9f0..6c0129ac 100644 --- a/codec2-dev/octave/newamp_batch.m +++ b/codec2-dev/octave/newamp_batch.m @@ -25,8 +25,8 @@ function [non_masked_f_log non_masked_amp_log] = newamp_batch(samname, optional_ postfilter = 1; decimate_in_time = 1; synth_phase = 1; - freq_quant = 1; - amp_quant = 2; + freq_quant = 0; + amp_quant = 0; non_masked_f_log = []; non_masked_m_log = []; non_masked_amp_log = []; @@ -34,6 +34,8 @@ function [non_masked_f_log non_masked_amp_log] = newamp_batch(samname, optional_ model_name = strcat(samname,"_model.txt"); model = load(model_name); [frames nc] = size(model); + model_ = zeros(frames, nc); + nom_masked_m = zeros(frames,max_amp); if nargin == 1 Am_out_name = sprintf("%s_am.out", samname); @@ -52,11 +54,13 @@ function [non_masked_f_log non_masked_amp_log] = newamp_batch(samname, optional_ faw = fopen(Aw_out_name,"wb"); end + % encoder loop ------------------------------------------------------ + for f=1:frames - %printf("%d ", f); - L = min([model(f,2) max_amp-1]); - Wo = model(f,1); - Am = model(f,3:(L+2)); + printf("%d ", f); + model_(f,2) = L = min([model(f,2) max_amp-1]); + model_(f,1) = Wo = model(f,1); + model_(f,3:(L+2)) = Am = model(f,3:(L+2)); AmdB = 20*log10(Am); @@ -70,42 +74,49 @@ function [non_masked_f_log non_masked_amp_log] = newamp_batch(samname, optional_ [decmaskdB masker_freqs_kHz] = make_decmask_abys(maskdB, AmdB, Wo, L, mask_sample_freqs_kHz, freq_quant, amp_quant); non_masked_amp = decmaskdB; non_masked_amp_log = [non_masked_amp_log; non_masked_amp']; - non_masked_m = min(round(masker_freqs_kHz/(Wo*4/pi)),L); - non_masked_m_log = [non_masked_m_log; non_masked_m']; + non_masked_m(f,:) = min(round(masker_freqs_kHz/(Wo*4/pi)),L); + non_masked_m_log = [non_masked_m_log; non_masked_m(f,:)']; non_masked_f_log = [non_masked_f_log; masker_freqs_kHz]; maskdB_ = decmaskdB; else % just approximate decimation in freq by using those mask samples we can hear maskdB_ = maskdB; - non_masked_m = find(AmdB > maskdB); + a_non_masked_m = find(AmdB > maskdB); + non_masked_m(f,1:length(a_non_masked_m)) = a_non_masked_m; end + Am_ = zeros(1,max_amp); + Am_ = 10 .^ (maskdB_(1:L-1)/20); + model_(f,3:(L+1)) = Am_; + end + + + % decoder loop ----------------------------------------------------- + + for f=1:frames + L = min([model_(f,2) max_amp-1]); + Wo = model_(f,1); + Am_ = model_(f,3:(L+2)); + + maskdB_ = 20*log10(Am_); + mask_sample_freqs_kHz = (1:L)*Wo*4/pi; + if decimate_in_time % decimate mask samples in time - maskdB_ = decimate_frame_rate(maskdB_, model, 4, f, frames, mask_sample_freqs_kHz); + maskdB_ = decimate_frame_rate(maskdB_, model_, 4, f, frames, mask_sample_freqs_kHz); end % post filter - bump up samples by 6dB, reduce mask by same level to normalise gain if postfilter maskdB_pf = maskdB_ - 6; - maskdB_pf(non_masked_m) = maskdB_pf(non_masked_m) + 6; + m = max(find(non_masked_m(f,:) > 0)); + a_non_masked_m = non_masked_m(f,1:m); + maskdB_pf(a_non_masked_m) = maskdB_pf(a_non_masked_m) + 6; else maskdB_pf = maskdB_; end - if 0 - % Early work as per blog post part 1 - % Attempt 1 - maskdB_pf = zeros(1,L); - maskdB_pf(non_masked_m) = maskdB(non_masked_m); - % Attempt 2 - %maskdB_pf = maskdB; - % Attempt 3 - %maskdB_pf = maskdB; - %maskdB_pf(non_masked_m) += 6; - end - Am_ = zeros(1,max_amp); Am_(2:L) = 10 .^ (maskdB_pf(1:L-1)/20); % C array doesnt use A[0] fwrite(fam, Am_, "float32"); @@ -113,8 +124,7 @@ function [non_masked_f_log non_masked_amp_log] = newamp_batch(samname, optional_ if synth_phase % synthesis phase spectra from magnitiude spectra using minimum phase techniques fft_enc = 512; - model_ = model; - model_(f,3:(L+1)) = Am_(2:L); + model_(f,3:(L+2)) = 10 .^ (maskdB_pf(1:L)/20); phase = determine_phase(model_, f); assert(length(phase) == fft_enc); Aw = zeros(1, fft_enc*2); diff --git a/codec2-dev/octave/newamp_fbf.m b/codec2-dev/octave/newamp_fbf.m index 5f1e32f9..7a8150e7 100644 --- a/codec2-dev/octave/newamp_fbf.m +++ b/codec2-dev/octave/newamp_fbf.m @@ -23,7 +23,6 @@ function newamp_fbf(samname, f=10) plot_spectrum = 1; freq_quant = 0; amp_quant = 0; - abys = 1; sn_name = strcat(samname,"_sn.txt"); Sn = load(sn_name); @@ -84,12 +83,7 @@ function newamp_fbf(samname, f=10) % decimate in frequency mask_sample_freqs_kHz = (1:L)*Wo*4/pi; - if abys == 1 - [decmaskdB masker_freqs_kHz min_error mse_log1 mse_log2] = make_decmask_abys(maskdB, AmdB, Wo, L, mask_sample_freqs_kHz, freq_quant, amp_quant); - end - if abys == 2 - [decmaskdB masker_freqs_kHz min_error mse_log1 mse_log2] = make_decmask_abys2(maskdB, AmdB, Wo, L, mask_sample_freqs_kHz, freq_quant, amp_quant); - end + [decmaskdB masker_freqs_kHz min_error mse_log1 mse_log2] = make_decmask_abys(maskdB, AmdB, Wo, L, mask_sample_freqs_kHz, freq_quant, amp_quant); figure(2) tonef_kHz = masker_freqs_kHz; @@ -100,7 +94,6 @@ function newamp_fbf(samname, f=10) legend('boxoff'); %plot(mask_sample_freqs_kHz*1000, min_error); hold off; - grid figure(3) clf @@ -145,7 +138,6 @@ function newamp_fbf(samname, f=10) % interactive menu printf("\rframe: %d menu: n-next b-back a-Am ", f); - if freq_quant printf("F"); else @@ -153,11 +145,11 @@ function newamp_fbf(samname, f=10) end printf("-freq_quant "); if amp_quant - printf("M-amp_quant "); + printf("M"); else - printf("m-amp_quant "); + printf("m"); end - printf("q-quit"); + printf("-amp_quant q-quit"); fflush(stdout); k = kbhit(); @@ -169,7 +161,7 @@ function newamp_fbf(samname, f=10) endif if k == 'm' if amp_quant == 0 - amp_quant = 2; + amp_quant = 1; else amp_quant = 0; end -- 2.25.1