Sampling process illustration in Matlab

Here a simple example of sampling process of a signal in matlab is provided. Sampling is effectively applying pulse signal to a transistor that turns it on and off. In matlab this can be stimuated by applying a pulses to an input signal.

Here first impulse(h) is generated using filter and then applied to a sinuoid signal(x) to get the sampled signal(y).

The following line generates an impulse response h.

% generate discrete time impulse signal

a = [1 0 0 0 -1];
d = [1 zeros(1,127)];
h = filter(1,a,d);
n = 0:127;

The following line generates a cosine signal

% generate cosine signal

x = cos(2*pi*n/32);

% generate frequency response for signal x

[X,w] = freqz(x,1,128,whole);

% scale w for plotting properly

wp = fftshift(w)/pi-1;

The frequency spectrum of cosine signal is plotted below,

% plot frequency response of x
figure(1)
plot(wp,X)

Now the cosine signal x is multiplied by h which is effectively the process of sampling.

% sampling with 8 samples/ cycle

y = x.*h;

This sampled signal y is then plotted to get its frequency spectrum

Y = fft(y);

figure(2)
plot(wp,Y);

The overall process is shown in the graph which shows signals and its spectrum at various stages:



The overall matlab code is below,

% generate discrte time impulse signal

a = [1 0 0 0 -1];
d = [1 zeros(1,127)];
h = filter(1,a,d);
n = 0:127;

H = fft(h);

% generate cosine signal

x = cos(2*pi*n/32);

% generate frequency response for signal x

[X,w] = freqz(x,1,128,whole);

% scale w for plotting properly

wp = fftshift(w)/pi-1;

% sampling with 8 samples/ cycle

y = x.*h;

Y = fft(y);

figure

subplot(3,2,1)
plot(n,h)
title(Time Domain Responses)
xlabel(Time)
ylabel(h)
subplot(3,2,2)
plot(n,H);
title(Frequency Responses)
xlabel(Frequency)
ylabel(H)

subplot(3,2,3)
plot(n,x)
xlabel(Time)
ylabel(x)
subplot(3,2,4)
plot(wp,X);
xlabel(Frequency)
ylabel(X)

subplot(3,2,5)
plot(n,y)
xlabel(Time)
ylabel(y)
subplot(3,2,6)
plot(wp,Y);
xlabel(Frequency)
ylabel(Y)

Related Posts by Categories

0 comments:

Post a Comment