Browse Source

Remove channel loops from filterbank and iFilterbank, Minor fixes

master
Apostolos Fanakis 6 years ago
parent
commit
df884d13de
  1. 2
      Level_1/AACoder1.m
  2. 6
      Level_1/SSC.m
  3. 47
      Level_1/demoAAC1.m
  4. 86
      Level_1/filterbank.m
  5. 83
      Level_1/iFilterbank.m

2
Level_1/AACoder1.m

@ -26,7 +26,7 @@ function AACSeq1 = AACoder1(fNameIn)
frameTypes{i+1} = SSC(1, originalAudioData(nextFrameStart:nextFrameStop, :), frameTypes{i}); frameTypes{i+1} = SSC(1, originalAudioData(nextFrameStart:nextFrameStop, :), frameTypes{i});
end end
% Assignes a type to the last frame % Assigns a type to the last frame
if strcmp(frameTypes{length(frameTypes) - 1}, 'LSS') if strcmp(frameTypes{length(frameTypes) - 1}, 'LSS')
frameTypes{length(frameTypes)} = 'ESH'; frameTypes{length(frameTypes)} = 'ESH';
elseif strcmp(frameTypes{length(frameTypes) - 1}, 'ESH') elseif strcmp(frameTypes{length(frameTypes) - 1}, 'ESH')

6
Level_1/SSC.m

@ -30,9 +30,7 @@ function frameType = SSC(~, nextFrameT, prevFrameType)
% Determines the type of the next frame % Determines the type of the next frame
% Filters frame % Filters frame
nextFrameT = filter([0.7548 -0.7548], [1 -0.5095], nextFrameT, [], 1); nextFrameT = filter([0.7548 -0.7548], [1 -0.5095], nextFrameT, [], 1);
channelFrameType = {'nan', 'nan'}; channelFrameType = {'nan', 'nan'};
for channel = 1:2 for channel = 1:2
% Calculates sub-frame energy estimation % Calculates sub-frame energy estimation
[subFrames, ~] = buffer(nextFrameT(449:end - 448, channel), 256, 128, 'nodelay'); [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)) && ... if ~isempty(find(energyEstimations > 10^(-3), 1)) && ...
~isempty(find(energyRatios(energyEstimations > 10^(-3)) > 10, 1)) ~isempty(find(energyRatios(energyEstimations > 10^(-3)) > 10, 1))
% Next frame is ESH
if strcmp(prevFrameType, 'ESH') if strcmp(prevFrameType, 'ESH')
% This frame of this channel is an EIGHT_SHORT_SEQUENCE type % This frame of this channel is an EIGHT_SHORT_SEQUENCE type
% frame. This means the frames of both channels will be encoded % frame. This means the frames of both channels will be encoded
@ -63,8 +62,9 @@ function frameType = SSC(~, nextFrameT, prevFrameType)
end end
end end
% Joins decision for both channels
if strcmp(channelFrameType{1}, 'nan') || strcmp(channelFrameType{2}, 'nan') if strcmp(channelFrameType{1}, 'nan') || strcmp(channelFrameType{2}, 'nan')
error('SSC, l[73]: Internal error occured!') error('SSC, l[73]: Internal error occurred!')
end end
if strcmp(channelFrameType{1}, 'OLS') if strcmp(channelFrameType{1}, 'OLS')
frameType = channelFrameType{2}; frameType = channelFrameType{2};

47
Level_1/demoAAC1.m

