Browse Source

Make all matrices column vectors

master
Apostolos Fanakis 6 years ago
parent
commit
7b5c2767a6
  1. 4
      Level_1/AACoder1.m
  2. 4
      Level_1/SSC.m
  3. 36
      Level_1/filterbank.m
  4. 36
      Level_1/iFilterbank.m
  5. 4
      Level_2/AACoder2.m
  6. 4
      Level_2/SSC.m
  7. 16
      Level_2/TNS.m
  8. 36
      Level_2/filterbank.m
  9. 36
      Level_2/iFilterbank.m
  10. 4
      Level_2/iTNS.m
  11. 4
      Level_3/AACoder3.m
  12. 4
      Level_3/SSC.m
  13. 16
      Level_3/TNS.m
  14. 36
      Level_3/filterbank.m
  15. 36
      Level_3/iFilterbank.m
  16. 4
      Level_3/iTNS.m
  17. 40
      Level_3/psycho.m

4
Level_1/AACoder1.m

@ -18,7 +18,7 @@ function AACSeq1 = AACoder1(fNameIn)
[originalAudioData, ~] = audioread(fNameIn); [originalAudioData, ~] = audioread(fNameIn);
% Splits the audio in frames and determines the type of each frame % Splits the audio in frames and determines the type of each frame
frameTypes{fix((length(originalAudioData) - 1025) / 1024)} = 'OLS'; frameTypes{fix((length(originalAudioData) - 1025) / 1024), 1} = 'OLS';
frameTypes{1} = 'OLS'; frameTypes{1} = 'OLS';
for i = 1:length(frameTypes) - 2 for i = 1:length(frameTypes) - 2
nextFrameStart = (i + 1) * 1024 + 1; nextFrameStart = (i + 1) * 1024 + 1;
@ -36,7 +36,7 @@ function AACSeq1 = AACoder1(fNameIn)
end end
% Encodes audio file % Encodes audio file
AACSeq1(length(frameTypes)) = struct; AACSeq1(length(frameTypes), 1) = struct;
for i = 0:length(frameTypes) - 1 for i = 0:length(frameTypes) - 1
currFrameStart = i * 1024 + 1; currFrameStart = i * 1024 + 1;
currFrameStop = currFrameStart + 2047; currFrameStop = currFrameStart + 2047;

4
Level_1/SSC.m

@ -34,12 +34,12 @@ function frameType = SSC(~, nextFrameT, prevFrameType)
for channel = 1:2 for channel = 1:2
% Calculates sub-frame energy estimation % Calculates sub-frame energy estimation
[subFrames, ~] = buffer(nextFrameT(577:end - 448, channel), 128, 0, 'nodelay'); [subFrames, ~] = buffer(nextFrameT(577:end - 448, channel), 128, 0, 'nodelay');
energyEstimations = sum(subFrames .^ 2, 1); energyEstimations = sum(subFrames .^ 2, 1)';
% Calculates the ratio of the sub-frame energy to the average energy of % Calculates the ratio of the sub-frame energy to the average energy of
% the previous sub-frames % the previous sub-frames
energyRatios = movmean(energyEstimations, [8 0]); energyRatios = movmean(energyEstimations, [8 0]);
energyRatios = energyEstimations ./ [energyEstimations(1) energyRatios(1:end-1)]; energyRatios = energyEstimations ./ [energyEstimations(1); energyRatios(1:end-1)];
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))

36
Level_1/filterbank.m

