need help for understanding audio compression code using dct
3 views (last 30 days)
Show older comments
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();
0 Comments
Answers (1)
Jonas
on 4 Jul 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)
0 Comments
See Also
Categories
Find more on Simulation, Tuning, and Visualization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!