need help for understanding audio compression code using dct

조회 수: 3 (최근 30일)
Madhu Dharam
Madhu Dharam . 2022년 2월 17일
답변: Jonas . 2022년 7월 4일
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();

답변 (1개)

Jonas
Jonas 2022년 7월 4일
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)

카테고리

Help CenterFile Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by