@ -1,5 +1,5 @@
function SNR = demoAAC1(fNameIn, fNameOut) function SNR = demoAAC1(fNameIn, fNameOut)
%Implementation of WHAT?? //TODO!! %Function that demonstrates usage of the level 1 code
% Usage SNR = demoAAC1(fNameIn, fNameOut), where: % Usage SNR = demoAAC1(fNameIn, fNameOut), where:
% Inputs % Inputs
% - fNameIn is the filename and path of the file to encode % - fNameIn is the filename and path of the file to encode
@ -14,24 +14,35 @@ function SNR = demoAAC1(fNameIn, fNameOut)
decodedAudio = iAACoder1(AACSeq1, fNameOut); decodedAudio = iAACoder1(AACSeq1, fNameOut);
[audioData, ~] = audioread(fNameIn); [audioData, ~] = audioread(fNameIn);
% figure()
% plot(audioData(1:length(decodedAudio), 1) - decodedAudio(1:end, 1))
% figure() % 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)) % figure()
% snr(audioData(1:length(decodedAudio), 2), audioData(1:length(decodedAudio), 2) - decodedAudio(1:end, 2)) % plot(audioData(1025:length(decodedAudio)-1024, 2) - decodedAudio(1025:end-1024, 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))
snr(audioData(1024:length(decodedAudio)-1024, 1), audioData(1024:length(decodedAudio)-1024, 1) - decodedAudio(1024:end-1024, 1)) snr(audioData(1025:length(decodedAudio)-1024, 1), audioData(1025:length(decodedAudio)-1024, 1) - decodedAudio(1025:end-1024, 1))
snr(audioData(1024:length(decodedAudio)-1024, 2), audioData(1024:length(decodedAudio)-1024, 2) - decodedAudio(1024:end-1024, 2)) snr(audioData(1025:length(decodedAudio)-1024, 2), audioData(1025:length(decodedAudio)-1024, 2) - decodedAudio(1025:end-1024, 2))
SNR = 10*log10((var(audioData(1024:length(decodedAudio)-1024, :)) .^ 2) ./ ... SNR = 10*log10((sum(audioData(1025:length(decodedAudio)-1024, :)) .^ 2) ./ ...
(var(audioData(1024:length(decodedAudio)-1024, :) - decodedAudio(1024:end-1024, :)) .^ 2)); (sum(audioData(1025:length(decodedAudio)-1024, :) - decodedAudio(1025:end-1024, :)) .^ 2));
end end

86
Level_1/filterbank.m

