Browse Source

Fix for the first two frames on level 3

master
Apostolos Fanakis 6 years ago
parent
commit
70a35237fe
  1. 34
      Level_3/AACoder3.m
  2. 25
      Level_3/AACquantizer.m
  3. BIN
      Level_3/decoded3.wav
  4. BIN
      Level_3/encoded.mat

34
Level_3/AACoder3.m

@ -71,15 +71,26 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded)
[frameF(:, 1), TNScoeffsL] = TNS(frameF(:, 1), frameTypes{i+1}); [frameF(:, 1), TNScoeffsL] = TNS(frameF(:, 1), frameTypes{i+1});
[frameF(:, 2), TNScoeffsR] = TNS(frameF(:, 2), frameTypes{i+1}); [frameF(:, 2), TNScoeffsR] = TNS(frameF(:, 2), frameTypes{i+1});
if i < 2 if i == 0
% TODO: what happens on the first two frames? SMRL = psycho(...
SL = frameF(:, 1); originalAudioData(currFrameStart:currFrameStop, 1), ...
SR = frameF(:, 2); frameTypes{i+1}, zeros(2048, 1), zeros(2048, 1));
GL = 0; SMRR = psycho(...
GR = 0; originalAudioData(currFrameStart:currFrameStop, 2), ...
sfcL = 0; frameTypes{i+1}, zeros(2048, 1), zeros(2048, 1));
sfcR = 0; elseif i == 1
continue; prev1FrameStart = (i - 1) * 1024 + 1;
prev1FrameStop = prev1FrameStart + 2047;
SMRL = psycho(...
originalAudioData(currFrameStart:currFrameStop, 1), ...
frameTypes{i+1}, ...
originalAudioData(prev1FrameStart:prev1FrameStop, 1), ...
zeros(2048, 1));
SMRR = psycho(...
originalAudioData(currFrameStart:currFrameStop, 2), ...
frameTypes{i+1}, ...
originalAudioData(prev1FrameStart:prev1FrameStop, 2), ...
zeros(2048, 1));
else else
prev1FrameStart = (i - 1) * 1024 + 1; prev1FrameStart = (i - 1) * 1024 + 1;
prev1FrameStop = prev1FrameStart + 2047; prev1FrameStop = prev1FrameStart + 2047;
@ -95,10 +106,11 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded)
frameTypes{i+1}, ... frameTypes{i+1}, ...
originalAudioData(prev1FrameStart:prev1FrameStop, 2), ... originalAudioData(prev1FrameStart:prev1FrameStop, 2), ...
originalAudioData(prev2FrameStart:prev2FrameStop, 2)); originalAudioData(prev2FrameStart:prev2FrameStop, 2));
[SL, sfcL, GL] = AACquantizer(frameF(:, 1), frameTypes{i+1}, SMRL);
[SR, sfcR, GR] = AACquantizer(frameF(:, 2), frameTypes{i+1}, SMRR);
end end
[SL, sfcL, GL] = AACquantizer(frameF(:, 1), frameTypes{i+1}, SMRL);
[SR, sfcR, GR] = AACquantizer(frameF(:, 2), frameTypes{i+1}, SMRR);
TL = 0; TL = 0;
TR = 0; TR = 0;

25
Level_3/AACquantizer.m

