15,797,822 members

See more:

hi , i want to create a user interface in which the user will enter the sampling rate and the frequency, the interface will plot the sine wave when pressed on a button accordingly using chart series. simply what im trying to do is this code when written on matlab, 1 fs= 96000; 2 f= 1000; 3 nCyl=1; 4 t=0:1/fs:nCyl*1/f; 5 x=sin(2*pi*f*t); 6 plot(t,x) if you try to write this code on matlab a sine graph will appear,the thing i couldnt write as an equation in line 4, i want my user interface to give me the same sine graph after the user enters the sampling rate and frequency values. thank you

i have tried the following code when pressing the button, there is no errors just the graph is wrong, it doesnt work correctly i think the equation is wrong:

double phasecent = 0; double samplerate = 96000; double freq = 1000; for (int i = 0; i < samplerate; i++) { double t = Math.Sin(freq * (2 * Math.PI) * (phasecent) / samplerate); phasecent++; double x = Math.Sin(2 *Math.PI*freq*t); chart1.Series["b2"].Points.AddXY(x, t); }

Comments

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

What does it do that you didn't expect, or not do that you did?

What have you tried to do to find out why?

Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.

Use the "Improve question" widget to edit your question and provide better information.

"it doesnt work correctly"tells us nothing.So tell us what it is doing that you didn't expect, or not doing that you did.

Tell us what you did to get it to happen.

Tell us any error messages.

2 f= 1000;

3 nCyl=1;

4 t=0:1/fs:nCyl*1/f;

5 x=sin(2*pi*f*t);

6 plot(t,x)

i just want to know how to write this above formulas in c# language. as i said there is no errors the graph is not correct it will plott a stright line what i should get is a one cycle sine graph and its plotting something else if you try the above code in matlab you will understand what i should get

t=0:1/fs:nCyl*1/f;

x=sin(2*pi*f*t);

versus

double t = Math.Sin(freq * (2 * Math.PI) * (phasecent) / samplerate);

double x = Math.Sin(2 *Math.PI*freq*t);

Note that, in your C# code, you're using the Math.Sin function for both the X and Y values (in your case, Y is "t"). You might be confused because the Matlab code has them reversed. What it calls "x" is the sine value (plotted on the Y-axis) and its "t" is the X-axis, in seconds.