@ -25,17 +25,17 @@ function frameF = filterbank(frameT, frameType, winType)
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, 1) = movsum(kaiserLong(1:1024), [1024 0]);
kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong(1:1024)), [0 1024]); kaiserWindowLong(1025:2048, 1) = 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, 1) = movsum(kaiserShort(1:128), [128 0]);
kaiserWindowShort(129:256) = movsum(flipud(kaiserShort(1:128)), [0 128]); kaiserWindowShort(129:256, 1) = 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 % Initializes output array
@ -44,9 +44,9 @@ function frameF = filterbank(frameT, frameType, winType)
% Applies appropriate window to the frame % Applies appropriate window to the frame
if strcmp(frameType, 'OLS') if strcmp(frameType, 'OLS')
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT = bsxfun(@times, frameT, kaiserWindowLong'); frameT = bsxfun(@times, frameT, kaiserWindowLong);
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT = bsxfun(@times, frameT, 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
@ -54,12 +54,12 @@ function frameF = filterbank(frameT, frameType, winType)
frameF = mdct4(frameT); frameF = mdct4(frameT);
elseif strcmp(frameType, 'LSS') elseif strcmp(frameType, 'LSS')
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024)'); frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024));
frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end)'); frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end));
frameT(1601:end, :) = 0; frameT(1601:end, :) = 0;
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024)'); frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024));
frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end)'); frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end));
frameT(1601:end, :) = 0; frameT(1601:end, :) = 0;
else else
error('filterbank, l[20]: Unsupported window type input!') error('filterbank, l[20]: Unsupported window type input!')
@ -69,12 +69,12 @@ function frameF = filterbank(frameT, frameType, winType)
elseif strcmp(frameType, 'LPS') elseif strcmp(frameType, 'LPS')
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:448, :) = 0; frameT(1:448, :) = 0;
frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128)'); frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128));
frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), 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, :) = 0; frameT(1:448, :) = 0;
frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128)'); frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128));
frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), 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
@ -86,9 +86,9 @@ function frameF = filterbank(frameT, frameType, winType)
[subFrames, ~] = buffer(frameT(449:end-448, channel), 256, 128, 'nodelay'); [subFrames, ~] = buffer(frameT(449:end-448, channel), 256, 128, 'nodelay');
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
subFrames = subFrames .* repmat(kaiserWindowShort', [1 8]); subFrames = subFrames .* repmat(kaiserWindowShort, [1 8]);
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
subFrames = subFrames .* repmat(sinWindowShort', [1 8]); subFrames = subFrames .* repmat(sinWindowShort, [1 8]);
end end
for subFrameIndex = 1:8 for subFrameIndex = 1:8

36
Level_1/iFilterbank.m

@ -25,17 +25,17 @@ function frameT = iFilterbank(frameF, frameType, winType)
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, 1) = movsum(kaiserLong(1:1024), [1024 0]);
kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong(1:1024)), [0 1024]); kaiserWindowLong(1025:2048, 1) = 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, 1) = movsum(kaiserShort(1:128), [128 0]);
kaiserWindowShort(129:256) = movsum(flipud(kaiserShort(1:128)), [0 128]); kaiserWindowShort(129:256, 1) = 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 % Initializes output array
@ -46,9 +46,9 @@ function frameT = iFilterbank(frameF, frameType, winType)
frameT = imdct4(frameF); frameT = imdct4(frameF);
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT = bsxfun(@times, frameT, kaiserWindowLong'); frameT = bsxfun(@times, frameT, kaiserWindowLong);
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT = bsxfun(@times, frameT, 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
@ -56,12 +56,12 @@ function frameT = iFilterbank(frameF, frameType, winType)
frameT = imdct4(frameF); frameT = imdct4(frameF);
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024)'); frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024));
frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end)'); frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end));
frameT(1601:end, :) = 0; frameT(1601:end, :) = 0;
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024)'); frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024));
frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end)'); frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end));
frameT(1601:end, :) = 0; frameT(1601:end, :) = 0;
else else
error('filterbank, l[20]: Unsupported window type input!') error('filterbank, l[20]: Unsupported window type input!')
@ -71,12 +71,12 @@ function frameT = iFilterbank(frameF, frameType, winType)
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:448, :) = 0; frameT(1:448, :) = 0;
frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128)'); frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128));
frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), 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, :) = 0; frameT(1:448, :) = 0;
frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128)'); frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128));
frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), 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
@ -86,9 +86,9 @@ function frameT = iFilterbank(frameF, frameType, winType)
subFrame = imdct4(frameF((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128, channel)); subFrame = imdct4(frameF((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128, channel));
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
subFrame = subFrame .* kaiserWindowShort'; subFrame = subFrame .* kaiserWindowShort;
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) = ...

4
Level_2/AACoder2.m

@ -21,7 +21,7 @@ function AACSeq2 = AACoder2(fNameIn)
[originalAudioData, ~] = audioread(fNameIn); [originalAudioData, ~] = audioread(fNameIn);
% Splits the audio in frames and determines the type of each frame % Splits the audio in frames and determines the type of each frame
frameTypes{fix((length(originalAudioData) - 1025) / 1024)} = 'OLS'; frameTypes{fix((length(originalAudioData) - 1025) / 1024), 1} = 'OLS';
frameTypes{1} = 'OLS'; frameTypes{1} = 'OLS';
for i = 1:length(frameTypes) - 2 for i = 1:length(frameTypes) - 2
nextFrameStart = (i + 1) * 1024 + 1; nextFrameStart = (i + 1) * 1024 + 1;
@ -39,7 +39,7 @@ function AACSeq2 = AACoder2(fNameIn)
end end
% Encodes audio file % Encodes audio file
AACSeq2(length(frameTypes)) = struct; AACSeq2(length(frameTypes), 1) = struct;
for i = 0:length(frameTypes) - 1 for i = 0:length(frameTypes) - 1
currFrameStart = i * 1024 + 1; currFrameStart = i * 1024 + 1;
currFrameStop = currFrameStart + 2047; currFrameStop = currFrameStart + 2047;

4
Level_2/SSC.m

@ -34,12 +34,12 @@ function frameType = SSC(~, nextFrameT, prevFrameType)
for channel = 1:2 for channel = 1:2
% Calculates sub-frame energy estimation % Calculates sub-frame energy estimation
[subFrames, ~] = buffer(nextFrameT(577:end - 448, channel), 128, 0, 'nodelay'); [subFrames, ~] = buffer(nextFrameT(577:end - 448, channel), 128, 0, 'nodelay');
energyEstimations = sum(subFrames .^ 2, 1); energyEstimations = sum(subFrames .^ 2, 1)';
% Calculates the ratio of the sub-frame energy to the average energy of % Calculates the ratio of the sub-frame energy to the average energy of
% the previous sub-frames % the previous sub-frames
energyRatios = movmean(energyEstimations, [8 0]); energyRatios = movmean(energyEstimations, [8 0]);
energyRatios = energyEstimations ./ [energyEstimations(1) energyRatios(1:end-1)]; energyRatios = energyEstimations ./ [energyEstimations(1); energyRatios(1:end-1)];
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))

