Digital Signal Processing Lab: Bachelor of Technology in Electronics and Communication Engineering

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Digital Signal Processing Lab

Laboratory report submitted for the partial fulfillment


of the requirements for the degree of

Bachelor of Technology
in
Electronics and Communication Engineering

by

Vivek Nunia

Course Coordinator
Dr. Divyang Rawal

Department of Electronics and Communication Engineering


The LNM Institute of Information Technology, Jaipur

November 2020

Copyright © The LNMIIT 2020


All Rights Reserved
Contents
Chapter Page
1 Experiment 7 ....................................................................................................................................... 1

1.1 Objectives ......................................................................................................................................... 1

1.2 Software Required ............................................................................................................................ 1

1.3 Theory............................................................................................................................................... 1

1.3.1 Discrete Cosine Transform (DCT) .................................................................................................. 1

1.4 Procedure ......................................................................................................................................... 1

1.5 Observations..................................................................................................................................... 4

1.5.1 Observation 1 ................................................................................................................................ 4

1.5.2 Observation 2 ................................................................................................................................ 4

1.5.3 Observation 3 ................................................................................................................................ 6

1.5.4 Observation 4 ................................................................................................................................ 6

1.5.5 Observation 5 .............................................................................................................................. 10

1.6 Conclusion & Analysis Of Results ................................................................................................... 11

1.7 Precautions ..................................................................................................................................... 11

iii
Chapter 1

Experiment 7

1.1 Objectives
1. Discrete Cosine Transform and it’s energy compaction property.

2. Simulink based Audio compression.

1.2 Software Required


MATLAB

1.3 Theory
1.3.1 Discrete Cosine Transform (DCT)
A discrete cosine transform (DCT) expresses a finite sequence of data points in terms of a sum of
cosine functions oscillating at different frequencies. The DCT, first proposed by Nasir Ahmed in 1972,
is a widely used transformation technique in signal processing and data compression. The use of cosine
rather than sine functions is critical for compression, since it turns out that fewer cosine functions are
needed to approximate a typical signal, whereas for differential equations the cosines express a
particular choice of boundary conditions.
In image coding (such as MPEG and JPEG), and many audio coding algorithms (MPEG), the discrete
cosine transform (DCT) is used because of its nearly optimal asymptotic theoretical coding gain. For
1D signals, one of several DCT definitions (the one called DCT-II) is given by

.
In practice, the DCT is normally implemented using the same basic efficiency techniques as in FFT
algorithms. In MATLAB, the functions dct and dct2 are available for the 1D and 2D cases, respectively.
1.4 Procedure
1. Discrete Cosine Transform: Write a MATLAB code myCompression.m that takes the audio file
as the input, find the one dimension DCT, store the significant transform domain coefficients
and discards the non-significant coefficients, thus compress the original audio file in a compact

1
form. Reconstruction of the audio file is done by following all the steps of compression in
reverse manner.

2. Steps :

(a) Let x be the audio file of size 1 × N. The corresponding 1D-Discrete Cosine Transform y is
given by

where,

Also, n = 1,2,3,...,N.
(b) Use the above expression and write down the function for 1D-DCT for N = 8,16,...256.
(c) Divide the audio data into non-overlapping blocks of size 1×256. For each of them
compute the 1D-DCT.
(d) Now define two threshold value, Let’s say
Threshold1 = 0.09
Threshold2 = −0.09
Keep the DCT coefficients available from above step whose value is greater than
Threshold1 and less than Threshold2.

(e) Take the 1D-IDCT using

where,

Also, n = 1,2,3,...,N.
(f) Repeat the above procedure for below cases. Case 1: Threshold1 = 0.5
Threshold2 = −0.5

2
Case 2: Threshold1 = 0.01 Threshold2 =
−0.01

(g) Calculate the mean square error , where xidc is a reconstructed


approximation.
(h) Calculate compression ratio for all cases.

3
1.5 Observations
1.5.1 Observation 1

Use inbuilt DCT and IDCT function to compress and reconstruct any audio file.

%18uec010
clc;
clear all;
load handel.mat

[y,Fs]=audioread('input.wav');

y1=dct(y);
y2=idct(y1);
ms_err=sum(abs(y-y2))/1262745;

%sound(y2,Fs);
audiowrite('18UEC010.wav',y2,Fs); % reconstruct audio file.

1.5.2 Observation 2
Write down the myCompression.m and myDeCompression.m function using the expressions given
and cross verify with the inbuilt DCT-IDCT function.

function [y]= mycompression(w,n)

y= zeros(1,n);

