diff --git a/Level_3/AACoder3.m b/Level_3/AACoder3.m index 712b50d..efe52c1 100644 --- a/Level_3/AACoder3.m +++ b/Level_3/AACoder3.m @@ -38,7 +38,7 @@ function AACSeq3 = AACoder3(fNameIn, fnameAACoded) % frame's right channel % Declares constant window type - WINDOW_TYPE = 'SIN'; + WINDOW_TYPE = 'KBD'; % Reads the audio file [originalAudioData, ~] = audioread(fNameIn); diff --git a/Level_3/AACquantizer.m b/Level_3/AACquantizer.m index 382d195..0d881e3 100644 --- a/Level_3/AACquantizer.m +++ b/Level_3/AACquantizer.m @@ -49,19 +49,28 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR) abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... .^ (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 - quantErr = sumsqr(subFrameF - S(frameWlow:frameWhigh)); + quantErr = sumsqr(subFrameF - frameFDequant); % Gradually increases the quantization coefficient - while quantErr < T(band) && (band > 1 || ... - (quantErr(band) - quantErr(band - 1) + 1) <= 60) + while quantErr < T(band) && (band < 2 || ... + (quantCoeff(band) - quantCoeff(band - 1) + 1) <= 60) quantCoeff(band) = quantCoeff(band) + 1; S(frameWlow:frameWhigh) = sign(subFrameF) .* round(( ... abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... .^ (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 if band == 1 @@ -94,18 +103,29 @@ function [S, sfc, G] = AACquantizer(frameF, frameType, SMR) abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... .^ (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 - quantErr = sumsqr(subFrameF - S(currFrameStart + frameWlow:currFrameStart + frameWhigh)); + quantErr = sumsqr(subFrameF - frameFDequant); % Gradually increases the quantization coefficient - while quantErr < T(band) && (band > 1 || ... - (quantErr(band) - quantErr(band - 1) + 1) <= 60) + while quantErr < T(band) && (band < 2 || ... + (quantCoeff(band) - quantCoeff(band - 1) + 1) <= 60) quantCoeff(band) = quantCoeff(band) + 1; S(currFrameStart + frameWlow:currFrameStart + frameWhigh) = sign(subFrameF) .* round(( ... abs(subFrameF) .* 2 ^ (-quantCoeff(band) / 4)) ... .^ (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 if band == 1 diff --git a/Level_3/TNS.m b/Level_3/TNS.m index 866417e..c76a274 100644 --- a/Level_3/TNS.m +++ b/Level_3/TNS.m @@ -60,7 +60,8 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType) [linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER); % 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 if ~isstable(1, quantizedLinPredCoeff) @@ -102,10 +103,11 @@ function [frameFout, TNScoeffs] = TNS(frameFin, frameType) % Calculates the linear prediction coefficients [linPredCoeff, ~] = lpc(normalizedFrameFin, LPF_ORDER); - + % 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 if ~isstable(1, quantizedLinPredCoeff) error('TNS, l[79]: Inverse filter not stable!'); diff --git a/Level_3/decoded2.wav b/Level_3/decoded2.wav deleted file mode 100644 index acd03f6..0000000 Binary files a/Level_3/decoded2.wav and /dev/null differ diff --git a/Level_3/decoded3.wav b/Level_3/decoded3.wav new file mode 100644 index 0000000..5e5ad7d Binary files /dev/null and b/Level_3/decoded3.wav differ diff --git a/Level_3/demoAAC3.m b/Level_3/demoAAC3.m index f1c2666..f437a75 100644 --- a/Level_3/demoAAC3.m +++ b/Level_3/demoAAC3.m @@ -22,8 +22,8 @@ function [SNR, bitrate, compression] = demoAAC3(fNameIn, fNameOut, frameAACoded) % figure() % 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') +% for frame = 1:length(AACSeq3) +% if strcmp(AACSeq3(frame).frameType, 'LSS') || strcmp(AACSeq3(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)]); @@ -31,7 +31,7 @@ function [SNR, bitrate, compression] = demoAAC3(fNameIn, fNameOut, frameAACoded) % 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') +% elseif strcmp(AACSeq3(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)]); diff --git a/Level_3/encoded.mat b/Level_3/encoded.mat index d2d4dec..a075ad3 100644 Binary files a/Level_3/encoded.mat and b/Level_3/encoded.mat differ diff --git a/Level_3/psycho.m b/Level_3/psycho.m index 4ccbf7f..c1c5bc2 100644 --- a/Level_3/psycho.m +++ b/Level_3/psycho.m @@ -116,8 +116,10 @@ function SMR = psycho(frameT, frameType, frameTprev1, frameTprev2) % Convolves the partitioned energy and predictability with the % spreading function - bandEnergyConv = sum(bandEnergy .* spreadingLong, 2); - bandPredictabilityConv = sum(bandPredictability .* spreadingLong, 2); + bandEnergyConv = sum(repmat(bandEnergy, 1, ... + LONG_WINDOW_NUMBER_OF_BANDS) .* spreadingLong, 1)'; + bandPredictabilityConv = sum(repmat(bandPredictability, 1, ... + LONG_WINDOW_NUMBER_OF_BANDS) .* spreadingLong, 1)'; % Renormalizes values bandPredictabilityConv = bandPredictabilityConv ./ bandEnergyConv; @@ -203,8 +205,10 @@ function SMR = psycho(frameT, frameType, frameTprev1, frameTprev2) % Convolves the partitioned energy and predictability with the % spreading function - bandEnergyConv = sum(bandEnergy .* spreadingShort', 2); - bandPredictabilityConv = sum(bandPredictability .* spreadingShort', 2); + bandEnergyConv = sum(repmat(bandEnergy, 1, ... + SHORT_WINDOW_NUMBER_OF_BANDS) .* spreadingShort, 1)'; + bandPredictabilityConv = sum(repmat(bandPredictability, 1, ... + SHORT_WINDOW_NUMBER_OF_BANDS) .* spreadingShort, 1)'; % Renormalizes values bandPredictabilityConv = bandPredictabilityConv ./ bandEnergyConv;