Click here to Skip to main content
15,846,009 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi ...
I've drawn up a code of sine wave :
C#
private double DegreeToRadian(double angle)
        {
            return Math.PI * angle / int.Parse(textBox3.Text);
        }

        private void Draw(double N)
        {
            int Yoffset = panel1.Height / 2;
            Graphics G = panel1.CreateGraphics();
            Pen P = new Pen(Color.Yellow, 1.5f);
            float x1, y1;
            for (double X = 0; X <= 360; X++)
            {
                x1 = (float)X;
                y1 = Yoffset + ((float)Math.Sin(DegreeToRadian(X * N)) * int.Parse(textBox2.Text));
                G.DrawRectangle(P, x1, y1, .5f, .5f);
            }
            P.Dispose();
        }


Then Button_Click :

C#
Draw(int.Parse(textBox1.Text));


Now I want ... How do I display the frequency in hertz?
Posted
Comments
Kenneth Haugland 23-Oct-14 12:21pm    
I believe you will find what you need here:
http://en.wikipedia.org/wiki/Sine_wave
BillWoodruff 23-Oct-14 12:40pm    
The frequency in Hertz is in textBox2 ? Where do you want to display the frequency ?

If the 'Draw method is called repeatedly, are you aware it could be speeded up by a factor of at least #100 ? Why aren't you using the Paint Event ?

1 solution

Let's assume your sine function depends on time and its time domain is supposed to be infinite (time: −∞ to +∞). Then, if all 360° (2*π) of phase change takes time t, the frequency is 1/t.

Non-infinite time domains makes things quite complicated. If, for example, the function is zero (or any other constant value) everywhere except some fixed segment on the time axis, such as the segment depicted on your image, the spectrum will be continuous (and rather wide, in that case). It is calculated as Fourier transform https://en.wikipedia.org/wiki/Fourier_transform[^].

If the function is strictly periodical ("strictly" also implies infinite domain in time), the spectrum is discrete and is calculated as the Fourier series: https://en.wikipedia.org/wiki/Fourier_series[^].

Note that a spectrum consists of the points which are characterized not just with frequency, but frequency/phase, which, apparently also can be expressed as a complex number.

The case of the sine function on an infinite time domain I mentioned fist is just the special case of strictly periodic function, which is trivial and makes the discrete spectrum of just one line at the frequency 1/t and some phase.

—SA
 
Share this answer
 
Comments
[no name] 23-Oct-14 17:05pm    
The only Thing I complain is, that you are using 360°....and while writing and reading the answer again I see 2π...ok, nothing to complain. A 5; even your answer most probably overtaxes OP. Regards, Bruno
Sergey Alexandrovich Kryukov 23-Oct-14 17:16pm    
Thank you, Bruno.
As to degrees, I have more fundamental education than that when people pay too much attention for units and other conventional things. Folk wisdom says: if someone wakes you up and asks "what is the value of π?", appropriate canonical answer is: "a constant".
—SA
[no name] 23-Oct-14 17:42pm    
I don't think that I pay to much attention for Units...and especially not for conventions. But for me _rad_ is the base for this things.
Sergey Alexandrovich Kryukov 23-Oct-14 17:44pm    
Well, yes, of course, but can you explain why? :-)
—SA
[no name] 23-Oct-14 17:51pm    
No, at least not in it's fundamental details ;) I can only reply what I learned. And what I remember it was hard to switch from basic school- knowledge about 0...360° to 0...2π afterwards. But I'm looking Forward for the Explanation ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900