diff --git a/Level_1/AACoder1.m b/Level_1/AACoder1.m index ff6d6b8..6422fb6 100644 --- a/Level_1/AACoder1.m +++ b/Level_1/AACoder1.m @@ -26,7 +26,7 @@ function AACSeq1 = AACoder1(fNameIn) frameTypes{i+1} = SSC(1, originalAudioData(nextFrameStart:nextFrameStop, :), frameTypes{i}); end - % Assignes a type to the last frame + % Assigns a type to the last frame if strcmp(frameTypes{length(frameTypes) - 1}, 'LSS') frameTypes{length(frameTypes)} = 'ESH'; elseif strcmp(frameTypes{length(frameTypes) - 1}, 'ESH') diff --git a/Level_1/SSC.m b/Level_1/SSC.m index 4a03c4f..4df0078 100644 --- a/Level_1/SSC.m +++ b/Level_1/SSC.m @@ -30,9 +30,7 @@ function frameType = SSC(~, nextFrameT, prevFrameType) % Determines the type of the next frame % Filters frame nextFrameT = filter([0.7548 -0.7548], [1 -0.5095], nextFrameT, [], 1); - channelFrameType = {'nan', 'nan'}; - for channel = 1:2 % Calculates sub-frame energy estimation [subFrames, ~] = buffer(nextFrameT(449:end - 448, channel), 256, 128, 'nodelay'); @@ -45,6 +43,7 @@ function frameType = SSC(~, nextFrameT, prevFrameType) if ~isempty(find(energyEstimations > 10^(-3), 1)) && ... ~isempty(find(energyRatios(energyEstimations > 10^(-3)) > 10, 1)) + % Next frame is ESH 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 @@ -63,8 +62,9 @@ function frameType = SSC(~, nextFrameT, prevFrameType) end end + % Joins decision for both channels if strcmp(channelFrameType{1}, 'nan') || strcmp(channelFrameType{2}, 'nan') - error('SSC, l[73]: Internal error occured!') + error('SSC, l[73]: Internal error occurred!') end if strcmp(channelFrameType{1}, 'OLS') frameType = channelFrameType{2}; diff --git a/Level_1/demoAAC1.m b/Level_1/demoAAC1.m index 0fab49a..74bc46b 100644 --- a/Level_1/demoAAC1.m +++ b/Level_1/demoAAC1.m @@ -1,5 +1,5 @@ function SNR = demoAAC1(fNameIn, fNameOut) -%Implementation of WHAT?? //TODO!! +%Function that demonstrates usage of the level 1 code % Usage SNR = demoAAC1(fNameIn, fNameOut), where: % Inputs % - fNameIn is the filename and path of the file to encode @@ -14,24 +14,35 @@ function SNR = demoAAC1(fNameIn, fNameOut) decodedAudio = iAACoder1(AACSeq1, fNameOut); [audioData, ~] = audioread(fNameIn); - -% figure() -% plot(audioData(1:length(decodedAudio), 1) - decodedAudio(1:end, 1)) + % figure() -% plot(audioData(1:length(decodedAudio), 2) - decodedAudio(1:end, 2)) +% plot(audioData(1025:length(decodedAudio)-1024, 1) - decodedAudio(1025:end-1024, 1)) +% for frame = 1:length(AACSeq1) +% if strcmp(AACSeq1(frame).frameType, 'LSS') || strcmp(AACSeq1(frame).frameType, 'LPS') +% % Add lines +% h1 = line([(frame-1)*1024+1 (frame-1)*1024+1],[-2*10^(-15) 2*10^(-15)]); +% h2 = line([frame*1024+1 frame*1024+1],[-2*10^(-15) 2*10^(-15)]); +% % Set properties of lines +% set([h1 h2],'Color','y','LineWidth',2) +% % Add a patch +% patch([(frame-1)*1024+1 frame*1024+1 frame*1024+1 (frame-1)*1024+1],[-2*10^(-15) -2*10^(-15) 2*10^(-15) 2*10^(-15)],'y') +% elseif strcmp(AACSeq1(frame).frameType, 'ESH') +% % Add lines +% h1 = line([(frame-1)*1024+1 (frame-1)*1024+1],[-2*10^(-15) 2*10^(-15)]); +% h2 = line([frame*1024+1 frame*1024+1],[-2*10^(-15) 2*10^(-15)]); +% % Set properties of lines +% set([h1 h2],'Color','r','LineWidth',2) +% % Add a patch +% patch([(frame-1)*1024+1 frame*1024+1 frame*1024+1 (frame-1)*1024+1],[-2*10^(-15) -2*10^(-15) 2*10^(-15) 2*10^(-15)],'r') +% end +% end +% set(gca,'children',flipud(get(gca,'children'))) % -% 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)); - - figure() - plot(audioData(1024:length(decodedAudio)-1024, 1) - decodedAudio(1024:end-1024, 1)) - figure() - plot(audioData(1024:length(decodedAudio)-1024, 2) - decodedAudio(1024:end-1024, 2)) +% figure() +% plot(audioData(1025:length(decodedAudio)-1024, 2) - decodedAudio(1025:end-1024, 2)) - snr(audioData(1024:length(decodedAudio)-1024, 1), audioData(1024:length(decodedAudio)-1024, 1) - decodedAudio(1024:end-1024, 1)) - snr(audioData(1024:length(decodedAudio)-1024, 2), audioData(1024:length(decodedAudio)-1024, 2) - decodedAudio(1024:end-1024, 2)) - SNR = 10*log10((var(audioData(1024:length(decodedAudio)-1024, :)) .^ 2) ./ ... - (var(audioData(1024:length(decodedAudio)-1024, :) - decodedAudio(1024:end-1024, :)) .^ 2)); + 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 = 10*log10((sum(audioData(1025:length(decodedAudio)-1024, :)) .^ 2) ./ ... + (sum(audioData(1025:length(decodedAudio)-1024, :) - decodedAudio(1025:end-1024, :)) .^ 2)); end diff --git a/Level_1/filterbank.m b/Level_1/filterbank.m index 6e126cd..56ab914 100644 --- a/Level_1/filterbank.m +++ b/Level_1/filterbank.m @@ -24,63 +24,64 @@ function frameF = filterbank(frameT, frameType, winType) kaiserSumLong = sum(kaiserLong); kaiserShort = kaiser(129, 4*pi); kaiserSumShort = sum(kaiserShort); - + kaiserWindowLong(1:1024) = movsum(kaiserLong(1:1024), [1024 0]); kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong(1:1024)), [0 1024]); kaiserWindowLong = sqrt(kaiserWindowLong ./ kaiserSumLong); - + sinWindowLong = sin(pi * ((0:2047) + 0.5) / 2048); - + kaiserWindowShort(1:128) = movsum(kaiserShort(1:128), [128 0]); kaiserWindowShort(129:256) = movsum(flipud(kaiserShort(1:128)), [0 128]); kaiserWindowShort = sqrt(kaiserWindowShort ./ kaiserSumShort); - + sinWindowShort = sin(pi * ((0:255) + 0.5) / 256); end + % Initializes output array frameF(1024, 2) = 0; % Applies appropriate window to the frame - for channel=1:2 - if strcmp(frameType, 'OLS') - if strcmp(winType, 'KBD') - frameT(:, channel) = frameT(:, channel) .* kaiserWindowLong(:); - elseif strcmp(winType, 'SIN') - frameT(:, channel) = frameT(:, channel) .* sinWindowLong(:); - else - error('filterbank, l[20]: Unsupported window type input!') - end + if strcmp(frameType, 'OLS') + if strcmp(winType, 'KBD') + frameT = bsxfun(@times, frameT, kaiserWindowLong'); + elseif strcmp(winType, 'SIN') + frameT = bsxfun(@times, frameT, sinWindowLong'); + else + error('filterbank, l[20]: Unsupported window type input!') + end - frameF(:, channel) = mdct4(frameT(:, channel)); - elseif strcmp(frameType, 'LSS') - if strcmp(winType, 'KBD') - frameT(1:1024, channel) = frameT(1:1024, channel) .* kaiserWindowLong(1:1024)'; - frameT(1473:1600, channel) = frameT(1473:1600, channel) .* kaiserWindowShort(129:end)'; - frameT(1601:end, channel) = 0; - elseif strcmp(winType, 'SIN') - frameT(1:1024, channel) = frameT(1:1024, channel) .* sinWindowLong(1:1024)'; - frameT(1473:1600, channel) = frameT(1473:1600, channel) .* sinWindowShort(129:end)'; - frameT(1601:end, channel) = 0; - else - error('filterbank, l[20]: Unsupported window type input!') - end + frameF = mdct4(frameT); + elseif strcmp(frameType, 'LSS') + if strcmp(winType, 'KBD') + frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024)'); + frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end)'); + frameT(1601:end, :) = 0; + elseif strcmp(winType, 'SIN') + frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024)'); + frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end)'); + frameT(1601:end, :) = 0; + else + error('filterbank, l[20]: Unsupported window type input!') + end - frameF(:, channel) = mdct4(frameT(:, channel)); - elseif strcmp(frameType, 'LPS') - if strcmp(winType, 'KBD') - frameT(1:448, channel) = 0; - frameT(449:576, channel) = frameT(449:576, channel) .* kaiserWindowShort(1:128)'; - frameT(1025:end, channel) = frameT(1025:end, channel) .* kaiserWindowLong(1025:end)'; - elseif strcmp(winType, 'SIN') - frameT(1:448, channel) = 0; - frameT(449:576, channel) = frameT(449:576, channel) .* sinWindowShort(1:128)'; - frameT(1025:end, channel) = frameT(1025:end, channel) .* sinWindowLong(1025:end)'; - else - error('filterbank, l[20]: Unsupported window type input!') - end + frameF = mdct4(frameT); + elseif strcmp(frameType, 'LPS') + if strcmp(winType, 'KBD') + frameT(1:448, :) = 0; + frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128)'); + frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), kaiserWindowLong(1025:end)'); + elseif strcmp(winType, 'SIN') + frameT(1:448, :) = 0; + frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128)'); + frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), sinWindowLong(1025:end)'); + else + error('filterbank, l[20]: Unsupported window type input!') + end - frameF(:, channel) = mdct4(frameT(:, channel)); - elseif strcmp(frameType, 'ESH') + frameF = mdct4(frameT); + elseif strcmp(frameType, 'ESH') + for channel = 1:2 % Splits the frame into sub-frames [subFrames, ~] = buffer(frameT(449:end-448, channel), 256, 128, 'nodelay'); @@ -91,7 +92,8 @@ function frameF = filterbank(frameT, frameType, winType) end for subFrameIndex = 1:8 - frameF((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128, channel) = mdct4(subFrames(:, subFrameIndex)); + frameF((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128, channel) = ... + mdct4(subFrames(:, subFrameIndex)); end end end diff --git a/Level_1/iFilterbank.m b/Level_1/iFilterbank.m index 7ab0f56..683be02 100644 --- a/Level_1/iFilterbank.m +++ b/Level_1/iFilterbank.m @@ -28,59 +28,60 @@ function frameT = iFilterbank(frameF, frameType, winType) kaiserWindowLong(1:1024) = movsum(kaiserLong(1:1024), [1024 0]); kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong(1:1024)), [0 1024]); kaiserWindowLong = sqrt(kaiserWindowLong ./ kaiserSumLong); - + sinWindowLong = sin(pi * ((0:2047) + 0.5) / 2048); - + kaiserWindowShort(1:128) = movsum(kaiserShort(1:128), [128 0]); kaiserWindowShort(129:256) = movsum(flipud(kaiserShort(1:128)), [0 128]); kaiserWindowShort = sqrt(kaiserWindowShort ./ kaiserSumShort); - + sinWindowShort = sin(pi * ((0:255) + 0.5) / 256); end + % Initializes output array frameT(2048, 2) = 0; % Applies appropriate window to the frame - for channel=1:2 - if strcmp(frameType, 'OLS') - frameT(:, channel) = imdct4(frameF(:, channel)); + if strcmp(frameType, 'OLS') + frameT = imdct4(frameF); - if strcmp(winType, 'KBD') - frameT(:, channel) = frameT(:, channel) .* kaiserWindowLong(:); - elseif strcmp(winType, 'SIN') - frameT(:, channel) = frameT(:, channel) .* sinWindowLong(:); - else - error('filterbank, l[20]: Unsupported window type input!') - end - elseif strcmp(frameType, 'LSS') - frameT(:, channel) = imdct4(frameF(:, channel)); + if strcmp(winType, 'KBD') + frameT = bsxfun(@times, frameT, kaiserWindowLong'); + elseif strcmp(winType, 'SIN') + frameT = bsxfun(@times, frameT, sinWindowLong'); + else + error('filterbank, l[20]: Unsupported window type input!') + end + elseif strcmp(frameType, 'LSS') + frameT = imdct4(frameF); - if strcmp(winType, 'KBD') - frameT(1:1024, channel) = frameT(1:1024, channel) .* kaiserWindowLong(1:1024)'; - frameT(1473:1600, channel) = frameT(1473:1600, channel) .* kaiserWindowShort(129:end)'; - frameT(1601:end, channel) = 0; - elseif strcmp(winType, 'SIN') - frameT(1:1024, channel) = frameT(1:1024, channel) .* sinWindowLong(1:1024)'; - frameT(1473:1600, channel) = frameT(1473:1600, channel) .* sinWindowShort(129:end)'; - frameT(1601:end, channel) = 0; - else - error('filterbank, l[20]: Unsupported window type input!') - end - elseif strcmp(frameType, 'LPS') - frameT(:, channel) = imdct4(frameF(:, channel)); + if strcmp(winType, 'KBD') + frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024)'); + frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end)'); + frameT(1601:end, :) = 0; + elseif strcmp(winType, 'SIN') + frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024)'); + frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end)'); + frameT(1601:end, :) = 0; + else + error('filterbank, l[20]: Unsupported window type input!') + end + elseif strcmp(frameType, 'LPS') + frameT = imdct4(frameF); - if strcmp(winType, 'KBD') - frameT(1:448, channel) = 0; - frameT(449:576, channel) = frameT(449:576, channel) .* kaiserWindowShort(1:128)'; - frameT(1025:end, channel) = frameT(1025:end, channel) .* kaiserWindowLong(1025:end)'; - elseif strcmp(winType, 'SIN') - frameT(1:448, channel) = 0; - frameT(449:576, channel) = frameT(449:576, channel) .* sinWindowShort(1:128)'; - frameT(1025:end, channel) = frameT(1025:end, channel) .* sinWindowLong(1025:end)'; - else - error('filterbank, l[20]: Unsupported window type input!') - end - elseif strcmp(frameType, 'ESH') + if strcmp(winType, 'KBD') + frameT(1:448, :) = 0; + frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128)'); + frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), kaiserWindowLong(1025:end)'); + elseif strcmp(winType, 'SIN') + frameT(1:448, :) = 0; + frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128)'); + frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), sinWindowLong(1025:end)'); + else + error('filterbank, l[20]: Unsupported window type input!') + end + elseif strcmp(frameType, 'ESH') + for channel=1:2 for subFrameIndex = 1:8 subFrame = imdct4(frameF((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128, channel)); @@ -89,7 +90,7 @@ function frameT = iFilterbank(frameF, frameType, winType) elseif strcmp(winType, 'SIN') subFrame = subFrame .* sinWindowShort'; end - + frameT(448 + (subFrameIndex - 1) * 128 + 1:448 + (subFrameIndex + 1) * 128, channel) = ... frameT(448 + (subFrameIndex - 1) * 128 + 1:448 + (subFrameIndex + 1) * 128, channel) + subFrame; end