generate a sine wave that ends at 0

Hello everyone,
As the title says, i'm trying to generate a sine wave that ends at 0. I have this problem when i try to generate a signal with a frquency higher than 1, the signal does not stop at 0.
I use the following code for the sine wave:-
%---------------------
Fs=5000;
f=159.2;
t1=1;
t = 0 : 1/Fs : t1-1/Fs;
A=0.1;
sine=A*sin(2*pi*f*t);
%---------------------
is there any way to make the signal stops at 0? even if that means that the signal moves past the 5000 points.
Regards
Ali

2 Comments

One suggestion: the sinpi function may be of interest to you.
Wow, I don't know the existence of this function (Introduced in R2018b).
Thank you @Steven Lord.

Sign in to comment.

 Accepted Answer

Sam Chak
Sam Chak on 5 Apr 2022
Edited: Sam Chak on 5 Apr 2022
Are you trying to make the Sine wave to hit 0 at the end of the simulation?
f = 159.2; % frequency
Fs = 35*f;
desired_Tend = 1;
n = round(f*desired_Tend);
T = 1/f; % period
t = 0:1/Fs:n*T;
A = 0.1; % amplitude
x = A*sin(2*pi*f*t);
plot(t, x, 'linewidth', 1.5)
axis([0.99 1 min(x) max(x)])
grid on
xlabel('t')
ylabel('x(t)')
title('Sine Wave')
x(length(t))

6 Comments

Yes, but it also has to start from 0. This is important because i want to be able to repeat the signal.
Also, how did you calculate the new value for Fs to make it reach 0?
Fs has to be a multiple of the frequency, f. Similarly, for the sine wave to reach zero at every period , then the end time of the simulation must be a multiple of the period . The closest integer of n before reaching 1 second is . I have edited the Answer.
The plot actually starts from . But the final display was zoomed in and boxed in the axis() command.
Simple, you just have to disable the line by inserting the percent symbol '%' as shown below:
% axis([0.99 1 min(x) max(x)])
What starting and ending t value do you want? When you say "ends at 0" it's not clear whether you're talking about t or y. You might want to use linspace().
I'm talking about y. i don't like using linespace tbh, i find it a lil bit confusing.
thank you sam for your answer.
I just have a problem with n.
How do i calculate it and what if i want a signal that is longer than 1 sec?
I have edited the script in the Answer. This allows you to compute the nearest integer.
desired_Tend = 2;
n = round(f*desired_Tend); % calculate the nearest integer

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!