@ -24,63 +24,64 @@ function frameF = filterbank(frameT, frameType, winType)
kaiserSumLong = sum(kaiserLong); kaiserSumLong = sum(kaiserLong);
kaiserShort = kaiser(129, 4*pi); kaiserShort = kaiser(129, 4*pi);
kaiserSumShort = sum(kaiserShort); kaiserSumShort = sum(kaiserShort);
kaiserWindowLong(1:1024) = movsum(kaiserLong(1:1024), [1024 0]); kaiserWindowLong(1:1024) = movsum(kaiserLong(1:1024), [1024 0]);
kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong(1:1024)), [0 1024]); kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong(1:1024)), [0 1024]);
kaiserWindowLong = sqrt(kaiserWindowLong ./ kaiserSumLong); kaiserWindowLong = sqrt(kaiserWindowLong ./ kaiserSumLong);
sinWindowLong = sin(pi * ((0:2047) + 0.5) / 2048); sinWindowLong = sin(pi * ((0:2047) + 0.5) / 2048);
kaiserWindowShort(1:128) = movsum(kaiserShort(1:128), [128 0]); kaiserWindowShort(1:128) = movsum(kaiserShort(1:128), [128 0]);
kaiserWindowShort(129:256) = movsum(flipud(kaiserShort(1:128)), [0 128]); kaiserWindowShort(129:256) = movsum(flipud(kaiserShort(1:128)), [0 128]);
kaiserWindowShort = sqrt(kaiserWindowShort ./ kaiserSumShort); kaiserWindowShort = sqrt(kaiserWindowShort ./ kaiserSumShort);
sinWindowShort = sin(pi * ((0:255) + 0.5) / 256); sinWindowShort = sin(pi * ((0:255) + 0.5) / 256);
end end
% Initializes output array
frameF(1024, 2) = 0; frameF(1024, 2) = 0;
% Applies appropriate window to the frame % Applies appropriate window to the frame
for channel=1:2 if strcmp(frameType, 'OLS')
if strcmp(frameType, 'OLS') if strcmp(winType, 'KBD')
if strcmp(winType, 'KBD') frameT = bsxfun(@times, frameT, kaiserWindowLong');
frameT(:, channel) = frameT(:, channel) .* kaiserWindowLong(:); elseif strcmp(winType, 'SIN')
elseif strcmp(winType, 'SIN') frameT = bsxfun(@times, frameT, sinWindowLong');
frameT(:, channel) = frameT(:, channel) .* sinWindowLong(:); else
else error('filterbank, l[20]: Unsupported window type input!')
error('filterbank, l[20]: Unsupported window type input!') end
end
frameF(:, channel) = mdct4(frameT(:, channel)); frameF = mdct4(frameT);
elseif strcmp(frameType, 'LSS') elseif strcmp(frameType, 'LSS')
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:1024, channel) = frameT(1:1024, channel) .* kaiserWindowLong(1:1024)'; frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024)');
frameT(1473:1600, channel) = frameT(1473:1600, channel) .* kaiserWindowShort(129:end)'; frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end)');
frameT(1601:end, channel) = 0; frameT(1601:end, :) = 0;
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT(1:1024, channel) = frameT(1:1024, channel) .* sinWindowLong(1:1024)'; frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024)');
frameT(1473:1600, channel) = frameT(1473:1600, channel) .* sinWindowShort(129:end)'; frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end)');
frameT(1601:end, channel) = 0; frameT(1601:end, :) = 0;
else else
error('filterbank, l[20]: Unsupported window type input!') error('filterbank, l[20]: Unsupported window type input!')
end end
frameF(:, channel) = mdct4(frameT(:, channel)); frameF = mdct4(frameT);
elseif strcmp(frameType, 'LPS') elseif strcmp(frameType, 'LPS')
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:448, channel) = 0; frameT(1:448, :) = 0;
frameT(449:576, channel) = frameT(449:576, channel) .* kaiserWindowShort(1:128)'; frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128)');
frameT(1025:end, channel) = frameT(1025:end, channel) .* kaiserWindowLong(1025:end)'; frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), kaiserWindowLong(1025:end)');
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT(1:448, channel) = 0; frameT(1:448, :) = 0;
frameT(449:576, channel) = frameT(449:576, channel) .* sinWindowShort(1:128)'; frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128)');
frameT(1025:end, channel) = frameT(1025:end, channel) .* sinWindowLong(1025:end)'; frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), sinWindowLong(1025:end)');
else else
error('filterbank, l[20]: Unsupported window type input!') error('filterbank, l[20]: Unsupported window type input!')
end end
frameF(:, channel) = mdct4(frameT(:, channel)); frameF = mdct4(frameT);
elseif strcmp(frameType, 'ESH') elseif strcmp(frameType, 'ESH')
for channel = 1:2
% Splits the frame into sub-frames % Splits the frame into sub-frames
[subFrames, ~] = buffer(frameT(449:end-448, channel), 256, 128, 'nodelay'); [subFrames, ~] = buffer(frameT(449:end-448, channel), 256, 128, 'nodelay');
@ -91,7 +92,8 @@ function frameF = filterbank(frameT, frameType, winType)
end end
for subFrameIndex = 1:8 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 end
end end

83
Level_1/iFilterbank.m

