need help for understanding audio compression code using dct

2 visualizaciones (últimos 30 días)
Madhu Dharam
Madhu Dharam el 17 de Feb. de 2022
Respondida: Jonas el 4 de Jul. de 2022
THIS IS THE CODE:
clc
clear
close all
%%
[A,fs]=audioread('1 (1).wav');
%% Sender
C=[];
A=A';
for i=512:512:numel(A)
B=dct(A(i-511:i));
C=[C, B(1:128)];
end
%% Reciever
A2=[];
for i=128:128:numel(C)
S=[C(i-127:i),zeros(1,384)];
S=idct(S);
A2=[A2,S];
end
%% Evaluation
dis=numel(A)-numel(A2);
A2=[A2,zeros(1,dis)];
PSNR=psnr(A2,A)
MSError=mse(A2,A)
SNR=snr(A2,A)
%% plot
figure,
plot(A);
hold on
plot(A2,'r')
plot((A2-A),'y')
legend('Original' , 'Recieved','Difference')
title('Audio Compression By DCT');
xlabel('Samples');
ylabel('Amp.');
grid();

Respuestas (1)

Jonas
Jonas el 4 de Jul. de 2022
it shows, how a sound could sound if you transmit only 128 dct values per block instead of 512 sound values per block
% read audio file
[A,fs]=audioread('1 (1).wav');
% Sender
C=[];
A=A';
% compute dct for block of data a 512 samples and use only the first 128 resulting dct values
for i=512:512:numel(A)
B=dct(A(i-511:i));
C=[C, B(1:128)];
end
% Send those 128 per 512 actual sample data to the receiver
% Reciever
A2=[];
for i=128:128:numel(C)
% receiver takes each 128 dct values, adds zeros behind to get a 512
% sample block
S=[C(i-127:i),zeros(1,384)];
% restore a sound part from inverse dct from 128 dct values and 384
% zero values
S=idct(S);
% append the block to the overall result vactor
A2=[A2,S];
end
%% Evaluation
dis=numel(A)-numel(A2);
A2=[A2,zeros(1,dis)];
PSNR=psnr(A2,A)
MSError=mse(A2,A)
SNR=snr(A2,A)
%% plot
figure,
plot(A);
hold on
plot(A2,'r')
plot((A2-A),'y')
legend('Original' , 'Recieved','Difference')
title('Audio Compression By DCT');
xlabel('Samples');
ylabel('Amp.');
grid();
% listen to both sounds, first the original, then the version where each
% 512 sample block was restored from only 128 dct values
soundsc(A,fs)
pause(numel(A)/fs +0.5);
soundsc(A2)

Categorías

Más información sobre Audio I/O and Waveform Generation en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by