Hi, I have a question about generating a 1kHz sine wave.

First of all, we can't use the sin function so I just made a function that evaluates sin at some point...

int generate_sin(int t);
{
	int sin;
	sin = t - ((t^3)/(3*2*1)) + ((t^5)/(5*4*3*2*1));
	return sin;
}

Now, this function will evaulate sin at some number t but I am confused about the 1 kHz part...
I'm probably just confused about the math behind it but how do I generate an entire 1kHz wave.

Thanks

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, ...

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, ...

So, I Just have to multiply every 't' by 2*Pi*1000 (assuming the units is in seconds)
But, that will only evaluate it at a single point right? How do I get the entire waveform for one period?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.