how to covert negative samples of sine wave into positive?

27 views (last 30 days)
hello
I am having trouble to get unsigned samples of sinewave. For example generated 1MHz sine wave. But samples are with both positive and negative values. But this sinewave LUT not be stored in FPGA. i have to convert negative samples into positive. I tried 2's complement method but it changing shape of sine wave. could some one help on this how to do it in matlab?
%clc;
%clear all;
%close all;
% t=linspace(0,1e-8,10000);
t = 0:0.00406901041666666666666666666667e-6:1e-6;
f=1e6;
x=sin(2*pi*f*t);
plot(t,x);
xlabel('Time (us)');
ylabel('Amplitude');
a1=x*1000;
b1=round(a1);
[c,v]=size(b1);
n3 = c*v;
word_len =11;
data = reshape(b1',n3,1);
databin =fi(data,1,10);
h = bin(databin);
d = bin2dec(h);
when i plot d vlaues from the code i am getting sine wave as above figure. not exact sine wave. please help on this.
  5 Comments
kowsalya ramaraj
kowsalya ramaraj on 22 Jul 2019
Thank you so much all for kind response. iI am getting positive samples and sine wave with this expression sin(x) - min(sin(x(sin(x)<0))).....
Adam Danz
Adam Danz on 22 Jul 2019
Edited: Adam Danz on 25 Jul 2019
Great! I'll move that comment to the answers section so the question is marked as answered.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 22 Jul 2019
Edited: Adam Danz on 23 Jul 2019
To shift the sin curve upward so that it's minimum value is not negative,
x1 = sin(x) - min(sin(x(sin(x)<0)));
Another way to think about that is
y = sin(x);
y1 = y- min(y(y<0));
  2 Comments
Thorsten
Thorsten on 25 Jul 2019
Edited: Thorsten on 25 Jul 2019
Instead of y1 = y- min(y(y<0)); you can simply use
y1 = y - min(y);
or
y1 = y + 1;
because you know the minimum value of sin is -1.
Adam Danz
Adam Danz on 25 Jul 2019
In the simple case of sin(x), yes this is true. But with additional parameters such as a*sin(x) you'd have to find the minimum negative value.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!