LABREPORT3
LABREPORT3
LABREPORT3
Lab work:
Example 3.1:
Consider an analog signal x a ( t ) = cos(20t ) , 0 t 1 . It is sampled at Ts = 0.01, 0.05
and 0.1sec intervals to obtain x(n). For each Ts, plot x(n). Comment on your results.
Solution: The sampling theorem specifies the minimum sampling rate at which a
continuous-time signal needs to be uniformly sampled so that the original signal can
be completely recovered or reconstructed by these samples alone. This is usually
referred to as Shannon's sampling theorem in the literature.
Output:
Example 3.2:
Reconstruct the analog signal ya(t) from the samples x(n) utilizing the sinc
interpolation (Use t=0.001.) Estimate the frequency in ya(t) from your plot.
Comment on your results.
Ts=0.05; Fs=1/Ts;
xa2=x2*sinc(Fs*(ones(length(n2),1)*t-
(n2*Ts)'*ones(1,length(t))));
subplot(3,1,2); plot(t,xa2); axis([0,1,-1.1,1.1]);
ylabel('x_{a}(t)'); title('Reconstruction of x_{a}(t)
when Ts=0.05 ');
Ts=0.1; Fs=1/Ts;
xa3=x3*sinc(Fs*(ones(length(n3),1)*t-
(n3*Ts)'*ones(1,length(t))));
subplot(3,1,3);plot(t,xa3);axis([0,1,-1.1,1.1]);
ylabel('x_{a}(t)'); title('Reconstruction of x_{a}(t)
when Ts=0.1 ');
Output:
Example 3.3: Plot three functions on the same plot as follows:
(i) x1 = cos6t
(ii) x2 = cos14t
(iii) x3 = cos 26t Use t = nT, T=0.001sec and n =500
Now sample (i) at Ts = 0.1sec. Plot this on the same plot also. What do the results
indicate? Explain.
Solution: Aliasing is when a continuous-time sinusoid appears as a discrete-time
sinusoid with multiple frequencies. The sampling theorem establishes conditions
that prevent aliasing so that a continuous-time signal can be uniquely reconstructed
from its samples.
MATLAB codes
T=0.001; n=0:500;
x1=cos(2*pi*3*n*T); x2=cos(2*pi*7*n*T);
x3=cos(2*pi*13*n*T);
plot(n*T,x1); hold on; plot(n*T,x2,'r');
plot(n*T,x3,'g');
Ts=0.1; N=round(1/Ts); n1=0:N/2;
x1=cos(2*pi*3*n1*Ts); plot(n1*Ts,x1,'o');
Output:
Example 3.4:
Introduce the “chirp” phenomena where the instantaneous frequency of some
special signal will increase over time. Consider the signal
1
x(t ) = sin o t + t 2 Set o = 23000) rad/sec and = 2000 rad/sec2.
2
1. Store in the vector x the samples of the chirp signal on the interval 0t1,
and let T is the same value as above.
2. Use sound (x, fs) to play the chirp signal contained in x. Can you explain
what you just heard?
3. Can you predict the time at which the played signal has zero (or very low)
frequency? Use a longer x sequence to verify your prediction.
Solution: The more samples that are taken, the more detail about where the waves
rise and fall is recorded and the higher the quality of the audio. Also, the shape of
the sound wave is captured more accurately.
MATLAB code :
f=3000;
fs=8000;
Ts=1/fs;
t=[0:Ts:Ts*20*(fs-1)];
x=sin(2*pi*f*t+0.5*2000*t.^2);
sound(x,fs)
MATLAB code :
[y fs] = audioread('audioclip0.mp3');
plot(y)
Example 3.6:
Solution: This Simulink model was used to observe the effect of frequency in an
audio signal. The original signal can be smoothly run if the sampling time is set to
low.
Scope output:
Example 3.7:
Solution:
The downsampled signal's frequency spectrum will have its magnitude lowered by
the downsampling factor. Downsampling refers to converting a higher sampling
rate to a lower one. Downsampling is done to decrease the bit rate when
transmitting over a limited bandwidth or to convert to a more limited audio format.
Output:
Example 3.8:
Use the function to quantize a vector:
X = {1 2 -3 -4 5 10 12 13 16 11 0 -5 7 9 10 16} at a level (a) 2 (b) 4 (c) 8 (d) 16
Solution: Quantization is the process of replacing analog samples with approximate
values taken from a finite set of allowed values. The approximate values
corresponding to a sequence of analog samples can then be specified by a digital
signal for transmission, storage, or other digital processing.
MATLAB codes:
x=[1 2 -3 -4 5 10 12 13 16 11 0 -5 7 9 10 16 ];
z=length(x); n=1:z;
y1=uquant(x,16); y2=uquant(x,8);
y3=uquant(x,4); y4=uquant(x,2);
fprintf(' x = %s \n y1 = %s \n y2 = %s \n y3 = %s
\n y4 = %s \n ', ...
num2str(x), num2str(y1), num2str(y2), num2str(y3),
num2str(y4) );
figure;
subplot(2,2,1); stem(n,y1); hold on; plot(n,x,'k');
title('Quantization with 16 levels'); grid
subplot(2,2,2); stem(n,y2); hold on; plot(n,x,'k');
title('Quantization with 8 levels'); grid
subplot(2,2,3); stem(n,y3); hold on; plot(n,x,'k');
title('Quantization with 4 levels'); grid
subplot(2,2,4); stem(n,y4); hold on; plot(n,x,'k');
title('Quantization with 2 levels'); grid
Output:
>> EX8
x = 1 2 -3 -4 5 10 12 13 16 11 0 -5 7 9 10 16
y1 = 0.6 2 -3.6 -3.6 4.8 10.4 11.8 13.2
16 10.4 0.6 -5 7.6 9 10.4 16
y2 = 1 1 -2 -5 4 10 13 13 16 10 1 -5 7 10 10 16
y3 = 2 2 -5 -5 2 9 9 16 16 9 2 -5 9 9 9 16
y4 = -5 -5 -5 -5 -5 16 16 16 16 16 -5 -5 16 16 16 16
Example 3.9:
Solution: We simplify time into discrete numbers. Another example is capturing a digital image
by representing each pixel by a certain number of bits, thereby reducing the continuous color
spectrum of real-life to discrete colors Quantization.
Load an image:
x=imread('fountainbw.tif');
% Make sure that the image is available
x=double(x);
image(x)
50
100
150
200
250
300
350
400
50 100 150 200 250 300 350 400 450 500 550
Quantize the image at different level and see the effect:
y=imread('fountainbw.tif');
subplot(2,4,1); image(y); colormap(gray(256));
for b=1:7
N=2^b; J=uquant(y,N);
subplot(2,4,b+1); image(J); colormap(gray(256));
end
Output:
−1000| t|
Lab Evaluation: Let, x a ( t ) = e . Sample the function at Fs =
5000 sample/sec and 1000 sample/sec and comment on your result.
Solution:
MATLAB codes:
fs1=5000;
t1=0:1/fs1:20*1/fs1;
x1=exp(-1000*abs(t1));
fs2=1000;
t2=0:1/fs2:20*1/fs2;
x2=exp(-1000*abs(t2));
subplot(2,1,1);
plot(t1,x1);
hold on;
stem(t1,x1);
subplot(2,1,2);
plot(t2,x2);
hold on;
stem(t2,x2);
Output:
Home Work:
HW3.01: Given a sinusoid waveform with a frequency of 100 Hz,
x(t) = 4.5sin ( 2 100t ) sampled at 8000Hz. Write a MATLAB program to quantize
x(t) using 4 bits to obtain and plot the quantized signal, assuming the dynamic range
is between -5 to 5 volts.
Solution:
MATLAB codes:
fs = 8000;
T = 1/fs;
t = 0:T:.02;
x = 4.5*sin(2*pi*100*t);
z=length(x);
n=1:z;
b = input('input number of bits =>');
N=2^b;
y1=uquant(x,N);
subplot(2,2,1);
stem(n,y1,'k');
hold on;
plot(n,y1,'r','LineWidth',3);
hold on;
plot(n,x,'b','LineWidth',2);
subplot(2,2,2);stem(n,y1,'k');
subplot(2,2,3);plot(n,y1,'r','LineWidth',2);
subplot(2,2,4);plot(n,x,'r','LineWidth',2);
Output:
HW3.02:
An analog signal xa (t) =sin(1000t) is sampled using the following sampling
intervals. In each case, plot the spectrum in the resulting discrete-time signal.
(a) Ts = 0.1ms
(b) Ts = 1ms
(c) Ts = 0.01ms
Solution:
MATLAB codes:
%a
ts1=0.1*10^-3;
fs1=1/ts1;
t1=0:1/fs1:20*1/fs1;
x1=sin(1000*pi*t1);
subplot(3,1,1);
plot(t1,x1);
hold on;
stem(t1,x1);
%b
ts2=1*10^-3;
fs2=1/ts2;
t2=0:1/fs2:20*1/fs2;
x2=sin(1000*pi*t2);
subplot(3,1,2);
plot(t2,x2);
hold on;
stem(t2,x2);
%c
ts3=.01*10^-3;
fs3=1/ts3;
t3=0:1/fs3:20*1/fs3;
x3=sin(1000*pi*t3);
subplot(3,1,3);
plot(t3,x3);
hold on;
stem(t3,x3);
Otput:
Conclusion:
Sampling converts a time-varying voltage signal into a discrete-time signal, a
sequence of real numbers. Quantization replaces each real number with an
approximation from a finite set of discrete values. Most commonly, these discrete
values are represented as fixed-point words.