Sunteți pe pagina 1din 18

Experiment No.

: 1

Lab Group No.: 3 Student No.: 0906071

Course Number: EEE 312

Name of the Experiment: STUDY OF SAMPLING, QUANTIZATION AND ENCODING

Date of Performance: 20/11/2013 Date of Submission: 15/01/2014

Name: Md. Ehsanul Haque Nirjhar Department: EEE Section: B Partners Student No.: 0906070

Problem 1:
Generate a signal = sin210 + sin250 + sin2100 . Take the samples of this signal at a sampling rate 200Hz and 300Hz, quantize it by a 3 bit uniform quantizer and represent each level by a definite binary number. Also reconstruct the signal from sampled signal and quantized signal. Also obtain the SQNR. MATLAB Code:
clear all close all clc clf tf=0.1; %%%%% Final time %%%%%% Sampling frequency fs=300; b=3; %%%%% No. of bits/level indicator t=0:0.0001:tf; %%% Actual Signal x=sin(2*pi*10*t)+sin(2*pi*50*t)+sin(2*pi*100*t); y=0:1/fs:tf; %%%% Discrete Time domain samp=sin(2*pi*10*y)+sin(2*pi*50*y)+sin(2*pi*100*y);%%%% Sampled Signal del=(max(x)-min(x))/(2^b-1); %%% Difference between two levels z=interp1(y,samp,t); %%% Reconstructed signal after sampling for i=1:length(samp) if samp(i)>0 qr(i)=(floor((samp(i)+del/2+abs(min(x)))/del))*delabs(min(x)); else qr(i)=(ceil((samp(i)-del/2+abs(min(x)))/del))*del-abs(min(x)); end end d=interp1(y,qr,t); %%% Reconstructed signal after quantization er=(qr-samp).^2; %%% Quantization Error signal_power=mean(x.^2) %%% Signal power noise_power=mean(er) %%%% Noise Power SQNR=10*log10(signal_power/noise_power) enc_r=(qr-min(x))/del; %%% encoded signal for representing digits j=1; for i=1:b:b*length(enc_r) bin_r(i:i+b-1)=dec2binvec(enc_r(j),b); %%%% binary encoding tmp=linspace(y(j),y(j)+1/fs,b+1); w(i:i+b-1)=tmp(1:b); %%%% x axis for binary encoding plot j=j+1; end binenc=dec2bin(enc_r)

figure(1) plot(t,x); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfActual Signal'); figure(2) stem(y,samp); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title(['\bfSampled Signal at ',num2str(fs),'Hz']); figure(3) stairs(y,qr); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); set(gca,'YTick',min(x):del:max(x)); title(['\bfQuantized Signal(by rounding) at ',num2str(b),'bits']); figure(4) stairs(y,enc_r); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfEncoded Signal(Binary number representation)'); figure(5) stem(w,bin_r) ylim([-0.5 1.5]) xlim([0 w(length(w))]) xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfDigital Signal'); figure(6) plot(t,z); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfReconstructed Signal after Sampling'); figure(7) plot(t,d); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfReconstructed Signal after Quantization'); figure(8) plot(t,x,y,samp,'o',t,z,'k',t,d,'m'); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfComparison of Actual and Reconstructed Signals'); legend('Actual Signal','Sampled Signal','Reconstructed Signal Sampling','Reconstructed Signal after Quantization')

after

Plot: For sampling rate 200Hz, signal_power =1.4985, noise_power = 0.0503, SQNR=14.7382 dB
Actual Signal 3

1 Magnitude

-1

-2

-3

0.01

0.02

0.03

0.04

0.05 Time

0.06

0.07

0.08

0.09

0.1

Sampled Signal at 200Hz 2 1.5 1 0.5 Magnitude 0 -0.5 -1 -1.5 -2 -2.5

0.01

0.02

0.03

0.04

0.05 Time

0.06

0.07

0.08

0.09

0.1

Quantized Signal(by rounding) at 3bits 1.9658

1.1795

Magnitude

0.3932

-0.3932

-1.1795

-1.9658 0 0.01 0.02 0.03 0.04 0.05 Time 0.06 0.07 0.08 0.09 0.1

