Well there are a few things in your code that are a bit off. First: the sin function is a function that is usually defined on the real number range. You define the function on integers which is probably what you want, but I still wanted to point it out. Secondly you can get into trouble if you name variable like common functions like sin, cos, etc because of naming conflicts. But to your original question:
The function f: t->sin(t) has a period of 2*Pi so say t is in units of seconds then the frequency of the sin-wave is 1/(2*Pi) which is ~ 1/6, so the wave has a frequency of a 6th Hz. To modulate the wave to 1 kHz (1000Hz) you have to multiply the argument of the sin function with 2*Pi*1000. But that really depends on what your function argument t is: nano, milli-seconds, ...
drkybelk
Junior Poster in Training
73 posts since May 2010
Reputation Points: 12
Solved Threads: 13
Say you want only one period of the 1kHz wave. Since 1kHz = 1000/s this means you have 1000 periods per second. If you only want 1 then you have to evaluate your function in any arbitrary (1/1000)s interval [t0..t1] on the time axis. So you put your function in a loop and evaluate at distinct points between t0 and t1. For ease you can use t0=0.0 and t1=0.001. say you want 42 sample points in your interval then set t_step to (t1-t0)/42. In code that will look something like:
double t0 = 0.0;
double t1 = 0.001;
double numOfSamples = 42.0;
double t_step = (t1-t0)/numOfSamples;
for(double t = t0; t<=t1; t += t_step)
cout << t << "\t: " << generate_sin(t) << endl;
This should print out 42 (or 43 - I didn't debug) lines of values of your sine wave in the interval [0.0 .. 0.001] provided you got your generate_sin function right.
There is no way in C++ (or any other computer-language for digital computers, for that matter) that can generate the entire wave. It always has to be samples, that means distinct/pixelated values.
drkybelk
Junior Poster in Training
73 posts since May 2010
Reputation Points: 12
Solved Threads: 13