Browse Source

Add correct quantization for the TNS step, Various fixes

master
Apostolos Fanakis 6 years ago
parent
commit
f25e214b6b
  1. 2
      Level_3/AACoder3.m
  2. 36
      Level_3/AACquantizer.m
  3. 6
      Level_3/TNS.m
  4. BIN
      Level_3/decoded2.wav
  5. BIN
      Level_3/decoded3.wav
  6. 6
      Level_3/demoAAC3.m
  7. BIN
      Level_3/encoded.mat
  8. 12
      Level_3/psycho.m

2
Level_3/AACoder3.m

@ -38,7 +38,7 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded)
% frame's right channel % frame's right channel
% Declares constant window type % Declares constant window type
WINDOW_TYPE = 'SIN'; WINDOW_TYPE = 'KBD';
% Reads the audio file % Reads the audio file
[originalAudioData, ~] = audioread(fNameIn); [originalAudioData, ~] = audioread(fNameIn);

36
Level_3/AACquantizer.m

@ -49,19 +49,28 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR)
abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ...
.^ (3 / 4) + 0.4054); .^ (3 / 4) + 0.4054);
% Calculates dequantized frame samples
frameFDequant = sign(S(frameWlow:frameWhigh, 1)) .* ...
abs(S(frameWlow:frameWhigh, 1)) .^ ...
(4 / 3) .* 2 ^ (quantCoeff(band) / 4);
% Calculates current quantization error % Calculates current quantization error
quantErr = sumsqr(subFrameF - S(frameWlow:frameWhigh)); quantErr = sumsqr(subFrameF - frameFDequant);
% Gradually increases the quantization coefficient % Gradually increases the quantization coefficient
while quantErr < T(band) && (band > 1 || ... while quantErr < T(band) && (band < 2 || ...
(quantErr(band) - quantErr(band - 1) + 1) <= 60) (quantCoeff(band) - quantCoeff(band - 1) + 1) <= 60)
quantCoeff(band) = quantCoeff(band) + 1; quantCoeff(band) = quantCoeff(band) + 1;
S(frameWlow:frameWhigh) = sign(subFrameF) .* round(( ... S(frameWlow:frameWhigh) = sign(subFrameF) .* round(( ...
abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ...
.^ (3 / 4) + 0.4054); .^ (3 / 4) + 0.4054);
quantErr = sumsqr(subFrameF - S(frameWlow:frameWhigh)); frameFDequant = sign(S(frameWlow:frameWhigh, 1)) .* ...
abs(S(frameWlow:frameWhigh, 1)) .^ ...
(4 / 3) .* 2 ^ (quantCoeff(band) / 4);
quantErr = sumsqr(subFrameF - frameFDequant);
end end
if band == 1 if band == 1
@ -94,18 +103,29 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR)
abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ...
.^ (3 / 4) + 0.4054); .^ (3 / 4) + 0.4054);
% Calculates dequantized frame samples
frameFDequant = sign(S(currFrameStart + frameWlow: ...
currFrameStart + frameWhigh, 1)) .* abs(S( ...
currFrameStart + frameWlow:currFrameStart + frameWhigh, 1) ...
) .^ (4 / 3) .* 2 ^ (quantCoeff(band) / 4);
% Calculates current quantization error % Calculates current quantization error
quantErr = sumsqr(subFrameF - S(currFrameStart + frameWlow:currFrameStart + frameWhigh)); quantErr = sumsqr(subFrameF - frameFDequant);
% Gradually increases the quantization coefficient % Gradually increases the quantization coefficient
while quantErr < T(band) && (band > 1 || ... while quantErr < T(band) && (band < 2 || ...
(quantErr(band) - quantErr(band - 1) + 1) <= 60) (quantCoeff(band) - quantCoeff(band - 1) + 1) <= 60)
quantCoeff(band) = quantCoeff(band) + 1; quantCoeff(band) = quantCoeff(band) + 1;
S(currFrameStart + frameWlow:currFrameStart + frameWhigh) = sign(subFrameF) .* round(( ... S(currFrameStart + frameWlow:currFrameStart + frameWhigh) = sign(subFrameF) .* round(( ...
abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ...
.^ (3 / 4) + 0.4054); .^ (3 / 4) + 0.4054);
quantErr = sumsqr(subFrameF - S(currFrameStart + frameWlow:currFrameStart + frameWhigh)); frameFDequant = sign(S(currFrameStart + frameWlow: ...
currFrameStart + frameWhigh, 1)) .* abs(S( ...
currFrameStart + frameWlow:currFrameStart + frameWhigh, 1) ...
) .^ (4 / 3) .* 2 ^ (quantCoeff(band) / 4);
quantErr = sumsqr(subFrameF - frameFDequant);
end end
if band == 1 if band == 1

6
Level_3/TNS.m

@ -60,7 +60,8 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
[linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER); [linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER);
% Quantizes these coefficients % Quantizes these coefficients
quantizedLinPredCoeff = round(linPredCoeff, COEF_RES); quantizedLinPredCoeff = quantiz(linPredCoeff(2:end), -0.7:0.1:0.7);
quantizedLinPredCoeff = [1 -0.75 + 0.1 * quantizedLinPredCoeff];
% Filters MDCT coefficients % Filters MDCT coefficients
if ~isstable(1, quantizedLinPredCoeff) if ~isstable(1, quantizedLinPredCoeff)
@ -104,7 +105,8 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType)
[linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER); [linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER);
% Quantizes these coefficients % Quantizes these coefficients
quantizedLinPredCoeff = round(linPredCoeff, COEF_RES); quantizedLinPredCoeff = quantiz(linPredCoeff(2:end), -0.7:0.1:0.7);
quantizedLinPredCoeff = [1 -0.75 + 0.1 * quantizedLinPredCoeff];
% Filters MDCT coefficients % Filters MDCT coefficients
if ~isstable(1, quantizedLinPredCoeff) if ~isstable(1, quantizedLinPredCoeff)

BIN
Level_3/decoded2.wav

Binary file not shown.

BIN
Level_3/decoded3.wav

Binary file not shown.

6
Level_3/demoAAC3.m

@ -22,8 +22,8 @@ function [SNR, bitrate, compression] = demoAAC3(fNameIn, fNameOut, frameAACoded)
% figure() % figure()
% plot(audioData(1025:length(decodedAudio)-1024, 1) - decodedAudio(1025:end-1024, 1)) % plot(audioData(1025:length(decodedAudio)-1024, 1) - decodedAudio(1025:end-1024, 1))
% for frame = 1:length(AACSeq1) % for frame = 1:length(AACSeq3)
% if strcmp(AACSeq1(frame).frameType, 'LSS') || strcmp(AACSeq1(frame).frameType, 'LPS') % if strcmp(AACSeq3(frame).frameType, 'LSS') || strcmp(AACSeq3(frame).frameType, 'LPS')
% % Add lines % % Add lines
% h1 = line([(frame-1)*1024+1 (frame-1)*1024+1],[-2*10^(-15) 2*10^(-15)]); % 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)]); % h2 = line([frame*1024+1 frame*1024+1],[-2*10^(-15) 2*10^(-15)]);
@ -31,7 +31,7 @@ function [SNR, bitrate, compression] = demoAAC3(fNameIn, fNameOut, frameAACoded)
% set([h1 h2],'Color','y','LineWidth',2) % set([h1 h2],'Color','y','LineWidth',2)
% % Add a patch % % 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') % 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') % elseif strcmp(AACSeq3(frame).frameType, 'ESH')
% % Add lines % % Add lines
% h1 = line([(frame-1)*1024+1 (frame-1)*1024+1],[-2*10^(-15) 2*10^(-15)]); % 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)]); % h2 = line([frame*1024+1 frame*1024+1],[-2*10^(-15) 2*10^(-15)]);