16
Level_2/TNS.m

@ -34,7 +34,7 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
if ~strcmp(frameType, 'ESH') if ~strcmp(frameType, 'ESH')
% Calculates the energy per band % Calculates the energy per band
bandEnergy(LONG_WINDOW_NUMBER_OF_BANDS) = 0; bandEnergy(LONG_WINDOW_NUMBER_OF_BANDS, 1) = 0;
for band = 1:LONG_WINDOW_NUMBER_OF_BANDS - 1 for band = 1:LONG_WINDOW_NUMBER_OF_BANDS - 1
bandEnergy(band) = sumsqr(frameFin(TNSTables.B219a(band, 2) + 1:TNSTables.B219a(band + 1, 2))); bandEnergy(band) = sumsqr(frameFin(TNSTables.B219a(band, 2) + 1:TNSTables.B219a(band + 1, 2)));
end end
@ -42,7 +42,7 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
TNSTables.B219a(LONG_WINDOW_NUMBER_OF_BANDS, 2) + 1:end)); TNSTables.B219a(LONG_WINDOW_NUMBER_OF_BANDS, 2) + 1:end));
% Calculates the normalization factors % Calculates the normalization factors
bandIndices = quantiz(0:1023, TNSTables.B219a(:, 2) - 1); bandIndices = quantiz(0:1023, TNSTables.B219a(:, 2) - 1)';
normalizationFactor = sqrt(bandEnergy(bandIndices)); normalizationFactor = sqrt(bandEnergy(bandIndices));
% Smooths normalization factors % Smooths normalization factors
@ -54,7 +54,7 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
end end
% Normalizes MDCT coefficients according to each band energy % Normalizes MDCT coefficients according to each band energy
normalizedFrameFin = frameFin ./ normalizationFactor'; normalizedFrameFin = frameFin ./ normalizationFactor;
% Calculates the linear prediction coefficients % Calculates the linear prediction coefficients
[linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER); [linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER);
@ -66,15 +66,15 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
if ~isstable(1, quantizedLinPredCoeff) if ~isstable(1, quantizedLinPredCoeff)
error('TNS, l[79]: Inverse filter not stable!'); error('TNS, l[79]: Inverse filter not stable!');
else else
TNScoeffs = quantizedLinPredCoeff(2:end); TNScoeffs = quantizedLinPredCoeff(2:end)';
frameFout = filter(quantizedLinPredCoeff, 1, frameFin); frameFout = filter(quantizedLinPredCoeff, 1, frameFin);
end end
else else
% Initializes output vectors % Initializes output vectors
TNScoeffs(LPF_ORDER * 8) = 0; TNScoeffs(LPF_ORDER * 8, 1) = 0;
frameFout(1024) = 0; frameFout(1024) = 0;
bandEnergy(SHORT_WINDOW_NUMBER_OF_BANDS) = 0; bandEnergy(SHORT_WINDOW_NUMBER_OF_BANDS, 1) = 0;
for subFrameIndex = 1:8 for subFrameIndex = 1:8
subFrame = frameFin((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128); subFrame = frameFin((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128);
@ -86,7 +86,7 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
TNSTables.B219b(SHORT_WINDOW_NUMBER_OF_BANDS, 2) + 1:end)); TNSTables.B219b(SHORT_WINDOW_NUMBER_OF_BANDS, 2) + 1:end));
% Calculates the normalization factors % Calculates the normalization factors
bandIndices = quantiz(0:127, TNSTables.B219b(:, 2) - 1); bandIndices = quantiz(0:127, TNSTables.B219b(:, 2) - 1)';
normalizationFactor = sqrt(bandEnergy(bandIndices)); normalizationFactor = sqrt(bandEnergy(bandIndices));
% Smooths normalization factors % Smooths normalization factors
@ -98,7 +98,7 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
end end
% Normalizes MDCT coefficients according to each band energy % Normalizes MDCT coefficients according to each band energy
normalizedFrameFin = subFrame ./ normalizationFactor'; normalizedFrameFin = subFrame ./ normalizationFactor;
% Calculates the linear prediction coefficients % Calculates the linear prediction coefficients
[linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER); [linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER);

36
Level_2/filterbank.m

