How to level shift a sine wave mathematically?

Hello MatLab users,
Im currently working on a small program that gives me a certain number of values of a 60 Hz sine wave signal but unfortunately I was only able to create a sine wave that has positive and negative values, but what I really want is to get only positive values so I would need my sine wave to be level shifted up (for example 0 to 3). Is there any way to do this? I want to do this mathematically only. Help is appreciated, thank you all. I leave my code below and picture of plot of the sine wave.
f = 60;
fs = 1e4; % samples per second
t=0:1/fs:0.017; % 1/fs is seconds per sample
x = sin(2*3.14*f*t);
plot(t,x)
x(:)

댓글 수: 5

After you apply the suggestion of Geoff, you will get a sine wave that is between 0 and 2. Then you can change the range by simple multiplication;
x = (1 + sin(2*pi*f*t))*n/2; % sine wave values between 0 and n
Alex Avila
Alex Avila 2018년 8월 23일
Thank you Aquatris, your suggestion worked but what if I wanted the min not to be 0 and instead have a range between 0.5 and 3? and who is "Geoff" if you don't mind me asking? Thank you for your time and advice.
dpb
dpb 2018년 8월 23일
"_min not to be 0"_
That's what I showed in Answer is general scaling.
It's just mx+b applied to the function range...
Geoff did have a comment at the time I wrote mine. dqp gave you the answer below but if you need further explanation here it is;
First you create your sine wave that is between -1 and +1;
x = sin(2*pi*f*t);
You want this [-1,1] range to be [a,b] range instead, which is a simple linear mapping. This can be done by;
x_scaled = (x-(-1))*(b-a)/(1-(-1))+ a
So an example is;
t = 0:0.001:10;
x = sin(t);
a = 0.5; b = 3;
x_scaled = (x-(-1))*(b-a)/(1-(-1))+ a;
plot(t,x,t,x_scaled)
Alex Avila
Alex Avila 2018년 8월 24일
Thank you Aquatris for helping me out as well, your explantation was very clear.

댓글을 달려면 로그인하십시오.

 채택된 답변

dpb
dpb 2018년 8월 23일

1 개 추천

Sure, it's just a linear scaling from the [-1,1] interval to [minV,maxV]
x = sin(2*3.14*f*t);
M=[maxV-minV]/2; % slope --> [maxV-minV]/[1-(-1)]
B=1+minV; % intercept
z=M*(x+B); % scale
for your min,max of [0,3] then
M=(3-0)/2;
B=1+0;

댓글 수: 1

Alex Avila
Alex Avila 2018년 8월 24일
Thank you dpb for taking your time and helping me out with this, your answer was very helpful.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2018년 8월 23일

댓글:

2018년 8월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by