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
% 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
% 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
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
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
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
maskdB_ = left_fraction*maskdB_left + right_fraction*maskdB_right;
else
+ printf("\n");
maskdB_ = maskdB;
- %printf("\n");
end
endfunction
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 = [];
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);
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);
[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");
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);
plot_spectrum = 1;
freq_quant = 0;
amp_quant = 0;
- abys = 1;
sn_name = strcat(samname,"_sn.txt");
Sn = load(sn_name);
% 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;
legend('boxoff');
%plot(mask_sample_freqs_kHz*1000, min_error);
hold off;
- grid
figure(3)
clf
% interactive menu
printf("\rframe: %d menu: n-next b-back a-Am ", f);
-
if freq_quant
printf("F");
else
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();
endif
if k == 'm'
if amp_quant == 0
- amp_quant = 2;
+ amp_quant = 1;
else
amp_quant = 0;
end