Encoded Signal(Binary number representation) 6

4 Magnitude

0.01

0.02

0.03

0.04

0.05 Time

0.06

0.07

0.08

0.09

0.1

Digital Signal 1.5

Magnitude

0.5

-0.5

0.01

0.02

0.03

0.04

0.05 0.06 Time

0.07

0.08

0.09

0.1

Comparison of Actual and Reconstructed Signals 3 Actual Signal Sampled Signal Reconstructed Signal after Sampling Reconstructed Signal after Quantization

1 Magnitude

-1

-2

-3

0.01

0.02

0.03

0.04

0.05 Time

0.06

0.07

0.08

0.09

0.1

Plot: For sampling rate 300Hz, signal_power =1.4985, noise_power = 0.0560, SQNR=14.2781 dB
Actual Signal 3

1 Magnitude

-1

-2

-3

0.01

0.02

0.03

0.04

0.05 Time

0.06

0.07

0.08

0.09

0.1

Sampled Signal at 300Hz 3

1 Magnitude

-1

-2

-3

0.01

0.02

0.03

0.04

0.05 Time

0.06

0.07

0.08

0.09

0.1

Quantized Signal(by rounding) at 3bits 2.7522 1.9658

1.1795

Magnitude

0.3932

-0.3932

-1.1795

-1.9658 -2.7522 0 0.01 0.02 0.03 0.04 0.05 Time 0.06 0.07 0.08 0.09 0.1

Encoded Signal(Binary number representation) 7

Magnitude

0 0

0.01

0.02

0.03

0.04

0.05 Time

0.06

0.07

0.08

0.09

0.1

Digital Signal 1.5

Magnitude

0.5

-0.5

0.01

0.02

0.03

0.04

0.05 0.06 Time

0.07

0.08

0.09

0.1

Comparison of Actual and Reconstructed Signals 3 Actual Signal Sampled Signal Reconstructed Signal after Sampling Reconstructed Signal after Quantization

1 Magnitude

-1

-2

-3

0.01

0.02

0.03

0.04

0.05 Time

0.06

0.07

0.08

0.09

0.1

Problem 2:
Write MATLAB codes for each section of -law compander explained in the lab sheet and show wave shapes for each of the sections. Plot a graph showing how the SQNR varies with the number of bits of the encoding process and show the effect of the compander on the variation of SQNR with the number of bits. MATLAB code of -law compander:
clear all close all clc clf filename='letsdoit.wav'; [x, f, n] = wavread(filename,8000); sound(x,f) b=8; samp=downsample(x,5); mu=255; for i=1:length(samp) v=abs(samp(i))/abs(max(x)); uc(i)=(log(1+mu*v))*sign(samp(i))/log(1+mu); end del=2/(2^b-1); for i=1:length(samp) if samp(i)>0 qr(i)=(floor((uc(i)+del/2+1)/del))*del-1; else qr(i)=(ceil((uc(i)-del/2+1)/del))*del-1; end end

%%%% Quantizer

enc_r=(qr+1)/del; %%%% Encoded Signal bin_enc=dec2bin(enc_r); %%% displaying binary encoding j=1; for i=1:b:b*length(enc_r) bin_r(i:i+b-1)=dec2binvec(enc_r(j),b); %%%% binary encoding tmp=linspace(j,j+1,b+1); w(i:i+b-1)=tmp(1:b); %%%% x axis for binary encoding plot j=j+1; end dec=(del*enc_r)-1; for i=1:length(samp) v=abs(dec(i)); ue(i)=(((1+mu)^v)-1)*sign(dec(i))/mu; end

%%%% Mu-law expanded

z=interp1(1:length(samp),ue,1:0.2:length(samp)); figure(1) plot(x); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfAnalog input Signal') figure(2) stem(samp); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfSampled Signal') figure(3) stem(uc); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfMu-law Compressed Signal') figure(4) stem(qr) xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title(['\bfQuantized Signal at ',num2str(b),'bits']) figure(5) stem(enc_r) xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfEncoded Signal') figure(6) stem(w,bin_r); ylim([-0.5 1.5]) xlim([0 100]) xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfEncoded Digital Signal expressed in Impulse function'); figure(7) stem(dec); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfDecoded Signal') figure(8) stem(ue); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfMu-law Expanded Signal') figure(9) plot(z); xlabel('Time','color','b'); ylabel('Magnitude','color','b'); title('\bfReconstructed Signal') sound(z,f)