@ -25,17 +25,17 @@ function frameF = filterbank(frameT, frameType, winType)
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, 1) = movsum(kaiserLong(1:1024), [1024 0]);
kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong(1:1024)), [0 1024]); kaiserWindowLong(1025:2048, 1) = 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, 1) = movsum(kaiserShort(1:128), [128 0]);
kaiserWindowShort(129:256) = movsum(flipud(kaiserShort(1:128)), [0 128]); kaiserWindowShort(129:256, 1) = 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 % Initializes output array
@ -44,9 +44,9 @@ function frameF = filterbank(frameT, frameType, winType)
% Applies appropriate window to the frame % Applies appropriate window to the frame
if strcmp(frameType, 'OLS') if strcmp(frameType, 'OLS')
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT = bsxfun(@times, frameT, kaiserWindowLong'); frameT = bsxfun(@times, frameT, kaiserWindowLong);
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT = bsxfun(@times, frameT, 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
@ -54,12 +54,12 @@ function frameF = filterbank(frameT, frameType, winType)
frameF = mdct4(frameT); frameF = mdct4(frameT);
elseif strcmp(frameType, 'LSS') elseif strcmp(frameType, 'LSS')
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024)'); frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024));
frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end)'); frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end));
frameT(1601:end, :) = 0; frameT(1601:end, :) = 0;
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024)'); frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024));
frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end)'); frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end));
frameT(1601:end, :) = 0; frameT(1601:end, :) = 0;
else else
error('filterbank, l[20]: Unsupported window type input!') error('filterbank, l[20]: Unsupported window type input!')
@ -69,12 +69,12 @@ function frameF = filterbank(frameT, frameType, winType)
elseif strcmp(frameType, 'LPS') elseif strcmp(frameType, 'LPS')
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:448, :) = 0; frameT(1:448, :) = 0;
frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128)'); frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128));
frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), 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, :) = 0; frameT(1:448, :) = 0;
frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128)'); frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128));
frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), 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
@ -86,9 +86,9 @@ function frameF = filterbank(frameT, frameType, winType)
[subFrames, ~] = buffer(frameT(449:end-448, channel), 256, 128, 'nodelay'); [subFrames, ~] = buffer(frameT(449:end-448, channel), 256, 128, 'nodelay');
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
subFrames = subFrames .* repmat(kaiserWindowShort', [1 8]); subFrames = subFrames .* repmat(kaiserWindowShort, [1 8]);
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
subFrames = subFrames .* repmat(sinWindowShort', [1 8]); subFrames = subFrames .* repmat(sinWindowShort, [1 8]);
end end
for subFrameIndex = 1:8 for subFrameIndex = 1:8

36
Level_2/iFilterbank.m

@ -25,17 +25,17 @@ function frameT = iFilterbank(frameF, frameType, winType)
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, 1) = movsum(kaiserLong(1:1024), [1024 0]);
kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong(1:1024)), [0 1024]); kaiserWindowLong(1025:2048, 1) = 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, 1) = movsum(kaiserShort(1:128), [128 0]);
kaiserWindowShort(129:256) = movsum(flipud(kaiserShort(1:128)), [0 128]); kaiserWindowShort(129:256, 1) = 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 % Initializes output array
@ -46,9 +46,9 @@ function frameT = iFilterbank(frameF, frameType, winType)
frameT = imdct4(frameF); frameT = imdct4(frameF);
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT = bsxfun(@times, frameT, kaiserWindowLong'); frameT = bsxfun(@times, frameT, kaiserWindowLong);
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT = bsxfun(@times, frameT, 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
@ -56,12 +56,12 @@ function frameT = iFilterbank(frameF, frameType, winType)
frameT = imdct4(frameF); frameT = imdct4(frameF);
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024)'); frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024));
frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end)'); frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end));
frameT(1601:end, :) = 0; frameT(1601:end, :) = 0;
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024)'); frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024));
frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end)'); frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end));
frameT(1601:end, :) = 0; frameT(1601:end, :) = 0;
else else
error('filterbank, l[20]: Unsupported window type input!') error('filterbank, l[20]: Unsupported window type input!')
@ -71,12 +71,12 @@ function frameT = iFilterbank(frameF, frameType, winType)
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:448, :) = 0; frameT(1:448, :) = 0;
frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128)'); frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128));
frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), 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, :) = 0; frameT(1:448, :) = 0;
frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128)'); frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128));
frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), 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
@ -86,9 +86,9 @@ function frameT = iFilterbank(frameF, frameType, winType)
subFrame = imdct4(frameF((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128, channel)); subFrame = imdct4(frameF((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128, channel));
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
subFrame = subFrame .* kaiserWindowShort'; subFrame = subFrame .* kaiserWindowShort;
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) = ...

4
Level_2/iTNS.m

@ -25,14 +25,14 @@ function frameFout = iTNS(frameFin, frameType, TNScoeffs)
if ~strcmp(frameType, 'ESH') if ~strcmp(frameType, 'ESH')
% Inverses MDCT coefficients filtering % Inverses MDCT coefficients filtering
frameFout = filter(1, [1 TNScoeffs], frameFin); frameFout = filter(1, [1; TNScoeffs], frameFin);
else else
for subFrameIndex = 1:8 for subFrameIndex = 1:8
subFrame = frameFin((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128); subFrame = frameFin((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128);
% Inverses MDCT coefficients filtering % Inverses MDCT coefficients filtering
frameFout((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128) = ... frameFout((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128) = ...
filter(1, [1 TNScoeffs((subFrameIndex - 1) * 4 + 1:subFrameIndex * 4)], subFrame); filter(1, [1; TNScoeffs((subFrameIndex - 1) * 4 + 1:subFrameIndex * 4)], subFrame);
end end
end end
end end

4
Level_3/AACoder3.m

@ -44,7 +44,7 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded)
[originalAudioData, ~] = audioread(fNameIn); [originalAudioData, ~] = audioread(fNameIn);
% Splits the audio in frames and determines the type of each frame % Splits the audio in frames and determines the type of each frame
frameTypes{fix((length(originalAudioData) - 1025) / 1024)} = 'OLS'; frameTypes{fix((length(originalAudioData) - 1025) / 1024), 1} = 'OLS';
frameTypes{1} = 'OLS'; frameTypes{1} = 'OLS';
for i = 1:length(frameTypes) - 2 for i = 1:length(frameTypes) - 2
nextFrameStart = (i + 1) * 1024 + 1; nextFrameStart = (i + 1) * 1024 + 1;
@ -63,7 +63,7 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded)
% Encodes audio file % Encodes audio file
huffLUT = loadLUT(); huffLUT = loadLUT();
AACSeq3(length(frameTypes)) = struct; AACSeq3(length(frameTypes), 1) = struct;
for i = 0:length(frameTypes) - 1 for i = 0:length(frameTypes) - 1
currFrameStart = i * 1024 + 1; currFrameStart = i * 1024 + 1;
currFrameStop = currFrameStart + 2047; currFrameStop = currFrameStart + 2047;

4
Level_3/SSC.m

@ -34,12 +34,12 @@ function frameType = SSC(~, nextFrameT, prevFrameType)
for channel = 1:2 for channel = 1:2
% Calculates sub-frame energy estimation % Calculates sub-frame energy estimation
[subFrames, ~] = buffer(nextFrameT(577:end - 448, channel), 128, 0, 'nodelay'); [subFrames, ~] = buffer(nextFrameT(577:end - 448, channel), 128, 0, 'nodelay');
energyEstimations = sum(subFrames .^ 2, 1); energyEstimations = sum(subFrames .^ 2, 1)';
% Calculates the ratio of the sub-frame energy to the average energy of % Calculates the ratio of the sub-frame energy to the average energy of
% the previous sub-frames % the previous sub-frames
energyRatios = movmean(energyEstimations, [8 0]); energyRatios = movmean(energyEstimations, [8 0]);
energyRatios = energyEstimations ./ [energyEstimations(1) energyRatios(1:end-1)]; energyRatios = energyEstimations ./ [energyEstimations(1); energyRatios(1:end-1)];
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))

16
Level_3/TNS.m

@ -34,7 +34,7 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
if ~strcmp(frameType, 'ESH') if ~strcmp(frameType, 'ESH')
% Calculates the energy per band % Calculates the energy per band
bandEnergy(LONG_WINDOW_NUMBER_OF_BANDS) = 0; bandEnergy(LONG_WINDOW_NUMBER_OF_BANDS, 1) = 0;
for band = 1:LONG_WINDOW_NUMBER_OF_BANDS - 1 for band = 1:LONG_WINDOW_NUMBER_OF_BANDS - 1
bandEnergy(band) = sumsqr(frameFin(TNSTables.B219a(band, 2) + 1:TNSTables.B219a(band + 1, 2))); bandEnergy(band) = sumsqr(frameFin(TNSTables.B219a(band, 2) + 1:TNSTables.B219a(band + 1, 2)));
end end
@ -42,7 +42,7 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
TNSTables.B219a(LONG_WINDOW_NUMBER_OF_BANDS, 2) + 1:end)); TNSTables.B219a(LONG_WINDOW_NUMBER_OF_BANDS, 2) + 1:end));
% Calculates the normalization factors % Calculates the normalization factors
bandIndices = quantiz(0:1023, TNSTables.B219a(:, 2) - 1); bandIndices = quantiz(0:1023, TNSTables.B219a(:, 2) - 1)';
normalizationFactor = sqrt(bandEnergy(bandIndices)); normalizationFactor = sqrt(bandEnergy(bandIndices));
% Smooths normalization factors % Smooths normalization factors
@ -54,7 +54,7 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
end end
% Normalizes MDCT coefficients according to each band energy % Normalizes MDCT coefficients according to each band energy
normalizedFrameFin = frameFin ./ normalizationFactor'; normalizedFrameFin = frameFin ./ normalizationFactor;
% Calculates the linear prediction coefficients % Calculates the linear prediction coefficients
[linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER); [linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER);
@ -66,15 +66,15 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
if ~isstable(1, quantizedLinPredCoeff) if ~isstable(1, quantizedLinPredCoeff)
error('TNS, l[79]: Inverse filter not stable!'); error('TNS, l[79]: Inverse filter not stable!');
else else
TNScoeffs = quantizedLinPredCoeff(2:end); TNScoeffs = quantizedLinPredCoeff(2:end)';
frameFout = filter(quantizedLinPredCoeff, 1, frameFin); frameFout = filter(quantizedLinPredCoeff, 1, frameFin);
end end
else else
% Initializes output vectors % Initializes output vectors
TNScoeffs(LPF_ORDER * 8) = 0; TNScoeffs(LPF_ORDER * 8, 1) = 0;
frameFout(1024) = 0; frameFout(1024) = 0;
bandEnergy(SHORT_WINDOW_NUMBER_OF_BANDS) = 0; bandEnergy(SHORT_WINDOW_NUMBER_OF_BANDS, 1) = 0;
for subFrameIndex = 1:8 for subFrameIndex = 1:8
subFrame = frameFin((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128); subFrame = frameFin((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128);
@ -86,7 +86,7 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
TNSTables.B219b(SHORT_WINDOW_NUMBER_OF_BANDS, 2) + 1:end)); TNSTables.B219b(SHORT_WINDOW_NUMBER_OF_BANDS, 2) + 1:end));
% Calculates the normalization factors % Calculates the normalization factors
bandIndices = quantiz(0:127, TNSTables.B219b(:, 2) - 1); bandIndices = quantiz(0:127, TNSTables.B219b(:, 2) - 1)';
normalizationFactor = sqrt(bandEnergy(bandIndices)); normalizationFactor = sqrt(bandEnergy(bandIndices));
% Smooths normalization factors % Smooths normalization factors
@ -98,7 +98,7 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
end end
% Normalizes MDCT coefficients according to each band energy % Normalizes MDCT coefficients according to each band energy
normalizedFrameFin = subFrame ./ normalizationFactor'; normalizedFrameFin = subFrame ./ normalizationFactor;
% Calculates the linear prediction coefficients % Calculates the linear prediction coefficients
[linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER); [linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER);

