diff --git a/Level_3/AACoder3.m b/Level_3/AACoder3.m index 2c191e9..74fa922 100644 --- a/Level_3/AACoder3.m +++ b/Level_3/AACoder3.m @@ -71,15 +71,26 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded) [frameF(:, 1), TNScoeffsL] = TNS(frameF(:, 1), frameTypes{i+1}); [frameF(:, 2), TNScoeffsR] = TNS(frameF(:, 2), frameTypes{i+1}); - if i < 2 - % TODO: what happens on the first two frames? - SL = frameF(:, 1); - SR = frameF(:, 2); - GL = 0; - GR = 0; - sfcL = 0; - sfcR = 0; - continue; + if i == 0 + SMRL = psycho(... + originalAudioData(currFrameStart:currFrameStop, 1), ... + frameTypes{i+1}, zeros(2048, 1), zeros(2048, 1)); + SMRR = psycho(... + originalAudioData(currFrameStart:currFrameStop, 2), ... + frameTypes{i+1}, zeros(2048, 1), zeros(2048, 1)); + elseif i == 1 + 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 prev1FrameStart = (i - 1) * 1024 + 1; prev1FrameStop = prev1FrameStart + 2047; @@ -95,10 +106,11 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded) frameTypes{i+1}, ... originalAudioData(prev1FrameStart:prev1FrameStop, 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 + [SL, sfcL, GL] = AACquantizer(frameF(:, 1), frameTypes{i+1}, SMRL); + [SR, sfcR, GR] = AACquantizer(frameF(:, 2), frameTypes{i+1}, SMRR); + TL = 0; TR = 0; diff --git a/Level_3/AACquantizer.m b/Level_3/AACquantizer.m index 0d881e3..8d5bcd1 100644 --- a/Level_3/AACquantizer.m +++ b/Level_3/AACquantizer.m @@ -35,6 +35,7 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR) T(LONG_WINDOW_NUMBER_OF_BANDS, 1) = 0; quantCoeff(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 frameWlow = TNSTables.B219a(band, 2) + 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 % quantization - quantCoeff(band) = 16 * log2(max(frameF) ^ (3 / 4) / 8191) / 3; + quantCoeff(band) = initialQuantCoeff; S(frameWlow:frameWhigh, 1) = sign(subFrameF) .* floor(( ... abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... .^ (3 / 4) + 0.4054); @@ -61,7 +62,6 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR) while quantErr < T(band) && (band < 2 || ... (quantCoeff(band) - quantCoeff(band - 1) + 1) <= 60) quantCoeff(band) = quantCoeff(band) + 1; - S(frameWlow:frameWhigh) = sign(subFrameF) .* round(( ... abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... .^ (3 / 4) + 0.4054); @@ -72,6 +72,13 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR) quantErr = sumsqr(subFrameF - frameFDequant); 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 sfc(band) = quantCoeff(band); @@ -89,6 +96,7 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR) currFrameStart = (subFrameIndex - 1) * 128 + 1; currFrameStop = currFrameStart + 127; subFrame = frameF(currFrameStart:currFrameStop); + initialQuantCoeff = 16 * log2(max(subFrame) ^ (3 / 4) / 8191) / 3; for band = 1:SHORT_WINDOW_NUMBER_OF_BANDS frameWlow = TNSTables.B219b(band, 2); frameWhigh = TNSTables.B219b(band, 3); @@ -98,7 +106,7 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR) % Calculates an initial quantization coefficient and attempts % quantization - quantCoeff(band) = 16 * log2(max(subFrame) ^ (3 / 4) / 8191) / 3; + quantCoeff(band) = initialQuantCoeff; S(currFrameStart + frameWlow:currFrameStart + frameWhigh, 1) = sign(subFrameF) .* floor(( ... abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... .^ (3 / 4) + 0.4054); @@ -124,10 +132,17 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR) currFrameStart + frameWhigh, 1)) .* abs(S( ... currFrameStart + frameWlow:currFrameStart + frameWhigh, 1) ... ) .^ (4 / 3) .* 2 ^ (quantCoeff(band) / 4); - + quantErr = sumsqr(subFrameF - frameFDequant); 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 sfc(band, subFrameIndex) = quantCoeff(band); else diff --git a/Level_3/decoded3.wav b/Level_3/decoded3.wav index b2e9296..fca30cf 100644 Binary files a/Level_3/decoded3.wav and b/Level_3/decoded3.wav differ diff --git a/Level_3/encoded.mat b/Level_3/encoded.mat index 5936d84..89118ea 100644 Binary files a/Level_3/encoded.mat and b/Level_3/encoded.mat differ