From acb33e8ffb28b126adbf27bcf88456facc301a83 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Thu, 24 Jan 2019 12:36:41 +0200 Subject: [PATCH] Fixes and improvements --- Level_1/AACoder1.m | 7 +++++-- Level_1/SSC.m | 17 +++++------------ Level_1/demoAAC1.m | 22 ++++++++++++++++------ Level_1/filterbank.m | 29 ++++++++++++----------------- Level_1/iAACoder1.m | 2 +- Level_1/iFilterbank.m | 27 +++++++++++---------------- 6 files changed, 50 insertions(+), 54 deletions(-) diff --git a/Level_1/AACoder1.m b/Level_1/AACoder1.m index 1472bd2..ff6d6b8 100644 --- a/Level_1/AACoder1.m +++ b/Level_1/AACoder1.m @@ -11,6 +11,9 @@ function AACSeq1 = AACoder1(fNameIn) % coefficients of this frame's left channel, chr.frameF which are % the MDCT coefficients of this frame's right channel + % Declares constant window type + WINDOW_TYPE = 'KBD'; + % Reads the audio file [originalAudioData, ~] = audioread(fNameIn); @@ -37,10 +40,10 @@ function AACSeq1 = AACoder1(fNameIn) for i = 0:length(frameTypes) - 1 currFrameStart = i * 1024 + 1; currFrameStop = currFrameStart + 2047; - frameF = filterbank(originalAudioData(currFrameStart:currFrameStop, :), frameTypes{i+1}, 'KBD'); + frameF = filterbank(originalAudioData(currFrameStart:currFrameStop, :), frameTypes{i+1}, WINDOW_TYPE); AACSeq1(i + 1).frameType = frameTypes(i + 1); - AACSeq1(i + 1).winType = 'KBD'; + AACSeq1(i + 1).winType = WINDOW_TYPE; AACSeq1(i + 1).chl.frameF = frameF(:, 1); AACSeq1(i + 1).chr.frameF = frameF(:, 2); end diff --git a/Level_1/SSC.m b/Level_1/SSC.m index 058c656..766e706 100644 --- a/Level_1/SSC.m +++ b/Level_1/SSC.m @@ -40,18 +40,11 @@ function frameType = SSC(~, nextFrameT, prevFrameType) % Calculates the ratio of the sub-frame energy to the average energy of % the previous sub-frames - nextIsESH = false; - for subFrameIndex = 2:8 - energyRatio = energyEstimations(subFrameIndex) / ... - mean(energyEstimations(1:subFrameIndex - 1)); - - if (energyEstimations(subFrameIndex) > 10^(-3)) && (energyRatio > 10) - nextIsESH = true; - break; - end - end - - if nextIsESH == true + energyRatios = movmean(energyEstimations, [8 0]); + energyRatios = energyEstimations ./ [energyEstimations(1) energyRatios(1:end-1)]; + + if ~isempty(find(energyEstimations > 10^(-3), 1)) && ... + ~isempty(find(energyRatios(energyEstimations > 10^(-3)) > 10, 1)) 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 diff --git a/Level_1/demoAAC1.m b/Level_1/demoAAC1.m index 21fec1b..0fab49a 100644 --- a/Level_1/demoAAC1.m +++ b/Level_1/demoAAC1.m @@ -15,13 +15,23 @@ function SNR = demoAAC1(fNameIn, 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)) +% +% 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(1:length(decodedAudio), 1) - decodedAudio(1:end, 1)) + plot(audioData(1024:length(decodedAudio)-1024, 1) - decodedAudio(1024:end-1024, 1)) figure() - plot(audioData(1:length(decodedAudio), 2) - decodedAudio(1:end, 2)) + plot(audioData(1024:length(decodedAudio)-1024, 2) - decodedAudio(1024:end-1024, 2)) - 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)); + 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)); end diff --git a/Level_1/filterbank.m b/Level_1/filterbank.m index 98fea67..3cd3b4e 100644 --- a/Level_1/filterbank.m +++ b/Level_1/filterbank.m @@ -24,23 +24,18 @@ function frameF = filterbank(frameT, frameType, winType) kaiserSumLong = sum(kaiserLong); kaiserShort = kaiser(128, 4*pi); kaiserSumShort = sum(kaiserShort); - - for n = 1:1024 - kaiserWindowLong(n) = sqrt(sum(kaiserLong(1:n))/kaiserSumLong); - kaiserWindowLong(1024 + n) = sqrt(sum(kaiserLong(1:end-n+1))/kaiserSumLong); - - sinWindowLong(n) = sin(pi*(n + 0.5)/2048); - sinWindowLong(1024 + n) = sin(pi*(1024 + n + 0.5)/2048); - - end - - for n = 1:128 - kaiserWindowShort(n) = sqrt(sum(kaiserShort(1:n))/kaiserSumShort); - kaiserWindowShort(128 + n) = sqrt(sum(kaiserShort(1:end-n+1))/kaiserSumShort); - - sinWindowShort(n) = sin(pi*(n + 0.5)/256); - sinWindowShort(128 + n) = sin(pi*(128 + n + 0.5)/256); - end + + kaiserWindowLong(1:1024) = movsum(kaiserLong, [1024 0]); + kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong), [0 1024]); + kaiserWindowLong = sqrt(kaiserWindowLong ./ kaiserSumLong); + + sinWindowLong = sin(pi * ((0:2047) + 0.5) / 2048); + + kaiserWindowShort(1:128) = movsum(kaiserShort, [128 0]); + kaiserWindowShort(129:256) = movsum(flipud(kaiserShort), [0 128]); + kaiserWindowShort = sqrt(kaiserWindowShort ./ kaiserSumShort); + + sinWindowShort = sin(pi * ((0:255) + 0.5) / 256); end frameF(1024, 2) = 0; diff --git a/Level_1/iAACoder1.m b/Level_1/iAACoder1.m index 448212a..8a7ccce 100644 --- a/Level_1/iAACoder1.m +++ b/Level_1/iAACoder1.m @@ -19,7 +19,7 @@ function x = iAACoder1(AACSeq1, fNameOut) frameF(1024, 2) = 0; % Decodes audio file - for i = 0:length(AACSeq1)-1 + for i = 0:length(AACSeq1) - 1 currFrameStart = i * 1024 + 1; currFrameStop = currFrameStart + 2047; frameF(:, 1) = AACSeq1(i+1).chl.frameF; diff --git a/Level_1/iFilterbank.m b/Level_1/iFilterbank.m index c71e8d5..d8bcc87 100644 --- a/Level_1/iFilterbank.m +++ b/Level_1/iFilterbank.m @@ -25,22 +25,17 @@ function frameT = iFilterbank(frameF, frameType, winType) kaiserShort = kaiser(128, 4*pi); kaiserSumShort = sum(kaiserShort); - for n = 1:1024 - kaiserWindowLong(n) = sqrt(sum(kaiserLong(1:n))/kaiserSumLong); - kaiserWindowLong(1024 + n) = sqrt(sum(kaiserLong(1:end-n+1))/kaiserSumLong); - - sinWindowLong(n) = sin(pi*(n + 0.5)/2048); - sinWindowLong(1024 + n) = sin(pi*(1024 + n + 0.5)/2048); - - end - - for n = 1:128 - kaiserWindowShort(n) = sqrt(sum(kaiserShort(1:n))/kaiserSumShort); - kaiserWindowShort(128 + n) = sqrt(sum(kaiserShort(1:end-n+1))/kaiserSumShort); - - sinWindowShort(n) = sin(pi*(n + 0.5)/256); - sinWindowShort(128 + n) = sin(pi*(128 + n + 0.5)/256); - end + kaiserWindowLong(1:1024) = movsum(kaiserLong, [1024 0]); + kaiserWindowLong(1025:2048) = movsum(flipud(kaiserLong), [0 1024]); + kaiserWindowLong = sqrt(kaiserWindowLong ./ kaiserSumLong); + + sinWindowLong = sin(pi * ((0:2047) + 0.5) / 2048); + + kaiserWindowShort(1:128) = movsum(kaiserShort, [128 0]); + kaiserWindowShort(129:256) = movsum(flipud(kaiserShort), [0 128]); + kaiserWindowShort = sqrt(kaiserWindowShort ./ kaiserSumShort); + + sinWindowShort = sin(pi * ((0:255) + 0.5) / 256); end frameT(2048, 2) = 0;