BIN
Level_3/encoded.mat

Binary file not shown.

12
Level_3/psycho.m

@ -116,8 +116,10 @@ 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(repmat(bandEnergy, 1, ...
bandPredictabilityConv = sum(bandPredictability .* spreadingLong, 2); LONG_WINDOW_NUMBER_OF_BANDS) .* spreadingLong, 1)';
bandPredictabilityConv = sum(repmat(bandPredictability, 1, ...
LONG_WINDOW_NUMBER_OF_BANDS) .* spreadingLong, 1)';
% Renormalizes values % Renormalizes values
bandPredictabilityConv = bandPredictabilityConv ./ bandEnergyConv; bandPredictabilityConv = bandPredictabilityConv ./ bandEnergyConv;
@ -203,8 +205,10 @@ 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 .* spreadingShort', 2); bandEnergyConv = sum(repmat(bandEnergy, 1, ...
bandPredictabilityConv = sum(bandPredictability .* spreadingShort', 2); SHORT_WINDOW_NUMBER_OF_BANDS) .* spreadingShort, 1)';
bandPredictabilityConv = sum(repmat(bandPredictability, 1, ...
SHORT_WINDOW_NUMBER_OF_BANDS) .* spreadingShort, 1)';
% Renormalizes values % Renormalizes values
bandPredictabilityConv = bandPredictabilityConv ./ bandEnergyConv; bandPredictabilityConv = bandPredictabilityConv ./ bandEnergyConv;

Loading…
Cancel
Save