code to sample a signal

71 views (last 30 days)
Alex Nikoloudis
Alex Nikoloudis on 6 Jan 2021
Commented: Walter Roberson on 12 Aug 2024
x(t)=cos(180*π*t) at sampling rates of 200 and 1500 samples each second. Then plot the graphs.

Answers (3)

William Rose
William Rose on 4 Mar 2021
First, you know this cosine has a frequency of 90 Hz and therefore a period of 1/90 sec = 0.0111 sec, because the formula for a sine or cosine with freuqnecy f is . Therefore a plot that goes to 0.04 sec will include almost 4 cycles of the wave, so we aim for that.
SR1=200; %sampling rate
dt1=1/SR1; %sampling interval
t1=0:dt1:.04;
x1=cos(180*pi*t1);
SR2=1500; %sampling rate
dt2=1/SR2; %sampling interval
t2=0:dt2:.04;
x2=cos(180*pi*t2);
plot(t1,x1,'rx-',t2,x2,'bo-');
xlabel('Time (s)'); ylabel('x');
legend('SR=200','SR=1500');
  3 Comments
Walter Roberson
Walter Roberson on 2 Apr 2023
0.04 is Sampling time.
William Rose
William Rose on 2 Apr 2023
Thank you @Walter Roberson.
@mandah batjargal, I chose a sampling time, or (equivalently) sampling duration, of 0.04 s because it would be enough to see several cycles of the sinusoidal wave.
I like the fact that this problem illustrates that sampling at a rate that slightly exceeds the Nyquist freuency is not sufficient to provide a good visual image of the signal. For this reason I always recommend to students that, if they are using an anti-aliasing filter, they sample at 5 to 10 times its cutoff frequency. (Another reason to oversample is that no lowpass filter is a perfect brick wall, so there's likely to be some power at frequencies above the cutoff.)

Sign in to comment.


Hassaan
Hassaan on 21 Dec 2023
Edited: Voss on 21 Dec 2023
% Define the signal frequency
signalFrequency = 90; % in Hz
% Define the number of cycles to plot
numberOfCycles = 3; % for example, to plot the first 3 cycles
% Define the sampling rates
SR1 = 200; % First sampling rate in Hz
SR2 = 1500; % Second sampling rate in Hz
% Calculate the period of the cosine signal
signalPeriod = 1 / signalFrequency; % Period of the signal in seconds
% Calculate the time span for N cycles
timeSpan = numberOfCycles * signalPeriod;
% Create time vectors for the first few cycles
t1 = 0:1/SR1:timeSpan; % Time vector for first sampling rate
t2 = 0:1/SR2:timeSpan; % Time vector for second sampling rate
% Sample the cosine wave for the first few cycles
x1 = cos(2 * pi * signalFrequency * t1); % Sampled signal at first sampling rate
x2 = cos(2 * pi * signalFrequency * t2); % Sampled signal at second sampling rate
% Plot the sampled signals
figure; % Create a new figure
plot(t1, x1, 'bo-', t2, x2, 'rx-'); % Plot both signals on the same graph
xlabel('Time (s)'); % Label for the x-axis
ylabel('Amplitude'); % Label for the y-axis
title('Sampled Cosine Signal for First Few Cycles'); % Title of the plot
legend('SR=200', 'SR=1500'); % Legend to distinguish the two sampled rates
In this code:
  • The timeSpan variable is calculated to contain the time duration of N cycles of the cosine wave.
  • The t1 and t2 vectors are created to span this duration at the respective sampling rates.
  • The plot function is used to plot the first few cycles of the sampled signals.
If you liked the anwer and it solved your problem. Please do leave a upvote and a comment means a lot. Thank you.

Yerramsetty
Yerramsetty on 12 Aug 2024
to write and execute a program to sample and reconstruct audio signals using matlab software simulation
  1 Comment
Walter Roberson
Walter Roberson on 12 Aug 2024
I don't understand how this Answer solves the problem that was originally posted??

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!