diff --git a/Level_1/AACoder1.m b/Level_1/AACoder1.m index ddb6408..774da38 100644 --- a/Level_1/AACoder1.m +++ b/Level_1/AACoder1.m @@ -18,12 +18,20 @@ function AACSeq1 = AACoder1(fNameIn) [originalAudioData, ~] = audioread(fNameIn); % Splits the audio in frames and determines the type of each frame - frameTypes{fix((length(originalAudioData) - 1025) / 1024), 1} = 'OLS'; + frameTypes{ceil(length(originalAudioData) / 1024), 1} = 'OLS'; frameTypes{1} = 'OLS'; for i = 1:length(frameTypes) - 2 nextFrameStart = (i + 1) * 1024 + 1; nextFrameStop = nextFrameStart + 2047; - frameTypes{i+1} = SSC(1, originalAudioData(nextFrameStart:nextFrameStop, :), frameTypes{i}); + if nextFrameStop > length(originalAudioData) + tmpZeroPadded = originalAudioData(nextFrameStart:length(originalAudioData), :); + tmpZeroPadded = padarray(tmpZeroPadded, [(2048 - length(tmpZeroPadded)) 0], 'post'); + frameTypes{i+1} = SSC(1, tmpZeroPadded, frameTypes{i}); + + clearvars tmpZeroPadded + else + frameTypes{i+1} = SSC(1, originalAudioData(nextFrameStart:nextFrameStop, :), frameTypes{i}); + end end % Assigns a type to the last frame @@ -40,7 +48,16 @@ function AACSeq1 = AACoder1(fNameIn) for i = 0:length(frameTypes) - 1 currFrameStart = i * 1024 + 1; currFrameStop = currFrameStart + 2047; - frameF = filterbank(originalAudioData(currFrameStart:currFrameStop, :), frameTypes{i+1}, WINDOW_TYPE); + + if currFrameStop > length(originalAudioData) + tmpZeroPadded = originalAudioData(nextFrameStart:length(originalAudioData), :); + tmpZeroPadded = padarray(tmpZeroPadded, [(2048 - length(tmpZeroPadded)) 0], 'post'); + frameF = filterbank(tmpZeroPadded, frameTypes{i+1}, WINDOW_TYPE); + + clearvars tmpZeroPadded + else + frameF = filterbank(originalAudioData(currFrameStart:currFrameStop, :), frameTypes{i+1}, WINDOW_TYPE); + end AACSeq1(i + 1).frameType = frameTypes(i + 1); AACSeq1(i + 1).winType = WINDOW_TYPE; diff --git a/Level_1/decoded2.wav b/Level_1/decoded1.wav similarity index 99% rename from Level_1/decoded2.wav rename to Level_1/decoded1.wav index e0c852d..b8e866f 100644 Binary files a/Level_1/decoded2.wav and b/Level_1/decoded1.wav differ diff --git a/Level_1/demoAAC1.m b/Level_1/demoAAC1.m index ed497e6..f3aee29 100644 --- a/Level_1/demoAAC1.m +++ b/Level_1/demoAAC1.m @@ -15,7 +15,5 @@ function SNR = demoAAC1(fNameIn, fNameOut) [audioData, ~] = audioread(fNameIn); - snr(audioData(1025:length(decodedAudio)-1024, 1), audioData(1025:length(decodedAudio)-1024, 1) - decodedAudio(1025:end-1024, 1)) - snr(audioData(1025:length(decodedAudio)-1024, 2), audioData(1025:length(decodedAudio)-1024, 2) - decodedAudio(1025:end-1024, 2)) - SNR = snr(audioData(1025:length(decodedAudio)-1024, :), audioData(1025:length(decodedAudio)-1024, :) - decodedAudio(1025:end-1024, :)); + SNR = snr(audioData(1025:end-1378, :), audioData(1025:end-1378, :) - decodedAudio(1025:length(audioData)-1378, :)); end diff --git a/Level_2/AACoder2.m b/Level_2/AACoder2.m index b149c8e..b94f8f4 100644 --- a/Level_2/AACoder2.m +++ b/Level_2/AACoder2.m @@ -21,12 +21,20 @@ function AACSeq2 = AACoder2(fNameIn) [originalAudioData, ~] = audioread(fNameIn); % Splits the audio in frames and determines the type of each frame - frameTypes{fix((length(originalAudioData) - 1025) / 1024), 1} = 'OLS'; + frameTypes{ceil(length(originalAudioData) / 1024), 1} = 'OLS'; frameTypes{1} = 'OLS'; for i = 1:length(frameTypes) - 2 nextFrameStart = (i + 1) * 1024 + 1; nextFrameStop = nextFrameStart + 2047; - frameTypes{i+1} = SSC(1, originalAudioData(nextFrameStart:nextFrameStop, :), frameTypes{i}); + if nextFrameStop > length(originalAudioData) + tmpZeroPadded = originalAudioData(nextFrameStart:length(originalAudioData), :); + tmpZeroPadded = padarray(tmpZeroPadded, [(2048 - length(tmpZeroPadded)) 0], 'post'); + frameTypes{i+1} = SSC(1, tmpZeroPadded, frameTypes{i}); + + clearvars tmpZeroPadded + else + frameTypes{i+1} = SSC(1, originalAudioData(nextFrameStart:nextFrameStop, :), frameTypes{i}); + end end % Assigns a type to the last frame @@ -43,7 +51,16 @@ function AACSeq2 = AACoder2(fNameIn) for i = 0:length(frameTypes) - 1 currFrameStart = i * 1024 + 1; currFrameStop = currFrameStart + 2047; - frameF = filterbank(originalAudioData(currFrameStart:currFrameStop, :), frameTypes{i+1}, WINDOW_TYPE); + if currFrameStop > length(originalAudioData) + tmpZeroPadded = originalAudioData(nextFrameStart:length(originalAudioData), :); + tmpZeroPadded = padarray(tmpZeroPadded, [(2048 - length(tmpZeroPadded)) 0], 'post'); + frameF = filterbank(tmpZeroPadded, frameTypes{i+1}, WINDOW_TYPE); + + clearvars tmpZeroPadded + else + frameF = filterbank(originalAudioData(currFrameStart:currFrameStop, :), frameTypes{i+1}, WINDOW_TYPE); + end + [frameF(:, 1), TNScoeffsL] = TNS(frameF(:, 1), frameTypes{i+1}); [frameF(:, 2), TNScoeffsR] = TNS(frameF(:, 2), frameTypes{i+1}); diff --git a/Level_2/decoded2.wav b/Level_2/decoded2.wav index e0c852d..b8e866f 100644 Binary files a/Level_2/decoded2.wav and b/Level_2/decoded2.wav differ diff --git a/Level_2/demoAAC2.m b/Level_2/demoAAC2.m index f324598..b387950 100644 --- a/Level_2/demoAAC2.m +++ b/Level_2/demoAAC2.m @@ -14,8 +14,6 @@ function SNR = demoAAC2(fNameIn, fNameOut) decodedAudio = iAACoder2(AACSeq2, fNameOut); [audioData, ~] = audioread(fNameIn); - - snr(audioData(1025:length(decodedAudio)-1024, 1), audioData(1025:length(decodedAudio)-1024, 1) - decodedAudio(1025:end-1024, 1)) - snr(audioData(1025:length(decodedAudio)-1024, 2), audioData(1025:length(decodedAudio)-1024, 2) - decodedAudio(1025:end-1024, 2)) - SNR = snr(audioData(1025:length(decodedAudio)-1024, :), audioData(1025:length(decodedAudio)-1024, :) - decodedAudio(1025:end-1024, :)); + + SNR = snr(audioData(1025:end-1378, :), audioData(1025:end-1378, :) - decodedAudio(1025:length(audioData)-1378, :)); end diff --git a/Level_3/AACoder3.m b/Level_3/AACoder3.m index e0b0260..9a9619c 100644 --- a/Level_3/AACoder3.m +++ b/Level_3/AACoder3.m @@ -53,12 +53,20 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded) [originalAudioData, ~] = audioread(fNameIn); % Splits the audio in frames and determines the type of each frame - frameTypes{fix((length(originalAudioData) - 1025) / 1024), 1} = 'OLS'; + frameTypes{ceil(length(originalAudioData) / 1024), 1} = 'OLS'; frameTypes{1} = 'OLS'; for i = 1:length(frameTypes) - 2 nextFrameStart = (i + 1) * 1024 + 1; nextFrameStop = nextFrameStart + 2047; - frameTypes{i+1} = SSC(1, originalAudioData(nextFrameStart:nextFrameStop, :), frameTypes{i}); + if nextFrameStop > length(originalAudioData) + tmpZeroPadded = originalAudioData(nextFrameStart:length(originalAudioData), :); + tmpZeroPadded = padarray(tmpZeroPadded, [(2048 - length(tmpZeroPadded)) 0], 'post'); + frameTypes{i+1} = SSC(1, tmpZeroPadded, frameTypes{i}); + + clearvars tmpZeroPadded + else + frameTypes{i+1} = SSC(1, originalAudioData(nextFrameStart:nextFrameStop, :), frameTypes{i}); + end end % Assigns a type to the last frame @@ -76,7 +84,15 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded) for i = 0:length(frameTypes) - 1 currFrameStart = i * 1024 + 1; currFrameStop = currFrameStart + 2047; - frameF = filterbank(originalAudioData(currFrameStart:currFrameStop, :), frameTypes{i+1}, WINDOW_TYPE); + if currFrameStop > length(originalAudioData) + tmpZeroPadded = originalAudioData(nextFrameStart:length(originalAudioData), :); + tmpZeroPadded = padarray(tmpZeroPadded, [(2048 - length(tmpZeroPadded)) 0], 'post'); + frameF = filterbank(tmpZeroPadded, frameTypes{i+1}, WINDOW_TYPE); + + clearvars tmpZeroPadded + else + frameF = filterbank(originalAudioData(currFrameStart:currFrameStop, :), frameTypes{i+1}, WINDOW_TYPE); + end [frameF(:, 1), TNScoeffsL] = TNS(frameF(:, 1), frameTypes{i+1}); [frameF(:, 2), TNScoeffsR] = TNS(frameF(:, 2), frameTypes{i+1}); @@ -105,16 +121,50 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded) prev1FrameStop = prev1FrameStart + 2047; prev2FrameStart = (i - 2) * 1024 + 1; prev2FrameStop = prev2FrameStart + 2047; - SMRL = psycho(... - originalAudioData(currFrameStart:currFrameStop, 1), ... - frameTypes{i+1}, ... - originalAudioData(prev1FrameStart:prev1FrameStop, 1), ... - originalAudioData(prev2FrameStart:prev2FrameStop, 1)); - SMRR = psycho(... - originalAudioData(currFrameStart:currFrameStop, 2), ... - frameTypes{i+1}, ... - originalAudioData(prev1FrameStart:prev1FrameStop, 2), ... - originalAudioData(prev2FrameStart:prev2FrameStop, 2)); + + if prev1FrameStop > length(originalAudioData) + tmpZeroPaddedPrev = originalAudioData(prev1FrameStart:length(originalAudioData), :); + tmpZeroPaddedPrev = padarray(tmpZeroPaddedPrev, [(2048 - length(tmpZeroPaddedPrev)) 0], 'post'); + tmpZeroPadded = originalAudioData(currFrameStart:length(originalAudioData), :); + tmpZeroPadded = padarray(tmpZeroPadded, [(2048 - length(tmpZeroPadded)) 0], 'post'); + SMRL = psycho(... + tmpZeroPadded(:, 1), ... + frameTypes{i+1}, ... + tmpZeroPaddedPrev(:, 1), ... + originalAudioData(prev2FrameStart:prev2FrameStop, 2)); + SMRR = psycho(... + tmpZeroPadded(:, 2), ... + frameTypes{i+1}, ... + tmpZeroPaddedPrev(:, 2), ... + originalAudioData(prev2FrameStart:prev2FrameStop, 2)); + clearvars tmpZeroPadded tmpZeroPaddedPrev + elseif currFrameStop > length(originalAudioData) + tmpZeroPadded = originalAudioData(currFrameStart:length(originalAudioData), :); + tmpZeroPadded = padarray(tmpZeroPadded, [(2048 - length(tmpZeroPadded)) 0], 'post'); + SMRL = psycho(... + tmpZeroPadded(:, 1), ... + frameTypes{i+1}, ... + originalAudioData(prev1FrameStart:prev1FrameStop, 1), ... + originalAudioData(prev2FrameStart:prev2FrameStop, 2)); + SMRR = psycho(... + tmpZeroPadded(:, 2), ... + frameTypes{i+1}, ... + originalAudioData(prev1FrameStart:prev1FrameStop, 2), ... + originalAudioData(prev2FrameStart:prev2FrameStop, 2)); + + clearvars tmpZeroPadded + else + SMRL = psycho(... + originalAudioData(currFrameStart:currFrameStop, 1), ... + frameTypes{i+1}, ... + originalAudioData(prev1FrameStart:prev1FrameStop, 1), ... + originalAudioData(prev2FrameStart:prev2FrameStop, 1)); + SMRR = psycho(... + originalAudioData(currFrameStart:currFrameStop, 2), ... + frameTypes{i+1}, ... + originalAudioData(prev1FrameStart:prev1FrameStop, 2), ... + originalAudioData(prev2FrameStart:prev2FrameStop, 2)); + end end [SL, sfcL, GL] = AACquantizer(frameF(:, 1), frameTypes{i+1}, SMRL); diff --git a/Level_3/AACquantizer.m b/Level_3/AACquantizer.m index 8d5bcd1..1760c35 100644 --- a/Level_3/AACquantizer.m +++ b/Level_3/AACquantizer.m @@ -73,6 +73,7 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR) quantErr = sumsqr(subFrameF - frameFDequant); end + % Undoes the last incrementation if quantCoeff(band) ~= initialQuantCoeff quantCoeff(band) = quantCoeff(band) - 1; S(frameWlow:frameWhigh) = sign(subFrameF) .* round(( ... @@ -136,6 +137,7 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR) quantErr = sumsqr(subFrameF - frameFDequant); end + % Undoes the last incrementation if quantCoeff(band) ~= initialQuantCoeff quantCoeff(band) = quantCoeff(band) - 1; S(currFrameStart + frameWlow:currFrameStart + frameWhigh) = sign(subFrameF) .* round(( ... diff --git a/Level_3/decoded3.wav b/Level_3/decoded3.wav index fb9dcf2..65d8991 100644 Binary files a/Level_3/decoded3.wav and b/Level_3/decoded3.wav differ diff --git a/Level_3/demoAAC3.m b/Level_3/demoAAC3.m index bf0e1f6..dd6fe13 100644 --- a/Level_3/demoAAC3.m +++ b/Level_3/demoAAC3.m @@ -17,25 +17,25 @@ function [SNR, bitrate, compression] = demoAAC3(fNameIn, fNameOut, frameAACoded) AACSeq3 = AACoder3(fNameIn, frameAACoded); - totalSize = 0; + totalSize = 0; % In bits for frameIndex = 1:length(AACSeq3) totalSize = totalSize + 2; % frameType - totalSize = totalSize + 2 * 4; % TNS coefficients - totalSize = totalSize + length(AACSeq3(frameIndex).chl.G) * 4; - totalSize = totalSize + length(AACSeq3(frameIndex).chr.G) * 4; + totalSize = totalSize + 4 * 4; % TNS coefficients + totalSize = totalSize + length(AACSeq3(frameIndex).chl.G) * 16; + totalSize = totalSize + length(AACSeq3(frameIndex).chr.G) * 16; totalSize = totalSize + length(AACSeq3(frameIndex).chl.sfc); totalSize = totalSize + length(AACSeq3(frameIndex).chr.sfc); totalSize = totalSize + length(AACSeq3(frameIndex).chl.stream); totalSize = totalSize + length(AACSeq3(frameIndex).chr.stream); + totalSize = totalSize + 4; % Codebook of left channel + totalSize = totalSize + 4; % Codebook of right channel end decodedAudio = iAACoder3(AACSeq3, fNameOut); bitrate = totalSize / (length(decodedAudio) / 48000); [audioData, ~] = audioread(fNameIn); - compression = (length(audioData) * 64) / totalSize; + compression = (2 * length(audioData) * 16) / (length(audioData) / 48000) / bitrate; - snr(audioData(1025:length(decodedAudio)-1024, 1), audioData(1025:length(decodedAudio)-1024, 1) - decodedAudio(1025:end-1024, 1)) - snr(audioData(1025:length(decodedAudio)-1024, 2), audioData(1025:length(decodedAudio)-1024, 2) - decodedAudio(1025:end-1024, 2)) - SNR = snr(audioData(1025:length(decodedAudio)-1024, :), audioData(1025:length(decodedAudio)-1024, :) - decodedAudio(1025:end-1024, :)); + SNR = snr(audioData(1025:end-1378, :), audioData(1025:end-1378, :) - decodedAudio(1025:length(audioData)-1378, :)); end diff --git a/Level_3/encoded.mat b/Level_3/encoded.mat index 6892914..42a76f6 100644 Binary files a/Level_3/encoded.mat and b/Level_3/encoded.mat differ