# How to perform sine generation (in C)?

I am interested in how should a listing for a program look like that will generate a sine of f=50hz, using the next arguments for any moment in time.

float phis=162*PI/180;//initial phase of signal
////////////////////////////////////////////////////////////////////////
//  generates a sinusoid of dwLength in dwData
//      and maintains phis value updated
////////////////////////////////////////////////////////////////////////
void rc(short* dwDataOut, DWORD dwLength){
float omegaf=50/(float)dwLength/PI*90/100;
DWORD dwi;
//use phis
for (dwi=0;dwi<dwLength;dwi++)
dwDataOut[dwi]=4450*sin(dwi*omegaf+phis)+200;

//update wave phase
phis-=PI*(1-50/(float)dwLength)+PI/2;
while(phis<0)
phis+=2*PI;
}


The program I came up works correctly with a small marj of error only for f=50hz, because right from the start it appears with a small error in getting phis coefficient.

By applying this procedure in a program I'll make use of a cleaner signal from a sound wave, and obtain a signal that will be overlapped as antiphase signal to a more complex 50hz wave, for longer period of time, and so eliminating this harmonic. I’m interested in a more mathematical method to find the value for phis, and probably the arguments of sine function.

(is this function resulting in a true output for f=50hz? what are the errors that can be adjusted?)

• Welcome to DSP.SE! I'm not quite sure what you're trying to achieve with this code. Are you trying to be able to call rc multiple times and have the returned sine wave carry on from the end of the previous call, so that running the returns from many calls together is an undistorted sine wave? – Peter K. Sep 4 '15 at 20:32
• Thank you for the welcome Peter. Yes it's exactly that. – mypaul Sep 4 '15 at 20:38
• the call is only one, just that it's for reading many of the dwDataOut samples. Then let the program do subtractions, only for this 50hz frequency. in the future maybe i'll need it for the US powered devs where u use 60hz. – mypaul Sep 4 '15 at 20:48

I think your recalculation of phis in the function is not correct.

If I do this in scilab:

//25631
N = 100;
phis1=162*%pi/180;//initial phase of signal

[data1,phis2] = rc(N,phis1);
[data2,phis3] = rc(N,phis2);
[data3,phis4] = rc(N,phis3);

clf
plot([data1 data2 data3])


where rc is

////////////////////////////////////////////////////////////////////////
//  generates a sinusoid of dwLength in dwData
//      and maintains phis value updated
////////////////////////////////////////////////////////////////////////
function [dwDataOut,phis] = rc(dwLength, phis)
omegaf=50/dwLength/%pi*90/100;

dwDataOut = zeros(1,dwLength);

for dwi=1:dwLength,
dwDataOut(dwi) = 4450*sin(dwi*omegaf+phis)+200;
end

//update wave phase
//WAS: phis = phis - %pi*(1-50/dwLength)+%pi/2;
phis = dwLength*omegaf+phis;
while(phis<0)
phis = phis + 2*%pi;
end
endfunction


Note specifically the line phis = dwLength*omegaf+phis;

Then the plot is as below, which seems to be what you're after.

• where in the graph is the continuation point for when scilab switches buffers (between calls)? Yes i guess it was what i was after. Now i see the power plug frequency is closer to 49.75, and not 50.0hz..... THank you and u have an accepted answer. – mypaul Sep 4 '15 at 21:15
• Thanks! I've added the continuity points as red dots on the graph. – Peter K. Sep 4 '15 at 21:23