@ -28,59 +28,60 @@ function frameT = iFilterbank(frameF, frameType, winType)
kaiserWindowLong(1:1024) = movsum(kaiserLong(1:1024), [1024 0]); kaiserWindowLong(1:1024) = movsum(kaiserLong(1:1024), [1024 0]);
kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong(1:1024)), [0 1024]); kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong(1:1024)), [0 1024]);
kaiserWindowLong = sqrt(kaiserWindowLong ./ kaiserSumLong); kaiserWindowLong = sqrt(kaiserWindowLong ./ kaiserSumLong);
sinWindowLong = sin(pi * ((0:2047) + 0.5) / 2048); sinWindowLong = sin(pi * ((0:2047) + 0.5) / 2048);
kaiserWindowShort(1:128) = movsum(kaiserShort(1:128), [128 0]); kaiserWindowShort(1:128) = movsum(kaiserShort(1:128), [128 0]);
kaiserWindowShort(129:256) = movsum(flipud(kaiserShort(1:128)), [0 128]); kaiserWindowShort(129:256) = movsum(flipud(kaiserShort(1:128)), [0 128]);
kaiserWindowShort = sqrt(kaiserWindowShort ./ kaiserSumShort); kaiserWindowShort = sqrt(kaiserWindowShort ./ kaiserSumShort);
sinWindowShort = sin(pi * ((0:255) + 0.5) / 256); sinWindowShort = sin(pi * ((0:255) + 0.5) / 256);
end end
% Initializes output array
frameT(2048, 2) = 0; frameT(2048, 2) = 0;
% Applies appropriate window to the frame % Applies appropriate window to the frame
for channel=1:2 if strcmp(frameType, 'OLS')
if strcmp(frameType, 'OLS') frameT = imdct4(frameF);
frameT(:, channel) = imdct4(frameF(:, channel));
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(:, channel) = frameT(:, channel) .* kaiserWindowLong(:); frameT = bsxfun(@times, frameT, kaiserWindowLong');
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT(:, channel) = frameT(:, channel) .* sinWindowLong(:); frameT = bsxfun(@times, frameT, sinWindowLong');
else else
error('filterbank, l[20]: Unsupported window type input!') error('filterbank, l[20]: Unsupported window type input!')
end end
elseif strcmp(frameType, 'LSS') elseif strcmp(frameType, 'LSS')
frameT(:, channel) = imdct4(frameF(:, channel)); frameT = imdct4(frameF);
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:1024, channel) = frameT(1:1024, channel) .* kaiserWindowLong(1:1024)'; frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024)');
frameT(1473:1600, channel) = frameT(1473:1600, channel) .* kaiserWindowShort(129:end)'; frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end)');
frameT(1601:end, channel) = 0; frameT(1601:end, :) = 0;
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT(1:1024, channel) = frameT(1:1024, channel) .* sinWindowLong(1:1024)'; frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024)');
frameT(1473:1600, channel) = frameT(1473:1600, channel) .* sinWindowShort(129:end)'; frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end)');
frameT(1601:end, channel) = 0; frameT(1601:end, :) = 0;
else else
error('filterbank, l[20]: Unsupported window type input!') error('filterbank, l[20]: Unsupported window type input!')
end end
elseif strcmp(frameType, 'LPS') elseif strcmp(frameType, 'LPS')
frameT(:, channel) = imdct4(frameF(:, channel)); frameT = imdct4(frameF);
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:448, channel) = 0; frameT(1:448, :) = 0;
frameT(449:576, channel) = frameT(449:576, channel) .* kaiserWindowShort(1:128)'; frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128)');
frameT(1025:end, channel) = frameT(1025:end, channel) .* kaiserWindowLong(1025:end)'; frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), kaiserWindowLong(1025:end)');
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT(1:448, channel) = 0; frameT(1:448, :) = 0;
frameT(449:576, channel) = frameT(449:576, channel) .* sinWindowShort(1:128)'; frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128)');
frameT(1025:end, channel) = frameT(1025:end, channel) .* sinWindowLong(1025:end)'; frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), sinWindowLong(1025:end)');
else else
error('filterbank, l[20]: Unsupported window type input!') error('filterbank, l[20]: Unsupported window type input!')
end end
elseif strcmp(frameType, 'ESH') elseif strcmp(frameType, 'ESH')
for channel=1:2
for subFrameIndex = 1:8 for subFrameIndex = 1:8
subFrame = imdct4(frameF((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128, channel)); subFrame = imdct4(frameF((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128, channel));
@ -89,7 +90,7 @@ function frameT = iFilterbank(frameF, frameType, winType)
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
subFrame = subFrame .* sinWindowShort'; subFrame = subFrame .* sinWindowShort';
end end
frameT(448 + (subFrameIndex - 1) * 128 + 1:448 + (subFrameIndex + 1) * 128, channel) = ... frameT(448 + (subFrameIndex - 1) * 128 + 1:448 + (subFrameIndex + 1) * 128, channel) = ...
frameT(448 + (subFrameIndex - 1) * 128 + 1:448 + (subFrameIndex + 1) * 128, channel) + subFrame; frameT(448 + (subFrameIndex - 1) * 128 + 1:448 + (subFrameIndex + 1) * 128, channel) + subFrame;
end end

Loading…
Cancel
Save