How can the individual samples be visable in the plot

1 Ansicht (letzte 30 Tage)
Yian Chen
Yian Chen am 26 Sep. 2021
Bearbeitet: Image Analyst am 26 Sep. 2021
Like sin(2*pi*100*t) and t is between 0-0.5S, how could there be 25 samples?

Akzeptierte Antwort

Image Analyst
Image Analyst am 26 Sep. 2021
OK, I have a second answer. Maybe your "S" is not a value when you say 0.5S. Maybe "S" really means " seconds". In that case, try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
fprintf('Beginning to run %s.m ...\n', mfilename)
% Like sin(2*pi*100*t) and t is between 0-0.5S, how could there be 25 samples?
numSamples = 25;
t = linspace(0, 0.5, numSamples);
y = sin(2 * pi * 100 * t);
plot(t, y, 'b.-', 'LineWidth', 3, 'MarkerSize', 40);
grid on;
title('y = sin(2 * pi * 100 * t)', 'FontSize', 18);
xlabel('time in seconds', 'FontSize', 18);
ylabel('y', 'FontSize', 18);
g = gcf;
g.WindowState = 'maximized'
fprintf('Done running %s.m.\n', mfilename)
  2 Kommentare
Yian Chen
Yian Chen am 26 Sep. 2021
Hi, thanks for your answer, and s do mean second, if I want my samples defined from 0 to 0.5s but does not include 0.5s, how can I make it?
Image Analyst
Image Analyst am 26 Sep. 2021
Bearbeitet: Image Analyst am 26 Sep. 2021
Just change to
t = linspace(0, 0.49999999999, numSamples);
or you can set the range of x in the plot with xlim(), like to see from 0 to 0.4, use
xlim([0, 0.4]);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 26 Sep. 2021
Here, try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
fprintf('Beginning to run %s.m ...\n', mfilename)
% Like sin(2*pi*100*t) and t is between 0-0.5S, how could there be 25 samples?
S = 100; % Whatever S is (we don't know because you didn't specify)
numSamples = 25;
t = linspace(0, 0.5 * S, numSamples);
y = sin(2 * pi * 100 * t);
subplot(2, 1, 1)
plot(t, y, 'b.-', 'LineWidth', 3, 'MarkerSize', 30);
grid on;
title('y = sin(2 * pi * 100 * t) with 25 samples (it is badly aliased)', 'FontSize', 18);
xlabel('t', 'FontSize', 18);
ylabel('y', 'FontSize', 18);
% We have bad aliasing. Let's see what it looks like without aliasing.
numSamples = 2000;
t2000 = linspace(0, 0.5 * S, numSamples);
y2000 = sin(2 * pi * 100 * t2000);
subplot(2, 1, 2)
plot(t2000, y2000, 'r.-', 'LineWidth', 1);
grid on;
title('y = sin(2 * pi * 100 * t) with 2000 samples', 'FontSize', 18);
xlabel('t', 'FontSize', 18);
ylabel('y', 'FontSize', 18);
g = gcf;
g.WindowState = 'maximized'
fprintf('Done running %s.m.\n', mfilename)

Kategorien

Mehr zu Debugging and Analysis finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by