diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..21229f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.asv \ No newline at end of file diff --git a/Level_1/AACoder1.m b/Level_1/AACoder1.m index d3de782..0926305 100644 --- a/Level_1/AACoder1.m +++ b/Level_1/AACoder1.m @@ -1,5 +1,5 @@ function AACSeq1 = AACoder1(fNameIn) -%Implementation of WHAT?? //TODO!! +%Implementation of AAC encoder % Usage AACSeq1 = AACoder1(fNameIn), where: % Inputs % - fNameIn is the filename and path of the file to encode @@ -32,11 +32,12 @@ function AACSeq1 = AACoder1(fNameIn) frameTypes{length(frameTypes)} = 'OLS'; end + % Encodes audio file AACSeq1(length(frameTypes)) = struct; for i = 0:length(frameTypes)-1 currFrameStart = i * 1024 + 1; currFrameStop = currFrameStart + 2047; - frameF = filterbank(originalAudioData(currFrameStart:currFrameStop, :), frameTypes{i+1}, 'SIN'); + frameF = filterbank(originalAudioData(currFrameStart:currFrameStop, :), frameTypes{i+1}, 'KBD'); AACSeq1(i + 1).frameType = frameTypes(i + 1); AACSeq1(i + 1).winType = 'KBD'; @@ -44,7 +45,7 @@ function AACSeq1 = AACoder1(fNameIn) AACSeq1(i + 1).chr.frameF = frameF(:, 2); end - if true + if false [idx,label] = grp2idx(sort(frameTypes)); hist(idx,unique(idx)); set(gca,'xTickLabel',label) diff --git a/Level_1/SSC.m b/Level_1/SSC.m index db5d485..058c656 100644 --- a/Level_1/SSC.m +++ b/Level_1/SSC.m @@ -1,5 +1,5 @@ function frameType = SSC(~, nextFrameT, prevFrameType) -%Implementation of the SSC step +%Implementation of the Sequence Segmentation Control step % Usage frameType = SSC(frameT, nextFrameT, prevFrameType), where: % Inputs % - frameT is a frame in the time domain, containing both channels of @@ -29,29 +29,29 @@ function frameType = SSC(~, nextFrameT, prevFrameType) % Determines the type of the next frame % Filters frame - nextFrameT = filter([0.7548, -0.7548], [1, -0.5095], nextFrameT, [], 2); + nextFrameT = filter([0.7548, -0.7548], [1, -0.5095], nextFrameT, [], 1); - channelFrameType = {'OLS', 'OLS'}; + channelFrameType = {'nan', 'nan'}; for channel = 1:2 % Calculates sub-frame energy estimation - [subFrames, ~] = buffer(nextFrameT(449:end-448, channel), 256, 128, 'nodelay'); - energyEstimations = sum(subFrames.^2, 1); + [subFrames, ~] = buffer(nextFrameT(449:end - 448, channel), 256, 128, 'nodelay'); + energyEstimations = sum(subFrames .^ 2, 1); % Calculates the ratio of the sub-frame energy to the average energy of % the previous sub-frames - nextIsESH = 0; - for subFrameIndex = 1:8 + nextIsESH = false; + for subFrameIndex = 2:8 energyRatio = energyEstimations(subFrameIndex) / ... - mean(energyEstimations(1:subFrameIndex-1)); + mean(energyEstimations(1:subFrameIndex - 1)); if (energyEstimations(subFrameIndex) > 10^(-3)) && (energyRatio > 10) - nextIsESH = 1; + nextIsESH = true; break; end end - if nextIsESH == 1 + if nextIsESH == true if strcmp(prevFrameType, 'ESH') % This frame of this channel is an EIGHT_SHORT_SEQUENCE type % frame. This means the frames of both channels will be encoded @@ -70,6 +70,9 @@ function frameType = SSC(~, nextFrameT, prevFrameType) end end + if strcmp(channelFrameType{1}, 'nan') || strcmp(channelFrameType{2}, 'nan') + error('SSC, l[73]: Internal error occured!') + end if strcmp(channelFrameType{1}, 'OLS') frameType = channelFrameType{2}; elseif strcmp(channelFrameType{2}, 'OLS') diff --git a/Level_1/decoded2.wav b/Level_1/decoded2.wav new file mode 100644 index 0000000..374d997 Binary files /dev/null and b/Level_1/decoded2.wav differ diff --git a/Level_1/demoAAC1.m b/Level_1/demoAAC1.m index 38cba32..21fec1b 100644 --- a/Level_1/demoAAC1.m +++ b/Level_1/demoAAC1.m @@ -14,6 +14,14 @@ function SNR = demoAAC1(fNameIn, fNameOut) decodedAudio = iAACoder1(AACSeq1, fNameOut); [audioData, ~] = audioread(fNameIn); - SNR = sum(10*log10((sum(audioData(1:length(decodedAudio), :)) .^ 2)./ ... - (sum(audioData(1:length(decodedAudio), :) - decodedAudio) .^ 2))); + + figure() + plot(audioData(1:length(decodedAudio), 1) - decodedAudio(1:end, 1)) + figure() + plot(audioData(1:length(decodedAudio), 2) - decodedAudio(1:end, 2)) + + snr(audioData(1:length(decodedAudio), 1), audioData(1:length(decodedAudio), 1) - decodedAudio(1:end, 1)) + snr(audioData(1:length(decodedAudio), 2), audioData(1:length(decodedAudio), 2) - decodedAudio(1:end, 2)) + SNR = 10*log10((sum(audioData(1:length(decodedAudio), :)) .^ 2) ./ ... + (sum(audioData(1:length(decodedAudio), :) - decodedAudio) .^ 2)); end diff --git a/Level_1/filterbank.m b/Level_1/filterbank.m index f1a1f7b..98fea67 100644 --- a/Level_1/filterbank.m +++ b/Level_1/filterbank.m @@ -56,7 +56,7 @@ function frameF = filterbank(frameT, frameType, winType) error('filterbank, l[20]: Unsupported window type input!') end - frameF = mdct4(frameT); + frameF(:, channel) = mdct4(frameT(:, channel)); elseif strcmp(frameType, 'LSS') if strcmp(winType, 'KBD') frameT(1:1024, channel) = frameT(1:1024, channel) .* kaiserWindowLong(1:1024)'; @@ -70,7 +70,7 @@ function frameF = filterbank(frameT, frameType, winType) error('filterbank, l[20]: Unsupported window type input!') end - frameF = mdct4(frameT); + frameF(:, channel) = mdct4(frameT(:, channel)); elseif strcmp(frameType, 'LPS') if strcmp(winType, 'KBD') frameT(1:448, channel) = 0; @@ -84,7 +84,7 @@ function frameF = filterbank(frameT, frameType, winType) error('filterbank, l[20]: Unsupported window type input!') end - frameF = mdct4(frameT); + frameF(:, channel) = mdct4(frameT(:, channel)); elseif strcmp(frameType, 'ESH') % Splits the frame into sub-frames [subFrames, ~] = buffer(frameT(449:end-448, channel), 256, 128, 'nodelay'); diff --git a/Level_1/iAACoder1.m b/Level_1/iAACoder1.m index a1ee75d..448212a 100644 --- a/Level_1/iAACoder1.m +++ b/Level_1/iAACoder1.m @@ -1,5 +1,5 @@ function x = iAACoder1(AACSeq1, fNameOut) -%Implementation of WHAT?? //TODO!! +%Implementation of AAC decoder % Usage x = iAACoder1(AACSeq1, fNameOut), where: % Inputs % - fNameOut is the filename and path of the file that will be @@ -13,8 +13,12 @@ function x = iAACoder1(AACSeq1, fNameOut) % Output % - x is an array containing the decoded audio samples + % Initializes an array to hold the decoded samples decodedAudio(1024 * (length(AACSeq1) + 1), 2) = 0; + % Initializes an array to hold both audio channels frameF(1024, 2) = 0; + + % Decodes audio file for i = 0:length(AACSeq1)-1 currFrameStart = i * 1024 + 1; currFrameStop = currFrameStart + 2047; diff --git a/Level_1/iFilterbank.m b/Level_1/iFilterbank.m index ad812fd..c71e8d5 100644 --- a/Level_1/iFilterbank.m +++ b/Level_1/iFilterbank.m @@ -48,7 +48,7 @@ function frameT = iFilterbank(frameF, frameType, winType) % Applies appropriate window to the frame for channel=1:2 if strcmp(frameType, 'OLS') - frameT = imdct4(frameF); + frameT(:, channel) = imdct4(frameF(:, channel)); if strcmp(winType, 'KBD') frameT(:, channel) = frameT(:, channel) .* kaiserWindowLong(:); @@ -58,7 +58,7 @@ function frameT = iFilterbank(frameF, frameType, winType) error('filterbank, l[20]: Unsupported window type input!') end elseif strcmp(frameType, 'LSS') - frameT = imdct4(frameF); + frameT(:, channel) = imdct4(frameF(:, channel)); if strcmp(winType, 'KBD') frameT(1:1024, channel) = frameT(1:1024, channel) .* kaiserWindowLong(1:1024)'; @@ -72,7 +72,7 @@ function frameT = iFilterbank(frameF, frameType, winType) error('filterbank, l[20]: Unsupported window type input!') end elseif strcmp(frameType, 'LPS') - frameT = imdct4(frameF); + frameT(:, channel) = imdct4(frameF(:, channel)); if strcmp(winType, 'KBD') frameT(1:448, channel) = 0; @@ -94,9 +94,9 @@ function frameT = iFilterbank(frameF, frameType, winType) elseif strcmp(winType, 'SIN') subFrame = subFrame .* sinWindowShort'; end - - frameT(449 + (subFrameIndex - 1) * 128 + 1:449 + (subFrameIndex + 1) * 128) = ... - frameT(449 + (subFrameIndex - 1) * 128 + 1:449 + (subFrameIndex + 1) * 128) + subFrame'; + + frameT(448 + (subFrameIndex - 1) * 128 + 1:448 + (subFrameIndex + 1) * 128, channel) = ... + frameT(448 + (subFrameIndex - 1) * 128 + 1:448 + (subFrameIndex + 1) * 128, channel) + subFrame; end end end