Code Segment:: All All

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Code segment:

close all
clear all
clc
fs=.0001;
b=3;
xa=0:.00001:.05;
ya=f(xa);
subplot(2,3,1),plot(xa,ya);
%sampling
x=0:fs:.05;
y=f(x);
subplot(2,3,2),stem(x,y);
%quantization
a=min(y):((max(y)-min(y))/(2^b-1)):max(y);
for i=1:length(y)
for j=1:length(a)
if ((a(j)<y(i))&&(a(j+1)>y(i)))
y1(i)=a(j);
y2(i)=j;
end
end
end
subplot(2,3,3),plot(x,y1);
%encoding
yen=dec2bin(y2);
%decoding
y3=bin2dec(yen);
for i=1:length(yen)
if y3(i)==0
ydec(i)=0;
else
ydec(i)=a(y3(i));
end
% ydec(i)=a(y3(i)+1);
end
z=ydec;
subplot(2,3,4),plot(x,z)
%reconstruction
f1=.001*fs;
x1=min(x):f1:max(x);
z=interp1(x,y,x1);
subplot(2,3,5),plot(x1,z);

For error curve the code segment is following,
fs=.0001;
b=[2 3 4 5 6 7];
for c=1:6
xa=0:.00001:.05;
ya=f(xa
%sampling
x=0:fs:.05;
y=f(x);
%quantization
a=min(y):((max(y)-min(y))/(2^b(c)-1)):max(y);
for i=1:length(y)
for j=1:length(a)
if ((a(j)<y(i))&&(a(j+1)>y(i)))
y1(i)=a(j);
end
end
end
sigms=sum((y1-y).^2)/(length(y)-1);
sigmx=sum(x.^2)/(length(xa)-1);
sqnr=sigmx/sigms;
db(c)=10*log10(sqnr);
end
plot(b,db);

Final outputs:
Main signal Sampled signal

Quantized signal Decoded signal


Reconstructed signal Error curve



For b=3 and fs=.01 the encoded signal is (due to the massive size of yen for higher fs lower value is
chosen here for example)
yen =

000
101
111
000
101
001
And the final decoded signal is (for b=3 and fs=.01)
ydec =

0 0.5435 0.8152 0 0.5435 0



Discussion:
For higher sampling frequency the data points number is higher thus it gives a better transmission of
signal. Similar thing can be said for the bitrate. Basically bitrate controls the data quality. The higher the
bitrate the higher levels of quantization level becomes thus the final output becomes more like the
actual one. But it doesnt means that if we continue to increase the bitrate the error level is gong to just
keep decreasing. It becomes almost saturated after a point.

You might also like