# I need help plotting points with * symbol at intervals equal to Ts = 1/fs = 1/7s (Here is my code)?

2 views (last 30 days)
Michael on 9 Feb 2023
Answered: Meet on 9 Feb 2023
Question: Simulate the sampling of this waveform at fs = 7 Hz by plotting a * symbol at intervals
equal to Ts = 1/fs = 1/7 s.
Hint: Could use a for-loop to generate the sample time, Ts, insert it into the equation
for the 5-Hz sine wave, and plot the resulting point.
Code:
clear all;
close all;
clc;
%hwk1_NotLab_Problem1.8.m
f = 5; % Fundamental Frequency in Hz
N = 1000; % Length of array
T_T = 1; % Total Time
Ts = T_T/N;
t = 0:Ts:T_T; % Time vector
x = sin(2*pi*f*t); % 1s of 5-Hz sine wave in 1000 point array
figure;
plot(t,x);
xlabel('Time');
ylabel('Amplitude');
title('1s of 5-Hz sine wave in 1000 point array');
grid on

Sarvesh Kale on 9 Feb 2023
Does the following code help ?
t=0:0.001:1 ;
f=5;
x=sin(2*pi*f*t);
plot(t,x,'Color',[1 0 0 ],'LineWidth',2);
grid on
hold on
impulse_time = [0:1/7:1];
x_sample=sin(2*pi*f*impulse_time);
stem(impulse_time,x_sample,'*','LineStyle','none','LineWidth',2,'Color',[0 0 1]); If you change the LineStyle property to '-' then it will have a stem like output, more information on step can be found at the following link https://in.mathworks.com/help/matlab/ref/stem.html
Thank you

Jonas on 9 Feb 2023
to plot with * symbol, add it to the plot command, e.g.
plot(1:10,rand(10,1),'*') if you still need a line, add a line style:
plot(1:10,rand(10,1),'-*') Meet on 9 Feb 2023
Hi,
Based on the code that you have provided, the sampling rate can be set by changing the value of f to 7. You can add * markers in the plot by passing an additional argument for ‘Marker’ in the plot function.
Please refer the below modified code:
clear all;
close all;
clc;
%hwk1_NotLab_Problem1.8.m
%%
f = 7; % Fundamental Frequency in Hz
N = 1000; % Length of array
T_T = 1; % Total Time
Ts = T_T/N;
t = 0:Ts:T_T; % Time vector
x = sin(2*pi*f*t); % 1s of 7-Hz sine wave in 1000 point array
%%
figure;
plot(t,x,Color="r",Marker="*",MarkerEdgeColor="blue");
xlabel('Time');
ylabel('Amplitude');
title('1s of 7-Hz sine wave in 1000 point array');
grid on;
The generated plot will be as follows: You can refer to plot documentation for more customizations in your plots.