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++)

//update wave phase

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?)

  • $\begingroup$ 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? $\endgroup$
    – Peter K.
    Sep 4, 2015 at 20:32
  • 1
    $\begingroup$ Thank you for the welcome Peter. Yes it's exactly that. $\endgroup$
    – mypaul
    Sep 4, 2015 at 20:38
  • $\begingroup$ 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. $\endgroup$
    – mypaul
    Sep 4, 2015 at 20:48

1 Answer 1


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

If I do this in scilab:

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);

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)

dwDataOut = zeros(1,dwLength);

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

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

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

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

enter image description here

  • $\begingroup$ 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. $\endgroup$
    – mypaul
    Sep 4, 2015 at 21:15
  • $\begingroup$ Thanks! I've added the continuity points as red dots on the graph. $\endgroup$
    – Peter K.
    Sep 4, 2015 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.