for i= 1:n
if i==1
b(i)=1/sqrt(n) ;
else
b(i)= sqrt(2/n);
end
end

for i=1:n
for j= 1:n
y(i)= y(i)+ b(i)*w(j)*cos((pi*((2*j)-1)*(i-1))/(2*n));
end
end
end
function [y] = myDecompression(w,n)

y=zeros(1,n);

for i= 1:n
if i==1
b(i)=1/sqrt(n) ;
else
b(i)= sqrt(2/n);
end
end

for i=1:n
for j=1:n
y(i)= y(i) + b(j)*w(j)*cos((pi*((2*i)-1)*(j-1))/(2*n));
end
end
end

%18uec010
clc;
close all;
clear all;
x = [0,1,-1,7,5,5,4,1];
N = 8;
y1 = dct(x);
y2 = idct(y1); % reconstruct input vector signal using inbuild function.

y3= mycompression(x,N);

y4= myDecompression(y3,N);% reconstruct input vector signal using my own


function.

Figure 1.1 Y1 matrix


Figure 1.2 Y2 matrix

1.5.3 Observation 3

Divide the whole audio file in non-overlapping blocks of size 1 × 256. Use above function to
compress the whole audio file with coefficients saved according to the threshold values. Use my
DeCompression.m function to reconstruct audio file, and write in ”‘outputcompression.wav”’ file.

%18uec010
clc;
close all;
clear all;
[a,fs] = audioread('input.wav');

length1=ceil(length(a)/256);
length2=length1*256;

A=[a ; zeros((length2-length(a)),1)];
count1 = 0;

for i=1:256:1262592
y1=A(i:i+255);
y2=mycompression(y1,256);
for j=1:256
if(y2(j)>(-0.09) && y2(j)<(0.09))
y2(j)=0;
count1=count1+1;
else
y2(j)=y2(j);
end
end
y3=myDecompression(y2,256);
r(i:i+255)=y3; % reconstruct audio file.

end

audiowrite('outputcompression.wav',r,fs);

1.5.4 Observation 4
Repeat the above steps for given different threshold cases and make a table for mse and
compression ratio for given threshold values.

%18uec047
clc
clear all

[a,Fs]=audioread('input.wav');

length1=ceil(length(a)/256);
length2=length1*256;

A=[a ; zeros((length2-length(a)),1)];
count1=0;
count2=0;
count3=0;

for i=1:256:1262592
y1=A(i:i+255);
y2=dct(y1);
for j=1:256
if(y2(j)>(-0.5) && y2(j)<(0.5))
y2(j)=0;
count1=count1+1;
else
y2(j)=y2(j);
end
end
y3=idct(y2);
r(i:i+255)=y3;
end

for i=1:256:1262592
y1=A(i:i+255);
y2=dct(y1);
for j=1:256
if(y2(j)>(-0.01) && y2(j)<(0.01))
y2(j)=0;
count2=count2+1;
else
y2(j)=y2(j);
end
end
y3=idct(y2);
r1(i:i+255)=y3;
end

for i=1:256:1262592
y1=A(i:i+255);
y2=dct(y1);
for j=1:256
if(y2(j)>(-0.09) && y2(j)<(0.09))
y2(j)=0;
count3=count3+1;
else
y2(j)=y2(j);
end
end
y3=idct(y2);
r2(i:i+255)=y3;
end

A1=A(1:1262592);

m1=mean((A1-r').^2);
m2=mean((A1-r1').^2);
m3=mean((A1-r2').^2);

b=256*4932;

c1=(b-count1)/b;
c2=(b-count2)/b;
c3=(b-count3)/b;

M=[m2 m3 m1];
C=[c2 c3 c1];

thrs=[0.01 0.09 0.5];

subplot(2,1,1);
plot(thrs,C);
xlabel("Threshold");
ylabel("Compression Ratio");
title("Compression Ratio VS Threshold");

subplot(2,1,2);
plot(thrs,M);
xlabel("Threshold");
ylabel("MSE");
title("MSE VS Threshold");
sgtitle('Vivek Nunia -18uec010');

2
3

1.5.5 Observation 5
Repeat the Experiment in Simulink.

Figure 1.5 Simulink Implementation.


1.6 Conclusion & Analysis Of Results
1. We could implement mycompression and myDecompression function.

2. Compression Ratio decreases with increase in Threshold.

3. MSE increases with increase in Threshold.

1.7 Precautions
1. Observation should be taken properly.

2. Make sure the system doesn’t go into an infinite loop.

3. Graphs should be labelled properly.

14

You might also like