36
Level_3/filterbank.m

@ -25,17 +25,17 @@ function frameF = filterbank(frameT, frameType, winType)
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, 1) = movsum(kaiserLong(1:1024), [1024 0]);
kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong(1:1024)), [0 1024]); kaiserWindowLong(1025:2048, 1) = 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, 1) = movsum(kaiserShort(1:128), [128 0]);
kaiserWindowShort(129:256) = movsum(flipud(kaiserShort(1:128)), [0 128]); kaiserWindowShort(129:256, 1) = 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 % Initializes output array
@ -44,9 +44,9 @@ function frameF = filterbank(frameT, frameType, winType)
% Applies appropriate window to the frame % Applies appropriate window to the frame
if strcmp(frameType, 'OLS') if strcmp(frameType, 'OLS')
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT = bsxfun(@times, frameT, kaiserWindowLong'); frameT = bsxfun(@times, frameT, kaiserWindowLong);
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT = bsxfun(@times, frameT, 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
@ -54,12 +54,12 @@ function frameF = filterbank(frameT, frameType, winType)
frameF = mdct4(frameT); frameF = mdct4(frameT);
elseif strcmp(frameType, 'LSS') elseif strcmp(frameType, 'LSS')
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024)'); frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024));
frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end)'); frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end));
frameT(1601:end, :) = 0; frameT(1601:end, :) = 0;
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024)'); frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024));
frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end)'); frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end));
frameT(1601:end, :) = 0; frameT(1601:end, :) = 0;
else else
error('filterbank, l[20]: Unsupported window type input!') error('filterbank, l[20]: Unsupported window type input!')
@ -69,12 +69,12 @@ function frameF = filterbank(frameT, frameType, winType)
elseif strcmp(frameType, 'LPS') elseif strcmp(frameType, 'LPS')
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:448, :) = 0; frameT(1:448, :) = 0;
frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128)'); frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128));
frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), 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, :) = 0; frameT(1:448, :) = 0;
frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128)'); frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128));
frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), 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
@ -86,9 +86,9 @@ function frameF = filterbank(frameT, frameType, winType)
[subFrames, ~] = buffer(frameT(449:end-448, channel), 256, 128, 'nodelay'); [subFrames, ~] = buffer(frameT(449:end-448, channel), 256, 128, 'nodelay');
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
subFrames = subFrames .* repmat(kaiserWindowShort', [1 8]); subFrames = subFrames .* repmat(kaiserWindowShort, [1 8]);
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
subFrames = subFrames .* repmat(sinWindowShort', [1 8]); subFrames = subFrames .* repmat(sinWindowShort, [1 8]);
end end
for subFrameIndex = 1:8 for subFrameIndex = 1:8

36
Level_3/iFilterbank.m

@ -25,17 +25,17 @@ function frameT = iFilterbank(frameF, frameType, winType)
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, 1) = movsum(kaiserLong(1:1024), [1024 0]);
kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong(1:1024)), [0 1024]); kaiserWindowLong(1025:2048, 1) = 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, 1) = movsum(kaiserShort(1:128), [128 0]);
kaiserWindowShort(129:256) = movsum(flipud(kaiserShort(1:128)), [0 128]); kaiserWindowShort(129:256, 1) = 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 % Initializes output array
@ -46,9 +46,9 @@ function frameT = iFilterbank(frameF, frameType, winType)
frameT = imdct4(frameF); frameT = imdct4(frameF);
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT = bsxfun(@times, frameT, kaiserWindowLong'); frameT = bsxfun(@times, frameT, kaiserWindowLong);
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT = bsxfun(@times, frameT, 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
@ -56,12 +56,12 @@ function frameT = iFilterbank(frameF, frameType, winType)
frameT = imdct4(frameF); frameT = imdct4(frameF);
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024)'); frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), kaiserWindowLong(1:1024));
frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end)'); frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), kaiserWindowShort(129:end));
frameT(1601:end, :) = 0; frameT(1601:end, :) = 0;
elseif strcmp(winType, 'SIN') elseif strcmp(winType, 'SIN')
frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024)'); frameT(1:1024, :) = bsxfun(@times, frameT(1:1024, :), sinWindowLong(1:1024));
frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end)'); frameT(1473:1600, :) = bsxfun(@times, frameT(1473:1600, :), sinWindowShort(129:end));
frameT(1601:end, :) = 0; frameT(1601:end, :) = 0;
else else
error('filterbank, l[20]: Unsupported window type input!') error('filterbank, l[20]: Unsupported window type input!')
@ -71,12 +71,12 @@ function frameT = iFilterbank(frameF, frameType, winType)
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
frameT(1:448, :) = 0; frameT(1:448, :) = 0;
frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128)'); frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), kaiserWindowShort(1:128));
frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), 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, :) = 0; frameT(1:448, :) = 0;
frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128)'); frameT(449:576, :) = bsxfun(@times, frameT(449:576, :), sinWindowShort(1:128));
frameT(1025:end, :) = bsxfun(@times, frameT(1025:end, :), 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
@ -86,9 +86,9 @@ function frameT = iFilterbank(frameF, frameType, winType)
subFrame = imdct4(frameF((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128, channel)); subFrame = imdct4(frameF((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128, channel));
if strcmp(winType, 'KBD') if strcmp(winType, 'KBD')
subFrame = subFrame .* kaiserWindowShort'; subFrame = subFrame .* kaiserWindowShort;
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) = ...

4
Level_3/iTNS.m

@ -25,14 +25,14 @@ function frameFout = iTNS(frameFin, frameType, TNScoeffs)
if ~strcmp(frameType, 'ESH') if ~strcmp(frameType, 'ESH')
% Inverses MDCT coefficients filtering % Inverses MDCT coefficients filtering
frameFout = filter(1, [1 TNScoeffs], frameFin); frameFout = filter(1, [1; TNScoeffs], frameFin);
else else
for subFrameIndex = 1:8 for subFrameIndex = 1:8
subFrame = frameFin((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128); subFrame = frameFin((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128);
% Inverses MDCT coefficients filtering % Inverses MDCT coefficients filtering
frameFout((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128) = ... frameFout((subFrameIndex - 1) * 128 + 1:subFrameIndex * 128) = ...
filter(1, [1 TNScoeffs((subFrameIndex - 1) * 4 + 1:subFrameIndex * 4)], subFrame); filter(1, [1; TNScoeffs((subFrameIndex - 1) * 4 + 1:subFrameIndex * 4)], subFrame);
end end
end end
end end

40
Level_3/psycho.m

@ -64,17 +64,17 @@ function SMR = psycho(frameT, frameType, frameTprev1, frameTprev2)
tmpSum = tmpz + tmpy; tmpSum = tmpz + tmpy;
spreadingShort(tmpy >= -100) = 10 .^ (tmpSum(tmpy >= -100) ./ 10); spreadingShort(tmpy >= -100) = 10 .^ (tmpSum(tmpy >= -100) ./ 10);
hannLong = 0.5 - 0.5 * cos(pi * ((0:2047) + 0.5) / 1024); hannLong = 0.5 - 0.5 * cos(pi * ((0:2047) + 0.5) / 1024)';
hannShort = 0.5 - 0.5 * cos(pi * ((0:255) + 0.5) / 128); hannShort = 0.5 - 0.5 * cos(pi * ((0:255) + 0.5) / 128)';
clearvars tmpx tmpz tmpy clearvars tmpx tmpz tmpy
end end
if ~strcmp(frameType, 'ESH') if ~strcmp(frameType, 'ESH')
% Applies window to the frames % Applies window to the frames
windowedFrameT = frameT .* hannLong'; windowedFrameT = frameT .* hannLong;
windowedFrameTprev1 = frameTprev1 .* hannLong'; windowedFrameTprev1 = frameTprev1 .* hannLong;
windowedFrameTprev2 = frameTprev2 .* hannLong'; windowedFrameTprev2 = frameTprev2 .* hannLong;
% Calculates the FFT of each frame % Calculates the FFT of each frame
frameF = fft(windowedFrameT); frameF = fft(windowedFrameT);
@ -103,8 +103,8 @@ function SMR = psycho(frameT, frameType, frameTprev1, frameTprev2)
% Calculates the energy and weighted predictability in the % Calculates the energy and weighted predictability in the
% threshold calculation partitions % threshold calculation partitions
bandEnergy(LONG_WINDOW_NUMBER_OF_BANDS) = 0; bandEnergy(LONG_WINDOW_NUMBER_OF_BANDS, 1) = 0;
bandPredictability(LONG_WINDOW_NUMBER_OF_BANDS) = 0; bandPredictability(LONG_WINDOW_NUMBER_OF_BANDS, 1) = 0;
for band = 1:LONG_WINDOW_NUMBER_OF_BANDS for band = 1:LONG_WINDOW_NUMBER_OF_BANDS
bandEnergy(band) = sumsqr(frameFMag(TNSTables.B219a(band, 2) + 1: ... bandEnergy(band) = sumsqr(frameFMag(TNSTables.B219a(band, 2) + 1: ...
TNSTables.B219a(band, 3) + 1)); TNSTables.B219a(band, 3) + 1));
@ -116,8 +116,8 @@ function SMR = psycho(frameT, frameType, frameTprev1, frameTprev2)
% Convolves the partitioned energy and predictability with the % Convolves the partitioned energy and predictability with the
% spreading function % spreading function
bandEnergyConv = sum(bandEnergy .* spreadingLong', 2); bandEnergyConv = sum(bandEnergy .* spreadingLong, 2);
bandPredictabilityConv = sum(bandPredictability .* spreadingLong', 2); bandPredictabilityConv = sum(bandPredictability .* spreadingLong, 2);
% Renormalizes values % Renormalizes values
bandPredictabilityConv = bandPredictabilityConv ./ bandEnergyConv; bandPredictabilityConv = bandPredictabilityConv ./ bandEnergyConv;
@ -140,29 +140,29 @@ function SMR = psycho(frameT, frameType, frameTprev1, frameTprev2)
qThrN = eps() * 1024 .* 10 .^ (TNSTables.B219a(:, 6) ./ 10); qThrN = eps() * 1024 .* 10 .^ (TNSTables.B219a(:, 6) ./ 10);
noiseLevel = max(energyThreshold, qThrN); noiseLevel = max(energyThreshold, qThrN);
SMR = bandEnergy ./ noiseLevel'; SMR = bandEnergy ./ noiseLevel;
else else
% Splits the frame into sub-frames % Splits the frame into sub-frames
[subFramesT, ~] = buffer(frameT(449:end-448), 256, 128, 'nodelay'); [subFramesT, ~] = buffer(frameT(449:end-448), 256, 128, 'nodelay');
[subFramesTprev1, ~] = buffer(frameTprev1(449:end-448), 256, 128, 'nodelay'); [subFramesTprev1, ~] = buffer(frameTprev1(449:end-448), 256, 128, 'nodelay');
bandEnergy(SHORT_WINDOW_NUMBER_OF_BANDS) = 0; bandEnergy(SHORT_WINDOW_NUMBER_OF_BANDS, 1) = 0;
bandPredictability(SHORT_WINDOW_NUMBER_OF_BANDS) = 0; bandPredictability(SHORT_WINDOW_NUMBER_OF_BANDS, 1) = 0;
qThrN = eps() * 128 .* 10 .^ (TNSTables.B219b(:, 6) ./ 10); qThrN = eps() * 128 .* 10 .^ (TNSTables.B219b(:, 6) ./ 10);
SMR(SHORT_WINDOW_NUMBER_OF_BANDS, 8) = 0; SMR(SHORT_WINDOW_NUMBER_OF_BANDS, 8) = 0;
for subFrameIndex = 1:8 for subFrameIndex = 1:8
% Applies window to the frames % Applies window to the frames
windowedFrameT = subFramesT(:, subFrameIndex) .* hannShort'; windowedFrameT = subFramesT(:, subFrameIndex) .* hannShort;
if subFrameIndex == 1 if subFrameIndex == 1
windowedFrameTprev1 = subFramesTprev1(:, 8) .* hannShort'; windowedFrameTprev1 = subFramesTprev1(:, 8) .* hannShort;
windowedFrameTprev2 = subFramesTprev1(:, 7) .* hannShort'; windowedFrameTprev2 = subFramesTprev1(:, 7) .* hannShort;
elseif subFrameIndex == 2 elseif subFrameIndex == 2
windowedFrameTprev1 = subFramesT(:, subFrameIndex - 1) .* hannShort'; windowedFrameTprev1 = subFramesT(:, subFrameIndex - 1) .* hannShort;
windowedFrameTprev2 = subFramesTprev1(:, 8) .* hannShort'; windowedFrameTprev2 = subFramesTprev1(:, 8) .* hannShort;
else else
windowedFrameTprev1 = subFramesT(:, subFrameIndex - 1) .* hannShort'; windowedFrameTprev1 = subFramesT(:, subFrameIndex - 1) .* hannShort;
windowedFrameTprev2 = subFramesT(:, subFrameIndex - 1) .* hannShort'; windowedFrameTprev2 = subFramesT(:, subFrameIndex - 1) .* hannShort;
end end
% Calculates the FFT of each frame % Calculates the FFT of each frame
@ -226,7 +226,7 @@ function SMR = psycho(frameT, frameType, frameTprev1, frameTprev2)
% Calculates the noise level % Calculates the noise level
noiseLevel = max(energyThreshold, qThrN); noiseLevel = max(energyThreshold, qThrN);
SMR(:, subFrameIndex) = bandEnergy ./ noiseLevel'; SMR(:, subFrameIndex) = bandEnergy ./ noiseLevel;
end end
end end
end end

Loading…
Cancel
Save