Plot:
Analog input Signal 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

Magnitude

1000

2000

3000

4000 Time

5000

6000

7000

8000

Sampled Signal 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

Magnitude

200

400

600

800 Time

1000

1200

1400

1600

Mu-law Compressed Signal 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

Magnitude

200

400

600

800 Time

1000

1200

1400

1600

Quantized Signal at 8bits 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

Magnitude

200

400

600

800 Time

1000

1200

1400

1600

Encoded Signal 300

250

200 Magnitude

150

100

50

200

400

600

800 Time

1000

1200

1400

1600

Encoded Digital Signal expressed in Impulse function 1.5

Magnitude

0.5

-0.5

10

20

30

40

50 Time

60

70

80

90

100

Decoded Signal 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

Magnitude

200

400

600

800 Time

1000

1200

1400

1600

Mu-law Expanded Signal 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

Magnitude

200

400

600

800 Time

1000

1200

1400

1600

Reconstructed Signal 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

Magnitude

1000

2000

3000

4000 Time

5000

6000

7000

8000

MATLAB code of Variation of SQNR with number of bits:


clear all close all clc clf filename='letsdoit.wav'; [x, f, n] = wavread(filename,8000); b=4:14; samp=downsample(x,5); mu=255; for n=1:length(b) del=(max(x)-min(x))/(2^b(n)-1); d=2/(2^b(n)-1); for i=1:length(samp) v=abs(samp(i))/abs(max(x)); uc(i)=(log(1+mu*v))*sign(samp(i))/log(1+mu); end for i=1:length(samp) if samp(i)>0 qr(i)=(floor((uc(i)+d/2+1)/d))*d-1; q(i)=(floor((samp(i)+del/2+abs(min(x)))/del))*del-abs(min(x)); else qr(i)=(ceil((uc(i)-d/2+1)/d))*d-1; q(i)=(ceil((samp(i)-del/2+abs(min(x)))/del))*delabs(min(x)); end end

enc_r=(qr+1)/del; dec=(del*enc_r)-1;

%%%% Encoded Signal

for i=1:length(samp) v=abs(dec(i)); ue(i)=(((1+mu)^v)-1)*sign(dec(i))/mu; end en=(q'-samp).^2; er=(ue'*max(x)-samp).^2; p=mean((x).^2); %%%actual power noise_compander=mean(er); noise=mean(en); SQNR_comp(n)=10*log10(p/noise_compander); SQNR(n)=10*log10(p/noise); end s=1.76+6*b; %%% Theoretical SQNR plot(b,SQNR,b,SQNR_comp,b,s) title('\bfSQNR vs. Bits Graph') xlabel('Number of Bits','color','b') ylabel('SQNR','color','b') legend('SQNR for Uniform Quantizer','SQNR SQNR')

for

Mu-law','Theoretical

Plot:
SQNR vs. Bits Graph 90 80 70 60 SQNR 50 40 30 20 10 SQNR for Uniform Quantizer SQNR for Mu-law Theoretical SQNR 4 5 6 7 8 9 10 Number of Bits 11 12 13 14

Discussion:
The signal can be reconstructed easily if the sampling frequency is more than twice the frequency of the signal which follows the Nyquists theorem. Sampling done at the exact twice of the signal frequency causes problem as sampling is done at the null points at that time. For signals containing multiple frequencies, sampling should be done at twice the frequency of the highest frequency present in the signal. Quantization by rounding is more useful than by truncating as it causes less noise. By increasing the number of bits of the encoding process, noise power can be decreased. Then it would be easy to reconstruct the signal. Quantization is an irreversible process as the values from which rounding or truncating is done, can not be recovered. After using -law compander we get a normalized reconstructed signal. If we want to get the actual magnitude, amplifier should be used.

S-ar putea să vă placă și