The simulation of sampling with MATLAB is complicated by the representation of analog signals and the numerical computation of the analog Fourier transform. Two sampling rates are needed:
one being the sampling rate under study,fs, and the other being the one used to simulate the analog signal,fsim>>fs. The computation of the analog Fourier transform ofx(t)can be done approximately using the fast Fourier transform (FFT) multiplied by the sampling period. For now, think of the FFT as an algorithm to compute the Fourier transform of a discretized signal.
To illustrate the sampling procedure consider sampling a sinusoidx(t)=cos(2πf0t)where f0=1 KHz. To simulate this as an analog signal we choose a sampling periodTsim=0.5×10−4sec/sample or a sampling frequencyfsim=20, 000 samples/sec.
No aliasing sampling—If we samplex(t)with a sampling frequencyfs=6000>2f0=2000 Hz, the sampled signaly(t)will not display aliasing in its frequency representation, as we are satisfying the Nyquist sampling rate condition. Figure 7.7(a) displays the signalx(t)and its sampled versiony(t), as well as their approximate Fourier transforms. The magnitude spectrum|X()|corresponds to the sinusoidx(t), while|Y()|is the first period of the spectrum of the sampled signal (recall the spec- trum of the sampled signal is periodic of periods=2πfs). In this case, when no aliasing occurs, the first period of the spectrum ofy(t)coincides with the spectrum ofx(t)(notice that as a sinusoid, the magnitude spectrum|X()|is zero except at the frequency of the sinusoid or±1 KHz; likewise|Y()|
is zero except at±1 KHz and the range of frequencies is [−fs/2,fs/2]=[−3, 3] KHz). In Figure 7.7(b) we show the sinc interpolation of three samples ofy(t); the solid line is the interpolated values or the sum of sincs centered at the three samples. At the bottom of that figure we show the sinc interpola- tion, for all the samples, obtained using our functionsincinterp. The sampling is implemented using our functionsampling.
0 1 2 3 4 5 6 7
−1 0 1
t (sec)
x(t), y(t) x(t) xr(t)
(a) (b)
−010 −8 −6 −4 −2 0 2 4 6 8 0.2
0.4
−03 −2 −1 0 1 2
0.2 0.4
f (KHz)
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
−0.5 0 0.5 1 1.5
0 1 2 3 4 5 6
−1
−0.5 0 0.5 1
t (sec)
×10−3
×10−4
×10−3
|X(Ω)||Y(Ω)|
Interpolated sinc Analog signal
Sampled signal
FIGURE 7.7
No aliasing: sampling simulation ofx(t)=cos(2000πt)usingfs=6000samples/sec. (a) Plots are of the signal x(t)and the sampled signaly(t), and their spectra (|Y()|is periodic and so a period is shown). (b) The top plot illustrates the sinc interpolation of three samples, and the bottom plot is the sinc-interpolated signalxr(t)and the sampled signal. In this casexr(t)is very close to the original signal.
Sampling with aliasing—In Figure 7.8 we show the case when the sampling frequency isfs=800<
2fs=2000, so that in this case we have aliasing. This can be seen in the sampled signaly(t)in the top plot of Figure 7.8(a), which appears as if we were sampling a sinusoid of lower frequency. It can also be seen in the spectra ofx(t)andy(t):|X()|is the same as in the previous case, but now|Y()|, which is a period of the spectrum of the sampled signaly(t), displays a frequency of 200 Hz, lower than that ofx(t), within the frequency range [−400, 400] Hz or [−fs/2,fs/2]. Aliasing has occurred.
Finally, the sinc interpolation gives a sinusoid of frequency 0.2 KHz, different fromx(t).
Similar situations occur when a more complex signal is sampled. If the signal to be sampled is x(t)=2−cos(πf0t)−sin(2πf0t)wheref0=500 Hz, if we use a sampling frequency offs=6000>2 fmax=2 f0=1000 Hz, there will be no aliasing. On the other hand, if the sampling frequency is fs=800<2fmax=2f0 =1000 Hz, frequency aliasing will occur. In the no aliasing sampling, the spectrum|Y()|(in a frequency range [−3000, 3000]=[−fs/2,fs/2]) corresponding to a period of the Fourier transform of the sampled signaly(t)shows the same frequencies as |X()|. The recon- structed signal equals the original signal. See Figure 7.9(a). When we usefs=800 Hz, the given signalx(t)is undersampled and aliasing occurs. The spectrum|Y()|corresponding to a period of the Fourier transform of the undersampled signaly(t)does not show the same frequencies as|X()|.
The reconstructed signal shown in the bottom right plot of Figure 7.9(b) does not resemble the original signal.
7.2 Uniform Sampling 435
0 1 2 3 4 5 6 7 8
−1 0 1
t (sec)
0 0.5 1 1.5 2 2.5 3 3.5
−1.5−1
−0.5 0 0.5 1 1.5
0
0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05
−1
−0.5 0 0.5 1
t (sec)
x(t), y(t)
−010 −8 −6 −4 −2 0 2 4 6 8 0.2
0.4
|X(Ω)|
−00.4 −0.3 −0.2−0.1 0 0.1 0.2 0.3 0.2
0.4
f (KHz)
|Y(Ω)|
×10−3
×10−3
×10−3 x(t), xr(t)
(a) (b)
Analog signal Sampled signal
FIGURE 7.8
Aliasing: sampling simulation ofx(t)=cos(2000πt)usingfs=800samples/sec. (a) Plots display the original signalx(t)and the sampled signaly(t)(it looks like a lower-frequency signal being sampled). The sprectra ofx(t) andy(t)are shown below (|Y()|is periodic and displays a lower frequency than|X()|). (b) Sinc interpolation for three samples and the whole signal. The reconstructed signalxr(t)is a sinusoid of period0.5×10−2or 200-Hz frequency due to aliasing.
The following function implements the sampling and computes the Fourier transform of the analog signal and of the sampled signal using the fast Fourier transform. It gives the range of frequencies for each of the spectra.
function [y,y1,X,fx,Y,fy] = sampling(x,L,fs)
%
% Sampling
% x analog signal
% L length of simulated x
% fs sampling rate
% y sampled signal
% X,Y magnitude spectra of x,y
% fx,fy frequency ranges for X,Y
%
fsim = 20000; % analog signal sampling frequency
% sampling with rate fsim/fs delta = fsim/fs;
y1 = zeros(1,L);
0 2 4 6 0
1 2 3 4
x(t), y(t)
−010 −5 0 5 0.5
1 1.5 2
f (KHz)
−0.4−0.2 0 0.2 0
0.5 1 1.5 2
f (KHz)
0
0.01 0.02 0.03 0.04 0.05 0
1 2 3 4
t (sec) t (sec) ×10−3
|Y(Ω)| |X(Ω)| xr(t)
(a) (b)
0 2 4 6
0 1 2 3 4
t (sec)
x(t), y(t)
Analog signal Sampled signal
Analog signal Sampled signal
−010 −5 0 5 0.5
1 1.5 2
f (KHz)
−3−2−1 0 1 2 0
0.5 1 1.5 2
f (KHz)
0 2 4 6
0 1 2 3 4
t (sec)
|Y(Ω)|
×10−3
×10−3
|X(Ω)| xr(t)
FIGURE 7.9
Sampling ofx(t)=2−cos(500πt)−sin(1000πt)with (a) no aliasing (fs=6000samples/sec) and (b) with aliasing (fs=800samples/sec).
y1(1:delta:L) = x(1:delta:L);
y = x(1:delta:L);
% analog FT and DTFT of signals dtx = 1/fsim;
X = fftshift(abs(fft(x)))∗dtx;
N = length(X); k = 0:(N−1); fx = 1/N.*k; fx = fx∗fsim/1000−fsim/2000;
dty = 1/fs;
Y = fftshift(abs(fft(y)))∗dty;
N = length(Y); k = 0:(N−1); fy = 1/N.*k; fy = fy∗fs/1000−fs/2000;
The following function computes the sinc interpolation of the samples.
function [t,xx,xr] = sincinterp(x,Ts)
%
% Sinc interpolation
% x sampled signal
% Ts sampling period of x
% xx,xr original samples and reconstructed in range t
%
N = length(x) t = 0:dT:N;
xr = zeros(1,N∗100+1);
for k = 1:N,