@ -35,6 +35,7 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR)
T(LONG_WINDOW_NUMBER_OF_BANDS, 1) = 0; T(LONG_WINDOW_NUMBER_OF_BANDS, 1) = 0;
quantCoeff(LONG_WINDOW_NUMBER_OF_BANDS, 1) = 0; quantCoeff(LONG_WINDOW_NUMBER_OF_BANDS, 1) = 0;
sfc(LONG_WINDOW_NUMBER_OF_BANDS, 1) = 0; sfc(LONG_WINDOW_NUMBER_OF_BANDS, 1) = 0;
initialQuantCoeff = 16 * log2(max(frameF) ^ (3 / 4) / 8191) / 3;
for band = 1:LONG_WINDOW_NUMBER_OF_BANDS for band = 1:LONG_WINDOW_NUMBER_OF_BANDS
frameWlow = TNSTables.B219a(band, 2) + 1; frameWlow = TNSTables.B219a(band, 2) + 1;
frameWhigh = TNSTables.B219a(band, 3) + 1; frameWhigh = TNSTables.B219a(band, 3) + 1;
@ -44,7 +45,7 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR)
% Calculates an initial quantization coefficient and attempts % Calculates an initial quantization coefficient and attempts
% quantization % quantization
quantCoeff(band) = 16 * log2(max(frameF) ^ (3 / 4) / 8191) / 3; quantCoeff(band) = initialQuantCoeff;
S(frameWlow:frameWhigh, 1) = sign(subFrameF) .* floor(( ... S(frameWlow:frameWhigh, 1) = sign(subFrameF) .* floor(( ...
abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ...
.^ (3 / 4) + 0.4054); .^ (3 / 4) + 0.4054);
@ -61,7 +62,6 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR)
while quantErr < T(band) && (band < 2 || ... while quantErr < T(band) && (band < 2 || ...
(quantCoeff(band) - quantCoeff(band - 1) + 1) <= 60) (quantCoeff(band) - quantCoeff(band - 1) + 1) <= 60)
quantCoeff(band) = quantCoeff(band) + 1; quantCoeff(band) = quantCoeff(band) + 1;
S(frameWlow:frameWhigh) = sign(subFrameF) .* round(( ... S(frameWlow:frameWhigh) = sign(subFrameF) .* round(( ...
abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ...
.^ (3 / 4) + 0.4054); .^ (3 / 4) + 0.4054);
@ -72,6 +72,13 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR)
quantErr = sumsqr(subFrameF - frameFDequant); quantErr = sumsqr(subFrameF - frameFDequant);
end end
if quantCoeff(band) ~= initialQuantCoeff
quantCoeff(band) = quantCoeff(band) - 1;
S(frameWlow:frameWhigh) = sign(subFrameF) .* round(( ...
abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ...
.^ (3 / 4) + 0.4054);
end
if band == 1 if band == 1
sfc(band) = quantCoeff(band); sfc(band) = quantCoeff(band);
@ -89,6 +96,7 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR)
currFrameStart = (subFrameIndex - 1) * 128 + 1; currFrameStart = (subFrameIndex - 1) * 128 + 1;
currFrameStop = currFrameStart + 127; currFrameStop = currFrameStart + 127;
subFrame = frameF(currFrameStart:currFrameStop); subFrame = frameF(currFrameStart:currFrameStop);
initialQuantCoeff = 16 * log2(max(subFrame) ^ (3 / 4) / 8191) / 3;
for band = 1:SHORT_WINDOW_NUMBER_OF_BANDS for band = 1:SHORT_WINDOW_NUMBER_OF_BANDS
frameWlow = TNSTables.B219b(band, 2); frameWlow = TNSTables.B219b(band, 2);
frameWhigh = TNSTables.B219b(band, 3); frameWhigh = TNSTables.B219b(band, 3);
@ -98,7 +106,7 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR)
% Calculates an initial quantization coefficient and attempts % Calculates an initial quantization coefficient and attempts
% quantization % quantization
quantCoeff(band) = 16 * log2(max(subFrame) ^ (3 / 4) / 8191) / 3; quantCoeff(band) = initialQuantCoeff;
S(currFrameStart + frameWlow:currFrameStart + frameWhigh, 1) = sign(subFrameF) .* floor(( ... S(currFrameStart + frameWlow:currFrameStart + frameWhigh, 1) = sign(subFrameF) .* floor(( ...
abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ...
.^ (3 / 4) + 0.4054); .^ (3 / 4) + 0.4054);
@ -124,10 +132,17 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR)
currFrameStart + frameWhigh, 1)) .* abs(S( ... currFrameStart + frameWhigh, 1)) .* abs(S( ...
currFrameStart + frameWlow:currFrameStart + frameWhigh, 1) ... currFrameStart + frameWlow:currFrameStart + frameWhigh, 1) ...
) .^ (4 / 3) .* 2 ^ (quantCoeff(band) / 4); ) .^ (4 / 3) .* 2 ^ (quantCoeff(band) / 4);
quantErr = sumsqr(subFrameF - frameFDequant); quantErr = sumsqr(subFrameF - frameFDequant);
end end
if quantCoeff(band) ~= initialQuantCoeff
quantCoeff(band) = quantCoeff(band) - 1;
S(currFrameStart + frameWlow:currFrameStart + frameWhigh) = sign(subFrameF) .* round(( ...
abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ...
.^ (3 / 4) + 0.4054);
end
if band == 1 if band == 1
sfc(band, subFrameIndex) = quantCoeff(band); sfc(band, subFrameIndex) = quantCoeff(band);
else else

BIN
Level_3/decoded3.wav

Binary file not shown.

BIN
Level_3/encoded.mat

Binary file not shown.
Loading…
Cancel
Save