MATLAB: Code (m-file) to sample the signal

#need code

: x(t) = 4cos(200πt), at sampling frequency equal to 400 Hz and then to plot the sampled signal x[n], consider 10 cycles of x(t).

Best Answer

Try this (assuming it's not your homework, because you didn't tag it as homework):
% Formula for a cosine is cos(2 * pi * t / period).
% So, 200 * pi = 2 * pi / period, so period = 2/200 = 0.01.
% Thus 10 periods would mean a max t of 10 * 0.01 = 0.1.
% Sampling rate is 400 Hz, or one sample every 1/400 of a second.
% Number of Samples = (elasped time) / (time per sample)
numSamples = round(0.1 / (1/400))
% That is only 40 and gives a severe undersampling of the signal
% giving a weird shape.
% Make up a t axis going from 0 to 0.1 with numSamples samples.
t = linspace(0, 0.1, numSamples);
xt = 4 * cos(200 * pi * t);
plot(t, xt, 'b.-', 'LineWidth', 2, 'MarkerSize', 15);
grid on;
title('x(t) vs. t', 'FontSize', fontSize);
xlabel('t', 'FontSize', fontSize);
ylabel('x(t)', 'FontSize', fontSize);