## EE 451

Sampling a signal

• To sample a signal in MATLAB, generate a time vector at the appropiate rate, and use this to generate the signal. Plot using the stem function. For example:
```% Sample the sinusoid x = sin(2 pi f t), where f = 2 kHz.
% Let x1 be the signal sampled at 10 kHz.
% Let x2 be the signal sampled at 3 kHz.

f = 2000;
T = 1/f;
tmin = 0;
tmax = 5*T;             % Plot 5 cycles
dt1 = 1/10000;
dt2 = 1/3000;
t1 = tmin:dt1:tmax;
t2 = tmin:dt2:tmax;
x1 = sin(2*pi*f*t1);
x2 = sin(2*pi*f*t2);
subplot(211)
stem(t1,x1);
subplot(212)
stem(t2,x2);
```
• It is useful to plot the continuous time signal on the same plot. All signals in MATLAB are discrete-time, but they will look like continuous-time signals if the sampling rate is much higher than the Nyquist rate:
```% Sample the sinusoid x = sin(2 pi f t), where f = 2 kHz, and plot the sampled
% signals over the continuous-time signal.
% Let x1 be the signal sampled at 10 kHz.
% Let x2 be the signal sampled at 3 kHz.

f = 2000;
T = 1/f;
tmin = 0;
tmax = 5*T;
dt = T/100;
dt1 = 1/10000;
dt2 = 1/3000;
t = tmin:dt:tmax;
t1 = tmin:dt1:tmax;
t2 = tmin:dt2:tmax;
x = sin(2*pi*f*t);
x1 = sin(2*pi*f*t1);
x2 = sin(2*pi*f*t2);
subplot(211)
plot(t,x,'r');
hold on
stem(t1,x1);
subplot(212)
plot(t,x,'r');
hold on
stem(t2,x2);
```

Bill Rison, <rison@ee.nmt.edu >