3.4 SAMPLING AND RECONSTRUCTION OF ANALOG SIGNALS
3.4.4 MATLAB IMPLEMENTATION For interpolation between samples MATLAB provides several approaches
The function sinc(x), which generates the (sinπx)/πx function, can be used to implement (3.37), given a finite number of samples. If {x(n), n1≤n≤n2} is given, and if we want to interpolate xa(t) on a very fine grid with the grid interval ∆t, then from (3.37)
xa(m∆t)≈
n2
n=n1
x(n) sinc [Fs(m∆t−nTs)], t1≤m∆t≤t2 (3.43) which can be implemented as a matrix-vector multiplication operation as shown below.
>> n = n1:n2; t = t1:t2; Fs = 1/Ts; nTs = n*Ts; % Ts is the sampling interval
>> xa = x * sinc(Fs*(ones(length(n),1)*t-nTs’*ones(1,length(t))));
Note that it is not possible to obtain anexactanalogxa(t) in light of the fact that we have assumed a finite number of samples. We now demon- strate the use of thesincfunction in the following two examples and also study the aliasing problem in the time domain.
EXAMPLE 3.21 From the samplesx1(n) in Example 3.19a, reconstructxa(t) and comment on the results.
Solution Note thatx1(n) was obtained by samplingxa(t) atTs= 1/Fs= 0.0002 sec. We will use the grid spacing of 0.00005 sec over−0.005≤t ≤0.005, which gives x(n) over−25≤n≤25.
–5 –4 –3 –2 –1 0 1 2 3 4 5 0
0.2 0.4 0.6 0.8 1
t in msec.
xa(t)
Reconstructed Signal from x1(n) using sinc function
FIGURE 3.16 Reconstructed signal in Example 3.21
MATLAB script:
% Discrete-time Signal x1(n)
>> Ts = 0.0002; n = -25:1:25; nTs = n*Ts; x = exp(-1000*abs(nTs));
% Analog Signal reconstruction
>> Dt = 0.00005; t = -0.005:Dt:0.005;
>> xa = x * sinc(Fs*(ones(length(n),1)*t-nTs’*ones(1,length(t))));
% check
>> error = max(abs(xa - exp(-1000*abs(t)))) error =
0.0363
The maximum error between the reconstructed and the actual analog signal is 0.0363, which is due to the fact thatxa(t) is not strictly band-limited (and also we have a finite number of samples). From Figure 3.16, we note that visually
the reconstruction is excellent.
EXAMPLE 3.22 From the samples x2(n) in Example 3.17b reconstructxa(t) and comment on the results.
Solution In this casex2(n) was obtained by samplingxa(t) atTs= 1/Fs= 0.001 sec. We will again use the grid spacing of 0.00005 sec over−0.005≤t≤0.005, which givesx(n) over−5≤n≤5.
% Discrete-time Signal x2(n)
>> Ts = 0.001; n = -5:1:5; nTs = n*Ts; x = exp(-1000*abs(nTs));
% Analog Signal reconstruction
>> Dt = 0.00005; t = -0.005:Dt:0.005;
>> xa = x * sinc(Fs*(ones(length(n),1)*t-nTs’*ones(1,length(t))));
% check
>> error = max(abs(xa - exp(-1000*abs(t)))) error =
0.1852
–5 –4 –3 –2 –1 0 1 2 3 4 5 –0.5
0 0.5 1
t in msec.
xa(t)
Reconstructed Signal from x2(n) Using Sinc Function
FIGURE 3.17 Reconstructed signal in Example 3.22
The maximum error between the reconstructed and the actual analog signals is 0.1852, which is significant and cannot be attributed to the nonband-limitedness of xa(t) alone. From Figure 3.17, observe that the reconstructed signal differs from the actual one in many places over the interpolated regions. This is the visual demonstration of aliasing in the time domain.
The second MATLAB approach for signal reconstruction is a plotting approach. The stairsfunction plots a staircase (ZOH) rendition of the analog signal, given its samples, while theplot function depicts a linear (FOH) interpolation between samples.
EXAMPLE 3.23 Plot the reconstructed signal from the samplesx1(n) in Example 3.19 using the ZOH and the FOH interpolations. Comment on the plots.
Solution Note that in this reconstruction we do not compute xa(t) but merely plot it using its samples.
% Discrete-time Signal x1(n) : Ts = 0.0002
>> Ts = 0.0002; n = -25:1:25; nTs = n*Ts; x = exp(-1000*abs(nTs));
% Plots
>> subplot(2,1,1); stairs(nTs*1000,x);
>> xlabel(’t in msec.’); ylabel(’xa(t)’)
>> title(’Reconstructed Signal from x1(n) using zero-order-hold’); hold on
>> stem(n*Ts*1000,x); hold off
%
% Discrete-time Signal x2(n) : Ts = 0.001
>> Ts = 0.001; n = -5:1:5; nTs = n*Ts; x = exp(-1000*abs(nTs));
% Plots
>> subplot(2,1,2); plot(nTs*1000,x);
>> xlabel(’t in msec.’); ylabel(’xa(t)’)
>> title(’Reconstructed Signal from x2(n) using zero-order-hold’); hold on
>> stem(n*Ts*1000,x); hold off
–5 –4 –3 –2 –1 0 1 2 3 4 5 0
0.2 0.4 0.6 0.8 1
t in msec.
xa(t)
Reconstructed Signal from x1(n) using zero–order–hold
–5 –4 –3 –2 –1 0 1 2 3 4 5
0 0.2 0.4 0.6 0.8 1
t in msec.
xa(t)
Reconstructed Signal from x1(n) using first–order–hold
FIGURE 3.18 Signal reconstruction in Example 3.23
The plots are shown in Figure 3.18, from which we observe that the ZOH re- construction is a crude one and that the further processing of analog signal is necessary. The FOH reconstruction appears to be a good one, but a careful observation neart= 0 reveals that the peak of the signal is not correctly repro- duced. In general, if the sampling frequency is much higher than the Nyquist rate, then the FOH interpolation provides an acceptable reconstruction.
The third approach of reconstruction in MATLAB involves the use of cubic spline functions. Thespline function implements interpolation between sample points. It is invoked byxa = spline(nTs,x,t), in which x and nTs are arrays containing samples x(n) at nTs instances, respec- tively, and tarray contains a fine grid at whichxa(t) values are desired.
Note once again that it is not possible to obtain anexactanalog xa(t).
EXAMPLE 3.24 From the samplesx1(n) andx2(n) in Example 3.19, reconstructxa(t) using the splinefunction. Comment on the results.
Solution This example is similar to Examples 3.21 and 3.22. Hence sampling parameters are the same as before.
–5 –4 –3 –2 –1 0 1 2 3 4 5 0
0.2 0.4 0.6 0.8 1
t in msec.
xa(t)
Reconstructed Signal from x1(n) using cubic spline function
–5 –4 –3 –2 –1 0 1 2 3 4 5
0 0.2 0.4 0.6 0.8 1
t in msec.
xa(t)
Reconstructed Signal from x2(n) using cubic spline function
FIGURE 3.19 Reconstructed signal in Example 3.24
MATLAB script:
% a) Discrete-time Signal x1(n): Ts = 0.0002
>> Ts = 0.0002; n = -25:1:25; nTs = n*Ts; x = exp(-1000*abs(nTs));
% Analog Signal reconstruction
>> Dt = 0.00005; t = -0.005:Dt:0.005; xa = spline(nTs,x,t);
% check
>> error = max(abs(xa - exp(-1000*abs(t)))) error = 0.0317
The maximum error between the reconstructed and the actual analog signal is 0.0317, which is due to the nonideal interpolation and the fact that xa(t) is nonband-limited. Comparing this error with that from the sinc (or ideal) inter- polation, we note that this error is lower. The ideal interpolation generally suf- fers more from time-limitedness (or from a finite number of samples). From the top plot in Figure 3.19 we observe that visually the reconstruction is excellent.
MATLAB script:
% Discrete-time Signal x2(n): Ts = 0.001
>> Ts = 0.001; n = -5:1:5; nTs = n*Ts; x = exp(-1000*abs(nTs));
% Analog Signal reconstruction
>> Dt = 0.00005; t = -0.005:Dt:0.005; xa = spline(nTs,x,t);
% check
>> error = max(abs(xa - exp(-1000*abs(t)))) error = 0.1679
The maximum error in this case is 0.1679, which is significant and cannot be attributed to the nonideal interpolation or nonband-limitedness ofxa(t). From the bottom plot in Figure 3.19 observe that the reconstructed signal again differs from the actual one in many places over the interpolated regions.
From these examples it is clear that for practical purposes thespline interpolation provides the best results.